Skip to content
Snippets Groups Projects
Commit 0f0e9762 authored by zi ming's avatar zi ming
Browse files

wakakkakaaa

parent 1fafabff
No related branches found
No related tags found
No related merge requests found
File moved
File moved
package interpreter.lisp;
import java.util.ArrayList;
import java.util.List;
public class Case extends CompoundExpression {
private Expr condition;
private List<Expr> elements = new ArrayList<Expr>();
public Case(Expr cond, List<Expr> expr) {
this.condition = cond;
this.elements = expr;
}
public Expr getCondition() {
return condition;
}
public List<Expr> getElements() {
return elements;
}
@Override
public Expr evaluate(Context context) {
for(int i = 0; i < elements.size(); i++){
CompoundExpression compE = (CompoundExpression)elements.get(i);
List<Expr> list = compE.getElements();
if(list.get(0).evaluate(context).equals(condition.evaluate(context)))
return list.get(1).evaluate(context);
}
return Constants.FALSE;
}
}
......@@ -27,6 +27,8 @@ public class CompoundExpression implements Expr {
} else if (keyword.equals("if")) {
return new Conditional(elements.get(1), elements.get(2),
elements.get(3)).evaluate(context);
} else if (keyword.equals("case")) {
return new Case(elements.get(1), elements.subList(2,elements.size())).evaluate(context);
}
}
return new FunctionCall((Symbol) elements.get(0),elements.subList(1,elements.size())).evaluate(context);
......
......@@ -13,6 +13,7 @@ public class Context {
private Context() {
bindings.put(new Symbol("="), new Eq());
bindings.put(new Symbol("*"), new Mult());
bindings.put(new Symbol("+"), new Sum());
bindings.put(new Symbol("-"), new Sub());
// Add new top level bindings here
}
......
......@@ -3,8 +3,13 @@ package interpreter.lisp;
public class InterpreterTest {
public static void main(String[] args) {
Expr expr = Reader
.read("(def fac (x) (if (= x 0) 1 (* x (fac (- x 1)))))");
// Expr expr = Reader
// .read("(def fac (x) (if (= x 0) 1 (* x (fac (- x 1)))))");
// Expr expr = Reader.read("(def fac (x) (case x(3 0)))");
Expr expr = Reader.read("(case 1 (0 0) (1 0))");
/*
* The expression above is a function definition of the function 'fac'
* with parameter 'x', that calculates the factorial of x.
......@@ -25,8 +30,8 @@ public class InterpreterTest {
*/
Expr result = expr.evaluate(Context.getTopLevelContext());
Expr expr2 = Reader.read("(fac 5)"); // fac(5) in Python
result = expr2.evaluate(Context.getTopLevelContext());
// Expr expr2 = Reader.read("(fac 3)");
// result = expr2.evaluate(Context.getTopLevelContext());
System.out.println(result);
}
......
package interpreter.lisp;
public class Sum extends BinaryExpression<Num> {
/**
*
* We assume that the operands have been evaluated to Num objects
*
* @see interpreter.lisp.BinaryExpression#evalBinaryExpression(interpreter.lisp.Expr, interpreter.lisp.Expr)
*/
@Override
Expr evalBinaryExpression(Num value1, Num value2) {
return new Num(value1.getValue()+value2.getValue());
}
}
......@@ -7,14 +7,14 @@ public class PolymorphismTest {
ASubClass sub = new ASubClass();
ABaseClass subAsBase = new ASubClass();
// base.aMethod(sub);
base.aMethod(subAsBase); // The runtime type of subAsBase does not matter here ...
// base.aMethod(subAsBase); // The runtime type of subAsBase does not matter here ...
// base.aMethod(base);
// sub.aMethod(sub);
sub.aMethod(subAsBase);
// sub.aMethod(subAsBase);
// sub.aMethod(base);
subAsBase.aMethod(sub); // but it matters here. Why?
// subAsBase.aMethod(subAsBase);
// subAsBase.aMethod(base);
subAsBase.aMethod(subAsBase);
subAsBase.aMethod(base);
}
......
......@@ -13,9 +13,11 @@ public class Minus extends CompoundExpression {
@Override
public void accept(Visitor v) {
v.visit(leftOperand);
v.visit(rightOperand);
// v.visit(leftOperand);
// v.visit(rightOperand);
leftOperand.accept(v);
v.visit(this);
rightOperand.accept(v);
}
}
......@@ -6,12 +6,12 @@ public class PrintVisitor extends Visitor {
@Override
public void visit(Number exp) {
System.out.print(MessageFormat.format("{0} ", exp.getValue()));
System.out.print(MessageFormat.format("({0} ", exp.getValue()));
}
@Override
public void visit(Variable exp) {
System.out.print(MessageFormat.format("{0} ", exp.getName()));
System.out.print(MessageFormat.format("{0}) ", exp.getName()));
}
@Override
......@@ -24,5 +24,5 @@ public class PrintVisitor extends Visitor {
System.out.print("- ");
}
}
......@@ -2,5 +2,5 @@ package visitor;
public abstract class SimpleExpression extends AbstractExpression {
}
......@@ -13,9 +13,11 @@ public class Sum extends CompoundExpression {
@Override
public void accept(Visitor v) {
// v.visit(leftOperand);
// v.visit(rightOperand);
leftOperand.accept(v);
rightOperand.accept(v);
v.visit(this);
rightOperand.accept(v);
}
......
package visitor;
import java.text.MessageFormat;
public class VisitorTest {
public static void main(String[] args) {
// CountingVariablesVisitor v = new CountingVariablesVisitor();
CountingVariablesVisitor v = new CountingVariablesVisitor();
AbstractExpression expression = new Sum(new Minus(new Number(1),
new Variable("x", 3)), new Sum(new Number(5), new Variable("y",
7)));
System.out.println(expression.toString());
// expression.accept(v);
// System.out.println(MessageFormat.format(
// "There were {0} variables in the expression", v.getCount()));
expression.accept(v);
System.out.println(MessageFormat.format(
"There were {0} variables in the expression", v.getCount()));
expression.accept(new PrintVisitor());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment