diff --git a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java index 16837e50..4e1ea1a7 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/Codegen.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.target.tree.*; import de.dhbwstuttgart.target.tree.expression.*; import de.dhbwstuttgart.target.tree.type.*; @@ -432,6 +433,10 @@ public class Codegen { mv.visitInsn(IXOR); break; } + case Instof instof: { + // TODO + throw new NotImplementedException(); + } case Shl shl: { generate(state, shl.left()); convertTo(state, shl.left().type(), op.type()); @@ -547,6 +552,9 @@ public class Codegen { } break; } + default: { + throw new NotImplementedException(); + } } } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index b22ef7b2..ca965b95 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -35,6 +35,7 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.ExpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.FltLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ForloopContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.IdentifierContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.InstanceofexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.IntLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LabeledstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.LambdaLVTIParameterContext; @@ -85,6 +86,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; import de.dhbwstuttgart.syntaxtree.statement.FieldVar; import de.dhbwstuttgart.syntaxtree.statement.IfStmt; +import de.dhbwstuttgart.syntaxtree.statement.InstanceOf; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.statement.LocalVar; @@ -482,8 +484,10 @@ public class StatementGenerator { return convert(mathexpr); case RelationalexpressionContext comparison: return convert(comparison); + case InstanceofexpressionContext instanceOf: + return convert(instanceOf); /* - * TODO: syntaxtree for instanceof vorbereiten case InstanceofexpressionContext instanceof: case SwitchexpressionContext switchexpression: + * TODO: case SwitchexpressionContext switchexpression: */ case EqualityexpressionContext equal: return convert(equal); @@ -700,6 +704,14 @@ public class StatementGenerator { return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart()); } + private Expression convert(Java17Parser.InstanceofexpressionContext expression) { + if (Objects.isNull(expression.pattern())) { + return new InstanceOf(convert(expression.expression()), TypeGenerator.convert(expression.typeType(), reg, generics), expression.getStart()); + } else { + throw new NotImplementedException(); + } + } + private BinaryExpr.Operator convertBinaryOperator(String operator) { // return BinaryExpr.Operator.ADD; if (operator.equals("+")) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 0aa350c9..c3562f7f 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -134,6 +134,11 @@ public abstract class AbstractASTWalker implements ASTVisitor { } + @Override + public void visit(BoolExpression logical) { + + } + @Override public void visit(Block block) { for (Statement stmt : block.getStatements()) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 59390f19..480d2165 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -13,6 +13,8 @@ public interface StatementVisitor { void visit(BinaryExpr binary); + void visit(BoolExpression logical); + void visit(Block block); void visit(CastExpr castExpr); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index cd93959c..4fe8f9fb 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -15,15 +15,16 @@ public class BinaryExpr extends Expression { SUB, // - MUL, // * MOD, // Modulo Operator % - AND, // && - OR, // || + AND, // & + OR, // | DIV, // / LESSTHAN, // < BIGGERTHAN, // > LESSEQUAL, // <= BIGGEREQUAL, // >= EQUAL, // == - NOTEQUAL // != + NOTEQUAL, // != + INSTOF // instanceof } public final Operator operation; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BoolExpression.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BoolExpression.java new file mode 100644 index 00000000..018c6dc6 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/BoolExpression.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class BoolExpression extends Expression { + + public enum Operator { + AND, // && + OR, // || + } + + public final Operator operation; + public final Expression lexpr; + public final Expression rexpr; + + public BoolExpression(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset) { + super(type, offset); + this.operation = operation; + this.lexpr = lexpr; + this.rexpr = rexpr; + } + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index aea18dc3..f3ac9a86 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -1,17 +1,26 @@ package de.dhbwstuttgart.syntaxtree.statement; -import de.dhbwstuttgart.exceptions.NotImplementedException; +import org.antlr.v4.runtime.Token; + import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; public class InstanceOf extends BinaryExpr { - public Expression expr; private RefTypeOrTPHOrWildcardOrGeneric reftype; - public InstanceOf(int offset, int variableLength) { - super(null, null, null, null, null); - throw new NotImplementedException(); - // #JB# 20.04.2005 + public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, Token offset) { + super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar("", reftype, reftype.getOffset()), offset); + this.reftype = reftype; + } + + public InstanceOf(Expression expr, RefTypeOrTPHOrWildcardOrGeneric reftype, String name, Token offset) { + super(BinaryExpr.Operator.INSTOF, TypePlaceholder.fresh(offset), expr, new LocalVar(name, reftype, reftype.getOffset()), offset); + this.reftype = reftype; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReftype() { + return reftype; } @Override diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java new file mode 100644 index 00000000..bfe48a0c --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Switch.java @@ -0,0 +1,21 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class Switch extends Statement { + + public Switch(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + super(type, offset); + // TODO Auto-generated constructor stub + } + + @Override + public void accept(StatementVisitor visitor) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'accept'"); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java new file mode 100644 index 00000000..65ac46c9 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/SwitchExpression.java @@ -0,0 +1,21 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import org.antlr.v4.runtime.Token; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class SwitchExpression extends Expression { + + public SwitchExpression(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + super(type, offset); + // TODO Auto-generated constructor stub + } + + @Override + public void accept(StatementVisitor visitor) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'accept'"); + } + +} diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 151a70ce..69465821 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -202,6 +202,13 @@ public class OutputGenerator implements ASTVisitor { binary.rexpr.accept(this); } + @Override + public void visit(BoolExpression logical) { + logical.lexpr.accept(this); + out.append(" op "); + logical.rexpr.accept(this); + } + @Override public void visit(Block block) { tab(); @@ -257,12 +264,18 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(InstanceOf instanceOf) { - + instanceOf.lexpr.accept(this); + out.append(" instanceof "); + instanceOf.rexpr.accept(this); } @Override public void visit(LocalVar localVar) { - out.append(localVar.name); + if (localVar.name.isEmpty()) { + localVar.getType().accept(this); + } else { + out.append(localVar.name); + } } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index a5c4d644..f6ebc6ff 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -107,9 +107,15 @@ public class StatementToTargetExpression implements StatementVisitor { case BIGGEREQUAL -> new TargetBinaryOp.GreaterOrEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case EQUAL -> new TargetBinaryOp.Equal(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case NOTEQUAL -> new TargetBinaryOp.NotEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); + case INSTOF -> new TargetBinaryOp.Instof(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); }; } + @Override + public void visit(BoolExpression bool) { + System.out.println("BoolExpression"); + } + @Override public void visit(Block block) { result = converter.convert(block); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java b/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java index 685e9ad2..a1c4e9f4 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/TracingStatementVisitor.java @@ -34,6 +34,12 @@ public abstract class TracingStatementVisitor implements StatementVisitor { binary.rexpr.accept(this); } + @Override + public void visit(BoolExpression bool) { + bool.lexpr.accept(this); + bool.rexpr.accept(this); + } + @Override public void visit(Block block) { for (var expr : block.statements) diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java index 12d56d5b..545bc851 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java @@ -5,44 +5,81 @@ import de.dhbwstuttgart.target.tree.type.TargetType; public sealed interface TargetBinaryOp extends TargetExpression { TargetExpression left(); + TargetExpression right(); // Arithmetic - record Add(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Sub(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Div(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Mul(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Rem(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Add(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Sub(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Div(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Mul(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Rem(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } // Bitwise - record BAnd(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record BOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record XOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Shl(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Shr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record UShr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record BAnd(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record BOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record XOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Shl(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Shr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record UShr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } // Conditional - record And(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} - record Or(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record And(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Or(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } + + record Instof(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp { + } sealed interface TargetRelationalOp extends TargetBinaryOp { @Override default TargetType type() { return TargetType.Boolean; } + TargetType exprType(); } // Comparison // exprType is the type that both arguments get converted to before comparison - record Equal(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} - record Greater(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} - record GreaterOrEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} - record Less(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} - record LessOrEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} - record NotEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp {} + record Equal(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } + + record Greater(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } + + record GreaterOrEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } + + record Less(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } + + record LessOrEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } + + record NotEqual(TargetType exprType, TargetExpression left, TargetExpression right) implements TargetRelationalOp { + } } - - diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 151bc309..be27249d 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -343,6 +343,12 @@ public class TYPEStmt implements StatementVisitor { } } + @Override + public void visit(BoolExpression bool) { + // TODO + return; + } + @Override public void visit(Literal literal) { // Nothing to do here. Literale erzeugen keine Constraints