%{ #include #include #include #include "gamebuilder.h" #include "astnode.h" typedef void *yyscan_t; %} %locations %pure-parser /* %define api.pure */ %debug %error-verbose %parse-param { yyscan_t yyscanner } %lex-param { yysacn_t yyscanner } %parse-param { GameBuilder &bld } %union { int64_t iValue; double dValue; Bu::String *sValue; bool bValue; } %{ int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *error ) { printf("%d:%d-%d:%d: %s\n", llocp->first_line, llocp->first_column, llocp->last_line, llocp->last_column, error ); } %} %token tokGame %token tokGlobal %token tokFunction %token tokSituation %token tokSetup %token tokEnter %token tokAnd %token tokOr %token tokWhile %token tokFor %token tokEach %token tokDo %token tokIn %token tokIf %token tokThen "then" %token tokElse %token tokNot %token tokCommand %token tokOption %token tokPlayer %token tokLtEq %token tokGtEq %token tokCmp %token tokReturn %token tokIgnore %token tokPlusAssign %token tokMinusAssign %token tokTimesAssign %token tokDivideAssign %token tokSituationName %token tokIdent %token tokGoto %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 NOT %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign %left tokIn tokAnd tokOr %left '-' '+' %left '*' '/' %right '<' '>' tokLtEq tokGtEq tokCmp %left '(' ')' '[' ']' %left NEG %% input: gameDecls globalDecl bodyDecl ; gameDecls: | gameDecls tokGame '.' tokIdent '=' literal ';' { bld.setGameParam( *($4) ); } ; globalDecl: | tokGlobal '{' { bld.beginGlobal(); } globalExprList '}' { bld.closeGlobal(); } ; globalExprList: | globalExprList cmpltGlobalExpr ; cmpltGlobalExpr: globalExpr ';' | commandDecl | ignoreDecl ; globalExpr: tokIdent '=' expr ; bodyDecl: | bodyDecl situation | bodyDecl function ; situationMode: tokSituation tokSituationName { bld.beginSituation( *($2), Situation::inputCommand ); } | tokCommand tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputCommand ); } | tokOption tokSituation tokSituationName { bld.beginSituation( *($3), Situation::inputOption ); } ; situation: situationMode '{' situationMembers '}' { bld.endSituation(); } ; situationMembers: | situationMembers situationModeFunc | situationMembers commandDecl | situationMembers optionDecl | situationMembers ignoreDecl ; situationModeFunc: situationMode '{' cmpltExprList '}' { bld.closeSituationMode(); } ; situationMode: tokSetup { bld.beginSituationMode( Situation::modeSetup ); } | tokEnter { bld.beginSituationMode( Situation::modeEnter ); } ; function: tokFunction tokIdent { bld.beginFunction( *($2) ); } '(' funcParamList ')' { bld.endFunctionParams(); } '{' cmpltExprList '}' { bld.endFunction(); } ; funcParamList: | tokIdent { bld.addFunctionParam( *($1) ); } funcParamListEx ; funcParamListEx: | funcParamListEx ',' tokIdent { bld.addFunctionParam( *($3) ); } ; cmpltExprList: | cmpltExprList cmpltExpr ; cmpltExpr: expr ';' { bld.endCmpltExpr(); } | tokReturn '(' expr ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();} | tokReturn '(' ')' ';' { bld.addNode( AstNode::tReturn ); bld.endCmpltExpr();} | tokGoto '(' expr ')' ';' { bld.addNode( AstNode::tGoto ); bld.endCmpltExpr();} | ifbase | tokFor tokEach { bld.addNode( AstNode::tForEach ); bld.addNode( AstNode::tScope ); } forIterator tokIn { bld.closeNode(); bld.addNode( AstNode::tScope ); } expr tokDo '{' { bld.closeNode(); bld.addNode( AstNode::tScope ); } cmpltExprList '}' { bld.closeNode(); bld.closeNode(); } | tokWhile { bld.addNode( AstNode::tWhile ); bld.addNode( AstNode::tScope ); } expr { bld.closeNode(); bld.addNode( AstNode::tScope ); } tokDo '{' cmpltExprList '}' { bld.closeNode(); bld.closeNode(); } ; forIterator: tokIdent { bld.addVarRef( *($1), sidLocal ); } | tokIdent ':' tokIdent { bld.addVarRef( *($1), sidLocal ); bld.addVarRef( *($3), sidLocal ); } ; ifbase: tokIf { bld.addNode( AstNode::tIf ); bld.addNode( AstNode::tScope ); } expr { bld.closeNode(); bld.addNode( AstNode::tScope ); } tokThen '{' cmpltExprList '}' { bld.closeNode(); } ifnext { bld.closeNode(); } ; ifnext: | tokElse { bld.addNode( AstNode::tScope ); } '{' cmpltExprList '}' { bld.closeNode(); } | tokElse { bld.addNode( AstNode::tScope ); } ifbase { bld.closeNode(); } ; varRef: tokIdent { bld.addVarRef( *($1), sidLocal ); } | tokPlayer '.' tokIdent { bld.addVarRef( *($3), sidPlayer ); } | tokGlobal '.' tokIdent { bld.addVarRef( *($3), sidGlobal ); } | tokSituation '.' tokIdent { bld.addVarRef( *($3), sidSituation ); } ; literal: tokInt { bld.addLiteral( Variable( $1 ) ); } | tokFloat { bld.addLiteral( Variable( $1 ) ); } | tokString { bld.addLiteral( Variable( *($1) ) ); } | tokBool { bld.addLiteral( Variable( $1 ) ); } | tokNull { bld.addLiteral( Variable( Variable::tNull ) ); } | tokSituationName { bld.addLiteral( Variable::newSituationName( *($1) ) ); } ; expr: literal | tokIdent '(' funcCallParams ')' { bld.addFuncCall( *($1) ); } | varRef | varRef '=' expr { bld.addNode( AstNode::tStore ); } | varRef tokPlusAssign expr { bld.addNode( AstNode::tPlusStore ); } | varRef tokMinusAssign expr { bld.addNode( AstNode::tMinusStore ); } | varRef tokTimesAssign expr { bld.addNode( AstNode::tMultiplyStore ); } | varRef tokDivideAssign expr { bld.addNode( AstNode::tDivideStore ); } | expr '+' expr { bld.addNode( AstNode::tPlus ); } | expr '-' expr { bld.addNode( AstNode::tMinus ); } | expr '/' expr { bld.addNode( AstNode::tDivide ); } | expr '*' expr { bld.addNode( AstNode::tMultiply ); } | expr tokAnd expr { bld.addNode( AstNode::tAnd ); } | expr tokOr expr { bld.addNode( AstNode::tOr ); } | expr tokIn expr { bld.addNode( AstNode::tIn ); } | expr tokCmp expr { bld.addNode( AstNode::tComp ); } | expr '<' expr { bld.addNode( AstNode::tCompLt ); } | expr '>' expr { bld.addNode( AstNode::tCompGt ); } | expr tokLtEq expr { bld.addNode( AstNode::tCompLtEq ); } | expr tokGtEq expr { bld.addNode( AstNode::tCompGtEq ); } | '(' expr ')' | expr '[' expr ']' { bld.addNode( AstNode::tIndex ); } | expr '[' expr ']' tokPlusAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tPlusStore ); } | expr '[' expr ']' tokMinusAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tMinusStore ); } | expr '[' expr ']' tokTimesAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tMultiplyStore ); } | expr '[' expr ']' tokDivideAssign { bld.addNode( AstNode::tIndex ); } expr { bld.addNode( AstNode::tDivideStore ); } | expr '[' expr ']' '=' expr { bld.addNode( AstNode::tInsert ); } | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' | '{' '}' { bld.addLiteral( Variable( Variable::tDictionary ) ); } | '{' { bld.addLiteral( Variable( Variable::tDictionary ) ); } dictValues '}' | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } ; funcCallParams: | expr funcCallParamsEx { bld.stackMod( AstNode::tPop ); } ; funcCallParamsEx: | funcCallParamsEx ',' expr { bld.stackMod( AstNode::tPop ); } ; listValues: expr { bld.addNode( AstNode::tAppend ); } | listValues ',' expr { bld.addNode( AstNode::tAppend ); } ; dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); } | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } ; optionDecl: tokOption ':' tokString { bld.beginOption( *$3 ); } '{' cmpltExprList '}' { bld.closeOption(); } ; commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } commandParamList '{' { bld.endCommandParams(); } cmpltExprList '}' { bld.closeCommand(); } ; commandParamList: | commandParamList tokString { bld.addCommandLiteral( *$2 ); } | commandParamList tokIdent { bld.addCommandParam( *$2 ); } ; ignoreDecl: tokIgnore ':' tokString ignoreList ';' ; ignoreList: | ignoreList tokString ; %% /* void yylex_init( yyscan_t * ); void yylex_destroy( yyscan_t ); int main() { yyscan_t scanner; yylex_init ( &scanner ); yyparse( scanner ); yylex_destroy ( scanner ); return 0; } */