Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20b974fd2a | ||
|
|
a504743a40 | ||
|
|
70764640d6 | ||
|
|
090263b6ca |
@@ -7,7 +7,6 @@ import java.lang.Integer;
|
||||
import java.lang.String;
|
||||
import java.lang.System;
|
||||
import java.io.PrintStream;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public sealed interface LazyList permits Empty, Cons {
|
||||
public Integer fst();
|
||||
@@ -15,11 +14,11 @@ public sealed interface LazyList permits Empty, Cons {
|
||||
}
|
||||
|
||||
//Der Konstruktor Cons muss lazy sein, deshalb hier Supplier<...>
|
||||
record Cons(Integer x, Supplier<LazyList> l) implements LazyList {
|
||||
record Cons(Integer x, Fun0$$<LazyList> l) implements LazyList {
|
||||
public Integer fst() { return this.x; }
|
||||
public LazyList rest() { return this.l.get(); }
|
||||
public LazyList rest() { return this.l.apply(); }
|
||||
public String toString() {
|
||||
return "Cons(" + this.x.toString() + ", " + this.l.get().toString() + ")";
|
||||
return "Cons(" + this.x.toString() + ", " + this.l.apply().toString() + ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,23 +3,29 @@ import java.lang.String;
|
||||
import java.lang.System;
|
||||
import java.lang.Boolean;
|
||||
import java.io.PrintStream;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.Function;
|
||||
|
||||
import LazyList;
|
||||
import Cons;
|
||||
import Empty;
|
||||
|
||||
public class Primzahlen {
|
||||
static LazyList from(int i) { return new Cons(i, () -> from(i+1)); }
|
||||
static LazyList from(Integer i) { return new Cons(i, () -> from(i+1)); }
|
||||
|
||||
LazyList filter(Function<Integer, Boolean> p, LazyList l) {
|
||||
return switch (l) {
|
||||
case Empty() -> l;
|
||||
case Cons(Integer x, Supplier<LazyList> l1) ->
|
||||
p.apply(x) ? new Cons(x, () -> filter(p, l1.get())) : filter(p, l1.get());
|
||||
LazyList filter(Fun1$$<Integer, Boolean> p, LazyList l) {
|
||||
return switch(l) {
|
||||
case Empty e -> e;
|
||||
case Cons(Integer x, Fun0$$<LazyList> l1) ->
|
||||
p.apply(x) ? new Cons(x, () -> filter(p, l1.apply())) : filter(p, l1.apply());
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
/*LazyList filter(Fun1$$<Integer, Boolean> p, Empty()@l) {
|
||||
return l;
|
||||
}
|
||||
|
||||
LazyList filter(Fun1$$<Integer, Boolean> p, Cons(Integer x, Fun0$$<LazyList> l1)) {
|
||||
return p.apply(x) ? new Cons(x, () -> filter(p, l1.apply())) : filter(p, l1.apply());
|
||||
}*/
|
||||
|
||||
LazyList dropMul(Integer x, LazyList xs) {
|
||||
return filter(y -> (y % x != 0), xs);
|
||||
@@ -37,8 +43,8 @@ public class Primzahlen {
|
||||
if (n == 0) return new Empty();
|
||||
else return switch (l) {
|
||||
case Empty() -> l;
|
||||
case Cons(Integer x, Supplier<LazyList> l1) ->
|
||||
new Cons(x, () -> take(n-1, l1.get()));
|
||||
case Cons(Integer x, Fun0$$<LazyList> l1) ->
|
||||
new Cons(x, () -> take(n-1, l1.apply()));
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import java.lang.Integer;
|
||||
|
||||
public record RecordTestInstanceVariables(Integer a, Integer b) {
|
||||
public Integer getA() { return a; }
|
||||
public Integer getB() { return b; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
public class StaticFail {
|
||||
void nonStaticM() {}
|
||||
|
||||
static void staticM() {
|
||||
nonStaticM();
|
||||
}
|
||||
}
|
||||
@@ -1189,6 +1189,12 @@ public class Codegen {
|
||||
}
|
||||
case TargetMethodCall call: {
|
||||
if (!call.isStatic()) {
|
||||
if (call.expr() instanceof TargetThis && state.isStatic) {
|
||||
throw new CodeGenException(
|
||||
"Attempted to call instance method " + call.name() +
|
||||
" with descriptor " + call.getDescriptor() + " from a static context"
|
||||
);
|
||||
}
|
||||
generate(state, call.expr());
|
||||
boxPrimitive(state, call.expr().type());
|
||||
}
|
||||
|
||||
@@ -714,6 +714,9 @@ public class JavaTXCompiler {
|
||||
new SuperWildcardType(toRefType(targetSuperWildcard.innerType()), new NullToken());
|
||||
case TargetGenericType targetGenericType -> new GenericRefType(targetGenericType.name(), new NullToken());
|
||||
case TargetPrimitiveType targetPrimitiveType -> toRefType(TargetType.toWrapper(targetPrimitiveType));
|
||||
case TargetFunNType targetFunNType ->
|
||||
new RefType(new JavaClassName(FunNGenerator.getSuperClassName(targetFunNType.funNParams().size() - 1, targetFunNType.returnArguments())),
|
||||
targetFunNType.funNParams().stream().map(JavaTXCompiler::toRefType).toList(), new NullToken());
|
||||
case TargetSpecializedType targetSpecializedType ->
|
||||
new RefType(new JavaClassName(targetSpecializedType.name()),
|
||||
targetSpecializedType.params().stream().map(JavaTXCompiler::toRefType).toList(), new NullToken()
|
||||
|
||||
+20
-16
@@ -80,7 +80,7 @@ public class SyntaxTreeGenerator {
|
||||
HashMap<String, Integer> allmodifiers = new HashMap<>();
|
||||
// PL 2018-11-01 fields eingefuegt, damit die fields immer die gleiche TPH
|
||||
// bekommen
|
||||
private final Map<String, FieldEntry> fields = new HashMap<>();
|
||||
//private final Map<String, FieldEntry> fields = new HashMap<>();
|
||||
// PL 2019-10-23: Muss für jede Klasse neu initilisiert werden
|
||||
List<Statement> fieldInitializations = new ArrayList<>();
|
||||
List<Statement> staticFieldInitializations = new ArrayList<>();
|
||||
@@ -178,6 +178,7 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
|
||||
private ClassOrInterface convertClass(Java17Parser.ClassDeclarationContext ctx, int modifiers) {
|
||||
var fieldDecls = new HashMap<String, FieldEntry>();
|
||||
String className = this.pkgName + (this.pkgName.length() > 0 ? "." : "") + 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?
|
||||
@@ -206,7 +207,7 @@ public class SyntaxTreeGenerator {
|
||||
List<RefType> implementedInterfaces = new ArrayList<>();
|
||||
List<RefType> permittedSubtypes = null;
|
||||
for (ClassBodyDeclarationContext clsbodydecl : ctx.classBody().classBodyDeclaration()) {
|
||||
convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics);
|
||||
convert(clsbodydecl, fielddecl, constructors, methods, name, superClass, generics, fieldDecls);
|
||||
}
|
||||
if (constructors.isEmpty()) {
|
||||
constructors.add(generateStandardConstructor(ctx.identifier().getText(), name, superClass, genericClassParameters, offset));
|
||||
@@ -255,6 +256,8 @@ public class SyntaxTreeGenerator {
|
||||
List<Pattern> constructorParameters = new ArrayList<>();
|
||||
List<Statement> constructorStatements = new ArrayList<>();
|
||||
|
||||
var fieldDecl = new HashMap<String, FieldEntry>();
|
||||
|
||||
List<Java17Parser.RecordComponentContext> components = recordDeclaration.recordHeader().recordComponentList() != null ?
|
||||
recordDeclaration.recordHeader().recordComponentList().recordComponent(): List.of();
|
||||
for (RecordComponentContext component : components) {
|
||||
@@ -270,6 +273,7 @@ public class SyntaxTreeGenerator {
|
||||
fielddecl.add(new Field(fieldname, fieldtype, fieldmodifiers, fieldoffset));
|
||||
constructorParameters.add(new FormalParameter(fieldname, fieldtype, fieldoffset));
|
||||
FieldVar fieldvar = new FieldVar(new This(offset), fieldname, fieldtype, fieldoffset);
|
||||
fieldDecl.put(fieldname, new FieldEntry(fieldname, fieldtype, Modifier.PRIVATE));
|
||||
constructorStatements.add(new Assign(new AssignToField(fieldvar), new LocalVar(fieldname, fieldtype, fieldoffset), offset));
|
||||
Statement returnStatement = new Return(fieldvar, offset);
|
||||
methods.add(new Method(allmodifiers.get("public"), fieldname, fieldtype, new ParameterList(new ArrayList<>(), offset), new Block(Arrays.asList(returnStatement), offset), new GenericDeclarationList(new ArrayList<>(), offset), offset));
|
||||
@@ -279,7 +283,7 @@ public class SyntaxTreeGenerator {
|
||||
//Optional<Constructor> initializations = Optional.of(implicitConstructor);
|
||||
constructors.add(implicitConstructor);
|
||||
for (ClassBodyDeclarationContext bodyDeclaration : recordDeclaration.recordBody().classBodyDeclaration()) {
|
||||
convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics);
|
||||
convert(bodyDeclaration, fielddecl, constructors, methods, name, superClass, generics, fieldDecl);
|
||||
}
|
||||
if (!Objects.isNull(recordDeclaration.IMPLEMENTS())) {
|
||||
implementedInterfaces.addAll(convert(recordDeclaration.typeList(), generics));
|
||||
@@ -288,7 +292,7 @@ public class SyntaxTreeGenerator {
|
||||
return new Record(modifiers, name, fielddecl, Optional.empty(), staticCtor, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset, fileName);
|
||||
}
|
||||
|
||||
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics) {
|
||||
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics, HashMap<String, FieldEntry> fieldDecls) {
|
||||
MemberdeclContext member;
|
||||
if (classBody instanceof MemberdeclContext) {
|
||||
member = (MemberdeclContext) classBody;
|
||||
@@ -303,11 +307,11 @@ public class SyntaxTreeGenerator {
|
||||
break;
|
||||
}
|
||||
case MemberfieldContext memberfield: {
|
||||
fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics));
|
||||
fields.addAll(convert(memberfield.fieldDeclaration(), membermodifiers, generics, fieldDecls));
|
||||
break;
|
||||
}
|
||||
case MembermethodContext membermethod: {
|
||||
Method convertedMethod = convert(membermodifiers, membermethod.method(), name, superClass, generics);
|
||||
Method convertedMethod = convert(membermodifiers, membermethod.method(), name, superClass, generics, fieldDecls);
|
||||
if (convertedMethod instanceof Constructor constructor) {
|
||||
constructors.add(constructor);
|
||||
} else {
|
||||
@@ -316,7 +320,7 @@ public class SyntaxTreeGenerator {
|
||||
break;
|
||||
}
|
||||
case MemberconstructorContext memberconstructor: {
|
||||
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics));
|
||||
constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics, fieldDecls));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -324,7 +328,7 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
} else if (classBody instanceof Java17Parser.ClassblockContext ctx && ctx.STATIC() != null) {
|
||||
// Static blocks
|
||||
var stmtgen = new StatementGenerator(superClass, compiler, reg, generics, this.fields, new HashMap<>());
|
||||
var stmtgen = new StatementGenerator(superClass, compiler, reg, generics, fieldDecls, new HashMap<>());
|
||||
var block = stmtgen.convert(((Java17Parser.ClassblockContext) classBody).block(), false);
|
||||
staticFieldInitializations.addAll(block.statements);
|
||||
}
|
||||
@@ -434,7 +438,7 @@ public class SyntaxTreeGenerator {
|
||||
retType = new Void(bodydeclaration.refType().getStart());
|
||||
}
|
||||
}
|
||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, generics, fields, new HashMap<>());
|
||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, generics, new HashMap<>(), new HashMap<>());
|
||||
ParameterList paramlist = stmtgen.convert(bodydeclaration.formalParameters().formalParameterList(), true);
|
||||
MethodBodyContext body = bodydeclaration.methodBody();
|
||||
Block block = null;
|
||||
@@ -507,7 +511,7 @@ public class SyntaxTreeGenerator {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Method convert(int modifiers, Java17Parser.MethodContext methodContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
|
||||
public Method convert(int modifiers, Java17Parser.MethodContext methodContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics, Map<String, FieldEntry> fieldDecls) {
|
||||
GenericsRegistry localgenerics = generics;
|
||||
MethodDeclarationContext methoddeclaration;
|
||||
GenericDeclarationListContext genericdeclarations;
|
||||
@@ -540,7 +544,7 @@ public class SyntaxTreeGenerator {
|
||||
retType = new Void(header.refType().getStart());
|
||||
}
|
||||
}
|
||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, localgenerics, fields, new HashMap<>());
|
||||
StatementGenerator stmtgen = new StatementGenerator(superClass, compiler, reg, localgenerics, fieldDecls, new HashMap<>());
|
||||
ParameterList paramlist = stmtgen.convert(header.formalParameters().formalParameterList(), true);
|
||||
MethodBodyContext body = methoddeclaration.methodBody();
|
||||
Block block = null;
|
||||
@@ -559,7 +563,7 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public Constructor convert(int modifiers, Java17Parser.ConstructorContext constructorContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
|
||||
public Constructor convert(int modifiers, Java17Parser.ConstructorContext constructorContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics, Map<String, FieldEntry> fields) {
|
||||
GenericsRegistry localgenerics = generics;
|
||||
GenericDeclarationListContext genericdeclarations;
|
||||
GenericDeclarationList gtvDeclarations;
|
||||
@@ -586,7 +590,7 @@ public class SyntaxTreeGenerator {
|
||||
return new Constructor(modifiers, name, retType, paramlist, block, gtvDeclarations, constructordeclaration.getStart());
|
||||
}
|
||||
|
||||
List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers, GenericsRegistry generics) {
|
||||
List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers, GenericsRegistry generics, HashMap<String, FieldEntry> fields) {
|
||||
List<Field> ret = new ArrayList<>();
|
||||
RefTypeOrTPHOrWildcardOrGeneric fieldType;
|
||||
if (fieldDeclContext.typeType() != null) {
|
||||
@@ -598,9 +602,9 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
for (Java17Parser.VariableDeclaratorContext varDecl : fieldDeclContext.variableDeclarators().variableDeclarator()) {
|
||||
String fieldName = varDecl.variableDeclaratorId().getText();
|
||||
this.fields.put(fieldName, new FieldEntry(fieldName, fieldType, modifiers));
|
||||
fields.put(fieldName, new FieldEntry(fieldName, fieldType, modifiers));
|
||||
if (varDecl.variableInitializer() != null) {
|
||||
initializeField(varDecl, Modifier.isStatic(modifiers), fieldType, generics);
|
||||
initializeField(varDecl, Modifier.isStatic(modifiers), fieldType, generics, fields);
|
||||
}
|
||||
ret.add(new Field(fieldName, fieldType, modifiers, varDecl.getStart()));
|
||||
}
|
||||
@@ -612,7 +616,7 @@ public class SyntaxTreeGenerator {
|
||||
}
|
||||
|
||||
// Initialize a field by creating implicit constructor.
|
||||
private void initializeField(Java17Parser.VariableDeclaratorContext ctx, boolean isStatic, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics) {
|
||||
private void initializeField(Java17Parser.VariableDeclaratorContext ctx, boolean isStatic, RefTypeOrTPHOrWildcardOrGeneric typeOfField, GenericsRegistry generics, Map<String, FieldEntry> fields) {
|
||||
StatementGenerator statementGenerator = new StatementGenerator(superClass, compiler, reg, generics, fields, new HashMap<>());
|
||||
var assignment = statementGenerator.generateFieldAssignment(ctx, typeOfField);
|
||||
if (isStatic) {
|
||||
|
||||
@@ -80,8 +80,8 @@ public class ASTToTargetAST {
|
||||
this(new JavaGenerics(compiler, set), new TxGenerics(compiler, set));
|
||||
}
|
||||
|
||||
public static Generics nullGenerics() {
|
||||
return new Generics(null, new ResultSet(Set.of()));
|
||||
public static Generics nullGenerics(JavaTXCompiler compiler) {
|
||||
return new Generics(compiler, new ResultSet(Set.of()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ public class ASTToTargetAST {
|
||||
}
|
||||
|
||||
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, JavaTXCompiler compiler) {
|
||||
return findMethod(owner, name, argumentList, Generics.nullGenerics().javaGenerics(), compiler);
|
||||
return findMethod(owner, name, argumentList, Generics.nullGenerics(compiler).javaGenerics(), compiler);
|
||||
}
|
||||
|
||||
public static Optional<Method> findMethod(ClassOrInterface owner, String name, List<TargetType> argumentList, IGenerics generics, JavaTXCompiler compiler) {
|
||||
@@ -483,7 +483,7 @@ public class ASTToTargetAST {
|
||||
var res = new ArrayList<MethodParameter>();
|
||||
for (var i = 0; i < input.getFormalparalist().size(); i++) {
|
||||
var param = input.getFormalparalist().get(i);
|
||||
var pattern = (TargetPattern) convert(param, Generics.nullGenerics().javaGenerics);
|
||||
var pattern = (TargetPattern) convert(param, Generics.nullGenerics(compiler).javaGenerics);
|
||||
if (pattern instanceof TargetComplexPattern) pattern = pattern.withName("__var" + i);
|
||||
res.add(new MethodParameter(pattern));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
|
||||
import de.dhbwstuttgart.bytecode.CodeGenException;
|
||||
import de.dhbwstuttgart.core.ConsoleInterface;
|
||||
import de.dhbwstuttgart.util.Logger;
|
||||
import de.dhbwstuttgart.util.Logger.LogLevel;
|
||||
@@ -674,6 +675,15 @@ public class TestComplete {
|
||||
System.out.println(clazz.getDeclaredMethod("toString").invoke(instance));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordTestInstanceVariables() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "RecordTestInstanceVariables.jav");
|
||||
var clazz = classFiles.get("RecordTestInstanceVariables");
|
||||
var instance = clazz.getDeclaredConstructor(Integer.class, Integer.class).newInstance(10, 20);
|
||||
assertEquals(10, clazz.getDeclaredMethod("getA").invoke(instance));
|
||||
assertEquals(20, clazz.getDeclaredMethod("getB").invoke(instance));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void genericRecordTest() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "GenericRecord.jav");
|
||||
@@ -1084,6 +1094,18 @@ public class TestComplete {
|
||||
assertEquals(50, m.invoke(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStaticFail() throws Exception {
|
||||
try {
|
||||
generateClassFiles(createClassLoader(), "StaticFail.jav");
|
||||
fail("No exception thrown!");
|
||||
} catch (CodeGenException e) {
|
||||
assertEquals("Attempted to call instance method nonStaticM with descriptor ()V from a static context", e.getMessage());
|
||||
} catch (RuntimeException e) {
|
||||
fail("Wrong exception thrown!");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFor() throws Exception {
|
||||
var classFiles = generateClassFiles(createClassLoader(), "For.jav");
|
||||
|
||||
Reference in New Issue
Block a user