assign, return exp val
This commit is contained in:
@@ -67,11 +67,11 @@ instruction [SymTable table] returns [Instruction out]:
|
|||||||
RETURN e=expression [table]
|
RETURN e=expression [table]
|
||||||
{
|
{
|
||||||
$out =new Return_instrImp($e.out);}
|
$out =new Return_instrImp($e.out);}
|
||||||
//| //ASSIGN
|
| //ASSIGN
|
||||||
//i=ident ASSIGN e=expression [table]
|
i=ident ASSIGN e=expression [table]
|
||||||
//{
|
{
|
||||||
// $out = new AssignImp($i.out, $e.out);
|
$out = new AssignImp($i.out, $e.out);
|
||||||
//}
|
}
|
||||||
| //DECLARATION
|
| //DECLARATION
|
||||||
t=type i=ident
|
t=type i=ident
|
||||||
|
|
||||||
@@ -140,12 +140,12 @@ lit [SymTable table] returns [Expression out, Type return_Type]:
|
|||||||
$out=$e.out;
|
$out=$e.out;
|
||||||
$return_Type=$e.return_Type;
|
$return_Type=$e.return_Type;
|
||||||
}
|
}
|
||||||
// | t=ident{
|
| t=ident{
|
||||||
// if($table.searchVar($t.out)){
|
if($table.searchVar($t.out)){
|
||||||
// $out=$t.out;
|
$out=new VarImp($t.out);
|
||||||
// $return_Type=$table.getvar_Type($t.out);
|
$return_Type=$table.getvar_Type($t.out);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
ident returns [String out]:
|
ident returns [String out]:
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public interface Interface{
|
|||||||
public interface ExprVisitor<H,S> {
|
public interface ExprVisitor<H,S> {
|
||||||
public S visitConst(ConstImp e,H h);
|
public S visitConst(ConstImp e,H h);
|
||||||
public S visitBinOp(BinopExpressionImp 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{
|
public interface Type{
|
||||||
|
|||||||
@@ -91,6 +91,9 @@ public class PrettyprinterVisitor implements ProgramVisitor<String,String>,
|
|||||||
return "VOID";
|
return "VOID";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String visitVar(VarImp e, String h) {
|
||||||
|
return e.name();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,8 +61,12 @@ public class Program{
|
|||||||
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
public <H, S> S accept(ExprVisitor<H, S> v, H h) {
|
||||||
return v.visitBinOp(this, 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
|
//Eval
|
||||||
public static class ProgramEval implements ProgramVisitor<Map<String, Integer>, Integer> {
|
public static class ProgramEval implements ProgramVisitor<SymTable, Integer> {
|
||||||
@Override
|
@Override
|
||||||
public Integer visitProgram(ProgramImp e, Map<String, Integer> h) {
|
public Integer visitProgram(ProgramImp e, SymTable h) {
|
||||||
Integer result = null;
|
Integer result = null;
|
||||||
|
SymTable symtable = new SymTable();
|
||||||
FunctionEval functionEval = new FunctionEval();
|
FunctionEval functionEval = new FunctionEval();
|
||||||
for (Function function : e.fonctions()) {
|
for (Function function : e.fonctions()) {
|
||||||
result = function.accept(functionEval, h);
|
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
|
@Override
|
||||||
public Integer visitFunction(FunctionImp e, Map<String, Integer> h) {
|
public Integer visitFunction(FunctionImp e, SymTable h) {
|
||||||
|
h.next_layer();
|
||||||
InstructionEval instructionEval = new InstructionEval();
|
InstructionEval instructionEval = new InstructionEval();
|
||||||
Integer result = null;
|
Integer result = null;
|
||||||
for (Instruction instr : e.instructions()) {
|
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
|
@Override
|
||||||
public Integer visitReturn(Return_instrImp e, Map<String, Integer> h) {
|
public Integer visitReturn(Return_instrImp e, SymTable h) {
|
||||||
return e.accept(this, h);
|
return e.accept(this, h);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Integer visitAssign(AssignImp e, Map<String, Integer> h) {
|
public Integer visitAssign(AssignImp e, SymTable h) {
|
||||||
return e.accept(this, h);
|
return e.accept(this, h);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Integer visitDeclaration(DeclarationImp e, Map<String, Integer> h) {
|
public Integer visitDeclaration(DeclarationImp e, SymTable h) {
|
||||||
return e.accept(this, h);
|
return e.accept(this, h);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ExprEval implements ExprVisitor<Map<String,Integer>,Integer>{
|
public static class ExprEval implements ExprVisitor<SymTable,Integer>{
|
||||||
public Integer visitConst(ConstImp c, Map<String,Integer> h){
|
public Integer visitConst(ConstImp c, SymTable h){
|
||||||
return c.c();
|
return c.c();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer visitBinOp(BinopExpressionImp e, Map<String, Integer> h) {
|
public Integer visitBinOp(BinopExpressionImp e, SymTable h) {
|
||||||
switch(e.op()) {
|
switch(e.op()) {
|
||||||
case Op.PLUS: return e.e1().accept(this, h)+e.e2().accept(this, h);
|
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.MINUS: return e.e1().accept(this, h)-e.e2().accept(this, h);
|
||||||
@@ -164,5 +170,13 @@ public class Program{
|
|||||||
default: throw new IllegalArgumentException();
|
default: throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer visitVar(VarImp v, SymTable h) {
|
||||||
|
if (h.searchVar(v.name())) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ public class SymTable {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Stack<PMap<String,Type>> stackmap(){
|
||||||
|
return this.stackmap();
|
||||||
|
}
|
||||||
|
|
||||||
public Type getvar_Type(String s){
|
public Type getvar_Type(String s){
|
||||||
for(int i= stackMap.size()-1; i>=0; i--){
|
for(int i= stackMap.size()-1; i>=0; i--){
|
||||||
if(stackMap.get(i).containsKey(s)){
|
if(stackMap.get(i).containsKey(s)){
|
||||||
|
|||||||
@@ -157,6 +157,11 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImpl>
|
|||||||
return new VoidLLVMImpl();
|
return new VoidLLVMImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InstrOrVal visitVar(VarImp e, SymTable h) {
|
||||||
|
return new InstrOrVal(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
FUNC INT main() {
|
FUNC INT main() {
|
||||||
INT a,b,c
|
INT a,b,c
|
||||||
RETURN 4 + 6 * 5 + 3 }
|
a := 2
|
||||||
|
RETURN 4 + 6 * 5 + a }
|
||||||
Reference in New Issue
Block a user