Handling Merge of Joni

This commit is contained in:
Ahmad 2024-05-08 11:28:06 +02:00
commit 3ca5a1a6be
11 changed files with 32 additions and 41 deletions

View File

@ -81,7 +81,7 @@ public class ASTGenerator {
type.setObjectType(new Id(ctx.id().getText())); type.setObjectType(new Id(ctx.id().getText()));
return type; return type;
} }
throw new RuntimeException(); throw new RuntimeException("No type found!");
} }
public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) { public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) {
@ -99,6 +99,6 @@ public class ASTGenerator {
type.setObjectType(new Id(ctx.type().id().getText())); type.setObjectType(new Id(ctx.type().id().getText()));
return type; return type;
} }
throw new RuntimeException(); throw new RuntimeException("No return type found!");
} }
} }

View File

@ -26,7 +26,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
if (ctx.unaryOp().SUB() != null) { if (ctx.unaryOp().SUB() != null) {
return new Unary(UnaryOperator.SUB, expr); return new Unary(UnaryOperator.SUB, expr);
} }
throw new RuntimeException(); gi throw new RuntimeException("No unary operator found.");
} }
@Override @Override
@ -59,7 +59,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText())); return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText()));
if (ctx.CHARLITERAL() != null) if (ctx.CHARLITERAL() != null)
return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0)); return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0));
throw new RuntimeException(); throw new RuntimeException("No literal found!");
} }
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) { public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) {
@ -81,7 +81,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
if (ctx.NE() != null) return Operator.NE; if (ctx.NE() != null) return Operator.NE;
if (ctx.AND() != null) return Operator.AND; if (ctx.AND() != null) return Operator.AND;
if (ctx.OR() != null) return Operator.OR; if (ctx.OR() != null) return Operator.OR;
throw new RuntimeException(); throw new RuntimeException("No operator found!");
} }
@Override @Override

View File

@ -2,7 +2,7 @@ package de.maishai;
import de.maishai.antlr.DecafBaseVisitor; import de.maishai.antlr.DecafBaseVisitor;
import de.maishai.antlr.DecafParser; 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.Expression;
import de.maishai.ast.records.Statement; import de.maishai.ast.records.Statement;
import de.maishai.ast.Type; import de.maishai.ast.Type;
@ -68,9 +68,8 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
private Assignment generateAssign(DecafParser.AssignContext ctx) { private Assignment generateAssign(DecafParser.AssignContext ctx) {
Id id = new Id(ctx.id().getText()); Id id = new Id(ctx.id().getText());
AssignSign sign = getAssignSign(ctx.assignSign()); Expression expr = resolveFancyAssign(ctx.assignSign(), id, new ExpressionGenerator().visit(ctx.expr()));
Expression expr = new ExpressionGenerator().visit(ctx.expr()); return new Assignment(id, expr);
return new Assignment(id, sign, expr);
} }
//StatementExpression //StatementExpression
@ -102,15 +101,15 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
return new New(type, args); 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) if (ctx.ASSIGN() != null)
return AssignSign.ASSIGN; return expression;
if (ctx.ADD_ASSIGN() != null) if (ctx.ADD_ASSIGN() != null)
return AssignSign.ADD_ASSIGN; return new Binary(id, Operator.ADD, expression);
if (ctx.SUB_ASSIGN() != null) if (ctx.SUB_ASSIGN() != null)
return AssignSign.SUB_ASSIGN; return new Binary(id, Operator.SUB, expression);
if (ctx.MUL_ASSIGN() != null) if (ctx.MUL_ASSIGN() != null)
return AssignSign.MUL_ASSIGN; return new Binary(id, Operator.MUL, expression);
throw new RuntimeException(); throw new RuntimeException("No assign sign found!");
} }
} }

View File

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

View File

@ -1,7 +1,6 @@
package de.maishai.ast.records; 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.AssignSign;
import de.maishai.ast.records.Assignment; 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 lombok.Data;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -37,7 +41,6 @@ public class TypedAssignment implements TypedStatement {
return typedAssignment; return typedAssignment;
} }
@Override @Override
public void codeGen(MethodVisitor mv, MethodContext ctx) { public void codeGen(MethodVisitor mv, MethodContext ctx) {

View File

@ -54,13 +54,11 @@ public class AbstractSyntax_ClassWithConstructor {
List<Statement> statementList = List.of( List<Statement> statementList = List.of(
new Assignment( new Assignment(
new Id("x"), new Id("x"),
AssignSign.ASSIGN,
new IntLiteral(10) new IntLiteral(10)
), ),
new For( new For(
new Assignment( new Assignment(
new Id("i"), new Id("i"),
AssignSign.ASSIGN,
new IntLiteral(0) new IntLiteral(0)
), ),
new Binary( new Binary(
@ -70,7 +68,6 @@ public class AbstractSyntax_ClassWithConstructor {
), ),
new Assignment( new Assignment(
new Id("i"), new Id("i"),
AssignSign.ASSIGN,
new Binary( new Binary(
new Id("i"), new Id("i"),
Operator.ADD, Operator.ADD,
@ -88,7 +85,6 @@ public class AbstractSyntax_ClassWithConstructor {
new For( new For(
new Assignment( new Assignment(
new Id("j"), new Id("j"),
AssignSign.ASSIGN,
new IntLiteral(0) new IntLiteral(0)
), ),
new Binary( new Binary(
@ -98,7 +94,6 @@ public class AbstractSyntax_ClassWithConstructor {
), ),
new Assignment( new Assignment(
new Id("j"), new Id("j"),
AssignSign.ADD_ASSIGN,
new IntLiteral(1) new IntLiteral(1)
), ),
new Block( new Block(
@ -106,7 +101,6 @@ public class AbstractSyntax_ClassWithConstructor {
List.of( List.of(
new Assignment( new Assignment(
new Id("x"), new Id("x"),
AssignSign.ASSIGN,
new Binary( new Binary(
new Id("x"), new Id("x"),
Operator.MUL, Operator.MUL,

View File

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

View File

@ -13,7 +13,6 @@
// } // }
// } // }
//} //}
import de.maishai.ast.AssignSign;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.*; import de.maishai.ast.records.*;
import de.maishai.ast.Type; import de.maishai.ast.Type;
@ -60,7 +59,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of( List.of(
new Assignment( new Assignment(
new Id("x"), new Id("x"),
AssignSign.ASSIGN,
new Id("startValue") new Id("startValue")
), ),
new While( new While(
@ -79,7 +77,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of( List.of(
new Assignment( new Assignment(
new Id("innerRepetitions"), new Id("innerRepetitions"),
AssignSign.ASSIGN,
new Id("x") new Id("x")
), ),
new While( new While(
@ -93,7 +90,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
List.of( List.of(
new Assignment( new Assignment(
new Id("x"), new Id("x"),
AssignSign.ASSIGN,
new Binary( new Binary(
new Id("x"), new Id("x"),
Operator.MUL, Operator.MUL,
@ -103,14 +99,18 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
new Assignment( new Assignment(
new Id("innerRepetitions"), new Id("innerRepetitions"),
AssignSign.SUB_ASSIGN, AssignSign.SUB_ASSIGN,
new IntLiteral(1) new Binary(new Id("innerRepetitions"),
Operator.SUB,
new IntLiteral(1))
) )
) )
) )
), ),
new Assignment( new Assignment(
new Id("repetitions"), new Id("repetitions"),
AssignSign.SUB_ASSIGN, new Binary(new Id("repetitions"),
new Op)
new IntLiteral(1) new IntLiteral(1)
) )

View File

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

View File

@ -65,12 +65,10 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List.of( List.of(
new Assignment( new Assignment(
new Id("i"), new Id("i"),
AssignSign.ASSIGN,
new IntLiteral(11) new IntLiteral(11)
), ),
new Assignment( new Assignment(
new Id("returnValue"), new Id("returnValue"),
AssignSign.ASSIGN,
new BoolLiteral(false) new BoolLiteral(false)
), ),
new While( new While(
@ -84,12 +82,13 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List.of( List.of(
new Assignment( new Assignment(
new Id("i"), new Id("i"),
AssignSign.SUB_ASSIGN, new Binary(new Id("i"),
new IntLiteral(1) Operator.SUB,
new IntLiteral(1))
), ),
new Assignment( new Assignment(
new Id("returnValue"), new Id("returnValue"),
AssignSign.ASSIGN,
new Unary( new Unary(
UnaryOperator.NOT, UnaryOperator.NOT,
new Id("returnValue") new Id("returnValue")
@ -119,7 +118,6 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain {
List<Statement> statementList = List.of( List<Statement> statementList = List.of(
new Assignment( new Assignment(
new Id("instance"), new Id("instance"),
AssignSign.ASSIGN,
new New( new New(
TypeClassWithMoreComplexMethodAndMain, TypeClassWithMoreComplexMethodAndMain,
List.of() List.of()