assign, return exp val
This commit is contained in:
@@ -61,8 +61,12 @@ public class Program{
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitBinOp(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static record VarImp(String name) implements Expression {
|
||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||
return v.visitVar(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -105,10 +109,11 @@ public class Program{
|
||||
|
||||
|
||||
//Eval
|
||||
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
|
||||
public static class ProgramEval implements ProgramVisitor<SymTable, Integer> {
|
||||
@Override
|
||||
public Integer visitProgram(ProgramImp e, Map<String, Integer> h) {
|
||||
public Integer visitProgram(ProgramImp e, SymTable h) {
|
||||
Integer result = null;
|
||||
SymTable symtable = new SymTable();
|
||||
FunctionEval functionEval = new FunctionEval();
|
||||
for (Function function : e.fonctions()) {
|
||||
result = function.accept(functionEval, h);
|
||||
@@ -118,10 +123,11 @@ public class Program{
|
||||
}
|
||||
|
||||
|
||||
public static class FunctionEval implements FunctionVisitor<Map<String, Integer>, Integer> {
|
||||
public static class FunctionEval implements FunctionVisitor<SymTable, Integer> {
|
||||
|
||||
@Override
|
||||
public Integer visitFunction(FunctionImp e, Map<String, Integer> h) {
|
||||
public Integer visitFunction(FunctionImp e, SymTable h) {
|
||||
h.next_layer();
|
||||
InstructionEval instructionEval = new InstructionEval();
|
||||
Integer result = null;
|
||||
for (Instruction instr : e.instructions()) {
|
||||
@@ -132,29 +138,29 @@ public class Program{
|
||||
}
|
||||
|
||||
|
||||
public static class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
|
||||
public static class InstructionEval implements InstrVisitor<SymTable,Integer>{
|
||||
|
||||
@Override
|
||||
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
|
||||
public Integer visitReturn(Return_instrImp e, SymTable h) {
|
||||
return e.accept(this, h);
|
||||
}
|
||||
@Override
|
||||
public Integer visitAssign(AssignImp e, Map<String, Integer> h) {
|
||||
public Integer visitAssign(AssignImp e, SymTable h) {
|
||||
return e.accept(this, h);
|
||||
}
|
||||
@Override
|
||||
public Integer visitDeclaration(DeclarationImp e, Map<String, Integer> h) {
|
||||
public Integer visitDeclaration(DeclarationImp e, SymTable h) {
|
||||
return e.accept(this, h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
|
||||
public Integer visitConst(ConstImp c, Map<String,Integer> h){
|
||||
public static class ExprEval implements ExprVisitor<SymTable,Integer>{
|
||||
public Integer visitConst(ConstImp c, SymTable h){
|
||||
return c.c();
|
||||
}
|
||||
|
||||
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> h) {
|
||||
public Integer visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||
switch(e.op()) {
|
||||
case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h);
|
||||
case Op.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h);
|
||||
@@ -164,5 +170,13 @@ public class Program{
|
||||
default: throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
public Integer visitVar(VarImp v, SymTable h) {
|
||||
if (h.searchVar(v.name())) {
|
||||
return 0;
|
||||
} else {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user