-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoSpeed.cpp
More file actions
125 lines (107 loc) · 2.99 KB
/
Copy pathAutoSpeed.cpp
File metadata and controls
125 lines (107 loc) · 2.99 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
#include <iostream>
#include <vector>
#include "scanner.h"
#include "parser.h"
#include "ast_printer.h"
int main() {
// ===== All Test Programs =====
std::vector<std::string> tests = {
// TEST 1
R"(ignite() {
announce "Start!";
finishline 0;
})",
// TEST 2
R"(ignite() {
looplap (5 > 0) {
announce "Lap!";
}
finishline 0;
})",
// TEST 3
R"(ignite() {
looplap (3 > 1) {
announce "Lap 1!";
announce "Lap 2!";
}
finishline 0;
})",
// TEST 4
R"(ignite() {
gear fuel = 100;
fuel = fuel - 10;
announce fuel;
finishline 0;
})",
// TEST 5
R"(ignite() {
gear fuel = 20;
track (fuel < 30) {
announce "Low fuel!";
}
finishline 0;
})",
// TEST 6
R"(ignite() {
gear fuel = 20;
track (fuel < 30) {
announce "Low fuel!";
}
pitstop {
announce "Refueling...";
}
finishline 0;
})",
// TEST 7
R"(engine boost() {
announce "Boosting!";
}
ignite() {
announce "Race start!";
finishline 0;
})",
// TEST 8
R"(ignite() {
announce "Start";
{
announce "Inside nested";
}
finishline 0;
})",
// TEST 9 — invalid (should FAIL)
R"(ignite() {
looplap (5 > ) {
announce "Bad code";
}
finishline 0;
})"
};
int test_number = 1;
// ===== Run all tests =====
for (auto& code : tests) {
std::cout << "\n=====================================\n";
std::cout << "TEST #" << test_number++ << "\n";
std::cout << "=====================================\n";
try {
// ===== Scanner =====
auto tokens = scan(code);
std::cout << "TOKENS:\n";
for (auto& t : tokens) {
std::cout << "[" << t.line << "] "
<< tokenTypeToString(t.type)
<< " : " << t.value << "\n";
}
// ===== Parser =====
Parser parser(tokens);
auto stmts = parser.parse();
std::cout << "\n PARSE SUCCESS — " << stmts.size() << " statement(s)\n";
// ===== AST Printer =====
AstPrinter printer;
std::cout << "\nAST:\n" << printer.print(stmts) << "\n";
}
catch (std::exception& e) {
std::cout << "\n PARSE FAILED: " << e.what() << "\n";
}
}
return 0;
}