forked from JavaTX/JavaCompilerCore
Convert the AST
This commit is contained in:
parent
1ef3f22c18
commit
372182aa8b
@ -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;
|
package de.dhbwstuttgart.parser.antlr;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
|
@ -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;
|
package de.dhbwstuttgart.parser.antlr;
|
||||||
import org.antlr.v4.runtime.Lexer;
|
import org.antlr.v4.runtime.Lexer;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
@ -11,7 +11,7 @@ import org.antlr.v4.runtime.misc.*;
|
|||||||
|
|
||||||
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
|
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
|
||||||
public class Java8Lexer extends Lexer {
|
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 DFA[] _decisionToDFA;
|
||||||
protected static final PredictionContextCache _sharedContextCache =
|
protected static final PredictionContextCache _sharedContextCache =
|
||||||
@ -42,68 +42,79 @@ public class Java8Lexer extends Lexer {
|
|||||||
"DEFAULT_MODE"
|
"DEFAULT_MODE"
|
||||||
};
|
};
|
||||||
|
|
||||||
public static final String[] ruleNames = {
|
private static String[] makeRuleNames() {
|
||||||
"T__0", "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE", "CATCH",
|
return new String[] {
|
||||||
"CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE", "ELSE",
|
"T__0", "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE", "CATCH",
|
||||||
"ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO", "IMPLEMENTS",
|
"CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE", "ELSE",
|
||||||
"IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE", "NEW", "PACKAGE",
|
"ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO",
|
||||||
"PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT", "STATIC", "STRICTFP",
|
"IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE",
|
||||||
"SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW", "THROWS", "TRANSIENT",
|
"NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT",
|
||||||
"TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral", "DecimalIntegerLiteral",
|
"STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW",
|
||||||
"HexIntegerLiteral", "OctalIntegerLiteral", "BinaryIntegerLiteral", "IntegerTypeSuffix",
|
"THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
|
||||||
"DecimalNumeral", "Digits", "Digit", "NonZeroDigit", "DigitsAndUnderscores",
|
"DecimalIntegerLiteral", "HexIntegerLiteral", "OctalIntegerLiteral",
|
||||||
"DigitOrUnderscore", "Underscores", "HexNumeral", "HexDigits", "HexDigit",
|
"BinaryIntegerLiteral", "IntegerTypeSuffix", "DecimalNumeral", "Digits",
|
||||||
"HexDigitsAndUnderscores", "HexDigitOrUnderscore", "OctalNumeral", "OctalDigits",
|
"Digit", "NonZeroDigit", "DigitsAndUnderscores", "DigitOrUnderscore",
|
||||||
"OctalDigit", "OctalDigitsAndUnderscores", "OctalDigitOrUnderscore", "BinaryNumeral",
|
"Underscores", "HexNumeral", "HexDigits", "HexDigit", "HexDigitsAndUnderscores",
|
||||||
"BinaryDigits", "BinaryDigit", "BinaryDigitsAndUnderscores", "BinaryDigitOrUnderscore",
|
"HexDigitOrUnderscore", "OctalNumeral", "OctalDigits", "OctalDigit",
|
||||||
"FloatingPointLiteral", "DecimalFloatingPointLiteral", "ExponentPart",
|
"OctalDigitsAndUnderscores", "OctalDigitOrUnderscore", "BinaryNumeral",
|
||||||
"ExponentIndicator", "SignedInteger", "Sign", "FloatTypeSuffix", "HexadecimalFloatingPointLiteral",
|
"BinaryDigits", "BinaryDigit", "BinaryDigitsAndUnderscores", "BinaryDigitOrUnderscore",
|
||||||
"HexSignificand", "BinaryExponent", "BinaryExponentIndicator", "BooleanLiteral",
|
"FloatingPointLiteral", "DecimalFloatingPointLiteral", "ExponentPart",
|
||||||
"CharacterLiteral", "SingleCharacter", "StringLiteral", "StringCharacters",
|
"ExponentIndicator", "SignedInteger", "Sign", "FloatTypeSuffix", "HexadecimalFloatingPointLiteral",
|
||||||
"StringCharacter", "EscapeSequence", "OctalEscape", "ZeroToThree", "UnicodeEscape",
|
"HexSignificand", "BinaryExponent", "BinaryExponentIndicator", "BooleanLiteral",
|
||||||
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
|
"CharacterLiteral", "SingleCharacter", "StringLiteral", "StringCharacters",
|
||||||
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
|
"StringCharacter", "EscapeSequence", "OctalEscape", "ZeroToThree", "UnicodeEscape",
|
||||||
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC", "ADD",
|
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
|
||||||
"SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW", "COLONCOLON",
|
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
|
||||||
"ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN", "AND_ASSIGN",
|
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC",
|
||||||
"OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN", "RSHIFT_ASSIGN",
|
"ADD", "SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW",
|
||||||
"URSHIFT_ASSIGN", "Identifier", "JavaLetter", "JavaLetterOrDigit", "AT",
|
"COLONCOLON", "ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN",
|
||||||
"ELLIPSIS", "WS", "COMMENT", "LINE_COMMENT"
|
"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 = {
|
private static String[] makeLiteralNames() {
|
||||||
null, "'var'", "'abstract'", "'assert'", "'boolean'", "'break'", "'byte'",
|
return new String[] {
|
||||||
"'case'", "'catch'", "'char'", "'class'", "'const'", "'continue'", "'default'",
|
null, "'var'", "'abstract'", "'assert'", "'boolean'", "'break'", "'byte'",
|
||||||
"'do'", "'double'", "'else'", "'enum'", "'extends'", "'final'", "'finally'",
|
"'case'", "'catch'", "'char'", "'class'", "'const'", "'continue'", "'default'",
|
||||||
"'float'", "'for'", "'if'", "'goto'", "'implements'", "'import'", "'instanceof'",
|
"'do'", "'double'", "'else'", "'enum'", "'extends'", "'final'", "'finally'",
|
||||||
"'int'", "'interface'", "'long'", "'native'", "'new'", "'package'", "'private'",
|
"'float'", "'for'", "'if'", "'goto'", "'implements'", "'import'", "'instanceof'",
|
||||||
"'protected'", "'public'", "'return'", "'short'", "'static'", "'strictfp'",
|
"'int'", "'interface'", "'long'", "'native'", "'new'", "'package'", "'private'",
|
||||||
"'super'", "'switch'", "'synchronized'", "'this'", "'throw'", "'throws'",
|
"'protected'", "'public'", "'return'", "'short'", "'static'", "'strictfp'",
|
||||||
"'transient'", "'try'", "'void'", "'volatile'", "'while'", null, null,
|
"'super'", "'switch'", "'synchronized'", "'this'", "'throw'", "'throws'",
|
||||||
null, null, null, "'null'", "'('", "')'", "'{'", "'}'", "'['", "']'",
|
"'transient'", "'try'", "'void'", "'volatile'", "'while'", null, 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",
|
private static final String[] _LITERAL_NAMES = makeLiteralNames();
|
||||||
"ELSE", "ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF", "GOTO",
|
private static String[] makeSymbolicNames() {
|
||||||
"IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG", "NATIVE",
|
return new String[] {
|
||||||
"NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN", "SHORT",
|
null, null, "ABSTRACT", "ASSERT", "BOOLEAN", "BREAK", "BYTE", "CASE",
|
||||||
"STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS", "THROW",
|
"CATCH", "CHAR", "CLASS", "CONST", "CONTINUE", "DEFAULT", "DO", "DOUBLE",
|
||||||
"THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
|
"ELSE", "ENUM", "EXTENDS", "FINAL", "FINALLY", "FLOAT", "FOR", "IF",
|
||||||
"FloatingPointLiteral", "BooleanLiteral", "CharacterLiteral", "StringLiteral",
|
"GOTO", "IMPLEMENTS", "IMPORT", "INSTANCEOF", "INT", "INTERFACE", "LONG",
|
||||||
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
|
"NATIVE", "NEW", "PACKAGE", "PRIVATE", "PROTECTED", "PUBLIC", "RETURN",
|
||||||
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
|
"SHORT", "STATIC", "STRICTFP", "SUPER", "SWITCH", "SYNCHRONIZED", "THIS",
|
||||||
"COLON", "EQUAL", "LE", "GE", "NOTEQUAL", "AND", "OR", "INC", "DEC", "ADD",
|
"THROW", "THROWS", "TRANSIENT", "TRY", "VOID", "VOLATILE", "WHILE", "IntegerLiteral",
|
||||||
"SUB", "MUL", "DIV", "BITAND", "BITOR", "CARET", "MOD", "ARROW", "COLONCOLON",
|
"FloatingPointLiteral", "BooleanLiteral", "CharacterLiteral", "StringLiteral",
|
||||||
"ADD_ASSIGN", "SUB_ASSIGN", "MUL_ASSIGN", "DIV_ASSIGN", "AND_ASSIGN",
|
"NullLiteral", "LPAREN", "RPAREN", "LBRACE", "RBRACE", "LBRACK", "RBRACK",
|
||||||
"OR_ASSIGN", "XOR_ASSIGN", "MOD_ASSIGN", "LSHIFT_ASSIGN", "RSHIFT_ASSIGN",
|
"SEMI", "COMMA", "DOT", "ASSIGN", "GT", "LT", "BANG", "TILDE", "QUESTION",
|
||||||
"URSHIFT_ASSIGN", "Identifier", "AT", "ELLIPSIS", "WS", "COMMENT", "LINE_COMMENT"
|
"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);
|
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
package de.dhbwstuttgart.parser.antlr;
|
||||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
|||||||
package de.dhbwstuttgart.target.bytecode;
|
package de.dhbwstuttgart.target.bytecode;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.target.tree.*;
|
import de.dhbwstuttgart.target.tree.*;
|
||||||
import de.dhbwstuttgart.target.tree.expression.*;
|
import de.dhbwstuttgart.target.tree.expression.*;
|
||||||
import de.dhbwstuttgart.target.tree.type.TargetFunNType;
|
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) {
|
private void generateLambdaExpression(State state, TargetLambdaExpression lambda) {
|
||||||
var mv = state.mv;
|
var mv = state.mv;
|
||||||
var name = "lambda$" + state.lambdaCounter;
|
var name = "lambda$" + state.lambdaCounter;
|
||||||
@ -618,6 +624,9 @@ public class Codegen {
|
|||||||
case TargetBinaryOp op:
|
case TargetBinaryOp op:
|
||||||
generateBinaryOp(state, op);
|
generateBinaryOp(state, op);
|
||||||
break;
|
break;
|
||||||
|
case TargetUnaryOp op:
|
||||||
|
generateUnaryOp(state, op);
|
||||||
|
break;
|
||||||
case TargetAssign assign: {
|
case TargetAssign assign: {
|
||||||
switch (assign.left()) {
|
switch (assign.left()) {
|
||||||
case TargetLocalVar localVar: {
|
case TargetLocalVar localVar: {
|
||||||
|
@ -6,63 +6,75 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
|||||||
import de.dhbwstuttgart.syntaxtree.Field;
|
import de.dhbwstuttgart.syntaxtree.Field;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.Block;
|
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.syntaxtree.type.*;
|
||||||
import de.dhbwstuttgart.target.tree.*;
|
import de.dhbwstuttgart.target.tree.*;
|
||||||
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
|
import de.dhbwstuttgart.target.tree.expression.TargetBlock;
|
||||||
|
import de.dhbwstuttgart.target.tree.expression.TargetExpression;
|
||||||
import de.dhbwstuttgart.target.tree.type.*;
|
import de.dhbwstuttgart.target.tree.type.*;
|
||||||
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ASTToTargetAST {
|
public class ASTToTargetAST {
|
||||||
|
|
||||||
public TargetClass convert(ClassOrInterface input, ResultSet resultSet){
|
protected HashMap<TypePlaceholder, TargetType> sigma;
|
||||||
throw new NotImplementedException();
|
|
||||||
|
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<>();
|
List<TargetConstructor> targetConstructors = new ArrayList<>();
|
||||||
//TODO constructor conversion -> also reduce syntactic sugar
|
//TODO constructor conversion -> also reduce syntactic sugar
|
||||||
return new TargetClass(input.getModifiers(),input.getClassName().toString(), sigma.get(input.getSuperClass()),
|
return new TargetClass(input.getModifiers(),input.getClassName().toString(), sigma.get(input.getSuperClass()),
|
||||||
input.getSuperInterfaces().stream().map(it -> sigma.get(it)).collect(Collectors.toList()),
|
input.getSuperInterfaces().stream().map(it -> sigma.get(it)).collect(Collectors.toList()),
|
||||||
targetConstructors,
|
targetConstructors,
|
||||||
input.getFieldDecl().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(it, sigma)).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()
|
List<MethodParameter> params = input.getParameterList().getFormalparalist().stream()
|
||||||
.map(param -> new MethodParameter(convert(param.getType(), sigma), param.getName())).collect(Collectors.toList());
|
.map(param -> new MethodParameter(convert(param.getType()), param.getName())).collect(Collectors.toList());
|
||||||
// TODO
|
return new TargetMethod(
|
||||||
return new TargetMethod(0, null, input.name, params, null, convert(input.block));
|
input.modifier,
|
||||||
|
new TargetRefType(owner.getClassName().getClassName()),
|
||||||
|
input.name, params,
|
||||||
|
convert(input.getReturnType()),
|
||||||
|
convert(input.block));
|
||||||
}
|
}
|
||||||
|
|
||||||
private TargetBlock convert(Block block) {
|
protected TargetBlock convert(Block block) {
|
||||||
return new TargetBlock(block.statements.stream().map(e -> {
|
return new TargetBlock(block.statements.stream().map(this::convert).collect(Collectors.toList()));
|
||||||
StatementToTargetExpression converter = new StatementToTargetExpression();
|
|
||||||
e.accept(converter);
|
|
||||||
return converter.result;
|
|
||||||
}).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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map<TypePlaceholder, TargetType> sigma) {
|
protected TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input) {
|
||||||
return input.acceptTV(new TypeVisitor<>() {
|
return input.acceptTV(new TypeVisitor<>() {
|
||||||
@Override
|
@Override
|
||||||
public TargetType visit(RefType refType) {
|
public TargetType visit(RefType refType) {
|
||||||
return new TargetRefType(refType.getName().toString(),
|
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
|
@Override
|
||||||
public TargetType visit(SuperWildcardType superWildcardType) {
|
public TargetType visit(SuperWildcardType superWildcardType) {
|
||||||
return new TargetSuperWildcard(convert(superWildcardType.getInnerType(), sigma));
|
return new TargetSuperWildcard(convert(superWildcardType.getInnerType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -72,7 +84,7 @@ public class ASTToTargetAST {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TargetType visit(ExtendsWildcardType extendsWildcardType) {
|
public TargetType visit(ExtendsWildcardType extendsWildcardType) {
|
||||||
return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType(), sigma));
|
return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,117 +1,178 @@
|
|||||||
package de.dhbwstuttgart.target.generate;
|
package de.dhbwstuttgart.target.generate;
|
||||||
|
|
||||||
|
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||||||
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.*;
|
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 class StatementToTargetExpression implements StatementVisitor {
|
||||||
|
|
||||||
|
public StatementToTargetExpression(ASTToTargetAST converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
}
|
||||||
|
|
||||||
public TargetExpression result;
|
public TargetExpression result;
|
||||||
|
private final ASTToTargetAST converter;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ArgumentList argumentList) {
|
public void visit(ArgumentList argumentList) {
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LambdaExpression lambdaExpression) {
|
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
|
@Override
|
||||||
public void visit(Assign assign) {
|
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
|
@Override
|
||||||
public void visit(BinaryExpr binary) {
|
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
|
@Override
|
||||||
public void visit(Block block) {
|
public void visit(Block block) {
|
||||||
|
result = converter.convert(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(CastExpr castExpr) {
|
public void visit(CastExpr castExpr) {
|
||||||
|
result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(castExpr.expr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(EmptyStmt emptyStmt) {
|
public void visit(EmptyStmt emptyStmt) {
|
||||||
|
result = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(FieldVar fieldVar) {
|
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
|
@Override
|
||||||
public void visit(ForStmt forStmt) {
|
public void visit(ForStmt forStmt) {
|
||||||
|
// TODO Doesn't seem to be fully implemented yet
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(IfStmt ifStmt) {
|
public void visit(IfStmt ifStmt) {
|
||||||
|
result = new TargetIf(
|
||||||
|
converter.convert(ifStmt.expr),
|
||||||
|
converter.convert(ifStmt.then_block),
|
||||||
|
converter.convert(ifStmt.else_block)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(InstanceOf instanceOf) {
|
public void visit(InstanceOf instanceOf) {
|
||||||
|
result = new TargetInstanceOf(converter.convert(instanceOf.lexpr), converter.convert(instanceOf.rexpr.getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LocalVar localVar) {
|
public void visit(LocalVar localVar) {
|
||||||
|
result = new TargetLocalVar(converter.convert(localVar.getType()), localVar.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LocalVarDecl localVarDecl) {
|
public void visit(LocalVarDecl localVarDecl) {
|
||||||
|
// TODO No value, is this correct?
|
||||||
|
result = new TargetVarDecl(converter.convert(localVarDecl.getType()), localVarDecl.getName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MethodCall methodCall) {
|
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
|
@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
|
@Override
|
||||||
public void visit(NewArray newArray) {
|
public void visit(NewArray newArray) {
|
||||||
|
// TODO
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Return aReturn) {
|
public void visit(Return aReturn) {
|
||||||
|
result = new TargetReturn(converter.convert(aReturn.retexpr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ReturnVoid aReturn) {
|
public void visit(ReturnVoid aReturn) {
|
||||||
|
result = new TargetReturn(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(StaticClassName staticClassName) {
|
public void visit(StaticClassName staticClassName) {
|
||||||
|
result = new TargetClassName(converter.convert(staticClassName.getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Super aSuper) {
|
public void visit(Super aSuper) {
|
||||||
|
result = new TargetSuper(converter.convert(aSuper.getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(This aThis) {
|
public void visit(This aThis) {
|
||||||
|
result = new TargetThis(converter.convert(aThis.getType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -121,36 +182,63 @@ public class StatementToTargetExpression implements StatementVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(DoStmt whileStmt) {
|
public void visit(DoStmt whileStmt) {
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO These two might not be necessary
|
||||||
@Override
|
@Override
|
||||||
public void visit(AssignToField assignLeftSide) {
|
public void visit(AssignToField assignLeftSide) {
|
||||||
|
result = converter.convert(assignLeftSide.field);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(AssignToLocal assignLeftSide) {
|
public void visit(AssignToLocal assignLeftSide) {
|
||||||
|
result = converter.convert(assignLeftSide.localVar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(SuperCall superCall) {
|
public void visit(SuperCall superCall) {
|
||||||
|
this.visit((MethodCall) superCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ExpressionReceiver expressionReceiver) {
|
public void visit(ExpressionReceiver expressionReceiver) {
|
||||||
|
// TODO What is this?
|
||||||
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(UnaryExpr unaryExpr) {
|
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
|
@Override
|
||||||
public void visit(Literal literal) {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,5 @@ package de.dhbwstuttgart.target.tree.expression;
|
|||||||
import de.dhbwstuttgart.target.tree.type.TargetRefType;
|
import de.dhbwstuttgart.target.tree.type.TargetRefType;
|
||||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
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 {
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package de.dhbwstuttgart.target.tree.expression;
|
|||||||
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||||
import de.dhbwstuttgart.target.tree.type.TargetType;
|
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
|
@Override
|
||||||
public TargetType type() {
|
public TargetType type() {
|
||||||
|
@ -6,6 +6,7 @@ public sealed interface TargetUnaryOp extends TargetExpression {
|
|||||||
TargetExpression expr();
|
TargetExpression expr();
|
||||||
|
|
||||||
record Negate(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
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 Not(TargetType type, TargetExpression expr) implements TargetUnaryOp {}
|
||||||
|
|
||||||
record PreIncrement(TargetType type, TargetExpression expr) implements TargetStatementExpression {}
|
record PreIncrement(TargetType type, TargetExpression expr) implements TargetStatementExpression {}
|
||||||
|
@ -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()),
|
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());
|
new RefType(new JavaClassName("Object"), new NullToken()), false, new ArrayList<>(), new NullToken());
|
||||||
ResultSet emptyResultSet = new ResultSet(new HashSet<>());
|
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.getName().equals("EmptyClass");
|
||||||
assert emptyTargetClass.methods().size() == 0;
|
assert emptyTargetClass.methods().size() == 0;
|
||||||
assert emptyTargetClass.fields().size() == 0;
|
assert emptyTargetClass.fields().size() == 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user