Added BoolExpression and instanceof to AST

This commit is contained in:
luca9913 2023-06-27 20:25:05 +02:00
parent 994a1571b7
commit 2368a087c0
14 changed files with 211 additions and 33 deletions

View File

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

View File

@ -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("+")) {

View File

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

View File

@ -13,6 +13,8 @@ public interface StatementVisitor {
void visit(BinaryExpr binary);
void visit(BoolExpression logical);
void visit(Block block);
void visit(CastExpr castExpr);

View File

@ -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;

View File

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

View File

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

View File

@ -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'");
}
}

View File

@ -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'");
}
}

View File

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

View File

@ -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);

View File

@ -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)

View File

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

View File

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