Convert the AST

This commit is contained in:
Victorious3 2022-05-29 20:06:08 +02:00
parent 1ef3f22c18
commit 372182aa8b
11 changed files with 883 additions and 249 deletions

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.7
// Generated from Java8.g4 by ANTLR 4.8
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.ParserRuleContext;

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.7
// Generated from Java8.g4 by ANTLR 4.8
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
@ -11,7 +11,7 @@ import org.antlr.v4.runtime.misc.*;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class Java8Lexer extends Lexer {
static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); }
static { RuntimeMetaData.checkVersion("4.8", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =
@ -42,68 +42,79 @@ public class Java8Lexer extends Lexer {
"DEFAULT_MODE"
};
public static final String[] ruleNames = {
"T__0", "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE", "CATCH",
"CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE", "ELSE",
"ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO", "IMPLEMENTS",
"IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE", "NEW", "PACKAGE",
"PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT", "STATIC", "STRICTFP",
"SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW", "THROWS", "TRANSIENT",
"TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral", "DecimalIntegerLiteral",
"HexIntegerLiteral", "OctalIntegerLiteral", "BinaryIntegerLiteral", "IntegerTypeSuffix",
"DecimalNumeral", "Digits", "Digit", "NonZeroDigit", "DigitsAndUnderscores",
"DigitOrUnderscore", "Underscores", "HexNumeral", "HexDigits", "HexDigit",
"HexDigitsAndUnderscores", "HexDigitOrUnderscore", "OctalNumeral", "OctalDigits",
"OctalDigit", "OctalDigitsAndUnderscores", "OctalDigitOrUnderscore", "BinaryNumeral",
"BinaryDigits", "BinaryDigit", "BinaryDigitsAndUnderscores", "BinaryDigitOrUnderscore",
"FloatingPointLiteral", "DecimalFloatingPointLiteral", "ExponentPart",
"ExponentIndicator", "SignedInteger", "Sign", "FloatTypeSuffix", "HexadecimalFloatingPointLiteral",
"HexSignificand", "BinaryExponent", "BinaryExponentIndicator", "BooleanLiteral",
"CharacterLiteral", "SingleCharacter", "StringLiteral", "StringCharacters",
"StringCharacter", "EscapeSequence", "OctalEscape", "ZeroToThree", "UnicodeEscape",
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC", "ADD",
"SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW", "COLONCOLON",
"ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN", "AND_ASSIGN",
"OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN", "RSHIFT_ASSIGN",
"URSHIFT_ASSIGN", "Identifier", "JavaLetter", "JavaLetterOrDigit", "AT",
"ELLIPSIS", "WS", "COMMENT", "LINE_COMMENT"
};
private static String[] makeRuleNames() {
return new String[] {
"T__0", "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE", "CATCH",
"CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE", "ELSE",
"ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO",
"IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE",
"NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT",
"STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW",
"THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
"DecimalIntegerLiteral", "HexIntegerLiteral", "OctalIntegerLiteral",
"BinaryIntegerLiteral", "IntegerTypeSuffix", "DecimalNumeral", "Digits",
"Digit", "NonZeroDigit", "DigitsAndUnderscores", "DigitOrUnderscore",
"Underscores", "HexNumeral", "HexDigits", "HexDigit", "HexDigitsAndUnderscores",
"HexDigitOrUnderscore", "OctalNumeral", "OctalDigits", "OctalDigit",
"OctalDigitsAndUnderscores", "OctalDigitOrUnderscore", "BinaryNumeral",
"BinaryDigits", "BinaryDigit", "BinaryDigitsAndUnderscores", "BinaryDigitOrUnderscore",
"FloatingPointLiteral", "DecimalFloatingPointLiteral", "ExponentPart",
"ExponentIndicator", "SignedInteger", "Sign", "FloatTypeSuffix", "HexadecimalFloatingPointLiteral",
"HexSignificand", "BinaryExponent", "BinaryExponentIndicator", "BooleanLiteral",
"CharacterLiteral", "SingleCharacter", "StringLiteral", "StringCharacters",
"StringCharacter", "EscapeSequence", "OctalEscape", "ZeroToThree", "UnicodeEscape",
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC",
"ADD", "SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW",
"COLONCOLON", "ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN",
"AND_ASSIGN", "OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN",
"RSHIFT_ASSIGN", "URSHIFT_ASSIGN", "Identifier", "JavaLetter", "JavaLetterOrDigit",
"AT", "ELLIPSIS", "WS", "COMMENT", "LINE_COMMENT"
};
}
public static final String[] ruleNames = makeRuleNames();
private static final String[] _LITERAL_NAMES = {
null, "'var'", "'abstract'", "'assert'", "'boolean'", "'break'", "'byte'",
"'case'", "'catch'", "'char'", "'class'", "'const'", "'continue'", "'default'",
"'do'", "'double'", "'else'", "'enum'", "'extends'", "'final'", "'finally'",
"'float'", "'for'", "'if'", "'goto'", "'implements'", "'import'", "'instanceof'",
"'int'", "'interface'", "'long'", "'native'", "'new'", "'package'", "'private'",
"'protected'", "'public'", "'return'", "'short'", "'static'", "'strictfp'",
"'super'", "'switch'", "'synchronized'", "'this'", "'throw'", "'throws'",
"'transient'", "'try'", "'void'", "'volatile'", "'while'", null, null,
null, null, null, "'null'", "'('", "')'", "'{'", "'}'", "'['", "']'",
"';'", "','", "'.'", "'='", "'>'", "'<'", "'!'", "'~'", "'?'", "':'",
"'=='", "'<='", "'>='", "'!='", "'&&'", "'||'", "'++'", "'--'", "'+'",
"'-'", "'*'", "'/'", "'&'", "'|'", "'^'", "'%'", "'->'", "'::'", "'+='",
"'-='", "'*='", "'/='", "'&='", "'|='", "'^='", "'%='", "'<<='", "'>>='",
"'>>>='", null, "'@'", "'...'"
};
private static final String[] _SYMBOLIC_NAMES = {
null, null, "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE",
"CATCH", "CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE",
"ELSE", "ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO",
"IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE",
"NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT",
"STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW",
"THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
"FloatingPointLiteral", "BooleanLiteral", "CharacterLiteral", "StringLiteral",
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC", "ADD",
"SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW", "COLONCOLON",
"ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN", "AND_ASSIGN",
"OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN", "RSHIFT_ASSIGN",
"URSHIFT_ASSIGN", "Identifier", "AT", "ELLIPSIS", "WS", "COMMENT", "LINE_COMMENT"
};
private static String[] makeLiteralNames() {
return new String[] {
null, "'var'", "'abstract'", "'assert'", "'boolean'", "'break'", "'byte'",
"'case'", "'catch'", "'char'", "'class'", "'const'", "'continue'", "'default'",
"'do'", "'double'", "'else'", "'enum'", "'extends'", "'final'", "'finally'",
"'float'", "'for'", "'if'", "'goto'", "'implements'", "'import'", "'instanceof'",
"'int'", "'interface'", "'long'", "'native'", "'new'", "'package'", "'private'",
"'protected'", "'public'", "'return'", "'short'", "'static'", "'strictfp'",
"'super'", "'switch'", "'synchronized'", "'this'", "'throw'", "'throws'",
"'transient'", "'try'", "'void'", "'volatile'", "'while'", null, null,
null, null, null, "'null'", "'('", "')'", "'{'", "'}'", "'['", "']'",
"';'", "','", "'.'", "'='", "'>'", "'<'", "'!'", "'~'", "'?'", "':'",
"'=='", "'<='", "'>='", "'!='", "'&&'", "'||'", "'++'", "'--'", "'+'",
"'-'", "'*'", "'/'", "'&'", "'|'", "'^'", "'%'", "'->'", "'::'", "'+='",
"'-='", "'*='", "'/='", "'&='", "'|='", "'^='", "'%='", "'<<='", "'>>='",
"'>>>='", null, "'@'", "'...'"
};
}
private static final String[] _LITERAL_NAMES = makeLiteralNames();
private static String[] makeSymbolicNames() {
return new String[] {
null, null, "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE",
"CATCH", "CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE",
"ELSE", "ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF",
"GOTO", "IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG",
"NATIVE", "NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN",
"SHORT", "STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS",
"THROW", "THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
"FloatingPointLiteral", "BooleanLiteral", "CharacterLiteral", "StringLiteral",
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC",
"ADD", "SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW",
"COLONCOLON", "ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN",
"AND_ASSIGN", "OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN",
"RSHIFT_ASSIGN", "URSHIFT_ASSIGN", "Identifier", "AT", "ELLIPSIS", "WS",
"COMMENT", "LINE_COMMENT"
};
}
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
/**

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.7
// Generated from Java8.g4 by ANTLR 4.8
package de.dhbwstuttgart.parser.antlr;
import org.antlr.v4.runtime.tree.ParseTreeListener;

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.target.bytecode;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.target.tree.*;
import de.dhbwstuttgart.target.tree.expression.*;
import de.dhbwstuttgart.target.tree.type.TargetFunNType;
@ -517,6 +518,11 @@ public class Codegen {
}
}
private void generateUnaryOp(State state, TargetUnaryOp op) {
// TODO
throw new NotImplementedException();
}
private void generateLambdaExpression(State state, TargetLambdaExpression lambda) {
var mv = state.mv;
var name = "lambda$" + state.lambdaCounter;
@ -618,6 +624,9 @@ public class Codegen {
case TargetBinaryOp op:
generateBinaryOp(state, op);
break;
case TargetUnaryOp op:
generateUnaryOp(state, op);
break;
case TargetAssign assign: {
switch (assign.left()) {
case TargetLocalVar localVar: {

View File

@ -6,63 +6,75 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.Statement;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.target.tree.*;
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
import de.dhbwstuttgart.target.tree.type.*;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ASTToTargetAST {
public TargetClass convert(ClassOrInterface input, ResultSet resultSet){
throw new NotImplementedException();
protected HashMap<TypePlaceholder, TargetType> sigma;
public ASTToTargetAST(ResultSet resultSet) {
this.sigma = new HashMap<>();
}
public TargetClass convert(ClassOrInterface input, Map<TypePlaceholder, TargetType> sigma){
public TargetClass convert(ClassOrInterface input) {
List<TargetConstructor> targetConstructors = new ArrayList<>();
//TODO constructor conversion -> also reduce syntactic sugar
return new TargetClass(input.getModifiers(),input.getClassName().toString(), sigma.get(input.getSuperClass()),
input.getSuperInterfaces().stream().map(it -> sigma.get(it)).collect(Collectors.toList()),
targetConstructors,
input.getFieldDecl().stream().map(it -> convert(it, sigma)).collect(Collectors.toList()),
input.getMethods().stream().map(it -> convert(it, sigma)).collect(Collectors.toList()));
input.getFieldDecl().stream().map(it -> convert(it)).collect(Collectors.toList()),
input.getMethods().stream().map(it -> convert(input, it)).collect(Collectors.toList()));
}
private TargetMethod convert(Method input, Map<TypePlaceholder, TargetType> sigma) {
private TargetMethod convert(ClassOrInterface owner, Method input) {
List<MethodParameter> params = input.getParameterList().getFormalparalist().stream()
.map(param -> new MethodParameter(convert(param.getType(), sigma), param.getName())).collect(Collectors.toList());
// TODO
return new TargetMethod(0, null, input.name, params, null, convert(input.block));
.map(param -> new MethodParameter(convert(param.getType()), param.getName())).collect(Collectors.toList());
return new TargetMethod(
input.modifier,
new TargetRefType(owner.getClassName().getClassName()),
input.name, params,
convert(input.getReturnType()),
convert(input.block));
}
private TargetBlock convert(Block block) {
return new TargetBlock(block.statements.stream().map(e -> {
StatementToTargetExpression converter = new StatementToTargetExpression();
e.accept(converter);
return converter.result;
}).collect(Collectors.toList()));
protected TargetBlock convert(Block block) {
return new TargetBlock(block.statements.stream().map(this::convert).collect(Collectors.toList()));
}
private TargetField convert(Field input, Map<TypePlaceholder, TargetType> sigma) {
protected TargetExpression convert(Expression expr) {
var converter = new StatementToTargetExpression(this);
expr.accept(converter);
return converter.result;
}
private TargetField convert(Field input) {
return null;
}
private TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map<TypePlaceholder, TargetType> sigma) {
protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) {
return input.acceptTV(new TypeVisitor<>() {
@Override
public TargetType visit(RefType refType) {
return new TargetRefType(refType.getName().toString(),
refType.getParaList().stream().map((it) -> convert(it, sigma)).collect(Collectors.toList()));
refType.getParaList().stream().map(ASTToTargetAST.this::convert).collect(Collectors.toList()));
}
@Override
public TargetType visit(SuperWildcardType superWildcardType) {
return new TargetSuperWildcard(convert(superWildcardType.getInnerType(), sigma));
return new TargetSuperWildcard(convert(superWildcardType.getInnerType()));
}
@Override
@ -72,7 +84,7 @@ public class ASTToTargetAST {
@Override
public TargetType visit(ExtendsWildcardType extendsWildcardType) {
return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType(), sigma));
return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType()));
}
@Override

View File

@ -1,117 +1,178 @@
package de.dhbwstuttgart.target.generate;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
import de.dhbwstuttgart.target.tree.MethodParameter;
import de.dhbwstuttgart.target.tree.expression.*;
import de.dhbwstuttgart.target.tree.type.TargetFunNType;
import java.util.stream.StreamSupport;
public class StatementToTargetExpression implements StatementVisitor {
public StatementToTargetExpression(ASTToTargetAST converter) {
this.converter = converter;
}
public TargetExpression result;
private final ASTToTargetAST converter;
@Override
public void visit(ArgumentList argumentList) {
throw new NotImplementedException();
}
@Override
public void visit(LambdaExpression lambdaExpression) {
var parameters = StreamSupport
.stream(lambdaExpression.params.spliterator(), false)
.map(p -> new MethodParameter(converter.convert(p.getType()), p.getName()))
.toList();
result = new TargetLambdaExpression(
new TargetFunNType(parameters.size(), parameters.stream().map(MethodParameter::type).toList()),
parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(lambdaExpression.methodBody)
);
}
@Override
public void visit(Assign assign) {
TargetExpression left;
if (assign.lefSide instanceof AssignToLocal) {
left = converter.convert(((AssignToLocal) assign.lefSide).localVar);
} else {
left = converter.convert(((AssignToField) assign.lefSide).field);
}
result = new TargetAssign(converter.convert(assign.getType()), left, converter.convert(assign.rightSide));
}
@Override
public void visit(BinaryExpr binary) {
result = switch (binary.operation) {
case ADD -> new TargetBinaryOp.Add(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case SUB -> new TargetBinaryOp.Sub(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MUL -> new TargetBinaryOp.Mul(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MOD -> new TargetBinaryOp.Rem(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case AND -> new TargetBinaryOp.And(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case OR -> new TargetBinaryOp.Or(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case DIV -> new TargetBinaryOp.Div(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case LESSTHAN -> new TargetBinaryOp.Less(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case BIGGERTHAN -> new TargetBinaryOp.Greater(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case LESSEQUAL -> new TargetBinaryOp.LessOrEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
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));
};
}
@Override
public void visit(Block block) {
result = converter.convert(block);
}
@Override
public void visit(CastExpr castExpr) {
result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(castExpr.expr));
}
@Override
public void visit(EmptyStmt emptyStmt) {
result = null;
}
@Override
public void visit(FieldVar fieldVar) {
result = new TargetFieldVar(
converter.convert(fieldVar.getType()),
converter.convert(fieldVar.receiver.getType()),
false,
converter.convert(fieldVar.receiver),
fieldVar.fieldVarName);
}
@Override
public void visit(ForStmt forStmt) {
// TODO Doesn't seem to be fully implemented yet
throw new NotImplementedException();
}
@Override
public void visit(IfStmt ifStmt) {
result = new TargetIf(
converter.convert(ifStmt.expr),
converter.convert(ifStmt.then_block),
converter.convert(ifStmt.else_block)
);
}
@Override
public void visit(InstanceOf instanceOf) {
result = new TargetInstanceOf(converter.convert(instanceOf.lexpr), converter.convert(instanceOf.rexpr.getType()));
}
@Override
public void visit(LocalVar localVar) {
result = new TargetLocalVar(converter.convert(localVar.getType()), localVar.name);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
// TODO No value, is this correct?
result = new TargetVarDecl(converter.convert(localVarDecl.getType()), localVarDecl.getName(), null);
}
@Override
public void visit(MethodCall methodCall) {
var receiver = methodCall.receiver;
result = new TargetMethodCall(
converter.convert(methodCall.getType()),
methodCall.argTypes.stream().map(converter::convert).toList(),
converter.convert(receiver),
methodCall.getArgumentList().getArguments().stream().map(converter::convert).toList(),
converter.convert(receiver.getType()),
methodCall.name, false, false
);
}
@Override
public void visit(NewClass methodCall) {
public void visit(NewClass newClass) {
result = new TargetNew(
converter.convert(newClass.receiverType),
newClass.getArgumentList().getArguments().stream().map(converter::convert).toList()
);
}
@Override
public void visit(NewArray newArray) {
// TODO
throw new NotImplementedException();
}
@Override
public void visit(Return aReturn) {
result = new TargetReturn(converter.convert(aReturn.retexpr));
}
@Override
public void visit(ReturnVoid aReturn) {
result = new TargetReturn(null);
}
@Override
public void visit(StaticClassName staticClassName) {
result = new TargetClassName(converter.convert(staticClassName.getType()));
}
@Override
public void visit(Super aSuper) {
result = new TargetSuper(converter.convert(aSuper.getType()));
}
@Override
public void visit(This aThis) {
result = new TargetThis(converter.convert(aThis.getType()));
}
@Override
@ -121,36 +182,63 @@ public class StatementToTargetExpression implements StatementVisitor {
@Override
public void visit(DoStmt whileStmt) {
throw new NotImplementedException();
}
// TODO These two might not be necessary
@Override
public void visit(AssignToField assignLeftSide) {
result = converter.convert(assignLeftSide.field);
}
@Override
public void visit(AssignToLocal assignLeftSide) {
result = converter.convert(assignLeftSide.localVar);
}
@Override
public void visit(SuperCall superCall) {
this.visit((MethodCall) superCall);
}
@Override
public void visit(ExpressionReceiver expressionReceiver) {
// TODO What is this?
throw new NotImplementedException();
}
@Override
public void visit(UnaryExpr unaryExpr) {
result = switch (unaryExpr.operation) {
case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case MINUS -> new TargetUnaryOp.Negate(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PREINCREMENT -> new TargetUnaryOp.PreIncrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PREDECREMENT -> new TargetUnaryOp.PreDecrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case POSTINCREMENT -> new TargetUnaryOp.PostIncrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PLUS -> new TargetUnaryOp.Add(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case POSTDECREMENT -> new TargetUnaryOp.PostDecrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
};
}
@Override
public void visit(Literal literal) {
if (literal.value instanceof Integer
|| literal.value instanceof Short
|| literal.value instanceof Byte) {
result = new TargetLiteral.IntLiteral((int) literal.value);
} else if (literal.value instanceof Float) {
result = new TargetLiteral.FloatLiteral((float) literal.value);
} else if (literal.value instanceof Double) {
result = new TargetLiteral.DoubleLiteral((double) literal.value);
} else if (literal.value instanceof Long) {
result = new TargetLiteral.LongLiteral((long) literal.value);
} else if (literal.value instanceof Character) {
result = new TargetLiteral.CharLiteral((char) literal.value);
} else if (literal.value instanceof String) {
result = new TargetLiteral.StringLiteral((String) literal.value);
} else if (literal.value instanceof Boolean) {
result = new TargetLiteral.BooleanLiteral((boolean) literal.value);
}
}
}

View File

@ -3,5 +3,5 @@ package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.type.TargetRefType;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record TargetFieldVar(TargetType type, TargetRefType owner, boolean isStatic, TargetExpression left, String right) implements TargetExpression {
public record TargetFieldVar(TargetType type, TargetType owner, boolean isStatic, TargetExpression left, String right) implements TargetExpression {
}

View File

@ -3,7 +3,7 @@ package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.target.tree.type.TargetType;
public record TargetInstanceOf(Expression left, TargetType right) implements TargetExpression {
public record TargetInstanceOf(TargetExpression left, TargetType right) implements TargetExpression {
@Override
public TargetType type() {

View File

@ -6,6 +6,7 @@ public sealed interface TargetUnaryOp extends TargetExpression {
TargetExpression expr();
record Negate(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
record Add(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
record Not(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
record PreIncrement(TargetType type, TargetExpression expr) implements TargetStatementExpression {}

View File

@ -23,7 +23,7 @@ public class ASTToTypedTargetAST {
ClassOrInterface emptyClass = new ClassOrInterface(0, new JavaClassName("EmptyClass"), new ArrayList<>(), java.util.Optional.empty(), new ArrayList<>(), new ArrayList<>(), new GenericDeclarationList(new ArrayList<>(), new NullToken()),
new RefType(new JavaClassName("Object"), new NullToken()), false, new ArrayList<>(), new NullToken());
ResultSet emptyResultSet = new ResultSet(new HashSet<>());
TargetClass emptyTargetClass = new ASTToTargetAST().convert(emptyClass, emptyResultSet);
TargetClass emptyTargetClass = new ASTToTargetAST(emptyResultSet).convert(emptyClass);
assert emptyTargetClass.getName().equals("EmptyClass");
assert emptyTargetClass.methods().size() == 0;
assert emptyTargetClass.fields().size() == 0;