Skip to content

Commit f7a20a4

Browse files
authored
Merge pull request #184 from ZenCodeLang/fix/general-issues
Fix/general issues
2 parents 3e18e7a + 2382bab commit f7a20a4

File tree

9 files changed

+88
-61
lines changed

9 files changed

+88
-61
lines changed

CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/MatchedCallArguments.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private static <T extends AnyMethod> MatchedCallArguments<T> match(
168168
if (!matchedVarArg.isPresent()) {
169169
return matchedWidening;
170170
} else {
171-
return Stream.of(matchedVarArg.get(), matchedWidening).min(Comparator.comparing(match -> match.arguments.level)).orElseThrow(() -> new IllegalStateException("Should never happen"));
171+
return Stream.of(matchedWidening, matchedVarArg.get()).min(Comparator.comparing(match -> match.arguments.level)).orElseThrow(() -> new IllegalStateException("Should never happen"));
172172
}
173173
}
174174

CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/expression/StaticMemberCompilingExpression.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,24 @@ public Expression eval() {
3131

3232
ResolvedType resolved = compiler.resolve(type);
3333

34-
/*Optional<Expression> asGetter = resolved.findStaticGetter(name.name)
35-
.map(getter -> getter.call(compiler, position, TypeID.NONE));
36-
if (asGetter.isPresent()) {
37-
return asGetter.get();
38-
}*/
39-
40-
Optional<StaticCallable> staticGetter = resolved.findStaticGetter(name.name);
41-
if(staticGetter.isPresent()){
42-
return staticGetter.get().call(compiler, position, TypeID.NONE);
34+
Optional<Expression> byGetter = resolved
35+
.findStaticGetter(name.name)
36+
.map(getter -> getter.call(compiler, position, TypeID.NONE));
37+
38+
if (byGetter.isPresent()) {
39+
return byGetter.get();
4340
}
41+
4442
Optional<CompilableExpression> contextMember = resolved.getContextMember(name.name);
45-
if(contextMember.isPresent()){
43+
if (contextMember.isPresent()) {
4644
return contextMember.get().compile(compiler).eval();
4745
}
48-
return compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name));
49-
50-
// return resolved.findStaticGetter(name.name)
51-
// .map(getter -> getter.call(compiler, position, TypeID.NONE))
52-
// .orElseGet(() ->
53-
// resolved.getContextMember(name.name)
54-
// .map(member -> member.compile(compiler).eval())
55-
// .orElseGet(() -> compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name))));
46+
47+
return resolved
48+
.findField(name.name)
49+
.filter(ResolvedType.Field::isStatic)
50+
.map(field -> field.getStatic(compiler.at(position)))
51+
.orElseGet(() -> compiler.at(position).invalid(CompileErrors.noMemberInType(type, name.name)));
5652
}
5753

5854
@Override

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/DefinitionTypeID.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public boolean equals(Object obj) {
158158
final DefinitionTypeID other = (DefinitionTypeID) obj;
159159
return this.definition == other.definition
160160
&& Arrays.deepEquals(this.typeArguments, other.typeArguments)
161-
&& Objects.equals(outer, this.outer);
161+
&& Objects.equals(outer, other.outer);
162162
}
163163

164164
@Override

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/OptionalTypeID.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean equals(Object obj) {
8989
if (getClass() != obj.getClass())
9090
return false;
9191
final OptionalTypeID other = (OptionalTypeID) obj;
92-
return this.baseType == other.baseType;
92+
return this.baseType.equals(other.baseType);
9393
}
9494

9595
@Override

JavaIntegration/src/main/java/org/openzen/zencode/java/ScriptingEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public ScriptingEngine(ScriptingEngineLogger logger) {
6363
}
6464

6565
public ScriptingEngine(ScriptingEngineLogger logger, Function<String, InputStream> resourceGetter) {
66-
this(logger, resourceGetter, "stdlib", "math", "collections", "uuid");
66+
this(logger, resourceGetter, "stdlib", "math", "collections", "uuid", "javalib");
6767
}
6868

6969
public ScriptingEngine(ScriptingEngineLogger logger, Function<String, InputStream> resourceGetter, String... stdLibModulesToRegister) {

JavaIntegration/src/main/java/org/openzen/zencode/java/impl/conversion/JavaRuntimeTypeConverterImpl.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
import org.openzen.zencode.java.TypeVariableContext;
88
import org.openzen.zencode.java.module.JavaAnnotatedType;
99
import org.openzen.zencode.java.module.JavaNativePackageInfo;
10-
import org.openzen.zencode.java.module.JavaRuntimeClass;
1110
import org.openzen.zencode.shared.CodePosition;
1211
import org.openzen.zencode.shared.LiteralSourceFile;
1312
import org.openzen.zenscript.codemodel.FunctionHeader;
1413
import org.openzen.zenscript.codemodel.GenericMapper;
1514
import org.openzen.zenscript.codemodel.Modifiers;
16-
import org.openzen.zenscript.codemodel.ModuleSpace;
1715
import org.openzen.zenscript.codemodel.compilation.CompileContext;
1816
import org.openzen.zenscript.codemodel.definition.ClassDefinition;
1917
import org.openzen.zenscript.codemodel.definition.ZSPackage;
@@ -68,15 +66,10 @@ public TypeID getType(TypeVariableContext context, AnnotatedType type) {
6866
} else if (type.isAnnotationPresent(ZenCodeType.USize.class)) {
6967
result = BasicTypeID.USIZE;
7068
} else {
71-
boolean unsigned = type.isAnnotationPresent(ZenCodeType.Unsigned.class);
72-
result = loadType(context, JavaAnnotatedType.of(type), unsigned);
69+
result = loadType(context, JavaAnnotatedType.of(type));
7370
}
7471

75-
boolean isOptional = type.isAnnotationPresent(ZenCodeType.Nullable.class);
76-
if (isOptional && !result.isOptional())
77-
result = new OptionalTypeID(result);
78-
79-
return result;
72+
return optionallyWrap(type, result);
8073
}
8174

8275
@Override
@@ -106,19 +99,19 @@ public TypeID parseType(String type) {
10699
}
107100
}
108101

109-
private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boolean unsigned) {
102+
private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type) {
110103
final JavaAnnotatedType.ElementType elementType = type.getElementType();
111104

112105
try {
113106
switch (elementType) {
114107
case ANNOTATED_PARAMETERIZED_TYPE:
115-
return loadAnnotatedParameterizedType(context, (AnnotatedParameterizedType) type.getAnnotatedElement(), unsigned);
108+
return loadAnnotatedParameterizedType(context, (AnnotatedParameterizedType) type.getAnnotatedElement());
116109
case ANNOTATED_TYPE:
117-
return loadAnnotatedType(context, (AnnotatedType) type.getAnnotatedElement(), unsigned);
110+
return loadAnnotatedType(context, (AnnotatedType) type.getAnnotatedElement());
118111
case CLASS:
119-
return loadClass(context, (Class<?>) type.getType(), unsigned);
112+
return loadClass(context, (Class<?>) type.getType());
120113
case GENERIC_ARRAY:
121-
return loadGenericArray(context, (GenericArrayType) type.getType(), unsigned);
114+
return loadGenericArray(context, (GenericArrayType) type.getType());
122115
case PARAMETERIZED_TYPE:
123116
return loadParameterizedType(context, (ParameterizedType) type.getType());
124117
case TYPE_VARIABLE:
@@ -133,56 +126,52 @@ private TypeID loadType(TypeVariableContext context, JavaAnnotatedType type, boo
133126
throw new IllegalArgumentException("Invalid type " + elementType + ": not yet implemented or foolery");
134127
}
135128

136-
private TypeSymbol loadRawType(TypeVariableContext context, JavaAnnotatedType type, boolean unsigned) {
129+
private TypeSymbol loadRawType(JavaAnnotatedType type) {
137130
final JavaAnnotatedType.ElementType elementType = type.getElementType();
138131

139132
try {
140-
switch (elementType) {
141-
case CLASS:
142-
return findType((Class<?>) type.getType());
143-
default:
144-
throw new IllegalArgumentException("Didn't expect to get this kind of type: " + type);
133+
if (Objects.requireNonNull(elementType) == JavaAnnotatedType.ElementType.CLASS) {
134+
return findType((Class<?>) type.getType());
145135
}
136+
throw new IllegalArgumentException("Didn't expect to get this kind of type: " + type);
146137
} catch (final IllegalArgumentException e) {
147138
throw new IllegalArgumentException("Unable to analyze type: " + type, e);
148139
}
149140
}
150141

151-
private TypeID loadAnnotatedParameterizedType(TypeVariableContext context, AnnotatedParameterizedType type, boolean unsigned) {
142+
private TypeID loadAnnotatedParameterizedType(TypeVariableContext context, AnnotatedParameterizedType type) {
152143
final ParameterizedType parameterizedType = this.getTypeIfValid(JavaAnnotatedType.of(type.getType()), JavaAnnotatedType.ElementType.PARAMETERIZED_TYPE);
153144
final JavaAnnotatedType rawType = JavaAnnotatedType.of(parameterizedType.getRawType());
154145

155146
final JavaAnnotatedType[] actualTypeArguments = JavaAnnotatedType.arrayOf(type.getAnnotatedActualTypeArguments());
156147
final TypeID[] codeParameters = new TypeID[actualTypeArguments.length];
157148

158149
for (int i = 0; i < actualTypeArguments.length; i++) {
159-
codeParameters[i] = this.loadType(context, actualTypeArguments[i], false);
150+
codeParameters[i] = this.loadType(context, actualTypeArguments[i]);
160151
}
161152

162153
if (rawType.getElementType() == JavaAnnotatedType.ElementType.CLASS) {
154+
//noinspection SuspiciousMethodCalls
163155
if (specialTypes.containsKey(rawType.getType())) {
156+
//noinspection SuspiciousMethodCalls
164157
return specialTypes.get(rawType.getType()).apply(codeParameters);
165158
}
166159

167-
final TypeSymbol rawTypeSymbol = loadRawType(context, rawType, unsigned);
160+
final TypeSymbol rawTypeSymbol = loadRawType(rawType);
168161
return DefinitionTypeID.create(rawTypeSymbol, codeParameters);
169162
}
170-
return this.loadType(context, JavaAnnotatedType.of(type), unsigned);
163+
return this.loadType(context, JavaAnnotatedType.of(type));
171164
}
172165

173-
private TypeID loadAnnotatedType(TypeVariableContext context, AnnotatedType type, boolean unsigned) {
166+
private TypeID loadAnnotatedType(TypeVariableContext context, AnnotatedType type) {
174167
final JavaAnnotatedType annotatedType = JavaAnnotatedType.of(type.getType());
175-
return this.loadType(context, annotatedType, unsigned);
168+
TypeID result = this.loadType(context, annotatedType);
169+
return optionallyWrap(type, result);
176170
}
177171

178-
private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean unsigned) {
179-
if (unsigned) {
180-
return unsignedByClass.computeIfAbsent(type, it -> {
181-
throw new IllegalArgumentException("This class cannot be used as unsigned: " + it);
182-
});
183-
}
172+
private TypeID loadClass(TypeVariableContext context, Class<?> type) {
184173
if (type.isArray()) {
185-
final TypeID baseType = this.loadType(context, JavaAnnotatedType.of(type.getComponentType()), false);
174+
final TypeID baseType = this.loadType(context, JavaAnnotatedType.of(type.getComponentType()));
186175
return new ArrayTypeID(baseType);
187176
}
188177
if (type.isAnnotationPresent(FunctionalInterface.class)) {
@@ -205,9 +194,9 @@ private TypeID loadClass(TypeVariableContext context, Class<?> type, boolean uns
205194
return DefinitionTypeID.create(definition, typeParameters.toArray(TypeID.NONE));
206195
}
207196

208-
private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type, boolean unsigned) {
197+
private TypeID loadGenericArray(TypeVariableContext context, GenericArrayType type) {
209198
final JavaAnnotatedType componentType = JavaAnnotatedType.of(type.getGenericComponentType());
210-
final TypeID baseType = this.loadType(context, componentType, unsigned);
199+
final TypeID baseType = this.loadType(context, componentType);
211200
return new ArrayTypeID(baseType);
212201
}
213202

@@ -221,7 +210,7 @@ private TypeID loadParameterizedType(TypeVariableContext context, ParameterizedT
221210

222211
TypeID[] codeParameters = new TypeID[typeArguments.length];
223212
for (int i = 0; i < typeArguments.length; i++) {
224-
codeParameters[i] = this.loadType(context, typeArguments[i], false);
213+
codeParameters[i] = this.loadType(context, typeArguments[i]);
225214
}
226215

227216
if (rawType == Map.class) {
@@ -342,7 +331,7 @@ private TypeID loadFunctionalInterface(TypeVariableContext loadContext, Class<?>
342331
Map<TypeParameter, TypeID> mapping = new HashMap<>();
343332
TypeVariable<?>[] javaParameters = cls.getTypeParameters();
344333
for (int i = 0; i < parameters.length; i++) {
345-
mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i], false));
334+
mapping.put(context.get(javaParameters[i]), loadType(loadContext, parameters[i]));
346335
}
347336

348337
header = header.withGenericArguments(new GenericMapper(mapping, TypeID.NONE));
@@ -469,4 +458,11 @@ private void fillSpecialTypes() {
469458
specialTypes.put(ToLongFunction.class, args -> new FunctionTypeID(new FunctionHeader(BasicTypeID.LONG, args[0])));
470459
specialTypes.put(UnaryOperator.class, args -> new FunctionTypeID(new FunctionHeader(args[0], args[0])));
471460
}
461+
462+
private TypeID optionallyWrap(AnnotatedType type, TypeID result) {
463+
boolean isOptional = type.isAnnotationPresent(ZenCodeType.Nullable.class);
464+
if (isOptional && !result.isOptional())
465+
result = new OptionalTypeID(result);
466+
return result;
467+
}
472468
}

JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeTemplate.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.openzen.zenscript.codemodel.ssa.CodeBlockStatement;
2020
import org.openzen.zenscript.codemodel.ssa.SSAVariableCollector;
2121
import org.openzen.zenscript.codemodel.type.ArrayTypeID;
22+
import org.openzen.zenscript.codemodel.type.BasicTypeID;
2223
import org.openzen.zenscript.codemodel.type.TypeID;
2324
import org.openzen.zenscript.codemodel.type.builtin.BuiltinMethodSymbol;
2425
import org.openzen.zenscript.javashared.JavaClass;
@@ -213,9 +214,9 @@ private void loadMethods() {
213214
if (class_.cls.isEnum()) {
214215
Stream.of(
215216
BuiltinMethodSymbol.ENUM_NAME,
216-
BuiltinMethodSymbol.ENUM_ORDINAL,
217+
BuiltinMethodSymbol.ENUM_ORDINAL
217218
//BuiltinMethodSymbol.ENUM_VALUES,
218-
BuiltinMethodSymbol.ENUM_COMPARE
219+
// BuiltinMethodSymbol.ENUM_COMPARE
219220
).forEach(method -> methods
220221
.computeIfAbsent(method.getID(), x -> new ArrayList<>())
221222
.add(method)
@@ -232,6 +233,18 @@ private void loadMethods() {
232233
throw new IllegalStateException("We found an enum class without values() method: " + class_.cls.getCanonicalName(), exception);
233234
}
234235

236+
try {
237+
MethodID id = MethodID.operator(OperatorType.COMPARE);
238+
FunctionHeader header = new FunctionHeader(BasicTypeID.INT, target);
239+
Method method = class_.cls.getMethod("compareTo", Enum.class);
240+
JavaRuntimeMethod runtimeMethod = new JavaRuntimeMethod(class_, target, method, id, header, false, false);
241+
methods.computeIfAbsent(id, x -> new ArrayList<>()).add(runtimeMethod);
242+
class_.module.getCompiled().setMethodInfo(runtimeMethod, runtimeMethod);
243+
} catch (ReflectiveOperationException exception) {
244+
throw new IllegalStateException("Error while registering Enum#compareTo for: " + class_.cls.getCanonicalName(), exception);
245+
}
246+
247+
235248
}
236249
}
237250

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#output: ========
2+
#output: default value here
3+
#output: ========
4+
#debug
5+
function test(args...: string[] = ["default value here"]): void {
6+
for item in args println(item);
7+
}
8+
9+
println("========");
10+
test();
11+
println("========");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#output: ========
2+
#output: provided value
3+
#output: ========
4+
#debug
5+
function test(args...: string[] = ["default value here"]): void {
6+
for item in args println(item);
7+
}
8+
9+
println("========");
10+
test("provided value");
11+
println("========");

0 commit comments

Comments
 (0)