add expression to fieldecl

This commit is contained in:
StefanZ3 2024-06-28 19:46:09 +02:00
parent 4bc38c0ff9
commit 3b2a328182
2 changed files with 11 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException; import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Node; import abstractSyntaxTree.Node;
import abstractSyntaxTree.Program; import abstractSyntaxTree.Program;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
@ -18,11 +19,13 @@ import java.util.Objects;
public class FieldDecl extends AbstractType implements Node { public class FieldDecl extends AbstractType implements Node {
public String type; // from parser 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.type = type;
this.identifier = identifier; this.identifier = identifier;
this.expression = expression;
} }
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException {

View File

@ -56,7 +56,12 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
} }
public Node generateFieldDecl(DecafParser.LocalVarDeclContext ctx) { 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 @Override
@ -198,7 +203,6 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right); return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right);
} }
//
@Override @Override
public Node visitMethodCall(DecafParser.MethodCallContext ctx) { public Node visitMethodCall(DecafParser.MethodCallContext ctx) {
String methodName = ctx.Identifier().getText(); String methodName = ctx.Identifier().getText();