/
Lexer.java
79 lines (66 loc) · 2.43 KB
/
Lexer.java
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
import java.util.ArrayList;
public class Lexer {
// token class
public class Token {
// token type
public enum Type {
NUMBER,
ADD,
SUBTRACT,
MINUS,
MULTIPLY,
DIVIDE,
OPEN_PAREN,
CLOSE_PAREN,
EOF,
}
// token value and type
public String value;
public Type type;
// ctor to create a token obj
public Token(String value, Type type) {
this.value = value;
this.type = type;
}
}
// tokens list and raw expr
public ArrayList<Token> tokens = new ArrayList<Token>();
String expression;
// ctor that takes a string and lexes it to tokens
public Lexer(String expression) {
this.expression = expression;
lex();
}
// ctor that just takes the direct tokens
public Lexer(ArrayList<Token> tokens) {
this.tokens = tokens;
}
// take raw expr and convert it into a list of tokens
public void lex() {
// ptr keeps track of current loc
int ptr = 0;
// clear all whitespace
expression = expression.replace(" ", "");
while (ptr < expression.length()) {
// create token depending on operator
if (expression.charAt(ptr) == '(') tokens.add(new Token("(", Token.Type.OPEN_PAREN));
if (expression.charAt(ptr) == ')') tokens.add(new Token(")", Token.Type.CLOSE_PAREN));
if (expression.charAt(ptr) == '*') tokens.add(new Token("*", Token.Type.MULTIPLY));
if (expression.charAt(ptr) == '/') tokens.add(new Token("/", Token.Type.DIVIDE));
if (expression.charAt(ptr) == '+') tokens.add(new Token("+", Token.Type.ADD));
if (expression.charAt(ptr) == '-') tokens.add(new Token("-", Token.Type.SUBTRACT));
// create number token
if (Character.isDigit(expression.charAt(ptr))) {
int start = ptr;
while (ptr < expression.length() && Character.isDigit(expression.charAt(ptr))) ptr++;
tokens.add(new Token(expression.substring(start, ptr), Token.Type.NUMBER));
// decrement ptr bc we've gone past end of num
ptr--;
}
// increment to next char
ptr++;
}
// add eof token to signal end of expr
tokens.add(new Token("", Token.Type.EOF));
}
}