diff --git a/src/main/java/abstractSyntaxTree/Class/FieldDecl.java b/src/main/java/abstractSyntaxTree/Class/FieldDecl.java index a79fe5d..a7c500f 100644 --- a/src/main/java/abstractSyntaxTree/Class/FieldDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/FieldDecl.java @@ -4,6 +4,7 @@ import TypeCheck.AbstractType; import TypeCheck.TypeCheckException; import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckResult; +import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Node; import abstractSyntaxTree.Program; import org.objectweb.asm.ClassWriter; @@ -18,11 +19,13 @@ import java.util.Objects; public class FieldDecl extends AbstractType implements Node { public String type; // from parser - public String identifier; // from parser + public String identifier;// from parser + public IExpression expression; - public FieldDecl(String type, String identifier){ + public FieldDecl(String type, String identifier, IExpression expression){ this.type = type; this.identifier = identifier; + this.expression = expression; } public TypeCheckResult typeCheck(HashMap> typeContext) throws TypeCheckException { diff --git a/src/main/java/astGenerator/ASTGenerator.java b/src/main/java/astGenerator/ASTGenerator.java index 0b23d52..2127a6e 100644 --- a/src/main/java/astGenerator/ASTGenerator.java +++ b/src/main/java/astGenerator/ASTGenerator.java @@ -56,7 +56,12 @@ public class ASTGenerator extends DecafBaseVisitor { } public Node generateFieldDecl(DecafParser.LocalVarDeclContext ctx) { - return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText()); + if (ctx.expression() != null) { + IExpression expression = (IExpression) visit(ctx.expression()); + return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText(), expression); + } else { + return new FieldDecl(ctx.type().getText(), ctx.Identifier().getText(), null); + } } @Override @@ -198,7 +203,6 @@ public class ASTGenerator extends DecafBaseVisitor { return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right); } - // @Override public Node visitMethodCall(DecafParser.MethodCallContext ctx) { String methodName = ctx.Identifier().getText();