From deafaa26fc2dd471ee27cf67b30b1650ac8d4b58 Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Mon, 7 Apr 2025 15:02:10 +0200 Subject: [PATCH] assign, return exp val --- src/main/antlr/VSLParser.g | 22 +++++------ src/main/java/TP2/asd/Interface.java | 2 +- .../java/TP2/asd/PrettyprinterVisitor.java | 7 +++- src/main/java/TP2/asd/Program.java | 38 +++++++++++++------ src/main/java/TP2/asd/SymTable.java | 4 ++ src/main/java/TP2/asd/toLLVM_Visitor.java | 5 +++ tests/aLaMain.vsl | 3 +- 7 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 66ff1bb..0d9bc11 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -67,11 +67,11 @@ instruction [SymTable table] returns [Instruction out]: RETURN e=expression [table] { $out =new Return_instrImp($e.out);} - //| //ASSIGN - //i=ident ASSIGN e=expression [table] - //{ - // $out = new AssignImp($i.out, $e.out); - //} + | //ASSIGN + i=ident ASSIGN e=expression [table] + { + $out = new AssignImp($i.out, $e.out); + } | //DECLARATION t=type i=ident @@ -140,12 +140,12 @@ lit [SymTable table] returns [Expression out, Type return_Type]: $out=$e.out; $return_Type=$e.return_Type; } - // | t=ident{ - // if($table.searchVar($t.out)){ - // $out=$t.out; - // $return_Type=$table.getvar_Type($t.out); - // } - // } + | t=ident{ + if($table.searchVar($t.out)){ + $out=new VarImp($t.out); + $return_Type=$table.getvar_Type($t.out); + } + } ; ident returns [String out]: diff --git a/src/main/java/TP2/asd/Interface.java b/src/main/java/TP2/asd/Interface.java index 01702fe..e422475 100644 --- a/src/main/java/TP2/asd/Interface.java +++ b/src/main/java/TP2/asd/Interface.java @@ -53,7 +53,7 @@ public interface Interface{ public interface ExprVisitor { public S visitConst(ConstImp e,H h); public S visitBinOp(BinopExpressionImp e, H h); - //public S visitVal(ValImp e,H h); + public S visitVar(VarImp e,H h); } public interface Type{ diff --git a/src/main/java/TP2/asd/PrettyprinterVisitor.java b/src/main/java/TP2/asd/PrettyprinterVisitor.java index fbeb47c..d8af26d 100644 --- a/src/main/java/TP2/asd/PrettyprinterVisitor.java +++ b/src/main/java/TP2/asd/PrettyprinterVisitor.java @@ -90,7 +90,10 @@ public class PrettyprinterVisitor implements ProgramVisitor, public String visitVoid(Type_voidImp t, String h) { return "VOID"; } + + @Override + public String visitVar(VarImp e, String h) { + return e.name(); + } - - } diff --git a/src/main/java/TP2/asd/Program.java b/src/main/java/TP2/asd/Program.java index 64cae4e..fa15f17 100644 --- a/src/main/java/TP2/asd/Program.java +++ b/src/main/java/TP2/asd/Program.java @@ -61,8 +61,12 @@ public class Program{ public S accept(ExprVisitor v, H h) { return v.visitBinOp(this, h); } + } - + public static record VarImp(String name) implements Expression { + public S accept(ExprVisitor v, H h) { + return v.visitVar(this, h); + } } @@ -105,10 +109,11 @@ public class Program{ //Eval - public static class ProgramEval implements ProgramVisitor, Integer> { + public static class ProgramEval implements ProgramVisitor { @Override - public Integer visitProgram(ProgramImp e, Map 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, Integer> { + public static class FunctionEval implements FunctionVisitor { @Override - public Integer visitFunction(FunctionImp e, Map 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,Integer>{ + public static class InstructionEval implements InstrVisitor{ @Override - public Integer visitReturn(Return_instrImp e, Map h) { + public Integer visitReturn(Return_instrImp e, SymTable h) { return e.accept(this, h); } @Override - public Integer visitAssign(AssignImp e, Map h) { + public Integer visitAssign(AssignImp e, SymTable h) { return e.accept(this, h); } @Override - public Integer visitDeclaration(DeclarationImp e, Map h) { + public Integer visitDeclaration(DeclarationImp e, SymTable h) { return e.accept(this, h); } } - public static class ExprEval implements ExprVisitor,Integer>{ - public Integer visitConst(ConstImp c, Map h){ + public static class ExprEval implements ExprVisitor{ + public Integer visitConst(ConstImp c, SymTable h){ return c.c(); } - public Integer visitBinOp(BinopExpressionImp e, Map 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(); + } + } } } diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index a916f37..f861701 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -57,6 +57,10 @@ public class SymTable { return false; } + public Stack> stackmap(){ + return this.stackmap(); + } + public Type getvar_Type(String s){ for(int i= stackMap.size()-1; i>=0; i--){ if(stackMap.get(i).containsKey(s)){ diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index ce31b4a..fcdc82d 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -157,6 +157,11 @@ public class toLLVM_Visitor implements ProgramVisitor return new VoidLLVMImpl(); } + @Override + public InstrOrVal visitVar(VarImp e, SymTable h) { + return new InstrOrVal(null, null); + } + } diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index ed17c4c..9064544 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -1,3 +1,4 @@ FUNC INT main() { INT a,b,c - RETURN 4 + 6 * 5 + 3 } \ No newline at end of file + a := 2 + RETURN 4 + 6 * 5 + a } \ No newline at end of file