feat: add Method to get Result-Set and SyntaxTree instead of two different Methods.

This commit is contained in:
Ruben 2024-12-08 14:32:42 +01:00
parent e93058e76f
commit 3a9468a174
5 changed files with 118 additions and 44 deletions

View File

@ -5,6 +5,7 @@ import de.dhbw.compiler.bytecode.Codegen;
import de.dhbw.compiler.environment.CompilationEnvironment; import de.dhbw.compiler.environment.CompilationEnvironment;
import de.dhbw.compiler.environment.DirectoryClassLoader; import de.dhbw.compiler.environment.DirectoryClassLoader;
import de.dhbw.compiler.exceptions.DebugException; import de.dhbw.compiler.exceptions.DebugException;
import de.dhbw.compiler.languageServerInterface.LanguageServerTransferObject;
import de.dhbw.compiler.parser.JavaTXParser; import de.dhbw.compiler.parser.JavaTXParser;
import de.dhbw.compiler.parser.NullToken; import de.dhbw.compiler.parser.NullToken;
import de.dhbw.compiler.parser.scope.GenericsRegistry; import de.dhbw.compiler.parser.scope.GenericsRegistry;
@ -349,6 +350,92 @@ public class JavaTXCompiler {
return urm; return urm;
} }
/**
* TEMPORARY - Only for Language Server Usage
*/
public LanguageServerTransferObject getResultSetAndAbstractSyntax(File file) throws IOException, ClassNotFoundException {
var sf = sourceFiles.get(file);
Set<ClassOrInterface> allClasses = new HashSet<>();
allClasses.addAll(getAvailableClasses(sf));
allClasses.addAll(sf.getClasses());
var newClasses = CompilationEnvironment.loadDefaultPackageClasses(sf.getPkgName(), file, this).stream().map(ASTFactory::createClass).collect(Collectors.toSet());
for (var clazz : newClasses) {
// Don't load classes that get recompiled
if (sf.getClasses().stream().anyMatch(nf -> nf.getClassName().equals(clazz.getClassName())))
continue;
if (allClasses.stream().noneMatch(old -> old.getClassName().equals(clazz.getClassName())))
allClasses.add(clazz);
}
final ConstraintSet<Pair> cons = getConstraints(file);
Set<Set<UnifyPair>> results = new HashSet<>();
try {
var logFolder = new File(System.getProperty("user.dir") + "/logFiles/");
if (log) logFolder.mkdirs();
Writer logFile = log ? new FileWriter(new File(logFolder, "log_" + sourceFiles.keySet().iterator().next().getName())) : new OutputStreamWriter(new NullOutputStream());
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses.stream().toList(), logFile, classLoader, this);
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(this, cons);
Function<UnifyPair, UnifyPair> distributeInnerVars = x -> {
UnifyType lhs, rhs;
if (((lhs = x.getLhsType()) instanceof PlaceholderType) && ((rhs = x.getRhsType()) instanceof PlaceholderType) && (((PlaceholderType) lhs).isInnerType() || ((PlaceholderType) rhs).isInnerType())) {
((PlaceholderType) lhs).setInnerType(true);
((PlaceholderType) rhs).setInnerType(true);
}
return x;
};
logFile.write("Unify:" + unifyCons.toString());
unifyCons = unifyCons.map(distributeInnerVars);
logFile.write("\nUnify_distributeInnerVars: " + unifyCons.toString());
TypeUnify unify = new TypeUnify();
logFile.write("FC:\\" + finiteClosure.toString() + "\n");
logFile.write(ASTTypePrinter.print(sf));
logFile.flush();
Set<PlaceholderType> varianceTPHold;
Set<PlaceholderType> varianceTPH = new HashSet<>();
varianceTPH = varianceInheritanceConstraintSet(unifyCons);
List<Set<Constraint<UnifyPair>>> oderConstraints = unifyCons.getOderConstraints();
if (resultmodel) {
/* UnifyResultModel Anfang */
UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure);
UnifyResultListenerImpl li = new UnifyResultListenerImpl();
urm.addUnifyResultListener(li);
unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm, usedTasks);
logFile.write("RES_FINAL: " + li.getResults().toString() + "\n");
logFile.flush();
return new LanguageServerTransferObject(li.getResults(), sf, ASTTypePrinter.print(sf));
}
/* UnifyResultModel End */
else {
Set<Set<UnifyPair>> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure), usedTasks);
logFile.write("RES: " + result.toString() + "\n");
logFile.flush();
results.addAll(result);
results = results.stream().map(x -> {
Optional<Set<UnifyPair>> res = new RuleSet().subst(x.stream().map(y -> {
if (y.getPairOp() == PairOperator.SMALLERDOTWC)
y.setPairOp(PairOperator.EQUALSDOT);
return y; // alle Paare a <.? b erden durch a =. b ersetzt
}).collect(Collectors.toCollection(HashSet::new)));
if (res.isPresent()) {// wenn subst ein Erg liefert wurde was veraendert
return new TypeUnifyTask().applyTypeUnificationRules(res.get(), finiteClosure);
} else
return x; // wenn nichts veraendert wurde wird x zurueckgegeben
}).collect(Collectors.toCollection(HashSet::new));
logFile.write("RES_FINAL: " + results.toString() + "\n");
logFile.flush();
logFile.write("PLACEHOLDERS: " + PlaceholderType.EXISTING_PLACEHOLDERS);
logFile.flush();
}
} catch (IOException | ClassNotFoundException e) {
}
return new LanguageServerTransferObject(results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.generateTPHMap(cons))))).collect(Collectors.toList()), sf, ASTTypePrinter.print(sf));
}
public List<ResultSet> typeInference(File file) throws ClassNotFoundException, IOException { public List<ResultSet> typeInference(File file) throws ClassNotFoundException, IOException {
var sf = sourceFiles.get(file); var sf = sourceFiles.get(file);
Set<ClassOrInterface> allClasses = new HashSet<>();// environment.getAllAvailableClasses(); Set<ClassOrInterface> allClasses = new HashSet<>();// environment.getAllAvailableClasses();

View File

@ -19,7 +19,7 @@ public class LanguageServerInterface {
/** /**
* get final Result Set * get final Result Set
* */ * */
public List<ResultSet> getResultSet(String input) throws IOException, ClassNotFoundException { public LanguageServerTransferObject getResultSetAndAbstractSyntax(String input) throws IOException, ClassNotFoundException {
File tempSourcefile = File.createTempFile("pattern", ".java"); File tempSourcefile = File.createTempFile("pattern", ".java");
tempSourcefile.deleteOnExit(); tempSourcefile.deleteOnExit();
@ -28,36 +28,6 @@ public class LanguageServerInterface {
out.close(); out.close();
JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile);
return tx.typeInference(tempSourcefile); return tx.getResultSetAndAbstractSyntax(tempSourcefile);
}
/**
* get abstract Syntax
*/
public SourceFile getAbstractSyntax(String input) throws IOException, ClassNotFoundException {
File tempSourcefile = File.createTempFile("pattern", ".java");
tempSourcefile.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile));
out.write(input);
out.close();
JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile);
return tx.sourceFiles.get(tempSourcefile);
}
/**
* get abstract Syntax as String
*/
public String getAbstractSyntaxAsString(String input) throws IOException, ClassNotFoundException {
File tempSourcefile = File.createTempFile("pattern", ".java");
tempSourcefile.deleteOnExit();
BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile));
out.write(input);
out.close();
JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile);
return ASTTypePrinter.print(tx.sourceFiles.get(tempSourcefile));
} }
} }

View File

@ -0,0 +1,21 @@
package de.dhbw.compiler.languageServerInterface;
import de.dhbw.compiler.syntaxtree.SourceFile;
import de.dhbw.compiler.typeinference.result.ResultSet;
import java.util.List;
public class LanguageServerTransferObject {
List<ResultSet> resultSets;
SourceFile Ast;
String printedAst;
public LanguageServerTransferObject(List<ResultSet> resultSets, SourceFile Ast, String printedAst) {
this.resultSets = resultSets;
this.Ast = Ast;
this.printedAst = printedAst;
}
public List<ResultSet> getResultSets() {return resultSets;}
public SourceFile getAst() {return Ast;}
public String getPrintedAst() {return printedAst;}
}

View File

@ -32,7 +32,7 @@ public class TypeFinder {
* */ * */
public String findAvailableTypes(int line, int character, String currentTextDocument) throws IOException, ClassNotFoundException { public String findAvailableTypes(int line, int character, String currentTextDocument) throws IOException, ClassNotFoundException {
var resultSet = languageServer.getResultSet(currentTextDocument); var resultSet = languageServer.getResultSetAndAbstractSyntax(currentTextDocument);
return ""; return "";
} }
} }

View File

@ -9,20 +9,16 @@ public class CompilerInterfaceTest {
@Test @Test
public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException { public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException {
LanguageServerInterface languageServer = new LanguageServerInterface(); LanguageServerInterface languageServer = new LanguageServerInterface();
var abstractSyntaxString = languageServer.getAbstractSyntaxAsString("public class test{\n" + var res = languageServer.getResultSetAndAbstractSyntax("import java.lang.Integer; public class test{\n" +
" \n" + " \n" +
" public main(int test){\n" + " public main( test){\n" +
"return test;\n" + " Integer i = test; " +
" }\n" + " return i;\n" +
"}");
var resultSet = languageServer.getResultSet("public class test{\n" +
" \n" +
" public main(int test){\n" +
"return test;\n" +
" }\n" + " }\n" +
"}"); "}");
System.out.println(resultSet.toString()); System.out.println("TEST OUTPUT:");
System.out.println(abstractSyntaxString); System.out.println(res.getResultSets().toString());
System.out.println(res.getPrintedAst());
} }
} }