-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
399 lines (381 loc) · 14.5 KB
/
main.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.util.HashMap;
import java.util.Scanner;
import java.util.ArrayList;
/*
0 = \f.\x.x
succ = \n.\f.\x.f (n f x)
1 = run succ 0
+ = λm.λn.λf.λx.(m f) ((n f) x)
* = λn.λm.λf.λx.n (m f) x
2 = run succ 1
3 = run + 2 1
4 = run * 2 2
5 = (λf.(λx.(f (f (f (f (f x)))))))
7 = run succ (succ 5)
pred = λn.λf.λx.n (λg.λh.h (g f)) (λu.x) (λu.u)
6 = run pred 7
- = λm.λn.(n pred) m
10 = run succ (+ 3 6)
9 = run pred 10
not = λp.p false true
even? = λn.n not true
odd? = \x.not (even? x)
run even? 0
run even? 5
* */
public class main {
public static HashMap<String, Expression> variables = new HashMap<>();
public static ArrayList<String> splitTopLevelArgs(String e){
ArrayList<String> tokens = new ArrayList<>();
String str = "";
int numParen = 0;
boolean lambda = false;
int lParen = 0;
for (int i = 0; i < e.length(); i++) {
char c = e.charAt(i);
if(c == ' ' && numParen == 0 && !lambda){
if (!str.equals(" ") && !str.equals("")) {
tokens.add(str);
}str = "";
}
else if(c == '.' && !lambda){
lParen = numParen;
str += ".";
lambda = true;
if(lParen == 0){
tokens.add(str);
str = "";
}
if (e.charAt(i+1) == ' ')
i++;
}
else if(c == '('){
str += "(";
numParen++;
}
else if(c == ')'){
str += ")";
if(numParen == lParen){
lambda = false;
}
numParen--;
}
else{
str += c;
}
}
if(!(str.equals(""))){
tokens.add(str);
}
return tokens;
}
public static boolean spaceName (String inp){
String n = "";
for (int i = 0; i < inp.length() - 1 ; i++) {
n += inp.charAt(i);
}
return isName(n) && (inp.charAt(inp.length() - 1) == ' ');
}
public static Expression lambdify(String inp, boolean recurse){
if (inp.equals("")) {
return new Name("\n");
}
if(recurse){
inp = removeFirstParen(inp);
}
if (isName(inp)){
ArrayList<String> t = new ArrayList<>();
t.add(inp);
String replaced = replaceVariables(t, false).get(0);
if (isName(replaced))
return new Name(replaced);
return lambdify(replaced, false);
}
if(spaceName(inp)){
return new Name(inp.replaceAll(" ", ""));
}
ArrayList<String> tokens = splitTopLevelArgs(inp);
if(tokens.size() == 1){
tokens = replaceVariables(tokens, false);
return lambdify(tokens.get(0), true);
}
if(tokens.get(1).equals("=")){
tokens = replaceVariables(tokens, true);
if(variables.containsKey(tokens.get(0))){
return new Name(tokens.get(0) + " is already defined");
}
if(tokens.get(0).equals("run")){
return new Name("run cannot be a variable name [use run to evaluate an expression]");
}
tokens.remove(1);
if(tokens.size() == 2){
variables.put(tokens.get(0), lambdify(tokens.get(1), false));
return new Name("Added " + lambdify(tokens.get(1), false) + " as " + tokens.get(0));
}
String variablename = tokens.remove(0);
if (tokens.size() == 2 && tokens.get(0).equals("run")){
if(isName(tokens.get(1))){
variables.put(variablename, new Name(tokens.get(1)));
return new Name("Added " + new Name(tokens.get(1)) + " as " + variablename);
}
if(isLambda(tokens.get(1)) && !tokens.get(1).contains("(")){
variables.put(variablename, lambdify(tokens.get(1), false));
return new Name("Added " + lambdify(tokens.get(1), false) + " as " + variablename);
}
else{
Expression e = lambdify(tokens.get(1), true);
variables.put(variablename, e.eval());
return new Name("Added " + e.eval() + " as " + variablename);
}
}
if(tokens.get(0).equals("run")){
tokens.remove(0);
Expression newTree = buildTree(tokens, null);
Expression curTree = newTree.eval();
while(!(curTree.equals(newTree))){
newTree = curTree;
curTree = curTree.eval();
}
variables.put(variablename, curTree);
return new Name("Added " + curTree + " as " + variablename);
}
Expression newTree = buildTree(tokens, null);
variables.put(variablename, newTree);
return new Name("Added " + newTree + " as " + variablename);
}
tokens = replaceVariables(tokens, false);
if(tokens.get(0).equals("run")){
tokens.remove(0);
if(tokens.size() == 1){
return lambdify(tokens.get(0), false);
}
else{
Expression tree = buildTree(tokens, null);
Expression curTree = tree.eval();
while(!(curTree.equals(tree))){
tree = curTree;
curTree = curTree.eval();
}
return curTree;
}
}
return buildTree(tokens, null);
}
public static ArrayList<String> replaceVariables(ArrayList<String> tokens, boolean settingVar){
ArrayList<String> ret = new ArrayList<>();
if(settingVar){
ret.add(tokens.remove(0));
ret.add(tokens.remove(0));
}
for (int i = 0; i < tokens.size(); i++) {
if(contain(tokens.get(i))){
String word = "";
String newToken = "";
for (int j = 0; j < tokens.get(i).length(); j++) {
char c = tokens.get(i).charAt(j);
if (c == ' ' || c == ')') {
newToken += variables.getOrDefault(word, new Name(word));
newToken += c;
word = "";
} else if (c == '(') {
newToken += c;
} else {
word += c;
}
}
if(!word.equals("")){
newToken += variables.getOrDefault(word, new Name(word)).toString();
}
ret.add(newToken);
}
else{
ret.add(tokens.get(i));
}
}
return ret;
}
public static boolean contain(String token){
String cur = "";
boolean ret = false;
for (int i = 0; i < token.length() ; i++) {
char c = token.charAt(i);
if(c == ' '){
if(variables.containsKey(cur)){
ret = true;
}
cur = "";
}
else if(c != '(' && c != ')'){
cur += c;
}
}
if(variables.containsKey(cur)){
ret = true;
}
return ret;
}
public static String removeFirstParen(String inp){
String input = "";
for (int i = 1; i < inp.length() -1; i++) {
input += inp.charAt(i);
}
return input;
}
public static boolean isName(String inp){
char [] specialcharacters = new char[] {' ', '(', ')' , '.', '\\', 'λ'};
for (int i = 0; i < inp.length(); i++) {
char c = inp.charAt(i);
for (int j = 0; j < specialcharacters.length; j++) {
if(c == specialcharacters[j]){
return false;
}
}
}
return true;
}
public static String getRight(String inp){
String s = "";
boolean add = false;
for (int i = 0; i < inp.length() - 1; i++) {
char c = inp.charAt(i);
if(c == '.'){
add = true;
}
else if(add){
s += c;
}
}
return s;
}
public static Expression buildTree(ArrayList<String> tokens, Expression left) {
if (tokens.size() == 0) {
return left;
}
if (left == null) {
if(isLambda(tokens.get(0))){
if(tokens.get(0).charAt(0) == '('){
if(tokens.get(1).charAt(0) == '('){
left = new Application(lambdify(tokens.remove(0), true), lambdify(tokens.remove(0), true));
}
else{
left = new Application(lambdify(tokens.remove(0), true), lambdify(tokens.remove(0), false));
}
return buildTree(tokens, left);
}
else{
return new Function(lambdify(findName(tokens.remove(0)), false), lambdify(tokens.remove(0), false));
}
}
if(isLambda(tokens.get(1))){
if(tokens.get(0).charAt(0) == '('){
Function right;
if(tokens.get(1).charAt(0) == '('){
left = new Application(lambdify(tokens.remove(0), true), lambdify(tokens.remove(0), true));
return buildTree(tokens, left);
}
else{
if(tokens.get(2).charAt(0) == '('){
right = new Function(lambdify(findName(tokens.remove(1)), false), lambdify(tokens.remove(1), true));
}
else{
right = new Function(lambdify(findName(tokens.remove(1)), false), lambdify(tokens.remove(1), false));
}
left = new Application(lambdify(tokens.remove(0), true), right);
return buildTree(tokens, left);
}
}
else{
Function right;
if(tokens.get(1).charAt(0) == '('){
left = new Application(lambdify(tokens.remove(0), false), lambdify(tokens.remove(0), true));
return buildTree(tokens, left);
}
else{
if(tokens.get(2).charAt(0) == '('){
right = new Function(lambdify(findName(tokens.remove(1)), false), lambdify(tokens.remove(1), true));
}
else{
right = new Function(lambdify(findName(tokens.remove(1)), false), lambdify(tokens.remove(1), false));
}
left = new Application(lambdify(tokens.remove(0), false), right);
return buildTree(tokens, left);
}
}
}
else {
if (tokens.get(0).charAt(0) == '(' && tokens.get(1).charAt(0) == '(') {
left = new Application(lambdify(tokens.remove(0), true), lambdify(tokens.remove(0), true));
} else if (tokens.get(0).charAt(0) == '(' && tokens.get(1).charAt(0) != '(') {
left = new Application(lambdify(tokens.remove(0), true), lambdify(tokens.remove(0), false));
} else if (tokens.get(0).charAt(0) != '(' && tokens.get(1).charAt(0) == '(') {
left = new Application(lambdify(tokens.remove(0), false), lambdify(tokens.remove(0), true));
} else {
left = new Application(lambdify(tokens.remove(0), false), lambdify(tokens.remove(0), false));
}
}
return buildTree(tokens, left);
}
else {
if (isLambda(tokens.get(0))) {
if (tokens.get(0).charAt(0) == '(') {
left = new Application(left, lambdify(tokens.remove(0), false));
} else {
System.out.println(tokens.get(0));
left = new Application(left, new Function(lambdify(findName(tokens.remove(0)), false), lambdify(tokens.remove(0), false)));
}
} else {
if (tokens.get(0).charAt(0) == '(') {
left = new Application(left, lambdify(tokens.remove(0), true));
} else {
left = new Application(left, lambdify(tokens.remove(0), false));
}
}
}
return buildTree(tokens, left);
}
public static boolean isLambda(String inp){
for (int i = 0; i < inp.length(); i++) {
char c = inp.charAt(i);
if(c == '\\' || c == 'λ'){
return true;
}
}
return false;
}
public static String findName(String word){
String ret ="";
boolean add = false;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(c == '.'){
break;
}
if(add){
ret += c;
}
else if(c == '\\' || c == 'λ'){
add = true;
}
}
return ret;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String expression;
while(true){
System.out.print("> ");
expression = input.nextLine();
expression.replaceAll("\uFEFF", "");
if(expression.equals("exit")){
break;
}
else {
Expression lambdified = lambdify(expression, false);
if(!lambdified.equals(new Name("\n"))){
System.out.println(lambdified);
}
}
}
System.out.println("Goodbye!");
}
}