From 78a898db37bc3e6ffbc9a5111c6dac4f9c083d5e Mon Sep 17 00:00:00 2001 From: luca9913 Date: Mon, 20 Mar 2023 03:03:37 +0100 Subject: [PATCH] =?UTF-8?q?Bug=20fixes=20f=C3=BCr=20Tests=20in=20'parser'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../environment/CompilationEnvironment.java | 2 +- .../StatementGenerator.java | 29 ++++++++----------- .../SyntaxTreeGenerator.java | 2 +- src/test/java/parser/CastTest.jav | 2 ++ .../java/parser/FieldInitializationTest.jav | 2 ++ src/test/java/parser/FieldVarTest.jav | 2 +- src/test/java/parser/GeneralParserTest.java | 12 ++++---- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java index 2cd81c73..d4c04a95 100644 --- a/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/main/java/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -96,7 +96,7 @@ public class CompilationEnvironment { File dir = new File(forSourceFile.getAbsoluteFile().getParent()); String dirPath = dir.toString() + "/"; if (packageName.length() > 0) - dirPath = dirPath.substring(0, dirPath.length() - packageName.length()); + dirPath = dirPath.substring(0, dirPath.length() - packageName.length() - 1); String path = dirPath; ArrayList defaultPath = Lists.newArrayList(new File(path)); classLoader = new DirectoryClassLoader(defaultPath, classLoader); diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 9c329489..a94fe608 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -51,7 +51,6 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.PrefixexpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryClassrefContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryExpressionContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryIdentifierContext; -import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryInvocationContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimarySuperContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.PrimaryThisContext; @@ -65,7 +64,6 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.StringLiteralContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchexpressionstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SwitchstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.SynchronizedstmtContext; -import de.dhbwstuttgart.parser.antlr.Java17Parser.TextBlockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ThrowstmtContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchblockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.TrycatchresourceContext; @@ -292,9 +290,9 @@ public class StatementGenerator { private RefType convert(Java17Parser.CreatedNameContext createdname) { Java17Parser.TypeArgumentsContext genericArgs = null; genericArgs = null; - if(createdname.typeArgumentsOrDiamond() != null) + if (createdname.typeArgumentsOrDiamond() != null) genericArgs = createdname.typeArgumentsOrDiamond().typeArguments(); - if(genericArgs != null){ + if (genericArgs != null) { throw new NotImplementedException(); } IdentifierContext identifier = createdname.identifier(); @@ -394,7 +392,13 @@ public class StatementGenerator { } else { type = TypeGenerator.convert(declaration.typeType(), reg, generics); } - ret.addAll(generateLocalVariableAssignments(declaration.variableDeclarators().variableDeclarator(), type)); + if (Objects.isNull(declaration.variableDeclarators())) { + Token offset = declaration.identifier().getStart(); + ret.add(new Assign(new AssignToLocal(new LocalVar(declaration.identifier().getText(), type, offset)), + convert(declaration.expression()), offset)); + } else { + ret.addAll(generateLocalVariableAssignments(declaration.variableDeclarators().variableDeclarator(), type)); + } return ret; } @@ -824,20 +828,11 @@ public class StatementGenerator { private Expression convert(Java17Parser.CastexpressionContext castexpr) { ExpressionContext expr = castexpr.expression(); - Expression exprast = null; - if (expr instanceof LambdaexpressionContext) { - exprast = convert(expr); - } else if (expr instanceof PrefixexpressionContext pfe) { - if (!pfe.prefix.equals("~") && !pfe.prefix.equals("!")) { - throw new NotImplementedException(); - } else { - exprast = convert(expr); - } - } else { + if (expr instanceof PrefixexpressionContext pfe) { throw new NotImplementedException(); } - return new CastExpr(TypeGenerator.convert(castexpr.typeType(0), reg, generics), exprast, - castexpr.getStart()); + return new CastExpr(TypeGenerator.convert(castexpr.typeType(0), reg, generics), convert(expr), + expr.getStart()); } private Expression convert(Java17Parser.PrimaryContext primary) { diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index e66267de..b04868ed 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -172,7 +172,7 @@ public class SyntaxTreeGenerator { } private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx, int modifiers) { - String className = this.pkgName.length() > 0 ? this.pkgName + "." : "" + ctx.identifier().getText(); + String className = this.pkgName.length() > 0 ? this.pkgName + "." : ctx.identifier().getText(); JavaClassName name = reg.getName(className); // Holt den Package Namen mit dazu if (!name.toString().equals(className)) { // Kommt die Klasse schon in einem anderen Package vor? throw new TypeinferenceException( diff --git a/src/test/java/parser/CastTest.jav b/src/test/java/parser/CastTest.jav index 86ca7519..7552882e 100644 --- a/src/test/java/parser/CastTest.jav +++ b/src/test/java/parser/CastTest.jav @@ -1,3 +1,5 @@ +import java.lang.Object; +import java.lang.String; class CastTest{ void methode(){ diff --git a/src/test/java/parser/FieldInitializationTest.jav b/src/test/java/parser/FieldInitializationTest.jav index d4422548..d1743ef6 100644 --- a/src/test/java/parser/FieldInitializationTest.jav +++ b/src/test/java/parser/FieldInitializationTest.jav @@ -1,3 +1,5 @@ +import java.lang.String; + class FieldInitializationTest{ f; void m(){ diff --git a/src/test/java/parser/FieldVarTest.jav b/src/test/java/parser/FieldVarTest.jav index 6e897824..7b2d0551 100644 --- a/src/test/java/parser/FieldVarTest.jav +++ b/src/test/java/parser/FieldVarTest.jav @@ -1,4 +1,4 @@ -package test; +package parser; class Test{ Typ a; diff --git a/src/test/java/parser/GeneralParserTest.java b/src/test/java/parser/GeneralParserTest.java index 5a23ae5d..7c819d7e 100644 --- a/src/test/java/parser/GeneralParserTest.java +++ b/src/test/java/parser/GeneralParserTest.java @@ -31,22 +31,24 @@ public class GeneralParserTest { filenames.add("ImportTest.jav"); filenames.add("CastTest.jav"); filenames.add("StatementsTest.jav"); - //filenames.add("Methods.jav"); + // filenames.add("Methods.jav"); filenames.add("ImportTestGeneric.jav"); filenames.add("CastTest.jav"); - //filenames.add("BoundedParameter.jav"); - //filenames.add("GenericFieldVarTest.jav"); + // filenames.add("BoundedParameter.jav"); + // filenames.add("GenericFieldVarTest.jav"); filenames.add("FieldVarTest.jav"); filenames.add("StructuralTypes.jav"); filenames.add("ExtendsTest.jav"); filenames.add("PackageNameTest.jav"); filenames.add("AntlrTest.jav"); + filenames.add("Java17Rules.jav"); try { - for(String filename : filenames){ + for (String filename : filenames) { System.out.println(filename); new JavaTXCompiler(new File(rootDirectory + filename)); } - //new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + s)).collect(Collectors.toList())); + // new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + + // s)).collect(Collectors.toList())); } catch (Exception exc) { exc.printStackTrace(); fail();