diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 7c3984e..64384a4 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -2,7 +2,7 @@ package de.maishai; import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafParser; -import de.maishai.ast.AssignSign; +import de.maishai.ast.Operator; import de.maishai.ast.records.Expression; import de.maishai.ast.records.Statement; import de.maishai.ast.Type; @@ -68,9 +68,8 @@ public class StatementGenerator extends DecafBaseVisitor { private Assignment generateAssign(DecafParser.AssignContext ctx) { Id id = new Id(ctx.id().getText()); - AssignSign sign = getAssignSign(ctx.assignSign()); - Expression expr = new ExpressionGenerator().visit(ctx.expr()); - return new Assignment(id, sign, expr); + Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr());); + return new Assignment(id, expr); } //StatementExpression @@ -102,15 +101,15 @@ public class StatementGenerator extends DecafBaseVisitor { return new New(type, args); } - public static AssignSign getAssignSign(DecafParser.AssignSignContext ctx) { + public static Expression resolveFancyAssign(DecafParser.AssignSignContext ctx, Id id, Expression expression) { if (ctx.ASSIGN() != null) - return AssignSign.ASSIGN; + return expression; if (ctx.ADD_ASSIGN() != null) - return AssignSign.ADD_ASSIGN; + return new Binary(id, Operator.ADD, expression); if (ctx.SUB_ASSIGN() != null) - return AssignSign.SUB_ASSIGN; + return new Binary(id, Operator.SUB, expression); if (ctx.MUL_ASSIGN() != null) - return AssignSign.MUL_ASSIGN; + return new Binary(id, Operator.MUL, expression); throw new RuntimeException(); } } diff --git a/src/main/java/de/maishai/ast/Type.java b/src/main/java/de/maishai/ast/Type.java index cdd0390..4cbe465 100644 --- a/src/main/java/de/maishai/ast/Type.java +++ b/src/main/java/de/maishai/ast/Type.java @@ -22,4 +22,6 @@ public enum Type { this.objectType = objectType; } } + + } diff --git a/src/main/java/de/maishai/ast/records/Assignment.java b/src/main/java/de/maishai/ast/records/Assignment.java index 832b1b1..1d5d654 100644 --- a/src/main/java/de/maishai/ast/records/Assignment.java +++ b/src/main/java/de/maishai/ast/records/Assignment.java @@ -1,7 +1,6 @@ package de.maishai.ast.records; -import de.maishai.ast.AssignSign; -public record Assignment(Id loc, AssignSign assignment, Expression value) implements Statement { +public record Assignment(Id loc, Expression value) implements Statement { } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index ee4a825..e3d721c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -13,23 +13,11 @@ import java.util.Map; @Data public class TypedAssignment implements TypedStatement { private TypedId loc; - private AssignSign assignSign; private TypedExpression value; @Override public Type typeCheck(Map localVar, Map classes) { - if (!localVar.containsKey(loc.getName())) { - throw new RuntimeException("Variable not declared"); - } - //ASSIGN : '=';ADD_ASSIGN : '+='; SUB_ASSIGN : '-='; MUL_ASSIGN : '*='; - if (assignSign == AssignSign.ASSIGN || assignSign == AssignSign.ADD_ASSIGN || - assignSign == AssignSign.SUB_ASSIGN || assignSign == AssignSign.MUL_ASSIGN) { - if (localVar.get(loc.getName()) != Type.INT || value.typeCheck(localVar, classes) != Type.INT) { - throw new RuntimeException("Type mismatch"); - } - } - return localVar.get(loc.getName()); } @Override @@ -37,7 +25,6 @@ public class TypedAssignment implements TypedStatement { Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); typedAssignment.setLoc((TypedId) loc.convertToTypedAST(localVar, classes, untyped.loc())); - typedAssignment.setAssignSign(untyped.assignment()); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); return typedAssignment; } diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java index 692c764..1c4b839 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java @@ -54,13 +54,11 @@ public class AbstractSyntax_ClassWithConstructor { List statementList = List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new IntLiteral(10) ), new For( new Assignment( new Id("i"), - AssignSign.ASSIGN, new IntLiteral(0) ), new Binary( @@ -70,7 +68,6 @@ public class AbstractSyntax_ClassWithConstructor { ), new Assignment( new Id("i"), - AssignSign.ASSIGN, new Binary( new Id("i"), Operator.ADD, @@ -88,7 +85,6 @@ public class AbstractSyntax_ClassWithConstructor { new For( new Assignment( new Id("j"), - AssignSign.ASSIGN, new IntLiteral(0) ), new Binary( @@ -98,7 +94,6 @@ public class AbstractSyntax_ClassWithConstructor { ), new Assignment( new Id("j"), - AssignSign.ADD_ASSIGN, new IntLiteral(1) ), new Block( @@ -106,7 +101,6 @@ public class AbstractSyntax_ClassWithConstructor { List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new Binary( new Id("x"), Operator.MUL, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java index b16abb9..e8e95c1 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -18,7 +18,6 @@ //} -import de.maishai.ast.AssignSign; import de.maishai.ast.Operator; import de.maishai.ast.ReturnType; import de.maishai.ast.Type; @@ -57,7 +56,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new IntLiteral(10) ), new While( @@ -72,7 +70,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new Binary( new Id("x"), Operator.MUL, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index 70e7fb0..4b7b7c3 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -13,7 +13,6 @@ // } // } //} -import de.maishai.ast.AssignSign; import de.maishai.ast.Operator; import de.maishai.ast.records.*; import de.maishai.ast.Type; @@ -60,7 +59,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new Id("startValue") ), new While( @@ -79,7 +77,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List.of( new Assignment( new Id("innerRepetitions"), - AssignSign.ASSIGN, new Id("x") ), new While( @@ -93,7 +90,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { List.of( new Assignment( new Id("x"), - AssignSign.ASSIGN, new Binary( new Id("x"), Operator.MUL, @@ -103,14 +99,18 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { new Assignment( new Id("innerRepetitions"), AssignSign.SUB_ASSIGN, - new IntLiteral(1) + new Binary(new Id("innerRepetitions"), + Operator.SUB, + new IntLiteral(1)) + ) ) ) ), new Assignment( new Id("repetitions"), - AssignSign.SUB_ASSIGN, + new Binary(new Id("repetitions"), + new Op) new IntLiteral(1) ) diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java index bd024fc..8a767cb 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java @@ -55,7 +55,6 @@ class AbstractSyntax_ClassWithMethodAndField { List.of( new Assignment( new Id("c"), - AssignSign.ASSIGN, new Id("character") ) ) diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java index 467e24d..31d28b1 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -65,12 +65,10 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List.of( new Assignment( new Id("i"), - AssignSign.ASSIGN, new IntLiteral(11) ), new Assignment( new Id("returnValue"), - AssignSign.ASSIGN, new BoolLiteral(false) ), new While( @@ -84,12 +82,13 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List.of( new Assignment( new Id("i"), - AssignSign.SUB_ASSIGN, - new IntLiteral(1) + new Binary(new Id("i"), + Operator.SUB, + new IntLiteral(1)) + ), new Assignment( new Id("returnValue"), - AssignSign.ASSIGN, new Unary( UnaryOperator.NOT, new Id("returnValue") @@ -119,7 +118,6 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List statementList = List.of( new Assignment( new Id("instance"), - AssignSign.ASSIGN, new New( TypeClassWithMoreComplexMethodAndMain, List.of()