-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReiLexer.java
More file actions
99 lines (84 loc) · 2.76 KB
/
ReiLexer.java
File metadata and controls
99 lines (84 loc) · 2.76 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
package rei;
import java.util.*;
public class ReiLexer {
private final String source;
private final List<Token> tokens = new ArrayList<>();
private int start = 0, current = 0;
public ReiLexer(String source) {
this.source = source + "\n";
}
public List<Token> tokenize() {
while (!isAtEnd()) {
start = current;
scanToken();
}
tokens.add(new Token(Token.Type.EOF, ""));
return tokens;
}
private void scanToken() {
char c = advance();
switch (c) {
case '+': addToken(Token.Type.PLUS); break;
case '-': addToken(Token.Type.MINUS); break;
case '*': addToken(Token.Type.STAR); break;
case '/': addToken(Token.Type.SLASH); break;
case '=': addToken(Token.Type.EQUAL); break;
case '>': addToken(Token.Type.GT); break;
case '<': addToken(Token.Type.LT); break;
case '\n': addToken(Token.Type.NEWLINE); break;
case '{': addToken(Token.Type.LBRACE); break;
case '}': addToken(Token.Type.RBRACE); break;
case ' ':
case '\r':
case '\t':
break;
default:
if (isDigit(c)) {
number();
} else if (isAlpha(c)) {
identifier();
}
}
}
private void number() {
while (isDigit(peek())) advance();
String text = source.substring(start, current);
addToken(Token.Type.NUMBER, Integer.parseInt(text));
}
private void identifier() {
while (isAlphaNumeric(peek())) advance();
String text = source.substring(start, current);
switch (text) {
case "if": addToken(Token.Type.IF); break;
case "else": addToken(Token.Type.ELSE); break;
case "print": addToken(Token.Type.PRINT); break;
default: addToken(Token.Type.IDENTIFIER);
}
}
private boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
private boolean isAlphaNumeric(char c) {
return isAlpha(c) || isDigit(c);
}
private char advance() {
return source.charAt(current++);
}
private char peek() {
if (isAtEnd()) return '\0';
return source.charAt(current);
}
private boolean isAtEnd() {
return current >= source.length();
}
private void addToken(Token.Type type) {
addToken(type, null);
}
private void addToken(Token.Type type, Integer literal) {
String text = source.substring(start, current);
tokens.add(new Token(type, text, literal));
}
}