LLVM + interface visitor
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.asd.Program.*;
|
||||
|
||||
public class Eval {
|
||||
public class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
|
||||
public Integer visitConst(ConstImp c, Map<String,Integer> h){
|
||||
return c.c();
|
||||
}
|
||||
|
||||
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> 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);
|
||||
case Op.TIMES: return e.e1().accept(this, h)*e.e2().accept(this, h);
|
||||
case Op.DIV: return e.e1().accept(this, h)/e.e2().accept(this, h);
|
||||
case Op.MOD: return e.e1().accept(this, h)%e.e2().accept(this, h);
|
||||
default: throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class InstructionEval implements InstrVisitor<Map<String,Integer>,Integer>{
|
||||
|
||||
@Override
|
||||
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
|
||||
ExprEval exprEval = new ExprEval();
|
||||
return e.e().accept(exprEval, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visitAssign(AssignImp e, Map<String, Integer> h) {
|
||||
//h.put(e.i(), e.t());
|
||||
//TODO
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,52 @@
|
||||
package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import TP2.asd.Program.*;
|
||||
import TP2.llvm.Interface.*;
|
||||
import TP2.llvm.ProgramLLVM.*;
|
||||
|
||||
public interface Interface{
|
||||
//////////Program
|
||||
public interface ProgramI {
|
||||
public <H,S> S accept(ProgramVisitor<H,S> v, H h);
|
||||
public String prettyprinter();
|
||||
public ProgramLLVMImpl toLLVM();
|
||||
}
|
||||
public interface Function {
|
||||
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
public DefineLLVM toLLVM();
|
||||
}
|
||||
public interface Instruction {
|
||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
public InstructionLLVM toLLVM();
|
||||
}
|
||||
|
||||
public interface Expression {
|
||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
||||
public String prettyprinter();
|
||||
}
|
||||
|
||||
public interface ProgramVisitor<H,S> {
|
||||
public S visitProgram(ProgramImp programImp, H h);
|
||||
}
|
||||
|
||||
//////////Function
|
||||
public interface Function {
|
||||
public <H,S> S accept(FunctionVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
public DefineLLVM toLLVM();
|
||||
}
|
||||
|
||||
public interface FunctionVisitor<H,S> {
|
||||
public S visitFunction(FunctionImp e, H h);
|
||||
}
|
||||
|
||||
//////////Instruction
|
||||
public interface Instruction {
|
||||
public <H,S> S accept(InstrVisitor<H,S> v, H h);
|
||||
public String prettyprinter(String indent);
|
||||
public InstructionLLVM toLLVM();
|
||||
}
|
||||
|
||||
public interface InstrVisitor<H,S>{
|
||||
public S visitReturn(Return_instrImp e, H h);
|
||||
public S visitAssign(AssignImp e, H h);
|
||||
}
|
||||
|
||||
//////////Expression
|
||||
//We put prettyprinter here beause each expr will have to implement it like accept visitor
|
||||
//but each implement will be different for prettyprinter
|
||||
public interface Expression {
|
||||
public <H,S> S accept(ExprVisitor<H,S> v, H h);
|
||||
public String prettyprinter();
|
||||
}
|
||||
|
||||
public interface ExprVisitor<H,S> {
|
||||
public S visitConst(ConstImp e,H h);
|
||||
public S visitBinOp(BinopExpressionImp e, H h);
|
||||
|
||||
@@ -2,15 +2,10 @@ package TP2.asd;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
import org.antlr.grammar.v3.ANTLRParser.defaultNodeOption_return;
|
||||
|
||||
import TP2.asd.Interface.*;
|
||||
import TP2.llvm.ProgramLLVM.*;
|
||||
import TP2.llvm.Interface.*;
|
||||
|
||||
|
||||
|
||||
public class Program{
|
||||
|
||||
static String INDENT = " ";
|
||||
@@ -35,10 +30,10 @@ public class Program{
|
||||
for(int i = 0; i<fonctions.size(); i++){
|
||||
fonctionLLVM.add(fonctions.get(i).toLLVM());
|
||||
}
|
||||
return new ProgramLLVMImpl(0,new ArrayList<>(),fonctionLLVM);
|
||||
return new ProgramLLVMImpl(new ArrayList<>(),fonctionLLVM);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static record FunctionImp(Type type, String nom, ArrayList<Instruction> instructions)implements Function {
|
||||
@@ -65,7 +60,8 @@ public class Program{
|
||||
for(int i = 0; i<instructions.size(); i++){
|
||||
instrLLVM.add(instructions.get(i).toLLVM());
|
||||
}
|
||||
return new DefineLLVMImpl(nom, 32, instrLLVM);
|
||||
//return new DefineLLVMImpl(nom, "int", instrLLVM);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,48 @@
|
||||
package TP2.asd;
|
||||
import java.util.Stack;
|
||||
|
||||
|
||||
import org.pcollections.*;
|
||||
import TP2.asd.Interface.Type;
|
||||
|
||||
public class SymTable {
|
||||
private Stack<PMap<String,Type>> stackMap;
|
||||
|
||||
public SymTable(){
|
||||
|
||||
this.stackMap= new Stack<>();
|
||||
}
|
||||
|
||||
public void next_layer(){
|
||||
stackMap.push(HashTreePMap.empty());
|
||||
}
|
||||
|
||||
public void quit_layer() throws Exception{
|
||||
if(stackMap.isEmpty()){
|
||||
throw new Exception();
|
||||
}
|
||||
stackMap.pop();
|
||||
}
|
||||
public PMap<String,Type> peppapeek(){
|
||||
return stackMap.peek();
|
||||
}
|
||||
|
||||
public void addVar(String s, Type t){
|
||||
//Save temporary if not PMap wont save
|
||||
PMap<String, Type> pmap = this.peppapeek();
|
||||
pmap= pmap.plus(s,t);
|
||||
//Delete old ones
|
||||
stackMap.pop();
|
||||
//Push the new one
|
||||
stackMap.push(pmap);
|
||||
}
|
||||
|
||||
//Usually look for var in highest level , if not found research.
|
||||
public boolean searchVar(String s){
|
||||
for(int i= stackMap.size()-1; i>=0; i--){
|
||||
if(stackMap.get(i).containsKey(s)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user