-
Notifications
You must be signed in to change notification settings - Fork 0
add if statement #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,14 +14,23 @@ int yyerror(const char *s); | |
| int int_value; | ||
| struct Expression* expression; | ||
| struct Statement *statement; | ||
| struct StatementList *statement_list; | ||
| struct Block *block; | ||
| struct Elseif *elseif; | ||
| } | ||
|
|
||
| %token <int_value> BOOL_LITERAL | ||
| %token <double_value> DOUBLE_LITERAL | ||
| %token <int_value> INT_LITERAL | ||
| %token <identifier> IDENTIFIER; | ||
| %token FUNCTION IF ELSE FOR RETURN BREAK CONTINUE NIL | ||
|
|
||
| %type <expression> expr bool_expr | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 逻辑没啥问题,就是感觉摆在一起有些突兀,应该是一般表达式,bool表达式 |
||
| %type <statement> stmt if_stmt | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上,一般语句,if语句 |
||
| %type <statement_list> stmt_list | ||
| %type <block> block | ||
| %type <elseif> elseif elseif_list | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. %nonassoc '\n' 不要了?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 之前发现会冲突,加了这个,文法改了之后没有这个问题 |
||
| %nonassoc '\n' | ||
| %nonassoc '=' | ||
| %left AND OR | ||
| %nonassoc EQ NE | ||
|
|
@@ -31,18 +40,39 @@ int yyerror(const char *s); | |
| %nonassoc MINUS | ||
| %nonassoc NOT | ||
|
|
||
| %type <expression> expr | ||
| %type <statement> stmt | ||
|
|
||
| %% | ||
|
|
||
| stmt_list: | ||
| stmt | ||
| | stmt_list stmt | ||
| stmt {$$ = allocStatementList($1); } | ||
| | stmt_list '\n' stmt {$$ = chainStatementList($1, $3); } | ||
| ; | ||
|
|
||
| stmt: | ||
| expr '\n' { printExprValue(evalExpression($1)); } | ||
| expr { printExprValue(evalExpression($1)); } | ||
| | IDENTIFIER '=' expr { $$ = allocAssignStatement($1, $3); } | ||
| | block { $$ = allocBlockStatement($1); } | ||
| | if_stmt | ||
| ; | ||
|
|
||
| if_stmt: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这4类if场景最好有个示意图,$有些晕 |
||
| IF '(' bool_expr ')' block { $$ = allocIfStatement($3, $5, NULL, NULL); } | ||
| | IF '(' bool_expr ')' block ELSE block { $$ = allocIfStatement($3, $5, NULL, $7); } | ||
| | IF '(' bool_expr ')' block elseif_list { $$ = allocIfStatement($3, $5, $6, NULL); } | ||
| | IF '(' bool_expr ')' block elseif_list ELSE block { $$ = allocIfStatement($3, $5, $6, $8); } | ||
| ; | ||
|
|
||
| elseif_list: | ||
| elseif | ||
| | elseif_list elseif { $$ = chainElseifList($1, $2); } | ||
| ; | ||
|
|
||
| elseif: | ||
| ELSE IF '(' bool_expr ')' block { $$ = allocElseif($4, $6); } | ||
| ; | ||
|
|
||
| block: | ||
| '{' stmt_list '}' { $$ = allocBlock($2); } | ||
| | '{' '}' { $$ = allocBlock(NULL); } | ||
| ; | ||
|
|
||
| expr: | ||
|
|
@@ -54,18 +84,23 @@ expr: | |
| | expr '-' expr { $$ = allocBinaryExpression(SUB_EXPRESSION, $1, $3); } | ||
| | expr '*' expr { $$ = allocBinaryExpression(MUL_EXPRESSION, $1, $3); } | ||
| | expr '/' expr { $$ = allocBinaryExpression(DIV_EXPRESSION, $1, $3); } | ||
| | expr '>' expr { $$ = allocBinaryExpression(GT_EXPRESSION, $1, $3); } | ||
| | expr GE expr { $$ = allocBinaryExpression(GE_EXPRESSION, $1, $3); } | ||
| | expr '<' expr { $$ = allocBinaryExpression(LT_EXPRESSION, $1, $3); } | ||
| | expr LE expr { $$ = allocBinaryExpression(LE_EXPRESSION, $1, $3); } | ||
| | expr EQ expr { $$ = allocBinaryExpression(EQ_EXPRESSION, $1, $3); } | ||
| | expr NE expr { $$ = allocBinaryExpression(NE_EXPRESSION, $1, $3); } | ||
| | expr AND expr { $$ = allocBinaryExpression(AND_EXPRESSION, $1, $3); } | ||
| | expr OR expr { $$ = allocBinaryExpression(OR_EXPRESSION, $1, $3); } | ||
| | '!' expr %prec NOT { $$ = allocUnaryExpression(NOT_EXPRESSION, $2); } | ||
| | '-' expr %prec MINUS { $$ = allocUnaryExpression(MINUS_EXPRESSION, $2); } | ||
| | '(' expr ')' { $$ = $2; } | ||
| | bool_expr | ||
| ; | ||
|
|
||
| bool_expr: | ||
| expr '>' expr { $$ = allocBinaryExpression(GT_EXPRESSION, $1, $3); } | ||
| | expr GE expr { $$ = allocBinaryExpression(GE_EXPRESSION, $1, $3); } | ||
| | expr '<' expr { $$ = allocBinaryExpression(LT_EXPRESSION, $1, $3); } | ||
| | expr LE expr { $$ = allocBinaryExpression(LE_EXPRESSION, $1, $3); } | ||
| | expr EQ expr { $$ = allocBinaryExpression(EQ_EXPRESSION, $1, $3); } | ||
| | expr NE expr { $$ = allocBinaryExpression(NE_EXPRESSION, $1, $3); } | ||
| | expr AND expr { $$ = allocBinaryExpression(AND_EXPRESSION, $1, $3); } | ||
| | expr OR expr { $$ = allocBinaryExpression(OR_EXPRESSION, $1, $3); } | ||
| | '!' expr %prec NOT { $$ = allocUnaryExpression(NOT_EXPRESSION, $2); } | ||
| ; | ||
|
|
||
| %% | ||
|
|
||
| int yyerror(char const *str) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,24 @@ | |
|
|
||
| %% | ||
|
|
||
| [+\-*/\(\)<>!\n] { | ||
| [+\-*/\(\)<>!{}\n] { | ||
| return *yytext; | ||
| } | ||
|
|
||
| ">=" return GE; | ||
| "<=" return LE; | ||
| "==" return EQ; | ||
| "!=" return NE; | ||
| "&&" return AND; | ||
| "||" return OR; | ||
| ">=" return GE; | ||
| "<=" return LE; | ||
| "==" return EQ; | ||
| "!=" return NE; | ||
| "&&" return AND; | ||
| "||" return OR; | ||
| "if" return IF; | ||
| "else" return ELSE; | ||
| "for" return FOR; | ||
| "nil" return NIL; | ||
| "func" return FUNCTION; | ||
| "return" return RETURN; | ||
| "break" return BREAK; | ||
| "continue" return CONTINUE; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 漏了lock? |
||
|
|
||
| "true"|"false" { | ||
| yylval.int_value = strcmp(yytext, "false"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个只是符号,在lex中实际匹配的就是"func"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯嗯,lex里看到了,删除了