Skip to content

Commit 6945ba6

Browse files
committed
WIP fixing stdlibs and compilation on the new version
Added functional interface types (Java)
1 parent 253c367 commit 6945ba6

File tree

8 files changed

+58
-25
lines changed

8 files changed

+58
-25
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ public class DefinitionTypeID implements TypeID {
2929
public final TypeID[] typeArguments;
3030
public final DefinitionTypeID outer;
3131
private TypeID normalized;
32-
33-
public TypeID superType;
34-
32+
3533
public DefinitionTypeID(GlobalTypeRegistry typeRegistry, HighLevelDefinition definition, TypeID[] typeArguments) {
3634
this(typeRegistry, definition, typeArguments, null);
3735
}
@@ -50,7 +48,7 @@ public DefinitionTypeID(GlobalTypeRegistry typeRegistry, HighLevelDefinition def
5048
this.definition = definition;
5149
this.typeArguments = typeArguments;
5250
this.outer = outer;
53-
51+
5452
normalized = isDenormalized() ? normalize(typeRegistry) : this;
5553
if (normalized instanceof DefinitionTypeID && ((DefinitionTypeID)normalized).isDenormalized())
5654
throw new AssertionError();
@@ -63,10 +61,8 @@ private boolean isDenormalized() {
6361
for (TypeID typeArgument : typeArguments)
6462
if (!typeArgument.getNormalized().equals(typeArgument))
6563
return true;
66-
if (outer != null && !outer.getNormalized().equals(outer))
67-
return true;
68-
69-
return false;
64+
65+
return outer != null && !outer.getNormalized().equals(outer);
7066
}
7167

7268
private TypeID normalize(GlobalTypeRegistry typeRegistry) {
@@ -114,7 +110,6 @@ public Map<TypeParameter, TypeID> getTypeParameterMapping() {
114110
public DefinitionTypeID(HighLevelDefinition definition) {
115111
this.definition = definition;
116112
this.typeArguments = TypeID.NONE;
117-
this.superType = definition.getSuperType();
118113
this.outer = null;
119114
}
120115

@@ -185,7 +180,8 @@ public boolean hasInferenceBlockingTypeParameters(TypeParameter[] parameters) {
185180
if (typeArgument.hasInferenceBlockingTypeParameters(parameters))
186181
return true;
187182
}
188-
183+
184+
TypeID superType = definition.getSuperType();
189185
return superType != null && superType.hasInferenceBlockingTypeParameters(parameters);
190186
}
191187

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Set;
12+
13+
import org.openzen.zencode.shared.CodePosition;
1214
import org.openzen.zenscript.codemodel.GenericMapper;
1315
import org.openzen.zenscript.codemodel.HighLevelDefinition;
1416
import org.openzen.zenscript.codemodel.expression.Expression;
@@ -89,4 +91,12 @@ default boolean isEnum() {
8991
default boolean isDefinition(HighLevelDefinition definition) {
9092
return false;
9193
}
94+
95+
default boolean canCastImplicit(TypeID other) { return false; }
96+
97+
default boolean canCastExplicit(TypeID other) { return false; }
98+
99+
default Expression castImplicit(CodePosition position, TypeID other, Expression value) { return null; }
100+
101+
default Expression castExplicit(CodePosition position, TypeID other, Expression value) { return null; }
92102
}

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,8 @@ public Void visitDefinition(Void context, DefinitionTypeID definitionType) {
518518
cache.get(baseType.instance(mapper)).copyMembersTo(members, TypeMemberPriority.INHERITED);
519519
}
520520

521-
if (definitionType.superType != null) {
522-
cache.get(definitionType.superType).copyMembersTo(members, TypeMemberPriority.INHERITED);
523-
} else if(definition.getSuperType() != null) {
524-
cache.get(definition.getSuperType())
525-
.copyMembersTo(members, TypeMemberPriority.INHERITED);
521+
if (definition.getSuperType() != null) {
522+
cache.get(definition.getSuperType()).copyMembersTo(members, TypeMemberPriority.INHERITED);
526523
} else {
527524
getter(definition, OBJECT_HASHCODE, "objectHashCode", INT);
528525
}

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ public boolean canCastImplicit(TypeID toType) {
423423
throw new IllegalArgumentException("Cannot cast to undetermined type!");
424424
if (type == BasicTypeID.NULL && toType.isOptional())
425425
return true;
426+
if (type.canCastImplicit(toType))
427+
return true;
426428

427429
if (toType.isOptional() && canCastImplicit(toType.withoutOptional()))
428430
return true;
@@ -466,6 +468,8 @@ public boolean canCast(TypeID toType) {
466468
toType = toType.getNormalized();
467469
if (canCastImplicit(toType))
468470
return true;
471+
if (type.canCastExplicit(toType))
472+
return true;
469473

470474
for (TypeMember<CasterMemberRef> caster : casters) {
471475
if (caster.member.toType == toType)
@@ -552,6 +556,8 @@ public Expression castImplicit(CodePosition position, Expression value, TypeID t
552556
return value;
553557
if (type == toType)
554558
return value;
559+
if (type.canCastImplicit(toType))
560+
return type.castImplicit(position, toType, value);
555561

556562
if (type == BasicTypeID.NULL && toType.isOptional())
557563
return new NullExpression(position, toType);
@@ -578,6 +584,8 @@ public Expression castExplicit(CodePosition position, Expression value, TypeID t
578584
toType = toType.getNormalized();
579585
if (this.canCastImplicit(toType))
580586
return castImplicit(position, value, toType, false);
587+
if (type.canCastExplicit(toType))
588+
return type.castExplicit(position, toType, value);
581589

582590
final TypeMembers typeMembers = cache.get(type);
583591
if(this.type != typeMembers.type && typeMembers.canCast(toType)) {

JavaShared/src/main/java/org/openzen/zenscript/javashared/types/JavaFunctionalInterfaceTypeID.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.openzen.zenscript.javashared.types;
22

3+
import org.openzen.zencode.shared.CodePosition;
34
import org.openzen.zenscript.codemodel.FunctionHeader;
5+
import org.openzen.zenscript.codemodel.expression.Expression;
46
import org.openzen.zenscript.codemodel.type.FunctionTypeID;
57
import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
8+
import org.openzen.zenscript.codemodel.type.TypeID;
69
import org.openzen.zenscript.javashared.JavaMethod;
10+
import org.openzen.zenscript.javashared.expressions.JavaFunctionInterfaceCastExpression;
711

812
import java.lang.reflect.Method;
913

@@ -17,4 +21,22 @@ public JavaFunctionalInterfaceTypeID(GlobalTypeRegistry registry, FunctionHeader
1721
this.functionalInterfaceMethod = functionalInterfaceMethod;
1822
this.method = method;
1923
}
24+
25+
@Override
26+
public Expression castImplicit(CodePosition position, TypeID other, Expression value) {
27+
if (other instanceof JavaFunctionalInterfaceTypeID) {
28+
JavaFunctionalInterfaceTypeID otherType = (JavaFunctionalInterfaceTypeID)other;
29+
if (header.isEquivalentTo(otherType.header))
30+
return new JavaFunctionInterfaceCastExpression(position, otherType, value);
31+
32+
return null;
33+
} else {
34+
return null;
35+
}
36+
}
37+
38+
@Override
39+
public Expression castExplicit(CodePosition position, TypeID other, Expression value) {
40+
return this.castImplicit(position, other, value);
41+
}
2042
}

Parser/src/main/java/org/openzen/zenscript/parser/logger/ParserLogger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
public interface ParserLogger extends IZSLogger, CompileExceptionLogger {
77
default void logParseException(ParseException exception) {
8-
throwingErr("Parser Exeption", exception);
8+
throwingErr("Parser Exception @ " + exception.position.toString() + " : " + exception.message, exception);
99
}
1010
}

Parser/src/main/java/org/openzen/zenscript/parser/type/IParsedType.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
* @author Hoofdgebruiker
2626
*/
2727
public interface IParsedType {
28-
public static IParsedType parse(ZSTokenParser tokens) throws ParseException {
28+
static IParsedType parse(ZSTokenParser tokens) throws ParseException {
2929
IParsedType result = tryParse(tokens);
3030
if (result == null)
3131
throw new ParseException(tokens.getPosition(), "Type expected (got " + tokens.peek().content + ")");
3232

3333
return result;
3434
}
3535

36-
public static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseException {
36+
static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws ParseException {
3737
if (!tokens.isNext(T_LESS))
3838
return null;
3939

@@ -67,7 +67,7 @@ public static List<IParsedType> parseTypeArguments(ZSTokenParser tokens) throws
6767
return genericParameters;
6868
}
6969

70-
public static List<IParsedType> parseTypeArgumentsForCall(ZSTokenParser tokens) throws ParseException {
70+
static List<IParsedType> parseTypeArgumentsForCall(ZSTokenParser tokens) throws ParseException {
7171
List<IParsedType> typeArguments = null;
7272
if (tokens.optional(ZSTokenType.T_LESS) != null) {
7373
try {
@@ -85,7 +85,7 @@ public static List<IParsedType> parseTypeArgumentsForCall(ZSTokenParser tokens)
8585
return typeArguments;
8686
}
8787

88-
public static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
88+
static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
8989
CodePosition position = tokens.getPosition();
9090

9191
IParsedType result;
@@ -211,7 +211,7 @@ public static IParsedType tryParse(ZSTokenParser tokens) throws ParseException {
211211
return result;
212212
}
213213

214-
public static TypeID[] compileList(List<IParsedType> typeParameters, TypeResolutionContext context) {
214+
static TypeID[] compileList(List<IParsedType> typeParameters, TypeResolutionContext context) {
215215
TypeID[] result = TypeID.NONE;
216216
if (typeParameters != null) {
217217
result = new TypeID[typeParameters.size()];
@@ -222,7 +222,7 @@ public static TypeID[] compileList(List<IParsedType> typeParameters, TypeResolut
222222
return result;
223223
}
224224

225-
public static TypeID[] compileTypes(List<IParsedType> typeParameters, TypeResolutionContext context) {
225+
static TypeID[] compileTypes(List<IParsedType> typeParameters, TypeResolutionContext context) {
226226
TypeID[] result = TypeID.NONE;
227227
if (typeParameters != null && typeParameters.size() > 0) {
228228
result = new TypeID[typeParameters.size()];
@@ -233,13 +233,13 @@ public static TypeID[] compileTypes(List<IParsedType> typeParameters, TypeResolu
233233
return result;
234234
}
235235

236-
public TypeID compile(TypeResolutionContext context);
236+
TypeID compile(TypeResolutionContext context);
237237

238-
public default AnnotationDefinition compileAnnotation(BaseScope scope) {
238+
default AnnotationDefinition compileAnnotation(BaseScope scope) {
239239
return null;
240240
}
241241

242-
public default TypeID[] compileTypeArguments(BaseScope scope) {
242+
default TypeID[] compileTypeArguments(BaseScope scope) {
243243
return TypeID.NONE;
244244
}
245245
}
-1.77 KB
Binary file not shown.

0 commit comments

Comments
 (0)