assign, return exp val

This commit is contained in:
Vu Tuan Minh
2025-04-07 15:02:10 +02:00
parent 7076633d6a
commit deafaa26fc
7 changed files with 54 additions and 27 deletions

View File

@@ -53,7 +53,7 @@ public interface Interface{
public interface ExprVisitor<H,S> {
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{

View File

@@ -90,7 +90,10 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
public String visitVoid(Type_voidImp t, String h) {
return "VOID";
}
@Override
public String visitVar(VarImp e, String h) {
return e.name();
}
}

View File

@@ -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();
}
}
}
}

View File

@@ -57,6 +57,10 @@ public class SymTable {
return false;
}
public Stack<PMap<String,Type>> 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)){

View File

@@ -157,6 +157,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
return new VoidLLVMImpl();
}
@Override
public InstrOrVal visitVar(VarImp e, SymTable h) {
return new InstrOrVal(null, null);
}
}