add expression to LocalVarDecl in ast

This commit is contained in:
StefanZ3 2024-06-20 13:17:53 +02:00
parent e23d84cd09
commit c8c12e4d9a
2 changed files with 10 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -13,9 +14,11 @@ import java.util.*;
public class LocalVarDecl extends AbstractType implements IStatement{
String type;
String identifier;
public LocalVarDecl(String type, String identifier) {
IExpression expression;
public LocalVarDecl(String type, String identifier, IExpression expression) {
this.type = type;
this.identifier = identifier;
this.expression = expression;
}
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {

View File

@ -60,7 +60,12 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override
public Node visitLocalVarDecl(DecafParser.LocalVarDeclContext ctx) {
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText());
if (ctx.expression() != null) {
IExpression expression = (IExpression) visit(ctx.expression());
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), expression);
} else {
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), null);
}
}
@Override