From 7c90fe68b557d32b65955ffd18e2e79585a7282b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 11 Dec 2011 21:57:44 -0700 Subject: Getting closer. Real turing-complete code is coming soon --- src/parser.l | 37 +++++++++++++++++++++++++++++++++---- src/parser.y | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 77 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/parser.l b/src/parser.l index d5ff735..dff6507 100644 --- a/src/parser.l +++ b/src/parser.l @@ -1,5 +1,6 @@ %{ #include +#include #include "parser.tab.h" %} @@ -7,7 +8,7 @@ %x sitname %x comment -%x tdqstr tsqstr +%x dqstr tdqstr tsqstr %% [-{}<>=+/*,();] { return yytext[0]; } @@ -25,18 +26,46 @@ else { return tokElse; } command { return tokCommand; } goto { return tokGoto; } +true { yylval.bValue = true; return tokBool; } +false { yylval.bValue = false; return tokBool; } +null { return tokNull; } + "<<" { BEGIN( sitname ); } -[- a-zA-Z0-9]+ { printf("situation: %s\n", yytext ); return tokSituationName; } +[- a-zA-Z0-9]+ { yylval.sValue = new Bu::String( yytext ); return tokSituationName; } ">>" { BEGIN( INITIAL ); } . REJECT; "//"[^\n]* {} -"/\*" { BEGIN( comment ); } +"/\*" { BEGIN( comment ); } "\*/" { BEGIN( INITIAL ); } . {} -[a-zA-Z_][a-zA-Z0-9_]* { return tokIdent; } +[a-zA-Z_][a-zA-Z0-9_]* { yylval.sValue = new Bu::String( yytext ); return tokIdent; } + +[1-9][0-9]* { + yylval.iValue = strtoll( yytext, NULL, 10 ); + return tokInt; +} + +([1-9][0-9]*)?\.[0-9]* { + yylval.dValue = strtod( yytext, NULL ); + return tokFloat; +} + +\"\"\" { BEGIN( tdqstr ); yylval.sValue = new Bu::String(); } +[^"]+ { (*yylval.sValue) += yytext; } +\" { (*yylval.sValue) += yytext; } +\"\"\" { BEGIN( INITIAL ); return tokString; } + +\'\'\' { BEGIN( tsqstr ); yylval.sValue = new Bu::String(); } +[^']+ { (*yylval.sValue) += yytext; } +\' { (*yylval.sValue) += yytext; } +\'\'\' { BEGIN( INITIAL ); return tokString; } + +\" { BEGIN( dqstr ); yylval.sValue = new Bu::String(); } +[^"]+ { (*yylval.sValue) += yytext; } +\" { BEGIN( INITIAL ); return tokString; } [ \t\n\r]+ {} diff --git a/src/parser.y b/src/parser.y index b3da848..9fb2be9 100644 --- a/src/parser.y +++ b/src/parser.y @@ -1,6 +1,7 @@ %{ #include #include +#include int yylex(); void yyerror( const char *error ) { printf("%s\n", error ); } @@ -9,7 +10,8 @@ void yyerror( const char *error ) { printf("%s\n", error ); } %union { int64_t iValue; double dValue; - char *sValue; + Bu::String *sValue; + bool bValue; } %token tokGame @@ -23,15 +25,17 @@ void yyerror( const char *error ) { printf("%s\n", error ); } %token tokThen %token tokElse %token tokCommand -%token tokSituationName -%token tokIdent +%token tokSituationName +%token tokIdent %token tokGoto -%token tokString -%token tokInt -%token tokFloat -%token tokBool +%token tokString +%token tokInt +%token tokFloat +%token tokBool %token tokNull +%destructor { delete $$; printf("string deleted.\n"); } tokString tokSituationName + %token eos 0 "end of stream" %left '(' ')' '[' ']' @@ -39,12 +43,28 @@ void yyerror( const char *error ) { printf("%s\n", error ); } %left '-' '+' %% -input: - | input situation - | input function +input: gameDecl bodyDecl ; -situation: tokSituation tokSituationName '{' cmpltExprList '}' +gameDecl: tokGame '{' gameExprList '}' + ; + +gameExprList: + | gameExprList cmpltGameExpr + ; + +cmpltGameExpr: gameExpr ';' + ; + +gameExpr: tokIdent '=' expr + ; + +bodyDecl: + | bodyDecl situation + | bodyDecl function + ; + +situation: tokSituation tokSituationName { printf("Read situtaion: %s\n", (*$2).getStr() ); } '{' cmpltExprList '}' ; function: tokFunction tokIdent '(' ')' '{' '}' @@ -55,6 +75,7 @@ cmpltExprList: ; cmpltExpr: expr ';' + | tokGoto '(' expr ')' ';' ; expr: tokInt @@ -69,8 +90,19 @@ expr: tokInt | expr '*' expr | '(' expr ')' | expr '[' expr ']' - | tokGoto '(' expr ')' + | '[' ']' + | '[' listValues ']' + | '{' '}' + | '{' dictValues '}' ; + +listValues: expr + | listValues ',' expr + ; + +dictValues: expr ':' expr + | dictValues ',' expr ':' expr + ; %% int main() -- cgit v1.2.3