summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkugwa <kugwa2000@gmail.com>2015-11-13 16:28:29 +0800
committerkugwa <kugwa2000@gmail.com>2015-11-13 16:28:29 +0800
commit3c8d9314f05ee784886bac4598e5e30bc053a4fc (patch)
tree99c6479768e012ab5880179bf024ab3ca995a91f
parent7110b00ad315a492ce7c874262815c557e93e248 (diff)
downloadcompiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar.gz
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar.bz2
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar.lz
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar.xz
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.tar.zst
compiler2015-3c8d9314f05ee784886bac4598e5e30bc053a4fc.zip
Implement IF and IF...ELSE statements
1. Use "%right DL_RPAREN ELSE" to solve SR conflict. 2. Fix a bug in assign_expr: token ID is not an AST_NODE.
-rw-r--r--src/parser.y12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/parser.y b/src/parser.y
index 47ce98e..1b164ca 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -51,6 +51,8 @@ extern int g_anyErrorOccur;
%token ERROR
%token RETURN
+%right DL_RPAREN ELSE
+
%{
#include "lexer.c"
@@ -386,16 +388,16 @@ stmt : DL_LBRACE block DL_RBRACE
$$ = makeStmtNode(ASSIGN_STMT);
makeFamily($$, 2, $1, $3);
}
- /*
| IF DL_LPAREN relop_expr DL_RPAREN stmt
{
$$ = makeStmtNode(IF_STMT);
- makeFamily($$, 2, $3, $5);
+ makeFamily($$, 3, $3, $5, Allocate(NUL_NODE));
}
- | TODO: if then else
+ | IF DL_LPAREN relop_expr DL_RPAREN stmt ELSE stmt
{
+ $$ = makeStmtNode(IF_STMT);
+ makeFamily($$, 3, $3, $5, $7);
}
- */
| ID DL_LPAREN relop_expr_list DL_RPAREN DL_SEMICOL
{
$$ = makeStmtNode(FUNCTION_CALL_STMT);
@@ -440,7 +442,7 @@ nonempty_assign_expr_list : nonempty_assign_expr_list DL_COMMA assign_exp
assign_expr : ID OP_ASSIGN relop_expr
{
$$ = makeStmtNode(ASSIGN_STMT);
- makeFamily($$, 2, $1, $3);
+ makeFamily($$, 2, makeIDNode($1, NORMAL_ID), $3);
}
| relop_expr
{