From d89cc6299016c107eaabd09c68a3a9f1709a11f5 Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Tue, 14 Oct 2008 11:46:42 +0000 Subject: [PATCH] More lexiness git-svn-id: http://picoc.googlecode.com/svn/trunk@6 21eae674-98b7-11dd-bd71-f92a316d2d60 --- lex.c | 25 ++++++++++++++++++++++--- picoc.h | 6 ++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lex.c b/lex.c index fabe3d9..6557723 100644 --- a/lex.c +++ b/lex.c @@ -1,9 +1,11 @@ +#include #include #include "picoc.h" #define isCidstart(c) (isalpha(c) || (c)=='_') +#define isCident(c) (isalnum(c) || (c)=='_') #define LEXINC Lexer->Pos++ #define NEXTIS(c,x,y) { if (NextChar == (c)) { LEXINC; return (x); } else return (y); } @@ -54,14 +56,27 @@ enum LexToken LexCheckReservedWord(const Str *Word) enum LexToken LexGetNumber(struct LexState *Lexer) { - // XXX + Lexer->Value.Integer = strtol(Lexer->Pos, &Lexer->Pos, 10); return TokenIntegerConstant; } enum LexToken LexGetWord(struct LexState *Lexer) { - // XXX + const char *Pos = Lexer->Pos + 1; + enum LexToken Token; + + while (Lexer->Pos != Lexer->End && isCident(*Pos)) + Pos++; + + Lexer->Value.String.Str = Lexer->Pos; + Lexer->Value.String.Len = Pos - Lexer->Pos; + Lexer->Pos = Pos; + + Token = LexCheckReservedWord(&Lexer->Value.String); + if (Token != TokenNone) + return Token; + return TokenIdentifier; } @@ -75,7 +90,11 @@ enum LexToken LexGetStringConstant(struct LexState *Lexer) enum LexToken LexGetCharacterConstant(struct LexState *Lexer) { - // XXX + Lexer->Value.Integer = Lexer->Pos[1]; + if (Lexer->Pos[2] != '\'') + ProgramError(Lexer->FileName, Lexer->Line, "illegal character '%c'", Lexer->Pos[2]); + + Lexer->Pos += 3; return TokenCharacterConstant; } diff --git a/picoc.h b/picoc.h index 157ed79..58f7344 100644 --- a/picoc.h +++ b/picoc.h @@ -107,6 +107,11 @@ struct FuncDef int StartLine; }; +union AnyValue +{ + int Integer; + Str String; +}; /* lexer state - so we can lex nested files */ struct LexState @@ -115,6 +120,7 @@ struct LexState const char *Pos; const char *End; const Str *FileName; + union AnyValue Value; };