This discussion is archived
3 Replies Latest reply: Jan 14, 2010 5:29 AM by PhHein RSS

How to write an interpreter class for straight-line programming language

843810 Newbie
Currently Being Moderated
how do i write an interpreter class for this solution provided below provided you were given an AST abstract class? Please help if you can.

class Test4 {
static Stm test4 =
new CompoundStm(
new AssignStm(
"a",
new NumExp(5)
),
new PrintStm(
new PairExpList(
new IdExp("a"),
new PairExpList(
new EseqExp(
new AssignStm(
"a",
new OpExp(
new IdExp("a"),
OpExp.Times,
new NumExp(5)
)
),
new OpExp(
new IdExp("a"),
OpExp.Plus,
new NumExp(2)
)
),
new LastExpList(
new IdExp("a")
)
)
)
)
);

// a := 5 ; print( a , ( a := a * 5 , a + 2 ) , a )
// Prints:
// 5 27 25
// 5 27 25
public static void main(String [] args) {
try {
Interpreter.interpret(test4);
Interpreter.interpret(test4);
} catch (Exception e) {
System.out.println("Interpreter failed to handle valid input");
}
}
}