-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
287 lines (266 loc) · 5.84 KB
/
Program.cs
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
// using System.Collections;
//
// namespace LoxInterpreter
// {
// internal class Program
// {
// private static Dictionary<string, TokenType>() keywords = new Dictionary<string, TokenType>();
//
// static
// {
// keywords.Add("and")
// }
// public static void Main(string[] args)
// {
// Console.WriteLine("yes");
// }
// }
// }
//> Scanning scanner-class
using System;
using System.Collections.Generic;
//import static com.craftinginterpreters.lox.TokenType.*; // [static-import]
class Scanner {
//> keyword-map
private Dictionary<string, TokenType> keywords = new Dictionary<string, TokenType>
{
{
"and", AND
},
{
"class", CLASS
},
{
"else", ELSE
},
{
"false", FALSE
},
{
"for", FOR
},
{
"fun", FUN
},
{
"if", IF
},
{
"nil", NIL
},
{
"or", OR
},
{
"print", PRINT
},
{
"return", RETURN
},
{
"super", SUPER
},
{
"this", THIS
},
{
"true", TRUE
},
{
"var", VAR
},
{
"while", WHILE
}
};
//< keyword-map
private string source;
private List<Token> tokens = new ArrayList<Token>();
//> scan-state
private int start = 0;
private int current = 0;
private int line = 1;
//< scan-state
Scanner(String source) {
this.source = source;
}
//> scan-tokens
List<Token> scanTokens() {
while (!isAtEnd()) {
// We are at the beginning of the next lexeme.
start = current;
scanToken();
}
tokens.add(new Token(EOF, "", null, line));
return tokens;
}
//< scan-tokens
//> scan-token
private void scanToken() {
char c = advance();
switch (c) {
case '(': addToken(LEFT_PAREN); break;
case ')': addToken(RIGHT_PAREN); break;
case '{': addToken(LEFT_BRACE); break;
case '}': addToken(RIGHT_BRACE); break;
case ',': addToken(COMMA); break;
case '.': addToken(DOT); break;
case '-': addToken(MINUS); break;
case '+': addToken(PLUS); break;
case ';': addToken(SEMICOLON); break;
case '*': addToken(STAR); break; // [slash]
//> two-char-tokens
case '!':
addToken(match('=') ? BANG_EQUAL : BANG);
break;
case '=':
addToken(match('=') ? EQUAL_EQUAL : EQUAL);
break;
case '<':
addToken(match('=') ? LESS_EQUAL : LESS);
break;
case '>':
addToken(match('=') ? GREATER_EQUAL : GREATER);
break;
//< two-char-tokens
//> slash
case '/':
if (match('/')) {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd()) advance();
} else {
addToken(SLASH);
}
break;
//< slash
//> whitespace
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
//< whitespace
//> string-start
case '"': string(); break;
//< string-start
//> char-error
default:
/* Scanning char-error < Scanning digit-start
Lox.error(line, "Unexpected character.");
*/
//> digit-start
if (isDigit(c)) {
number();
//> identifier-start
} else if (isAlpha(c)) {
identifier();
//< identifier-start
} else {
Lox.error(line, "Unexpected character.");
}
//< digit-start
break;
//< char-error
}
}
//< scan-token
//> identifier
private void identifier() {
while (isAlphaNumeric(peek())) advance();
/* Scanning identifier < Scanning keyword-type
addToken(IDENTIFIER);
*/
//> keyword-type
String text = source.substring(start, current);
TokenType type = keywords.get(text);
if (type == null) type = IDENTIFIER;
addToken(type);
//< keyword-type
}
//< identifier
//> number
private void number() {
while (isDigit(peek())) advance();
// Look for a fractional part.
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek())) advance();
}
addToken(NUMBER,
Double.parseDouble(source.substring(start, current)));
}
//< number
//> string
private void string() {
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++;
advance();
}
if (isAtEnd()) {
Lox.error(line, "Unterminated string.");
return;
}
// The closing ".
advance();
// Trim the surrounding quotes.
String value = source.substring(start + 1, current - 1);
addToken(STRING, value);
}
//< string
//> match
private boolean match(char expected) {
if (isAtEnd()) return false;
if (source.charAt(current) != expected) return false;
current++;
return true;
}
//< match
//> peek
private char peek() {
if (isAtEnd()) return '\0';
return source.charAt(current);
}
//< peek
//> peek-next
private char peekNext() {
if (current + 1 >= source.length()) return '\0';
return source.charAt(current + 1);
} // [peek-next]
//< peek-next
//> is-alpha
private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_';
}
private boolean isAlphaNumeric(char c) {
return isAlpha(c) || isDigit(c);
}
//< is-alpha
//> is-digit
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
} // [is-digit]
//< is-digit
//> is-at-end
private boolean isAtEnd() {
return current >= source.length();
}
//< is-at-end
//> advance-and-add-token
private char advance() {
return source.charAt(current++);
}
private void addToken(TokenType type) {
addToken(type, null);
}
private void addToken(TokenType type, Object literal) {
String text = source.substring(start, current);
tokens.add(new Token(type, text, literal, line));
}
//< advance-and-add-token
}