From 23f541ff7d7d3503734fc5b6410a288cf910a357 Mon Sep 17 00:00:00 2001 From: StefanZ3 Date: Wed, 12 Jun 2024 20:19:02 +0200 Subject: [PATCH] add char and boolean values to ast --- src/main/java/Input.java | 9 +++++ .../Expression/BooleanConstantExpression.java | 27 +++++++++++++++ .../Expression/CharConstantExpression.java | 27 +++++++++++++++ src/main/java/astGenerator/ASTGenerator.java | 34 ++++++++++++++++--- 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java create mode 100644 src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java diff --git a/src/main/java/Input.java b/src/main/java/Input.java index 670a9c0..a73bfbf 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -16,3 +16,12 @@ class Example2 { return n; } } + +class Example3 { + char isNice; + char m(int n) { + boolean l; + l = false; + return 't'; + } +} diff --git a/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java new file mode 100644 index 0000000..131a82b --- /dev/null +++ b/src/main/java/abstractSyntaxTree/Expression/BooleanConstantExpression.java @@ -0,0 +1,27 @@ +package abstractSyntaxTree.Expression; + +import TypeCheck.AbstractType; +import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Parameter.ParameterList; +import org.objectweb.asm.MethodVisitor; + +import java.util.HashMap; +import java.util.LinkedHashMap; + +public class BooleanConstantExpression extends AbstractType implements IExpression{ + public boolean value; + + public BooleanConstantExpression(boolean value) { + this.value = value; + } + + @Override + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + + @Override + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { + + } +} diff --git a/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java b/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java new file mode 100644 index 0000000..1410082 --- /dev/null +++ b/src/main/java/abstractSyntaxTree/Expression/CharConstantExpression.java @@ -0,0 +1,27 @@ +package abstractSyntaxTree.Expression; + +import TypeCheck.AbstractType; +import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Parameter.ParameterList; +import org.objectweb.asm.MethodVisitor; + +import java.util.HashMap; +import java.util.LinkedHashMap; + +public class CharConstantExpression extends AbstractType implements IExpression{ + public char value; + + public CharConstantExpression(char value) { + this.value = value; + } + + @Override + public TypeCheckResult typeCheck(HashMap>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + + @Override + public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { + + } +} diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 886f7b0..3815ee8 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -132,7 +132,8 @@ public class ASTGenerator extends DecafBaseVisitor { Node statement = visitStatement(stmt); stmts.add((IStatement) statement); } - return new BlockStatement(stmts, "void"); + String returnType = getReturnType(stmts); + return new BlockStatement(stmts, returnType); } @Override @@ -191,12 +192,11 @@ public class ASTGenerator extends DecafBaseVisitor { return super.visitMethodCall(ctx); } - //add the expression list later to the constructor @Override public Node visitNewDecl(DecafParser.NewDeclContext ctx) { String name = ctx.Identifier().getText(); List expressions = generateExpressions(ctx.argumentList()); - return new NewStatementExpression(name); + return new NewStatementExpression(name, expressions); } @Override @@ -216,7 +216,7 @@ public class ASTGenerator extends DecafBaseVisitor { } else if (ctx.nonCalcExpr() != null) { return visitNonCalcExpr(ctx.nonCalcExpr()); } else if (ctx.value() != null) { - //todo + return visitValue(ctx.value()); } else if (ctx.binaryExpr() != null) { //todo } @@ -258,6 +258,8 @@ public class ASTGenerator extends DecafBaseVisitor { return new LocalVarIdentifier(identifier); } else if(ctx.instVar() != null) { return visitInstVar(ctx.instVar()); + } else if(ctx.calcExpr() != null) { + return visitCalcExpr(ctx.calcExpr()); } return null; } @@ -307,4 +309,28 @@ public class ASTGenerator extends DecafBaseVisitor { } return expressions; } + + @Override + public Node visitValue(DecafParser.ValueContext ctx) { + if (ctx.IntValue() != null) { + return new IntConstantExpression(Integer.parseInt(ctx.IntValue().getText())); + } else if (ctx.BooleanValue() != null) { + return new BooleanConstantExpression(Boolean.parseBoolean(ctx.BooleanValue().getText())); + } else if (ctx.CharValue() != null) { + String value = ctx.CharValue().toString(); + return new CharConstantExpression(value.charAt(1)); + } + return null; + } + + public String getReturnType(List statements){ + for(IStatement stmt: statements) { + if(stmt instanceof ReturnStatement) { + return "not"; + } + } + return "void"; + } } + +