add char and boolean values to ast

This commit is contained in:
StefanZ3 2024-06-12 20:19:02 +02:00
parent bc984a89e1
commit 23f541ff7d
4 changed files with 93 additions and 4 deletions

View File

@ -16,3 +16,12 @@ class Example2 {
return n;
}
}
class Example3 {
char isNice;
char m(int n) {
boolean l;
l = false;
return 't';
}
}

View File

@ -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<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
}
}

View File

@ -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<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
}
}

View File

@ -132,7 +132,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
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<Node> {
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<IExpression> expressions = generateExpressions(ctx.argumentList());
return new NewStatementExpression(name);
return new NewStatementExpression(name, expressions);
}
@Override
@ -216,7 +216,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
} 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<Node> {
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<Node> {
}
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<IStatement> statements){
for(IStatement stmt: statements) {
if(stmt instanceof ReturnStatement) {
return "not";
}
}
return "void";
}
}