-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.java
More file actions
60 lines (53 loc) · 2.04 KB
/
Copy pathRunner.java
File metadata and controls
60 lines (53 loc) · 2.04 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
// Kimberley Ni and Eylul Oktay
public class Runner {
Dictionary dict;
public Runner() {
dict = new Dictionary();
}
/*
* Given an expression tree, this returns the ran expression.
*/
public static Expression run(Expression exp) {
//System.out.println(exp);
Expression before = exp.copy();
//System.out.println(before);
Expression ran = runCycle(exp, false);
//System.out.println(ran);
while (!(before.equals(ran))) {
before = ran.copy();
ran = runCycle(ran, false);
//System.out.println(ran);
}
return Dictionary.getKey(ran);
}
public static Expression runCycle(Expression exp, Boolean ranRedex) {
// MAKE BASE CASES HERE
if (exp.isSimple() || ranRedex == true) {
return exp;
}
// If the expression is an Application
if (exp instanceof Application) {
Application app = (Application) exp;
Expression left = app.getLeft();
Expression right = app.getRight();
// If the left side of the application is a Function, perform beta reduction
if (left instanceof Function) {
Function func = (Function) left;
Expression body = func.apply(right); // Apply the argument to the function
// Recursively run the resulting expression
return runCycle(body, true);
} else {
// Otherwise, recursively run the left and right sides
Expression leftCopy = left.copy();
Expression leftAttempt = runCycle(left, ranRedex);
if (!(leftCopy.equals(leftAttempt))) ranRedex = true;
return new Application(leftAttempt, runCycle(right, ranRedex));
}
} else {
Function func = (Function) exp;
Variable param = func.getParameter();
Expression body = func.getBody();
return new Function(param, runCycle(body, ranRedex));
}
}
}