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

48
scripts/run_all_tests.sh Normal file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
# 批量测试所有.sy文件的语法解析
test_dir="/home/lingli/nudt-compiler-cpp/test/test_case/functional"
compiler="/home/lingli/nudt-compiler-cpp/build/bin/compiler"
if [ ! -f "$compiler" ]; then
echo "错误:编译器不存在,请先构建项目"
exit 1
fi
success_count=0
failed_count=0
failed_tests=()
echo "开始测试所有.sy文件的语法解析..."
echo "="
# 获取所有.sy文件并排序
for test_file in $(find "$test_dir" -name "*.sy" | sort); do
echo "测试: $(basename "$test_file")"
# 运行解析测试,将输出重定向到/dev/null
"$compiler" --emit-parse-tree "$test_file" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo " ✓ 成功"
((success_count++))
else
echo " ✗ 失败"
((failed_count++))
failed_tests+=($(basename "$test_file"))
fi
done
echo "="
echo "测试完成!"
echo "总测试数: $((success_count + failed_count))"
echo "成功: $success_count"
echo "失败: $failed_count"
if [ $failed_count -gt 0 ]; then
echo "失败的测试用例:"
for test in "${failed_tests[@]}"; do
echo " - $test"
done
fi

View File

@@ -171,21 +171,74 @@ blockItem
; ;
stmt stmt
: returnStmt : assignStmt
| returnStmt
| blockStmt
| ifStmt
| whileStmt
| breakStmt
| continueStmt
| expStmt
;
expStmt
: exp SEMICOLON
;
assignStmt
: lValue ASSIGN exp SEMICOLON
; ;
returnStmt returnStmt
: RETURN (exp)? SEMICOLON : 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 exp
: LPAREN exp RPAREN # parenExp : LPAREN exp RPAREN # parenExp
| lValue # lValueExp | lValue # lValueExp
| number # numberExp | 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 funcRParams
: ID (LBRACK exp RBRACK)* : exp (COMMA exp)*
; ;
number number