-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_full.py
More file actions
142 lines (128 loc) · 3.94 KB
/
Copy pathtest_full.py
File metadata and controls
142 lines (128 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""全面检查编译器功能"""
from compiler.compiler import Compiler
def test_all_features():
compiler = Compiler()
# 测试用例
test_cases = [
{
'name': '基础变量和运算',
'code': """int main() {
int x = 10;
int y = 20;
return x + y;
}""",
'expected_tokens': ['INT', 'IDENTIFIER', 'LEFT_PAREN', 'RIGHT_PAREN', 'LEFT_BRACE',
'INT', 'IDENTIFIER', 'ASSIGN', 'INTEGER_LITERAL', 'SEMICOLON',
'INT', 'IDENTIFIER', 'ASSIGN', 'INTEGER_LITERAL', 'SEMICOLON',
'RETURN', 'IDENTIFIER', 'PLUS', 'IDENTIFIER', 'SEMICOLON',
'RIGHT_BRACE']
},
{
'name': '条件语句',
'code': """int main() {
int a = 5;
if (a > 3) {
return 1;
} else {
return 0;
}
}"""
},
{
'name': '循环语句',
'code': """int main() {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum = sum + i;
}
return sum;
}"""
},
{
'name': '函数调用',
'code': """int add(int a, int b) {
return a + b;
}
int main() {
return add(3, 5);
}"""
},
{
'name': '数组操作',
'code': """int main() {
int arr[5];
arr[0] = 100;
return arr[0];
}"""
},
{
'name': '指针操作',
'code': """int main() {
int x = 42;
int *ptr = &x;
return *ptr;
}"""
},
{
'name': '复杂表达式',
'code': """int main() {
int a = 1, b = 2, c = 3;
return a * b + c / 2;
}"""
},
{
'name': '递归函数',
'code': """int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
return factorial(5);
}"""
}
]
print("=" * 70)
print("全面测试编译器功能")
print("=" * 70)
all_passed = True
for i, tc in enumerate(test_cases, 1):
print(f"\n[{i}/{len(test_cases)}] 测试: {tc['name']}")
print("-" * 50)
try:
result = compiler.compile(tc['code'])
if result['success']:
print(f"✓ 编译成功")
# 检查词法分析
if 'lexer_result' in result and result['lexer_result']:
tokens = result['lexer_result']
print(f" - 词法分析: {tokens['total_tokens']} 个 tokens")
# 检查语法分析
if 'parser_result' in result and result['parser_result']:
ast = result['parser_result']
print(f" - 语法分析: AST类型 = {ast['type']}")
# 检查语义分析
if 'semantic_result' in result and result['semantic_result']:
sem = result['semantic_result']
symbols = sem.get('global_symbols', [])
print(f" - 语义分析: {len(symbols)} 个全局符号")
# 检查代码生成
if 'code_result' in result and result['code_result']:
code = result['code_result']
print(f" - 代码生成: {code['lines']} 行汇编")
print(" ✓ 所有阶段通过")
else:
print(f"✗ 编译失败")
for err in result.get('errors', []):
print(f" Error: {err}")
all_passed = False
except Exception as e:
print(f"✗ 异常: {e}")
all_passed = False
print("\n" + "=" * 70)
if all_passed:
print("✓ 所有测试通过!")
else:
print("✗ 部分测试失败")
print("=" * 70)
if __name__ == "__main__":
test_all_features()