Skip to content
Snippets Groups Projects
Commit b45a8d64 authored by linha255's avatar linha255
Browse files

lab 1 task C first commit

parent 1fafabff
No related branches found
No related tags found
No related merge requests found
package interpreter.lisp;
public class Add extends BinaryExpression<Num>{
public Add() {
// TODO Auto-generated constructor stub
}
@Override
Expr evalBinaryExpression(Num value1, Num value2) {
// TODO Auto-generated method stub
return new Num(value1.getValue()+value2.getValue());
}
}
package interpreter.lisp;
import java.util.List;
public class CaseConditional extends CompoundExpression{
private Expr cond;
private List<Expr> valsExpr;
public CaseConditional(Expr cond, List<Expr> valExpr) {
// TODO Auto-generated constructor stub
this.cond = cond;
this.valsExpr = valExpr;
}
@Override
public Expr evaluate(Context context) {
// TODO Auto-generated method stub
Expr correctExpr;
for (int i = 0; i < valsExpr.size(); i++) {
CompoundExpression expr = (CompoundExpression) valsExpr.get(i);
if (new Eq().evalBinaryExpression(cond.evaluate(context),
expr.getElements().get(0).evaluate(context)) == Constants.TRUE) {
correctExpr =expr.getElements().get(1).evaluate(context);
return correctExpr;
}
}
return Constants.FALSE;
}
}
...@@ -6,6 +6,7 @@ import java.util.List; ...@@ -6,6 +6,7 @@ import java.util.List;
public class CompoundExpression implements Expr { public class CompoundExpression implements Expr {
private List<Expr> elements = new ArrayList<Expr>(); private List<Expr> elements = new ArrayList<Expr>();
private Context context;
public void add(Expr node) { public void add(Expr node) {
elements.add(node); elements.add(node);
...@@ -27,6 +28,8 @@ public class CompoundExpression implements Expr { ...@@ -27,6 +28,8 @@ public class CompoundExpression implements Expr {
} else if (keyword.equals("if")) { } else if (keyword.equals("if")) {
return new Conditional(elements.get(1), elements.get(2), return new Conditional(elements.get(1), elements.get(2),
elements.get(3)).evaluate(context); elements.get(3)).evaluate(context);
} else if (keyword.equals("case")) {
return new CaseConditional(elements.get(1), elements.subList(2, elements.size())).evaluate(context);
} }
} }
return new FunctionCall((Symbol) elements.get(0),elements.subList(1,elements.size())).evaluate(context); return new FunctionCall((Symbol) elements.get(0),elements.subList(1,elements.size())).evaluate(context);
......
...@@ -14,6 +14,8 @@ public class Context { ...@@ -14,6 +14,8 @@ public class Context {
bindings.put(new Symbol("="), new Eq()); bindings.put(new Symbol("="), new Eq());
bindings.put(new Symbol("*"), new Mult()); bindings.put(new Symbol("*"), new Mult());
bindings.put(new Symbol("-"), new Sub()); bindings.put(new Symbol("-"), new Sub());
bindings.put(new Symbol("+"), new Add());
// Add new top level bindings here // Add new top level bindings here
} }
......
...@@ -5,6 +5,9 @@ public class InterpreterTest { ...@@ -5,6 +5,9 @@ public class InterpreterTest {
public static void main(String[] args) { public static void main(String[] args) {
Expr expr = Reader Expr expr = Reader
.read("(def fac (x) (if (= x 0) 1 (* x (fac (- x 1)))))"); .read("(def fac (x) (if (= x 0) 1 (* x (fac (- x 1)))))");
/* /*
* The expression above is a function definition of the function 'fac' * The expression above is a function definition of the function 'fac'
* with parameter 'x', that calculates the factorial of x. * with parameter 'x', that calculates the factorial of x.
...@@ -25,8 +28,11 @@ public class InterpreterTest { ...@@ -25,8 +28,11 @@ public class InterpreterTest {
*/ */
Expr result = expr.evaluate(Context.getTopLevelContext()); Expr result = expr.evaluate(Context.getTopLevelContext());
Expr expr2 = Reader.read("(fac 5)"); // fac(5) in Python Expr expr2 = Reader.read("(case 2 ((* 1 2) (- 1 5)) (1 3)");
result = expr2.evaluate(Context.getTopLevelContext()); Expr expr3 = Reader.read("(case (* 3 (+ 1 2)) ((+ 6 2) 0) (14 2) ((+ 6 3) (- 2 1))))");
//Expr expr2 = Reader.read("(fac 5)"); // fac(5) in Python
result = expr3.evaluate(Context.getTopLevelContext());
System.out.println(result); System.out.println(result);
} }
......
...@@ -13,8 +13,10 @@ public class Minus extends CompoundExpression { ...@@ -13,8 +13,10 @@ public class Minus extends CompoundExpression {
@Override @Override
public void accept(Visitor v) { public void accept(Visitor v) {
v.visit(leftOperand); //v.visit(leftOperand);
v.visit(rightOperand); //v.visit(rightOperand);
leftOperand.accept(v);
rightOperand.accept(v);
v.visit(this); v.visit(this);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment