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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
#include "cps/Diagnostics.h"
#include "cps/Lexer.h"
#include <algorithm>
#include <charconv>
#include <limits>
#include <cctype>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <unordered_map>
using namespace cps;
namespace {
const std::unordered_map<std::string, int> Keywords = {
{"PP_DEFINE", tok_pp_define},
{"PP_UNDEF", tok_pp_undef},
{"PP_IFDEF", tok_pp_ifdef},
{"PP_IFNDEF", tok_pp_ifndef},
{"PP_ENDIF", tok_pp_endif},
{"CONSTANT", tok_constant},
{"INCLUDE", tok_include},
{"INCLUDE_OBJ", tok_include_obj},
{"EXTERN", tok_extern},
{"EXTERN_FUNCTION", tok_extern_function},
{"EXTERN_PROCEDURE", tok_extern_procedure},
{"GLOBAL", tok_global},
{"ENDGLOBAL", tok_endglobal},
{"DECLARE", tok_declare},
{"INTEGER", tok_integer_kw},
{"BOOLEAN", tok_boolean_kw},
{"REAL", tok_real_kw},
{"STRING", tok_string_kw},
{"CHAR", tok_char_kw},
{"C_TYPE", tok_c_type},
{"C_INT", tok_c_int},
{"C_CHAR", tok_c_char},
{"C_STRING", tok_c_string},
{"C_DOUBLE", tok_c_double},
{"TRUE", tok_true},
{"FALSE", tok_false},
{"INPUT", tok_input},
{"OUTPUT", tok_output},
{"CASE", tok_case},
{"ENDCASE", tok_endcase},
{"OTHERWISE", tok_otherwise},
{"IF", tok_if},
{"THEN", tok_then},
{"ELSE", tok_else},
{"ENDIF", tok_endif},
{"TRY", tok_try},
{"CATCH", tok_catch},
{"ENDTRY", tok_endtry},
{"THROW", tok_throw},
{"WHILE", tok_while},
{"ENDWHILE", tok_endwhile},
{"REPEAT", tok_repeat},
{"UNTIL", tok_until},
{"FOR", tok_for},
{"TO", tok_to},
{"STEP", tok_step},
{"NEXT", tok_next},
{"ARRAY", tok_array},
{"OF", tok_of},
{"DIV", tok_div},
{"MOD", tok_mod},
{"AND", tok_and},
{"OR", tok_or},
{"NOT", tok_not},
{"OPERATOR", tok_operator},
{"OVERLOAD", tok_overload},
{"TEMPLATE", tok_template},
{"FUNCTION", tok_function},
{"ENDFUNCTION", tok_endfunction},
{"PROCEDURE", tok_procedure},
{"ENDPROCEDURE", tok_endprocedure},
{"RETURN", tok_return},
{"RETURNS", tok_returns},
{"DEFAULT", tok_default},
{"CALL", tok_call},
{"BYREF", tok_byref},
{"BYVAL", tok_byval},
{"OPENFILE", tok_openfile},
{"READFILE", tok_readfile},
{"WRITEFILE", tok_writefile},
{"CLOSEFILE", tok_closefile},
{"READ", tok_read},
{"WRITE", tok_write},
{"APPEND", tok_append},
{"RANDOM", tok_random},
{"TYPE", tok_type},
{"ENDTYPE", tok_endtype},
{"SET", tok_set},
{"DEFINE", tok_define},
{"NEW", tok_new},
{"DELETE", tok_delete},
{"CLASS", tok_class},
{"ENDCLASS", tok_endclass},
{"PUBLIC", tok_public},
{"PRIVATE", tok_private},
};
}
std::string Lexer::tokenSpelling(int Tok, const Lexer &Lex) {
switch (Tok) {
case tok_eof: return "end of file";
case tok_identifier: return "identifier '" + Lex.IdentifierStr + "'";
case tok_number_int:
case tok_number_real: return "number";
case tok_string_literal:
case tok_c_string_literal: return "string literal";
case tok_char_literal: return "character literal";
case tok_assign: return "'<-'";
case tok_eq: return "'='";
case tok_ne: return "'<>'";
case tok_le: return "'<='";
case tok_ge: return "'>='";
case tok_colon: return "':'";
case tok_ellipsis: return "'...'";
default: break;
}
for (const auto &[Name, Kind] : Keywords)
if (Kind == Tok) return Name;
if (Tok > 0) return std::string("'") + static_cast<char>(Tok) + "'";
return "unknown token";
}
void Lexer::reportError(int Line, const char *Format, ...) {
va_list Args;
va_start(Args, Format);
cps::vsourceDiagnostic(cps::Severity::Error, Line, Format, Args);
va_end(Args);
HadError = true;
}
std::string Lexer::readEscape(bool IsChar) {
auto Error = [&](const char *Reason) {
reportError(TokenLine, "%s", Reason);
return std::string("?");
};
int Escape = Input.get();
if (Escape == EOF || Escape == '\n' || Escape == '\r') {
LastChar = Escape;
return Error("Unterminated escape sequence");
}
LastChar = Input.get();
switch (Escape) {
case '\'': return "'";
case '"': return "\"";
case '?': return "?";
case '\\': return "\\";
case 'a': return "\a";
case 'b': return "\b";
case 'f': return "\f";
case 'n': return "\n";
case 'r': return "\r";
case 't': return "\t";
case 'v': return "\v";
default: break;
}
auto Hex = [](int C) -> int {
if (C >= '0' && C <= '9') return C - '0';
if (C >= 'a' && C <= 'f') return C - 'a' + 10;
if (C >= 'A' && C <= 'F') return C - 'A' + 10;
return -1;
};
uint32_t Value = 0;
if (Escape >= '0' && Escape <= '7') {
Value = Escape - '0';
for (int I = 1; I < 3 && LastChar >= '0' && LastChar <= '7'; ++I) {
Value = Value * 8 + LastChar - '0';
LastChar = Input.get();
}
if (Value > 255) return Error("Octal escape exceeds one byte");
return std::string(1, static_cast<char>(Value));
}
if (Escape == 'x') {
if (Hex(LastChar) < 0) return Error("Hex escape requires a digit");
bool Overflow = false;
while (Hex(LastChar) >= 0) {
Value = Value * 16 + Hex(LastChar);
if (Value > 255) { Overflow = true; Value = 0; }
LastChar = Input.get();
}
if (Overflow) return Error("Hex escape exceeds one byte");
return std::string(1, static_cast<char>(Value));
}
if (Escape != 'u' && Escape != 'U') return Error("Unknown escape sequence");
for (int I = 0; I < (Escape == 'u' ? 4 : 8); ++I) {
if (Hex(LastChar) < 0) return Error("Incomplete Unicode escape");
Value = Value * 16 + Hex(LastChar);
LastChar = Input.get();
}
if (Value > 0x10ffff || (Value >= 0xd800 && Value <= 0xdfff))
return Error("Invalid Unicode code point");
if (IsChar && Value > 127) return Error("Unicode CHAR escape must be ASCII");
std::string Result;
if (Value <= 0x7f) Result += static_cast<char>(Value);
else {
if (Value <= 0x7ff) Result += static_cast<char>(0xc0 | (Value >> 6));
else {
if (Value <= 0xffff) Result += static_cast<char>(0xe0 | (Value >> 12));
else {
Result += static_cast<char>(0xf0 | (Value >> 18));
Result += static_cast<char>(0x80 | ((Value >> 12) & 63));
}
Result += static_cast<char>(0x80 | ((Value >> 6) & 63));
}
Result += static_cast<char>(0x80 | (Value & 63));
}
return Result;
}
int Lexer::readToken() {
while (isspace(LastChar)) {
if (LastChar == '\n') { CurrentLine++; LineStart = true; }
LastChar = Input.get();
}
TokenLine = CurrentLine;
if (LastChar == '#' && LineStart) {
do {
LastChar = Input.get();
} while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
reportError(TokenLine, "Unknown compiler directive");
return readToken();
}
LineStart = false;
if (LastChar == '"' || LastChar == '\'') {
const int Quote = LastChar;
const bool IsChar = Quote == '\'';
StringVal.clear();
LastChar = Input.get();
while (LastChar != Quote && LastChar != EOF && LastChar != '\n') {
if (LastChar == '\\') StringVal += readEscape(IsChar);
else {
StringVal += static_cast<char>(LastChar);
LastChar = Input.get();
}
}
if (LastChar == Quote) LastChar = Input.get();
else {
reportError(TokenLine, "Unterminated %s literal", IsChar ? "char" : "string");
}
if (IsChar) {
if (StringVal.size() != 1) {
reportError(TokenLine, "CHAR literal must contain exactly one character");
}
CharVal = StringVal.size() == 1 ? StringVal[0] : '?';
}
if (!IsChar && LastChar == 'C') {
LastChar = Input.get();
return tok_c_string_literal;
}
return IsChar ? tok_char_literal : tok_string_literal;
}
if (isalpha(LastChar) || LastChar == '_') {
IdentifierStr = static_cast<char>(LastChar);
while (isalnum((LastChar = Input.get())) || LastChar == '_')
IdentifierStr += static_cast<char>(LastChar);
if (IdentifierStr.size() >= 6) {
std::string Prefix = IdentifierStr.substr(0, 6);
std::transform(Prefix.begin(), Prefix.end(), Prefix.begin(),
[](unsigned char C) { return std::tolower(C); });
if (Prefix == "__cps_") {
reportError(TokenLine, "Reserved runtime identifier");
}
}
auto Keyword = Keywords.find(IdentifierStr);
if (Keyword != Keywords.end()) return Keyword->second;
return tok_identifier;
}
if (isdigit(LastChar)) {
std::string NumStr;
bool isReal = false;
do {
NumStr += static_cast<char>(LastChar);
LastChar = Input.get();
if (LastChar == '.' && !isReal) {
isReal = true;
NumStr += '.';
LastChar = Input.get();
}
} while (isdigit(LastChar));
if (isReal) {
RealVal = strtod(NumStr.c_str(), nullptr);
return tok_number_real;
}
auto Parsed = std::from_chars(NumStr.data(), NumStr.data() + NumStr.size(), NumVal);
if (Parsed.ec != std::errc{} ||
NumVal > uint64_t(std::numeric_limits<int64_t>::max()) + 1) {
reportError(TokenLine, "Integer literal is outside the i64 range");
NumVal = 0;
}
return tok_number_int;
}
if (LastChar == '<') {
LastChar = Input.get();
if (LastChar == '-') {
LastChar = Input.get();
return tok_assign;
}
if (LastChar == '=') {
LastChar = Input.get();
return tok_le;
}
if (LastChar == '>') {
LastChar = Input.get();
return tok_ne;
}
return '<';
}
if (LastChar == '>') {
LastChar = Input.get();
if (LastChar == '=') {
LastChar = Input.get();
return tok_ge;
}
return '>';
}
if (LastChar == '=') {
LastChar = Input.get();
return tok_eq;
}
if (LastChar == ':') {
LastChar = Input.get();
return tok_colon;
}
if (LastChar == '.' && Input.peek() == '.') {
Input.get();
if (Input.peek() == '.') {
Input.get();
LastChar = Input.get();
return tok_ellipsis;
}
reportError(CurrentLine, "Expected '...'");
LastChar = Input.get();
return tok_ellipsis;
}
if (LastChar == '/') {
int NextChar = Input.get();
if (NextChar == '/') {
do {
LastChar = Input.get();
} while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
if (LastChar != EOF)
return readToken();
} else {
LastChar = NextChar;
return '/';
}
}
if (LastChar == EOF)
return tok_eof;
int ThisChar = LastChar;
LastChar = Input.get();
return ThisChar;
}
Lexer::TokenData Lexer::captureToken(int Kind) const {
return {Kind, TokenLine, IdentifierStr, StringVal, NumVal, RealVal, CharVal};
}
void Lexer::restoreToken(const TokenData &Token) {
TokenLine = Token.Line;
IdentifierStr = Token.Identifier;
StringVal = Token.String;
NumVal = Token.Integer;
RealVal = Token.Real;
CharVal = Token.Character;
}
int Lexer::gettok() {
if (Lookahead.empty()) return readToken();
auto Token = std::move(Lookahead.front());
Lookahead.pop_front();
restoreToken(Token);
return Token.Kind;
}
int Lexer::peekToken(unsigned Offset) {
auto Current = captureToken(0);
while (Lookahead.size() <= Offset) {
int Kind = readToken();
Lookahead.push_back(captureToken(Kind));
}
restoreToken(Current);
return Lookahead[Offset].Kind;
}
int Lexer::peekLine(unsigned Offset) {
peekToken(Offset);
return Lookahead[Offset].Line;
}
|