More lexiness

git-svn-id: http://picoc.googlecode.com/svn/trunk@6 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2008-10-14 11:46:42 +00:00
parent 96674aad41
commit d89cc62990
2 changed files with 28 additions and 3 deletions

25
lex.c
View file

@ -1,9 +1,11 @@
#include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "picoc.h" #include "picoc.h"
#define isCidstart(c) (isalpha(c) || (c)=='_') #define isCidstart(c) (isalpha(c) || (c)=='_')
#define isCident(c) (isalnum(c) || (c)=='_')
#define LEXINC Lexer->Pos++ #define LEXINC Lexer->Pos++
#define NEXTIS(c,x,y) { if (NextChar == (c)) { LEXINC; return (x); } else return (y); } #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) enum LexToken LexGetNumber(struct LexState *Lexer)
{ {
// XXX Lexer->Value.Integer = strtol(Lexer->Pos, &Lexer->Pos, 10);
return TokenIntegerConstant; return TokenIntegerConstant;
} }
enum LexToken LexGetWord(struct LexState *Lexer) 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; return TokenIdentifier;
} }
@ -75,7 +90,11 @@ enum LexToken LexGetStringConstant(struct LexState *Lexer)
enum LexToken LexGetCharacterConstant(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; return TokenCharacterConstant;
} }

View file

@ -107,6 +107,11 @@ struct FuncDef
int StartLine; int StartLine;
}; };
union AnyValue
{
int Integer;
Str String;
};
/* lexer state - so we can lex nested files */ /* lexer state - so we can lex nested files */
struct LexState struct LexState
@ -115,6 +120,7 @@ struct LexState
const char *Pos; const char *Pos;
const char *End; const char *End;
const Str *FileName; const Str *FileName;
union AnyValue Value;
}; };