-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseMath.java
More file actions
349 lines (347 loc) · 11.5 KB
/
ParseMath.java
File metadata and controls
349 lines (347 loc) · 11.5 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
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
package com.csc190.finalproject;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.lang.reflect.InvocationTargetException;
//Temp:
import java.util.Arrays;
public class ParseMath{
public String equation;
public ParseMath(String equation){
this.equation = equation.replaceAll("\\s+","").replaceAll("\\*\\*","^").replaceAll("(\\d+)(x|\\()","$1*x").replaceAll("\\)\\(",")*(");
}
public Number forX(double x){
Number n = null;
try{
n = Interpreter.interpret(Parser.parse(Lexer.lex(this.equation.replaceAll("x", String.format("(%s)",x)))));
} catch (ParseException ex){
//Ignore ParseException
} catch (Exception ex){
ex.printStackTrace();
}
return n;
}
public static enum TokenType{
NUM,
LPAREN,
RPAREN,
MINUS,
PLUS,
MUL,
DIV,
EXP,
MOD
}
static class Lexer{
public static final Map<String,TokenType> CHARACTER_MAPPING = makeCharacterMap();
private static final Map<String,TokenType> makeCharacterMap(){
Map<String,TokenType> map = new HashMap<>();
map.put("(",TokenType.LPAREN);
map.put(")",TokenType.RPAREN);
map.put("-",TokenType.MINUS);
map.put("+",TokenType.PLUS);
map.put("*",TokenType.MUL);
map.put("/",TokenType.DIV);
map.put("^",TokenType.EXP);
return map;
}
public static final Set<String> NUMBERS = Set.of("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
public static class Token{
private TokenType type;
private String value;
public Token(String type){
this.type = TokenType.valueOf(type);
}
public Token(TokenType type){
this.type = type;
}
public Token(TokenType type, Object value){
this.type = type;
this.value = value.toString();
}
public String toString(){
if(this.value == null)
return this.type.toString();
return this.type.toString()+":"+this.value.toString();
}
public boolean equals(Object o){
if(o instanceof Token)
if(this.type.equals(((Token)o).type))
return this.value == ((Token)o).value || this.value.equals(((Token)o).value);
return false;
}
}
private String text;
private int index;
private String currentChar;
public Lexer(String s){
this.text = s;
this.index = -1;
this.advance();
}
private void advance(){
this.currentChar = ++this.index >= this.text.length() ? null : String.valueOf(this.text.charAt(this.index));
}
public static Token[] lex(String s){
return new Lexer(s).makeTokens();
}
public Token[] makeTokens(){
ArrayList<Token> tokens = new ArrayList<>();
while(currentChar != null){
if(NUMBERS.contains(this.currentChar)){
tokens.add(this.makeNumberToken());
} else if (CHARACTER_MAPPING.containsKey(this.currentChar)){
tokens.add(new Token(CHARACTER_MAPPING.get(this.currentChar)));
this.advance();
} else {
throw new ParseException("Invalid character: " + this.currentChar + " — " + this.text);
}
}
return tokens.toArray(new Token[tokens.size()]);
}
private Token makeNumberToken(){
String str = "";
boolean decimal = false;
while(this.currentChar != null && (NUMBERS.contains(this.currentChar) || this.currentChar.equals("."))){
if(this.currentChar.equals(".")){
if(decimal)
throw new ArithmeticException("Double decimal point");
decimal = true;
}
str += this.currentChar;
this.advance();
}
return new Token(TokenType.NUM, Double.valueOf(str));
}
}
static class Parser{
private Lexer.Token[] tokens;
private int index;
private Lexer.Token currentToken;
static abstract class ASTNode {}
static class BinOpNode extends ASTNode{
private ASTNode left;
private Lexer.Token op_token;
private ASTNode right;
public BinOpNode(ASTNode left, Lexer.Token op_token, ASTNode right){
this.left = left;
this.op_token = op_token;
this.right = right;
}
public ASTNode getLeft() {
return left;
}
public Lexer.Token getOpToken() {
return op_token;
}
public ASTNode getRight() {
return right;
}
public String toString() {
return String.format("(%s, %s, %s)", this.left, this.op_token, this.right);
}
}
static class NumberNode extends ASTNode {
private double value;
public NumberNode(double n){
this.value = n;
}
public double getValue(){
return this.value;
}
public String toString(){
return String.valueOf(this.value);
}
}
static class UnaryOpNode extends ASTNode {
private Lexer.Token op_token;
private ASTNode node;
public UnaryOpNode(Lexer.Token token, ASTNode node){
this.op_token = token;
this.node = node;
}
public Lexer.Token getOpToken(){
return op_token;
}
public ASTNode getNode(){
return node;
}
public String toString(){
return String.format("(%s, %s)",this.op_token.toString(),this.node.toString());
}
}
public Parser(Lexer.Token[] tokens){
this.tokens = tokens;
this.index = -1;
this.advance();
}
public static ASTNode parse(Lexer.Token[] tokens) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return new Parser(tokens).parse();
}
private ASTNode makebinopnode(String left_method, Set<TokenType> tokenTypes, String right_method) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
ASTNode left = (ASTNode)Parser.class.getMethod(left_method).invoke(this);
while(this.currentToken != null && tokenTypes.contains(this.currentToken.type)){
Lexer.Token op_tok = this.currentToken;
this.advance();
ASTNode right = (ASTNode)Parser.class.getMethod(right_method).invoke(this);
left = new BinOpNode(left, op_tok, right);
}
return left;
}
private ASTNode makebinopnode(String methodname, Set<TokenType> tokenTypes) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return this.makebinopnode(methodname, tokenTypes, methodname);
}
private void advance(){
this.currentToken = ++this.index >= this.tokens.length ? null : this.tokens[this.index];
}
public ASTNode parse() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return this.one();
}
public ASTNode one() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return makebinopnode("two",Set.<TokenType>of(TokenType.PLUS,TokenType.MINUS));
}
public ASTNode two() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return makebinopnode("three",Set.<TokenType>of(TokenType.MUL,TokenType.DIV));
}
public ASTNode three() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
if(this.currentToken.type == TokenType.MINUS){
Lexer.Token token = this.currentToken;
this.advance();
return new UnaryOpNode(token,this.four());
}
return this.four();
}
public ASTNode four() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
return makebinopnode("five",Set.<TokenType>of(TokenType.EXP));
}
public ASTNode five() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
if(this.currentToken.type.equals(TokenType.LPAREN)){
this.advance();
ASTNode node = this.one();
if(!this.currentToken.type.equals(TokenType.RPAREN))
throw new ParseException("')' expected");
this.advance();
return node;
} else if (this.currentToken.type.equals(TokenType.NUM)){
double num = Double.valueOf(this.currentToken.value);
this.advance();
return new NumberNode(num);
}
return null;
}
}
static class Interpreter{
static class Value extends Number {
private String value;
public Value(Parser.NumberNode node){
this.value = String.valueOf(node.value);
}
public Value(double value){
this.value = String.valueOf(value);
}
public Value add(Value other){
return new Value(this.doubleValue() + other.doubleValue());
}
public Value subtract(Value other){
return new Value(this.doubleValue() - other.doubleValue());
}
public Value multiply(Value other){
return new Value(this.doubleValue() * other.doubleValue());
}
public Value divide(Value other){
return new Value(this.doubleValue() / other.doubleValue());
}
public Value exp(Value other){
return new Value(Math.pow(this.doubleValue(),other.doubleValue()));
}
public Value mod(Value other){
return new Value(this.doubleValue() % other.doubleValue());
}
@Override
public double doubleValue(){
return Double.valueOf(this.value);
}
@Override
public int intValue(){
return Integer.valueOf(this.value);
}
@Override
public long longValue(){
return Long.valueOf(this.value);
}
@Override
public float floatValue(){
return Float.valueOf(this.value);
}
public String toString(){
return this.value;
}
}
private Parser.ASTNode root;
private Value value;
public Interpreter(Parser.ASTNode parent){
this.root = parent;
this.value = this.interpret();
}
public Value interpret(){
return this.visit(this.root);
}
public static Value interpret(Parser.ASTNode node){
return new Interpreter(node).interpret();
}
private Value visit(Parser.ASTNode node){
switch(node.getClass().getSimpleName()){
case "BinOpNode":
return this.visit_BinOpNode((Parser.BinOpNode)node);
case "NumberNode":
return this.visit_NumberNode((Parser.NumberNode)node);
case "UnaryOpNode":
return this.visit_UnaryOpNode((Parser.UnaryOpNode)node);
default:
throw new ParseException("Unknown Node type: " + node.getClass().getSimpleName());
}
}
private Value visit_BinOpNode(Parser.BinOpNode node) {
Value left = visit(node.left);
switch(node.op_token.type){
case PLUS:
return left.add(visit(node.right));
case MINUS:
return left.subtract(visit(node.right));
case MUL:
return left.multiply(visit(node.right));
case DIV:
return left.divide(visit(node.right));
case EXP:
return left.exp(visit(node.right));
case MOD:
return left.mod(visit(node.right));
}
throw new UnsupportedOperationException();
}
private Value visit_NumberNode(Parser.NumberNode node){
return new Value(node.value);
}
private Value visit_UnaryOpNode(Parser.UnaryOpNode node){
Value value = visit(node.node);
switch(node.op_token.type){
case MINUS:
return value.multiply(new Value(-1));
default:
throw new UnsupportedOperationException();
}
}
public String toString(){
return this.root.toString()+" = "+this.value;
}
}
}
class ParseException extends RuntimeException {
ParseException(){
super();
}
ParseException(String s){
super(s);
}
}