Lambda Ausdruck dem Parser anfügen

This commit is contained in:
JanUlrich 2017-04-19 15:26:37 +02:00
parent 29a17731fd
commit 3d835304cf
19 changed files with 2753 additions and 4658 deletions

View File

@ -5,6 +5,8 @@ import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
@ -21,7 +23,11 @@ public class JavaTXCompiler {
private List<SourceFile> sourceFiles = new ArrayList<>();
public void typeInference(){
public List<TypeInsertPoint> getTypeInserts(File forSourceFile){
return null;
}
public ResultSet typeInference(){
ConstraintSet cons = new ConstraintSet();
List<ClassOrInterface> allClasses = new ArrayList<>();
for(SourceFile sf : sourceFiles){
@ -34,6 +40,7 @@ public class JavaTXCompiler {
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
TypeUnify unify = new TypeUnify();
Set<Set<UnifyPair>> results = new HashSet<>();
for(List<Constraint<UnifyPair>> xCons : unifyCons.cartesianProduct()){
Set<UnifyPair> xConsSet = new HashSet<>();
for(Constraint<UnifyPair> constraint : xCons){
@ -43,8 +50,9 @@ public class JavaTXCompiler {
System.out.println(xConsSet);
Set<Set<UnifyPair>> result = unify.unify(xConsSet, finiteClosure);
System.out.println(result);
results.addAll(result);
}
return new ResultSet(results);
}
public void parse(File sourceFile) throws IOException, ClassNotFoundException {

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
@ -14,7 +15,6 @@ import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
@ -96,7 +96,7 @@ public class StatementGenerator {
}
/*
* StatementGeneration:
* StatementGeneration:
*/
private Statement convert(Java8Parser.StatementContext stmt) {
@ -268,8 +268,9 @@ public class StatementGenerator {
//Check for localVar:
if(localVars.contains(expression)){
return new LocalVar(expression, offset);
}else {
throw new NotImplementedException();
}else{
//throw new NotImplementedException();
//Dann Muss es ein Feld sein!
}
}
return generateFieldVarOrClassname(expression, offset);
@ -716,11 +717,29 @@ public class StatementGenerator {
return convert(expression.methodInvocation_lfno_primary());
}else if(expression.classInstanceCreationExpression_lfno_primary() != null) {
return convert(expression.classInstanceCreationExpression_lfno_primary());
}else if(expression.getText().equals("this")) {
return new This(expression.getStart());
}else if(expression.fieldAccess_lfno_primary() != null){
return convert(expression.fieldAccess_lfno_primary());
}else if(expression.methodReference_lfno_primary() != null){
throw new NotImplementedException();
}else if(expression.typeName() != null){
throw new NotImplementedException();
}else if(expression.unannPrimitiveType() != null){
throw new NotImplementedException();
}else if(expression.arrayAccess_lfno_primary() != null){
throw new NotImplementedException();
}else if(expression.fieldAccess_lfno_primary() != null){
throw new NotImplementedException();
}else{
throw new NotImplementedException();
}
}
private Expression convert(Java8Parser.FieldAccess_lfno_primaryContext fieldAccess_lfno_primaryContext) {
throw new NotImplementedException();
}
private Expression convert(Java8Parser.ClassInstanceCreationExpression_lfno_primaryContext newExpression) {
if(newExpression.expressionName()!= null)throw new NotImplementedException();
@ -784,8 +803,32 @@ public class StatementGenerator {
return ret;
}
private static Expression convert(Java8Parser.LambdaExpressionContext expression) {
throw new NotImplementedException();
private Expression convert(Java8Parser.LambdaExpressionContext expression) {
Java8Parser.LambdaParametersContext lambdaParams = expression.lambdaParameters();
ParameterList params;
if(lambdaParams.Identifier() != null){
List<FormalParameter> parameterList = new ArrayList<>();
parameterList.add(new FormalParameter(lambdaParams.Identifier().getText(),
TypePlaceholder.fresh(lambdaParams.getStart()), lambdaParams.getStart()));
params = new ParameterList(parameterList, lambdaParams.getStart());
}else if(lambdaParams.formalParameterList() != null){
params = convert(lambdaParams.formalParameterList());
//}else if( lambdaParams.inferredFormalParameterList != null){
}else {
params = new ParameterList(new ArrayList<>(), expression.getStart());
}
Block block;
if(expression.lambdaBody().expression() != null){
List<Statement> statements = new ArrayList<>();
statements.add(new Return(convert(expression.lambdaBody().expression()),
expression.lambdaBody().expression().getStart()));
block = new Block(statements, expression.lambdaBody().getStart());
}else{
block = convert(expression.lambdaBody().block());
}
return new LambdaExpression(TypePlaceholder.fresh(expression.getStart()),
params, block, expression.getStart());
}
}

View File

@ -314,6 +314,7 @@ public class SyntaxTreeGenerator{
// Initialize a field by creating implicit constructor.
private void initializeField(Java8Parser.FieldDeclarationContext ctx){
//TODO
throw new NotImplementedException();
}
public static int convertModifier(String modifier){

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
@ -16,7 +17,6 @@ import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.ArrayList;
import java.util.List;

View File

@ -352,7 +352,7 @@ variableDeclaratorList
;
variableDeclarator
: variableDeclaratorId //('=' variableInitializer)? //auskommentiert, weil variablenDecklaration sonst nicht eindeutig
: variableDeclaratorId ('=' variableInitializer)? //auskommentiert, weil variablenDecklaration sonst nicht eindeutig
;
variableDeclaratorId
@ -1011,17 +1011,17 @@ primaryNoNewArray_lf_primary_lfno_arrayAccess_lf_primary
;
primaryNoNewArray_lfno_primary
: literal
: literal //done
| typeName ('[' ']')* '.' 'class'
| unannPrimitiveType ('[' ']')* '.' 'class'
| 'void' '.' 'class'
| 'this'
| typeName '.' 'this'
| '(' expression ')'
| classInstanceCreationExpression_lfno_primary
| '(' expression ')' //done
| classInstanceCreationExpression_lfno_primary //done
| fieldAccess_lfno_primary
| arrayAccess_lfno_primary
| methodInvocation_lfno_primary
| methodInvocation_lfno_primary //done
| methodReference_lfno_primary
;
@ -1180,7 +1180,7 @@ lambdaExpression
lambdaParameters
: Identifier
| '(' formalParameterList? ')'
| '(' inferredFormalParameterList ')'
//| '(' inferredFormalParameterList ')'
;
inferredFormalParameterList

View File

@ -1,4 +1,4 @@
// Generated from Java8.g4 by ANTLR 4.5.3
// Generated from /home/janulrich/Development/intellijworkspace/JavaCompilerCore/src/de/dhbwstuttgart/parser/antlr/Java8.g4 by ANTLR 4.5.1
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.5.3", RuntimeMetaData.VERSION); }
static { RuntimeMetaData.checkVersion("4.5.1", RuntimeMetaData.VERSION); }
protected static final DFA[] _decisionToDFA;
protected static final PredictionContextCache _sharedContextCache =

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,11 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.syntaxtree.operator.Operator;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
// neu von Felix

View File

@ -1,10 +1,10 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class CastExpr extends Expression

View File

@ -1,10 +1,9 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class ForStmt extends Statement
{

View File

@ -1,10 +1,10 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class InstVar extends Expression

View File

@ -1,9 +1,9 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class InstanceOf extends BinaryExpr

View File

@ -1,32 +1,27 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.type.FunN;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import org.antlr.v4.runtime.Token;
/**
* @author A10023 - Andreas Stadelmeier
* Momentan erweitert LambdaExpression noch Expr und erbt dadurch auch von ExprStatement ist also auch ein Statement
*
* LambdaExpression Aufbau:
* ( ParameterList ) -> { method_body };
* ( ParameterList ) -> { methodBody };
*/
public class LambdaExpression extends Expression{
/**
* Wird bei der TYPE Methode gesetzt. Speichert den errechneten Typ des LambdaAusdrucks
*/
private FunN lambdaType;
private Block method_body;
private Block methodBody;
private ParameterList params;
public LambdaExpression(int offset, int variableLength) {
super(null,null);
public LambdaExpression(RefTypeOrTPHOrWildcardOrGeneric type, ParameterList params, Block methodBody,Token offset) {
super(type,offset);
this.methodBody = methodBody;
this.params = params;
}

View File

@ -1,11 +1,11 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.List;

View File

@ -1,11 +1,12 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import org.antlr.v4.runtime.Token;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
public class LocalVarDecl extends Statement
{

View File

@ -0,0 +1,17 @@
package de.dhbwstuttgart.typedeployment;
import org.antlr.v4.runtime.Token;
public class TypeInsertPoint {
Token point;
private String insertString;
public TypeInsertPoint(Token point, String toInsert){
this.point = point;
this.insertString = toInsert;
}
public String insert(String intoSource){
return new StringBuilder(intoSource).insert(point.getStartIndex(), insertString).toString();
}
}

View File

@ -0,0 +1,11 @@
package de.dhbwstuttgart.typeinference;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
import java.util.Set;
public class ResultSet {
public ResultSet(Set<Set<UnifyPair>> results){
}
}

12
test/javFiles/Lambda.jav Normal file
View File

@ -0,0 +1,12 @@
class Lambda{
String var;
methode(){
return () -> (f) -> {
f.apply(this,var);
return var;
};
}
}

View File

@ -17,7 +17,8 @@ public class JavaTXCompilerTest {
public void test() throws IOException, ClassNotFoundException {
JavaTXCompiler compiler = new JavaTXCompiler();
//compiler.parse(new File(rootDirectory+"Methods.jav"));
compiler.parse(new File(rootDirectory+"Generics.jav"));
//compiler.parse(new File(rootDirectory+"Generics.jav"));
compiler.parse(new File(rootDirectory+"Lambda.jav"));
compiler.typeInference();
}
}