-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlers.java
More file actions
128 lines (118 loc) · 3.26 KB
/
Handlers.java
File metadata and controls
128 lines (118 loc) · 3.26 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
package calc;
import java.io.*;
import java.util.Scanner;
class FileHandler {
String fileHandle(String exp, HashTable hash) {
try {
String d = "";
String line = "";
String fname = exp.substring(exp.indexOf("(")+1,exp.indexOf(")"));
Calculator c = new Calculator(hash); //new calculator object to handle processing of file
Scanner sc = new Scanner(new File(fname));
while(sc.hasNextLine()) {
line = sc.nextLine();
//System.out.println(line); DEBUG
d = c.calculate(line);
if (!Evaluator.isNum(d) && !d.isBlank())
break;
}
return "Line : " + line +", " + d ;
}
catch(FileNotFoundException ex) {
return ex.getMessage();
}
catch (Exception o) {
return new InvalidExpressionException().getMessage();
}
}
}
class FunctionHandler{
String handle(String s, HashTable h) {
try {
String original = s;
s = s.strip();
s = s.substring(4);
if (s.charAt(0) != ' ')
throw new InvalidSyntaxException(original);
s = s.strip();
int i = s.indexOf('(');
if (s.isEmpty() || s.substring(0,i).strip().contains(" ") )
throw new InvalidIdentifierException("' '");
String f_name = s.substring(0,i).strip();
if (!Evaluator.isAlpha(f_name))
throw new InvalidIdentifierException(f_name);
s = s.replace(" ", "");
int j = s.indexOf(')');
String var_name = s.substring(i+1,j);
if (! var_name.equals("x"))
throw new IncorrectParamException(var_name);
String exp = s.substring(j+2);
if (h.search(f_name) != null && h.search(f_name) instanceof Function)
throw new FunctionReDefinitionException(f_name);
else if (h.search(f_name) != null)
throw new IdentifierConflictException(var_name);
Function f = new calc.Function(f_name,exp);
h.insert(f);
return "";
}
catch (CalculatorException e){
return e.getMessage();
}
catch (Exception o) {
return new InvalidExpressionException().getMessage();
}
}
}
class AssignmentHandler{
String assign(String s, HashTable h) {
try {
s = s.strip();
int i = s.indexOf('=');
if ( s.isEmpty() || s.substring(0,i).strip().contains(" "))
throw new InvalidIdentifierException("' '");
String var_name = s.substring(0,i).strip();
if (!Evaluator.isAlpha(var_name))
throw new InvalidIdentifierException(var_name);
s = s.replace(" ", "");
i = s.indexOf('=');
s = s.substring(i+1);
//System.out.println("|"+ s + "|"); DEBUG
double value = new Evaluator(s,h).evaluate();
//System.out.println("Assigned " + value); DEBUG
UserDefined var = h.search(var_name);
if (var == null)
{
var = new Variable(var_name,value);
h.insert(var);
}
else if (var instanceof Variable)
((Variable)var).value = value;
else
throw new IdentifierConflictException(var_name);
return "";
}
catch (CalculatorException e) {
return e.getMessage();
}
catch (Exception o) {
return new InvalidExpressionException().getMessage();
}
}
}
class ExpressionHandler {
String evaluate(String exp, HashTable h) {
try {
Evaluator ev = new Evaluator(exp,h);
return ev.evaluate().toString();
}
catch (NullPointerException e) {
return new InvalidExpressionException().getMessage();
}
catch (CalculatorException e) {
return e.getMessage();
}
catch (Exception o) {
return new InvalidExpressionException().getMessage();
}
}
}