Merge branch 'refs/heads/main' into testsuites

This commit is contained in:
JonathanFleischmann 2024-05-08 11:12:16 +02:00
commit 388fddd466
9 changed files with 34 additions and 44 deletions

View File

@ -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<Statement> {
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<Statement> {
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();
}
}

View File

@ -22,4 +22,6 @@ public enum Type {
this.objectType = objectType;
}
}
}

View File

@ -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 {
}

View File

@ -2,7 +2,11 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.AssignSign;
import de.maishai.ast.records.Assignment;
import de.maishai.typedast.*;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
@ -11,7 +15,6 @@ import java.util.Map;
@Data
public class TypedAssignment implements TypedStatement {
private TypedId loc;
private AssignSign assignSign;
private TypedExpression value;
@Override
@ -19,15 +22,14 @@ public class TypedAssignment implements TypedStatement {
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());
Type typeLeft = localVar.get(loc.getName());
Type typeRight = value.typeCheck(localVar, classes);
if (typeLeft.equals(typeRight) ) {
return typeLeft;
}
throw new RuntimeException("type of left not equals with type right");
}
@Override
@ -35,7 +37,7 @@ 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.setAssignSign(untyped.assignment());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
return typedAssignment;
}

View File

@ -53,13 +53,11 @@ public class AbstractSyntax_ClassWithConstructor {
List<Statement> 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(
@ -69,7 +67,6 @@ public class AbstractSyntax_ClassWithConstructor {
),
new Assignment(
new Id("i"),
AssignSign.ASSIGN,
new Binary(
new Id("i"),
Operator.ADD,
@ -87,7 +84,6 @@ public class AbstractSyntax_ClassWithConstructor {
new For(
new Assignment(
new Id("j"),
AssignSign.ASSIGN,
new IntLiteral(0)
),
new Binary(
@ -97,7 +93,6 @@ public class AbstractSyntax_ClassWithConstructor {
),
new Assignment(
new Id("j"),
AssignSign.ADD_ASSIGN,
new IntLiteral(1)
),
new Block(
@ -105,7 +100,6 @@ public class AbstractSyntax_ClassWithConstructor {
List.of(
new Assignment(
new Id("x"),
AssignSign.ASSIGN,
new Binary(
new Id("x"),
Operator.MUL,

View File

@ -18,7 +18,6 @@
//}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator;
import de.maishai.ast.ReturnType;
import de.maishai.ast.Type;
@ -56,7 +55,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
List.of(
new Assignment(
new Id("x"),
AssignSign.ASSIGN,
new IntLiteral(10)
),
new While(
@ -71,7 +69,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
List.of(
new Assignment(
new Id("x"),
AssignSign.ASSIGN,
new Binary(
new Id("x"),
Operator.MUL,

View File

@ -13,7 +13,6 @@
// }
// }
//}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.Type;
@ -59,7 +58,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of(
new Assignment(
new Id("x"),
AssignSign.ASSIGN,
new Id("startValue")
),
new While(
@ -78,7 +76,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of(
new Assignment(
new Id("innerRepetitions"),
AssignSign.ASSIGN,
new Id("x")
),
new While(
@ -92,7 +89,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of(
new Assignment(
new Id("x"),
AssignSign.ASSIGN,
new Binary(
new Id("x"),
Operator.MUL,
@ -102,14 +98,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)
)

View File

@ -54,7 +54,6 @@ class AbstractSyntax_ClassWithMethodAndField {
List.of(
new Assignment(
new Id("c"),
AssignSign.ASSIGN,
new Id("character")
)
)

View File

@ -64,12 +64,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(
@ -83,12 +81,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")
@ -117,7 +116,6 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List<Statement> statementList = List.of(
new Assignment(
new Id("instance"),
AssignSign.ASSIGN,
new New(
TypeClassWithMoreComplexMethodAndMain,
List.of()