feat: 实现控制流语句、表达式优先级及测试脚本

This commit is contained in:
2026-03-23 09:45:12 +08:00
committed by CGH0S7
parent 7115f7e49d
commit 995e159f6f
2 changed files with 104 additions and 3 deletions

View File

@@ -171,21 +171,74 @@ blockItem
;
stmt
: returnStmt
: assignStmt
| returnStmt
| blockStmt
| ifStmt
| whileStmt
| breakStmt
| continueStmt
| expStmt
;
expStmt
: exp SEMICOLON
;
assignStmt
: lValue ASSIGN exp SEMICOLON
;
returnStmt
: RETURN (exp)? SEMICOLON
;
ifStmt
: IF LPAREN exp RPAREN stmt (ELSE stmt)?
;
whileStmt
: WHILE LPAREN exp RPAREN stmt
;
breakStmt
: BREAK SEMICOLON
;
continueStmt
: CONTINUE SEMICOLON
;
// 表达式
lValue
: ID (LBRACK exp RBRACK)*
;
exp
: LPAREN exp RPAREN # parenExp
| lValue # lValueExp
| number # numberExp
| ID LPAREN (funcRParams)? RPAREN # funcCallExp
| NOT exp # notExp
| ADD exp # unaryAddExp
| SUB exp # unarySubExp
| exp MUL exp # mulExp
| exp DIV exp # divExp
| exp MOD exp # modExp
| exp ADD exp # addExp
| exp SUB exp # subExp
| exp LT exp # ltExp
| exp LE exp # leExp
| exp GT exp # gtExp
| exp GE exp # geExp
| exp EQ exp # eqExp
| exp NE exp # neExp
| exp AND exp # andExp
| exp OR exp # orExp
;
lValue
: ID (LBRACK exp RBRACK)*
funcRParams
: exp (COMMA exp)*
;
number