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
143
144
145
146
|
#include "cps/Parser.h"
#include "cps/FunctionAST.h"
#include <cstdio>
using namespace cps;
std::unique_ptr<StmtAST> Parser::ParseTryStmt() {
getNextToken();
auto Body = ParseBlock({tok_catch, tok_endtry});
if (CurTok != tok_catch) {
fprintf(stderr, "error: Expected CATCH in TRY at line %d\n", Lex.getLine());
return nullptr;
}
getNextToken();
auto CatchBody = ParseBlock({tok_endtry, tok_catch});
if (CurTok != tok_endtry) {
fprintf(stderr, "error: Expected ENDTRY after CATCH at line %d\n", Lex.getLine());
return nullptr;
}
getNextToken();
return std::make_unique<TryStmtAST>(std::move(Body), std::move(CatchBody));
}
std::unique_ptr<StmtAST> Parser::ParseIfStmt() {
getNextToken();
auto Cond = ParseExpression();
if (!Cond) return nullptr;
if (CurTok != tok_then) {
fprintf(stderr, "error: expected THEN\n");
return nullptr;
}
getNextToken();
std::vector<std::unique_ptr<StmtAST>> ThenStmts = ParseBlock({tok_else, tok_endif});
std::vector<std::unique_ptr<StmtAST>> ElseStmts;
if (CurTok == tok_else) {
getNextToken();
ElseStmts = ParseBlock({tok_endif});
}
if (CurTok != tok_endif) {
fprintf(stderr, "error: expected ENDIF\n");
return nullptr;
}
getNextToken();
return std::make_unique<IfStmtAST>(std::move(Cond), std::move(ThenStmts), std::move(ElseStmts));
}
std::unique_ptr<StmtAST> Parser::ParseWhileStmt() {
getNextToken();
auto Cond = ParseExpression();
if (!Cond) return nullptr;
if (CurTok != tok_do) {
fprintf(stderr, "error: expected DO after WHILE condition\n");
return nullptr;
}
getNextToken();
std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_endwhile});
if (CurTok != tok_endwhile) {
fprintf(stderr, "error: expected ENDWHILE\n");
return nullptr;
}
getNextToken();
return std::make_unique<WhileStmtAST>(std::move(Cond), std::move(Body));
}
std::unique_ptr<StmtAST> Parser::ParseRepeatStmt() {
getNextToken();
std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_until});
if (CurTok != tok_until) {
fprintf(stderr, "error: expected UNTIL\n");
return nullptr;
}
getNextToken();
auto Cond = ParseExpression();
if (!Cond) return nullptr;
return std::make_unique<RepeatStmtAST>(std::move(Body), std::move(Cond));
}
std::unique_ptr<StmtAST> Parser::ParseForStmt() {
getNextToken();
if (CurTok != tok_identifier) {
fprintf(stderr, "error: expected identifier after FOR\n");
return nullptr;
}
std::string VarName = Lex.IdentifierStr;
getNextToken();
if (CurTok != tok_assign) {
fprintf(stderr, "error: expected '<-' in FOR loop\n");
return nullptr;
}
getNextToken();
auto Start = ParseExpression();
if (!Start) return nullptr;
if (CurTok != tok_to) {
fprintf(stderr, "error: expected TO in FOR loop\n");
return nullptr;
}
getNextToken();
auto End = ParseExpression();
if (!End) return nullptr;
std::unique_ptr<ExprAST> Step = nullptr;
if (CurTok == tok_step) {
getNextToken();
Step = ParseExpression();
if (!Step) return nullptr;
}
std::vector<std::unique_ptr<StmtAST>> Body = ParseBlock({tok_next});
if (CurTok != tok_next) {
fprintf(stderr, "error: expected NEXT\n");
return nullptr;
}
getNextToken();
if (CurTok != tok_identifier) {
fprintf(stderr, "error: expected identifier after NEXT (e.g., NEXT %s)\n", VarName.c_str());
return nullptr;
}
if (Lex.IdentifierStr != VarName) {
fprintf(stderr, "error: NEXT identifier '%s' does not match FOR variable '%s'\n",
Lex.IdentifierStr.c_str(), VarName.c_str());
return nullptr;
}
getNextToken();
return std::make_unique<ForStmtAST>(VarName, std::move(Start), std::move(End), std::move(Step), std::move(Body));
}
|