From 50c749450849362f079a13ba9e2efd35f8bae7ed Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Thu, 16 Oct 2008 07:04:23 +0000 Subject: [PATCH] Starting the parser git-svn-id: http://picoc.googlecode.com/svn/trunk@8 21eae674-98b7-11dd-bd71-f92a316d2d60 --- lex.c | 9 +++++++-- parse.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lex.c b/lex.c index b3e0ed4..d89c6f5 100644 --- a/lex.c +++ b/lex.c @@ -20,14 +20,19 @@ struct ReservedWord static struct ReservedWord ReservedWords[] = { + { "break", TokenBreak }, + { "case", TokenCase }, { "char", TokenCharType }, + { "default", TokenDefault }, { "do", TokenDo }, { "else", TokenElse }, { "for", TokenFor }, { "if", TokenIf }, { "int", TokenIntType }, - { "while", TokenWhile }, - { "void", TokenVoidType } + { "return", TokenReturn }, + { "switch", TokenSwitch }, + { "void", TokenVoidType }, + { "while", TokenWhile } }; void LexInit(struct LexState *Lexer, const Str *Source, const Str *FileName, int Line) diff --git a/parse.c b/parse.c index 9832124..2ac5a50 100644 --- a/parse.c +++ b/parse.c @@ -13,6 +13,37 @@ void ParseInit() TableInit(&GlobalTable, &GlobalHashTable[0], "global", GLOBAL_TABLE_SIZE); } +/* parse and run some code */ +void ParseRun(const Str *FileName, const Str *Source, int LineNo) +{ + enum LexToken Token; + struct LexState Lexer; + int ParseDepth = 0; + int IntValue; + + LexInit(&Lexer, Source, FileName, 1); + + while ( (Token = LexGetToken(&Lexer)) != TokenEOF) + { + /* do parsey things here */ + StrPrintf("token %d\n", (int)Token); + + switch (ParseDepth) + { + case 0: /* top level */ + if (Token == XXX + break; + + case X: /* primary expression */ + switch (Token) + { + case TokenIdentifier: IntValue = XXX; GoUp = TRUE; break; + case TokenIntegerConstant: IntValue = Lexer->Value.Integer; GoUp = TRUE; break; + case TokenStringConstant: StringValue = Lexer->Value.String; GoUp = TRUE; break; + case TokenOpenBracket: + } + } +} /* quick scan a source file for definitions */ void ParseScan(const Str *FileName, const Str *Source) @@ -29,7 +60,6 @@ void ParseScan(const Str *FileName, const Str *Source) } } - void ParseCallFunction(const Str *FuncIdent, int argc, char **argv) { }