blob: da2e47f08d16be7b15cea313f83b61f9f2da94b5 (
plain)
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
|
#include "cps/Lexer.h"
#include <cctype>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace cps;
int Lexer::gettok() {
static int LastChar = ' ';
while (isspace(LastChar)) {
if (LastChar == '\n') CurrentLine++;
LastChar = getchar();
}
if (isalpha(LastChar)) {
IdentifierStr = LastChar;
while (isalnum((LastChar = getchar())) || LastChar == '_')
IdentifierStr += LastChar;
if (IdentifierStr == "DECLARE") return tok_declare;
if (IdentifierStr == "INTEGER") return tok_integer_kw;
if (IdentifierStr == "BOOLEAN") return tok_integer_kw; // TODO: implement real BOOLEAN
if (IdentifierStr == "REAL") return tok_integer_kw; // TODO: implement real REAL
if (IdentifierStr == "INPUT") return tok_input;
if (IdentifierStr == "OUTPUT") return tok_output;
if (IdentifierStr == "IF") return tok_if;
if (IdentifierStr == "THEN") return tok_then;
if (IdentifierStr == "ELSE") return tok_else;
if (IdentifierStr == "ENDIF") return tok_endif;
if (IdentifierStr == "WHILE") return tok_while;
if (IdentifierStr == "DO") return tok_do;
if (IdentifierStr == "ENDWHILE") return tok_endwhile;
if (IdentifierStr == "REPEAT") return tok_repeat;
if (IdentifierStr == "UNTIL") return tok_until;
if (IdentifierStr == "FOR") return tok_for;
if (IdentifierStr == "TO") return tok_to;
if (IdentifierStr == "STEP") return tok_step;
if (IdentifierStr == "NEXT") return tok_next;
if (IdentifierStr == "ARRAY") return tok_array;
if (IdentifierStr == "OF") return tok_of;
if (IdentifierStr == "FUNCTION") return tok_function;
if (IdentifierStr == "ENDFUNCTION") return tok_endfunction;
if (IdentifierStr == "PROCEDURE") return tok_procedure;
if (IdentifierStr == "ENDPROCEDURE") return tok_endprocedure;
if (IdentifierStr == "RETURN") return tok_return;
if (IdentifierStr == "RETURNS") return tok_returns;
if (IdentifierStr == "CALL") return tok_call;
if (IdentifierStr == "BYREF") return tok_byref;
if (IdentifierStr == "BYVAL") return tok_byval;
return tok_identifier;
}
if (isdigit(LastChar)) {
std::string NumStr;
do {
NumStr += LastChar;
LastChar = getchar();
} while (isdigit(LastChar));
NumVal = strtoll(NumStr.c_str(), nullptr, 10);
return tok_number;
}
if (LastChar == '<') {
LastChar = getchar();
if (LastChar == '-') { // <-
LastChar = getchar();
return tok_assign;
}
if (LastChar == '=') { // <=
LastChar = getchar();
return tok_le;
}
if (LastChar == '>') { // <>
LastChar = getchar();
return tok_ne;
}
return '<';
}
if (LastChar == '>') {
LastChar = getchar();
if (LastChar == '=') { // >=
LastChar = getchar();
return tok_ge;
}
return '>';
}
if (LastChar == '=') {
LastChar = getchar();
return tok_eq;
}
if (LastChar == ':') {
LastChar = getchar();
return tok_colon;
}
if (LastChar == '(') { LastChar = getchar(); return '('; }
if (LastChar == ')') { LastChar = getchar(); return ')'; }
if (LastChar == '[') { LastChar = getchar(); return '['; }
if (LastChar == ']') { LastChar = getchar(); return ']'; }
if (LastChar == ',') { LastChar = getchar(); return ','; }
if (LastChar == '+') { LastChar = getchar(); return '+'; }
if (LastChar == '-') { LastChar = getchar(); return '-'; }
if (LastChar == '*') { LastChar = getchar(); return '*'; }
if (LastChar == '/') {
int NextChar = getchar();
if (NextChar == '/') {
do {
LastChar = getchar();
} while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
if (LastChar != EOF)
return gettok();
} else {
LastChar = NextChar;
return '/';
}
}
if (LastChar == EOF)
return tok_eof;
int ThisChar = LastChar;
LastChar = getchar();
return ThisChar;
}
|