Pārlūkot izejas kodu

- Improve work on the serializer and fixed some code issues that came up with them.

- Start work on the deserializer
Stan Hebben 6 gadus atpakaļ
vecāks
revīzija
ec46d47c02
77 mainītis faili ar 2134 papildinājumiem un 268 dzēšanām
  1. 2
    2
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingSubBlock.java
  2. 14
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java
  3. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionForMethod.java
  4. 35
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/ModuleContext.java
  5. 65
    12
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/StatementContext.java
  6. 39
    9
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/TypeContext.java
  7. 12
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java
  8. 9
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CompareExpression.java
  9. 1
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantLongExpression.java
  10. 3
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MatchExpression.java
  11. 7
    9
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstMember.java
  12. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstructorMember.java
  13. 8
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FieldMember.java
  14. 7
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java
  15. 3
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java
  16. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java
  17. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/InnerDefinitionMember.java
  18. 7
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/SetterMember.java
  19. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/StaticInitializerMember.java
  20. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/CasterMemberRef.java
  21. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ConstMemberRef.java
  22. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/DefinitionMemberRef.java
  23. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FieldMemberRef.java
  24. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java
  25. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/GetterMemberRef.java
  26. 3
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ImplementationMemberRef.java
  27. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/IteratorMemberRef.java
  28. 8
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java
  29. 3
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/SetterMemberRef.java
  30. 0
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/scope/BaseScope.java
  31. 20
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/CodeSerializationInput.java
  32. 2
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/CodeSerializationOutput.java
  33. 14
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/DecodingOperation.java
  34. 15
    15
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/BlockStatement.java
  35. 11
    10
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/SwitchCase.java
  36. 6
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/BuiltinID.java
  37. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/DefinitionMemberGroup.java
  38. 29
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java
  39. 5
    0
      CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java
  40. 2
    4
      Constructor/src/main/java/org/openzen/zenscript/constructor/Project.java
  41. 20
    0
      IDE/build.gradle
  42. 3
    0
      IDE/src/main/java/org/openzen/zenscript/ide/Arguments.java
  43. 1
    2
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/BaseMemberCompiler.java
  44. 2
    2
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java
  45. 1
    1
      JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java
  46. 20
    0
      ModuleDeserializer/build.gradle
  47. 969
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/CodeReader.java
  48. 33
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DecodingStage.java
  49. 69
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DefinitionMemberDeserializer.java
  50. 16
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DeserializationException.java
  51. 99
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DeserializingModule.java
  52. 38
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/Main.java
  53. 203
    0
      ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/ModuleDeserializer.java
  54. 2
    0
      ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/ExpressionEncoding.java
  55. 4
    3
      ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/FunctionHeaderEncoding.java
  56. 2
    0
      ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/StatementEncoding.java
  57. 12
    11
      ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/TypeEncoding.java
  58. 1
    0
      ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/TypeParameterEncoding.java
  59. 39
    21
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/CodeWriter.java
  60. 4
    1
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/EncodingModule.java
  61. 1
    1
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/EncodingStage.java
  62. 2
    1
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/Main.java
  63. 19
    11
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/ModuleSerializer.java
  64. 4
    1
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/SerializationOptions.java
  65. 25
    18
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/TableBuilder.java
  66. 0
    2
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/DefinitionMemberSerializer.java
  67. 44
    42
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/DefinitionSerializer.java
  68. 47
    7
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/ExpressionSerializer.java
  69. 20
    19
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/MemberSerializer.java
  70. 37
    7
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/StatementSerializer.java
  71. 7
    4
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/SwitchValueSerializer.java
  72. 6
    2
      ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/TypeSerializer.java
  73. 3
    4
      Parser/src/main/java/org/openzen/zenscript/parser/statements/ParsedStatementBlock.java
  74. 3
    3
      Parser/src/main/java/org/openzen/zenscript/parser/statements/ParsedSwitchCase.java
  75. 2
    2
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java
  76. 4
    4
      Shared/src/main/java/compactio/CompactBytesDataInput.java
  77. 2
    2
      Shared/src/main/java/compactio/CompactDataInput.java

+ 2
- 2
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingSubBlock.java Parādīt failu

@@ -15,9 +15,9 @@ import org.openzen.zenscript.codemodel.statement.Statement;
15 15
 public class StatementFormattingSubBlock {
16 16
 	public final String header;
17 17
 	public final List<String> literalStatements;
18
-	public final List<Statement> statements;
18
+	public final Statement[] statements;
19 19
 	
20
-	public StatementFormattingSubBlock(String header, List<String> literalStatements, List<Statement> statements) {
20
+	public StatementFormattingSubBlock(String header, List<String> literalStatements, Statement[] statements) {
21 21
 		this.header = header;
22 22
 		this.literalStatements = literalStatements;
23 23
 		this.statements = statements;

+ 14
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java Parādīt failu

@@ -339,6 +339,20 @@ public class FunctionHeader {
339 339
 		return true;
340 340
 	}
341 341
 	
342
+	public FunctionHeader instanceForCall(GlobalTypeRegistry registry, CallArguments arguments) {
343
+		if (arguments.getNumberOfTypeArguments() > 0) {
344
+			Map<TypeParameter, ITypeID> typeParameters = new HashMap<>();
345
+			for (int i = 0; i < this.typeParameters.length; i++) {
346
+				typeParameters.put(this.typeParameters[i], arguments.typeArguments[i]);
347
+			}
348
+			return withGenericArguments(
349
+					registry,
350
+					new GenericMapper(registry, typeParameters));
351
+		} else {
352
+			return this;
353
+		}
354
+	}
355
+	
342 356
 	public FunctionHeader withGenericArguments(GlobalTypeRegistry registry, GenericMapper mapper) {
343 357
 		if (typeParameters.length > 0) {
344 358
 			Map<TypeParameter, ITypeID> innerMap = new HashMap<>();

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionForMethod.java Parādīt failu

@@ -6,6 +6,7 @@
6 6
 package org.openzen.zenscript.codemodel.annotations;
7 7
 
8 8
 import java.util.ArrayList;
9
+import java.util.Arrays;
9 10
 import java.util.List;
10 11
 import org.openzen.zencode.shared.CodePosition;
11 12
 import org.openzen.zenscript.codemodel.expression.Expression;
@@ -82,10 +83,10 @@ public class PreconditionForMethod implements MemberAnnotation {
82 83
 		statements.add(new IfStatement(position, inverseCondition, throwStatement, null));
83 84
 		
84 85
 		if (body instanceof BlockStatement) {
85
-			statements.addAll(((BlockStatement)body).statements);
86
+			statements.addAll(Arrays.asList(((BlockStatement)body).statements));
86 87
 		} else {
87 88
 			statements.add(body);
88 89
 		}
89
-		return new BlockStatement(position, statements);
90
+		return new BlockStatement(position, statements.toArray(new Statement[statements.size()]));
90 91
 	}
91 92
 }

+ 35
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/ModuleContext.java Parādīt failu

@@ -0,0 +1,35 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.context;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.Module;
10
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
12
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
13
+
14
+/**
15
+ *
16
+ * @author Hoofdgebruiker
17
+ */
18
+public class ModuleContext {
19
+	public final GlobalTypeRegistry registry;
20
+	public final Module module;
21
+	public final List<ExpansionDefinition> expansions;
22
+	public final ZSPackage root;
23
+	
24
+	public ModuleContext(
25
+			GlobalTypeRegistry registry,
26
+			Module module,
27
+			List<ExpansionDefinition> expansions,
28
+			ZSPackage root)
29
+	{
30
+		this.registry = registry;
31
+		this.module = module;
32
+		this.expansions = expansions;
33
+		this.root = root;
34
+	}
35
+}

+ 65
- 12
CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/StatementContext.java Parādīt failu

@@ -10,9 +10,12 @@ import java.util.Arrays;
10 10
 import java.util.List;
11 11
 import org.openzen.zenscript.codemodel.FunctionHeader;
12 12
 import org.openzen.zenscript.codemodel.FunctionParameter;
13
+import org.openzen.zenscript.codemodel.expression.LambdaClosure;
14
+import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
13 15
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14 16
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
15 17
 import org.openzen.zenscript.codemodel.statement.VarStatement;
18
+import org.openzen.zenscript.codemodel.type.ITypeID;
16 19
 
17 20
 /**
18 21
  *
@@ -22,46 +25,73 @@ public class StatementContext extends TypeContext {
22 25
 	private final List<VarStatement> variables = new ArrayList<>();
23 26
 	private final LoopStatement[] loops;
24 27
 	private final FunctionHeader header;
28
+	private final LambdaClosure closure;
29
+	private final StatementContext lambdaOuter;
25 30
 	
26
-	public StatementContext() {
27
-		super(TypeParameter.NONE);
31
+	public VariantOptionSwitchValue variantOptionSwitchValue;
32
+	
33
+	public StatementContext(ModuleContext module, ITypeID thisType) {
34
+		super(module, TypeParameter.NONE, thisType);
35
+		
28 36
 		loops = LoopStatement.NONE;
29 37
 		header = null;
38
+		closure = null;
39
+		lambdaOuter = null;
30 40
 	}
31 41
 	
32
-	public StatementContext(FunctionHeader header) {
33
-		super(header.typeParameters);
42
+	public StatementContext(ModuleContext module, ITypeID thisType, FunctionHeader header) {
43
+		super(module, header.typeParameters, thisType);
44
+		
34 45
 		loops = LoopStatement.NONE;
35 46
 		this.header = header;
47
+		this.closure = null;
48
+		lambdaOuter = null;
36 49
 	}
37 50
 	
38 51
 	public StatementContext(TypeContext outer) {
39
-		super(outer.parameters);
52
+		super(outer.moduleContext, outer.typeParameters, outer.thisType);
40 53
 		loops = LoopStatement.NONE;
41 54
 		header = null;
55
+		closure = null;
56
+		lambdaOuter = null;
42 57
 	}
43 58
 	
44 59
 	public StatementContext(TypeContext outer, FunctionHeader header) {
45
-		super(outer, header.typeParameters);
60
+		super(outer, outer.thisType, header == null ? TypeParameter.NONE : header.typeParameters);
46 61
 		loops = LoopStatement.NONE;
47 62
 		this.header = header;
63
+		closure = null;
64
+		lambdaOuter = null;
48 65
 	}
49 66
 	
50 67
 	public StatementContext(StatementContext outer) {
51
-		super(outer.parameters);
68
+		super(outer.moduleContext, outer.typeParameters, outer.thisType);
52 69
 		
53 70
 		variables.addAll(outer.variables);
54 71
 		loops = outer.loops;
55 72
 		header = outer.header;
73
+		closure = outer.closure;
74
+		lambdaOuter = outer.lambdaOuter;
56 75
 	}
57 76
 	
58 77
 	public StatementContext(StatementContext outer, LoopStatement loop) {
59
-		super(outer.parameters);
78
+		super(outer.moduleContext, outer.typeParameters, outer.thisType);
60 79
 		
61 80
 		variables.addAll(outer.variables);
62 81
 		loops = Arrays.copyOf(outer.loops, outer.loops.length + 1);
63 82
 		loops[loops.length - 1] = loop;
64 83
 		header = outer.header;
84
+		closure = outer.closure;
85
+		lambdaOuter = outer.lambdaOuter;
86
+	}
87
+	
88
+	public StatementContext(StatementContext outer, FunctionHeader lambdaHeader, LambdaClosure lambdaClosure) {
89
+		super(outer, outer.thisType, lambdaHeader.typeParameters);
90
+		
91
+		loops = LoopStatement.NONE;
92
+		header = lambdaHeader;
93
+		this.closure = lambdaClosure;
94
+		lambdaOuter = outer;
65 95
 	}
66 96
 	
67 97
 	public void add(VarStatement variable) {
@@ -69,7 +99,10 @@ public class StatementContext extends TypeContext {
69 99
 	}
70 100
 	
71 101
 	public int getVariableId(VarStatement variable) {
72
-		return variables.indexOf(variable);
102
+		int id = variables.indexOf(variable);
103
+		if (id < 0)
104
+			throw new IllegalArgumentException("Variable not in scope: " + variable.name);
105
+		return id;
73 106
 	}
74 107
 	
75 108
 	public int getLoopId(LoopStatement loop) {
@@ -77,17 +110,37 @@ public class StatementContext extends TypeContext {
77 110
 			if (loops[i] == loop)
78 111
 				return i;
79 112
 		
80
-		return -1;
113
+		throw new IllegalArgumentException("Loop@" + loop.position + " not in scope");
81 114
 	}
82 115
 	
83 116
 	public int getParameterIndex(FunctionParameter parameter) {
84 117
 		if (header == null)
85
-			return -1;
118
+			throw new IllegalStateException("No parameters available");
86 119
 		
87 120
 		for (int i = 0; i < header.parameters.length; i++)
88 121
 			if (header.parameters[i] == parameter)
89 122
 				return i;
90 123
 		
91
-		return -1;
124
+		throw new IllegalArgumentException("Parameter" + parameter.name + " not in scope");
125
+	}
126
+	
127
+	public VarStatement getVariable(int id) {
128
+		return variables.get(id);
129
+	}
130
+
131
+	public LoopStatement getLoop(int id) {
132
+		return loops[id];
133
+	}
134
+	
135
+	public FunctionParameter getParameter(int id) {
136
+		return header.parameters[id];
137
+	}
138
+	
139
+	public LambdaClosure getLambdaClosure() {
140
+		return closure;
141
+	}
142
+	
143
+	public StatementContext getLambdaOuter() {
144
+		return lambdaOuter;
92 145
 	}
93 146
 }

+ 39
- 9
CodeModel/src/main/java/org/openzen/zenscript/codemodel/context/TypeContext.java Parādīt failu

@@ -6,36 +6,66 @@
6 6
 package org.openzen.zenscript.codemodel.context;
7 7
 
8 8
 import java.util.Arrays;
9
+import java.util.HashMap;
9 10
 import java.util.List;
11
+import java.util.Map;
12
+import org.openzen.zenscript.codemodel.GenericMapper;
10 13
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
14
+import org.openzen.zenscript.codemodel.type.ITypeID;
15
+import org.openzen.zenscript.codemodel.type.member.LocalMemberCache;
16
+import org.openzen.zenscript.codemodel.type.member.TypeMembers;
11 17
 
12 18
 /**
13 19
  *
14 20
  * @author Hoofdgebruiker
15 21
  */
16 22
 public class TypeContext {
17
-	protected final TypeParameter[] parameters;
23
+	protected final TypeParameter[] typeParameters;
24
+	public final ITypeID thisType;
25
+	private final LocalMemberCache memberCache;
26
+	public final ModuleContext moduleContext;
18 27
 	
19
-	public TypeContext(TypeParameter[] parameters) {
20
-		this.parameters = parameters;
28
+	public TypeContext(ModuleContext context, TypeParameter[] parameters, ITypeID thisType) {
29
+		this.typeParameters = parameters;
30
+		this.thisType = thisType;
31
+		memberCache = new LocalMemberCache(context.registry, context.expansions);
32
+		moduleContext = context;
21 33
 	}
22 34
 	
23
-	public TypeContext(TypeContext outer, TypeParameter[] inner) {
24
-		parameters = concat(outer.parameters, inner);
35
+	public TypeContext(TypeContext outer, ITypeID thisType, TypeParameter... inner) {
36
+		typeParameters = concat(outer.typeParameters, inner);
37
+		this.thisType = thisType;
38
+		moduleContext = outer.moduleContext;
39
+		memberCache = new LocalMemberCache(moduleContext.registry, moduleContext.expansions);
25 40
 	}
26 41
 	
27
-	public TypeContext(TypeContext outer, List<TypeParameter> inner) {
28
-		this(outer, inner.toArray(new TypeParameter[inner.size()]));
42
+	public TypeContext(TypeContext outer, ITypeID thisType, List<TypeParameter> inner) {
43
+		this(outer, thisType, inner.toArray(new TypeParameter[inner.size()]));
29 44
 	}
30 45
 	
31 46
 	public int getId(TypeParameter parameter) {
32
-		for (int i = 0; i < parameters.length; i++)
33
-			if (parameters[i] == parameter)
47
+		for (int i = 0; i < typeParameters.length; i++)
48
+			if (typeParameters[i] == parameter)
34 49
 				return i;
35 50
 		
36 51
 		return -1;
37 52
 	}
38 53
 	
54
+	public TypeParameter getTypeParameter(int index) {
55
+		return typeParameters[index];
56
+	}
57
+	
58
+	public TypeMembers getTypeMembers(ITypeID type) {
59
+		return memberCache.get(type);
60
+	}
61
+	
62
+	public GenericMapper getMapper() {
63
+		Map<TypeParameter, ITypeID> mapper = new HashMap<>();
64
+		for (TypeParameter parameter : typeParameters)
65
+			mapper.put(parameter, moduleContext.registry.getGeneric(parameter));
66
+		return new GenericMapper(moduleContext.registry, mapper);
67
+	}
68
+	
39 69
 	public static <T> T[] concat(T[] first, T[] second) {
40 70
 		T[] result = Arrays.copyOf(first, first.length + second.length);
41 71
 		System.arraycopy(second, 0, result, first.length, second.length);

+ 12
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java Parādīt failu

@@ -26,6 +26,10 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
26 26
  * @author Hoofdgebruiker
27 27
  */
28 28
 public class ZSPackage {
29
+	public static ZSPackage createRoot() {
30
+		return new ZSPackage(null, "");
31
+	}
32
+	
29 33
 	public final String name;
30 34
 	public final String fullName;
31 35
 	public final ZSPackage parent;
@@ -109,6 +113,14 @@ public class ZSPackage {
109 113
 		return null;
110 114
 	}
111 115
 	
116
+	public ZSPackage getRecursive(String name) {
117
+		int dot = name.indexOf('.');
118
+		if (dot < 0)
119
+			return getOrCreatePackage(name);
120
+		else
121
+			return getOrCreatePackage(name.substring(0, dot)).getRecursive(name.substring(dot + 1));
122
+	}
123
+	
112 124
 	public ZSPackage getOrCreatePackage(String name) {
113 125
 		if (subPackages.containsKey(name))
114 126
 			return subPackages.get(name);

+ 9
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CompareExpression.java Parādīt failu

@@ -23,11 +23,11 @@ public class CompareExpression extends Expression {
23 23
 	public final FunctionalMemberRef operator;
24 24
 	public final CompareType comparison;
25 25
 	
26
-	public CompareExpression(CodePosition position, Expression left, Expression right, FunctionalMemberRef operator, CompareType comparison, TypeScope scope) {
26
+	public CompareExpression(CodePosition position, Expression left, Expression right, FunctionalMemberRef operator, CompareType comparison) {
27 27
 		super(position, BasicTypeID.BOOL, binaryThrow(position, left.thrownType, right.thrownType));
28 28
 		
29 29
 		this.left = left;
30
-		this.right = scope == null ? right : right.castImplicit(position, scope, operator.getHeader().parameters[0].type);
30
+		this.right = right;
31 31
 		this.operator = operator;
32 32
 		this.comparison = comparison;
33 33
 	}
@@ -46,11 +46,16 @@ public class CompareExpression extends Expression {
46 46
 	public Expression transform(ExpressionTransformer transformer) {
47 47
 		Expression tLeft = left.transform(transformer);
48 48
 		Expression tRight = right.transform(transformer);
49
-		return left == tLeft && right == tRight ? this : new CompareExpression(position, tLeft, tRight, operator, comparison, null);
49
+		return left == tLeft && right == tRight ? this : new CompareExpression(position, tLeft, tRight, operator, comparison);
50 50
 	}
51 51
 
52 52
 	@Override
53 53
 	public Expression normalize(TypeScope scope) {
54
-		return new CompareExpression(position, left.normalize(scope), right.normalize(scope), operator, comparison, scope);
54
+		return new CompareExpression(
55
+				position,
56
+				left.normalize(scope),
57
+				right.normalize(scope).castImplicit(position, scope, operator.getHeader().parameters[0].type),
58
+				operator,
59
+				comparison);
55 60
 	}
56 61
 }

+ 1
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantLongExpression.java Parādīt failu

@@ -12,6 +12,7 @@ import org.openzen.zenscript.codemodel.type.BasicTypeID;
12 12
 /**
13 13
  *
14 14
  * @author Hoofdgebruiker
15
+ * 
15 16
  */
16 17
 public class ConstantLongExpression extends Expression {
17 18
 	public final long value;

+ 3
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MatchExpression.java Parādīt failu

@@ -6,7 +6,6 @@
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8 8
 import java.util.ArrayList;
9
-import java.util.Collections;
10 9
 import java.util.List;
11 10
 import org.openzen.zencode.shared.CodePosition;
12 11
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
@@ -82,14 +81,15 @@ public class MatchExpression extends Expression {
82 81
 			statements.add(new ExpressionStatement(matchCase.value.position, caseExpression));
83 82
 			if (reachable)
84 83
 				statements.add(new BreakStatement(matchCase.value.position, switchStatement));
85
-			SwitchCase switchCase = new SwitchCase(matchCase.key, statements);
84
+			SwitchCase switchCase = new SwitchCase(matchCase.key, statements.toArray(new Statement[statements.size()]));
86 85
 			switchStatement.cases.add(switchCase);
87 86
 			
88 87
 			if (matchCase.key == null)
89 88
 				hasDefault = true;
90 89
 		}
91 90
 		if (!hasDefault) {
92
-			switchStatement.cases.add(new SwitchCase(null, Collections.singletonList(new ExpressionStatement(position, new PanicExpression(position, BasicTypeID.VOID, new ConstantStringExpression(position, "Missing case"))))));
91
+			Statement defaultCase = new ExpressionStatement(position, new PanicExpression(position, BasicTypeID.VOID, new ConstantStringExpression(position, "Missing case")));
92
+			switchStatement.cases.add(new SwitchCase(null, new Statement[] { defaultCase }));
93 93
 		}
94 94
 		return new SwitchedMatch(result, switchStatement);
95 95
 	}

+ 7
- 9
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstMember.java Parādīt failu

@@ -24,13 +24,11 @@ import org.openzen.zenscript.codemodel.type.member.TypeMembers;
24 24
 public class ConstMember extends PropertyMember {
25 25
 	public final String name;
26 26
 	public Expression value;
27
-	public final BuiltinID builtin;
28 27
 	
29 28
 	public ConstMember(CodePosition position, HighLevelDefinition definition, int modifiers, String name, ITypeID type, BuiltinID builtin) {
30
-		super(position, definition, modifiers, type, null);
29
+		super(position, definition, modifiers, type, builtin);
31 30
 		
32 31
 		this.name = name;
33
-		this.builtin = builtin;
34 32
 	}
35 33
 
36 34
 	@Override
@@ -38,14 +36,9 @@ public class ConstMember extends PropertyMember {
38 36
 		return "const " + name;
39 37
 	}
40 38
 
41
-	@Override
42
-	public BuiltinID getBuiltin() {
43
-		return builtin;
44
-	}
45
-
46 39
 	@Override
47 40
 	public void registerTo(TypeMembers members, TypeMemberPriority priority, GenericMapper mapper) {
48
-		members.addConst(new ConstMemberRef(this, mapper));
41
+		members.addConst(new ConstMemberRef(members.type, this, mapper));
49 42
 	}
50 43
 
51 44
 	@Override
@@ -73,4 +66,9 @@ public class ConstMember extends PropertyMember {
73 66
 	public boolean isAbstract() {
74 67
 		return false;
75 68
 	}
69
+
70
+	@Override
71
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
72
+		return new ConstMemberRef(type, this, mapper);
73
+	}
76 74
 }

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ConstructorMember.java Parādīt failu

@@ -48,8 +48,8 @@ public class ConstructorMember extends FunctionalMember {
48 48
 			firstExpression = (ExpressionStatement) body;
49 49
 		} else if (body instanceof BlockStatement) {
50 50
 			BlockStatement blockBody = (BlockStatement) body;
51
-			if (blockBody.statements.size() > 0 && blockBody.statements.get(0) instanceof ExpressionStatement)
52
-				firstExpression = (ExpressionStatement) blockBody.statements.get(0);
51
+			if (blockBody.statements.length > 0 && blockBody.statements[0] instanceof ExpressionStatement)
52
+				firstExpression = (ExpressionStatement) blockBody.statements[0];
53 53
 		}
54 54
 		
55 55
 		return firstExpression != null && (

+ 8
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/FieldMember.java Parādīt failu

@@ -67,7 +67,7 @@ public class FieldMember extends PropertyMember {
67 67
 			this.autoGetter.setBody(new ReturnStatement(position, new GetFieldExpression(
68 68
 					position,
69 69
 					new ThisExpression(position, thisType),
70
-					new FieldMemberRef(this, null))));
70
+					new FieldMemberRef(thisType, this, null))));
71 71
 		} else {
72 72
 			this.autoGetter = null;
73 73
 		}
@@ -76,7 +76,7 @@ public class FieldMember extends PropertyMember {
76 76
 			this.autoSetter.setBody(new ExpressionStatement(position, new SetFieldExpression(
77 77
 					position,
78 78
 					new ThisExpression(position, thisType),
79
-					new FieldMemberRef(this, null),
79
+					new FieldMemberRef(thisType, this, null),
80 80
 					new GetFunctionParameterExpression(position, this.autoSetter.parameter))));
81 81
 		} else {
82 82
 			this.autoSetter = null;
@@ -118,7 +118,7 @@ public class FieldMember extends PropertyMember {
118 118
 
119 119
 	@Override
120 120
 	public void registerTo(TypeMembers members, TypeMemberPriority priority, GenericMapper mapper) {
121
-		members.addField(new FieldMemberRef(this, mapper), priority);
121
+		members.addField(new FieldMemberRef(members.type, this, mapper), priority);
122 122
 	}
123 123
 	
124 124
 	@Override
@@ -157,4 +157,9 @@ public class FieldMember extends PropertyMember {
157 157
 	public boolean isAbstract() {
158 158
 		return false;
159 159
 	}
160
+
161
+	@Override
162
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
163
+		return new FieldMemberRef(type, this, mapper);
164
+	}
160 165
 }

+ 7
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java Parādīt failu

@@ -9,6 +9,7 @@ import org.openzen.zencode.shared.CodePosition;
9 9
 import org.openzen.zencode.shared.ConcatMap;
10 10
 import org.openzen.zenscript.codemodel.GenericMapper;
11 11
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
12 13
 import org.openzen.zenscript.codemodel.member.ref.GetterMemberRef;
13 14
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14 15
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
@@ -54,7 +55,7 @@ public class GetterMember extends PropertyMember {
54 55
 	
55 56
 	@Override
56 57
 	public void registerTo(TypeMembers members, TypeMemberPriority priority, GenericMapper mapper) {
57
-		members.addGetter(new GetterMemberRef(this, mapper), priority);
58
+		members.addGetter(new GetterMemberRef(members.type, this, mapper), priority);
58 59
 	}
59 60
 
60 61
 	@Override
@@ -89,4 +90,9 @@ public class GetterMember extends PropertyMember {
89 90
 		if (type == BasicTypeID.UNDETERMINED)
90 91
 			type = override.getType();
91 92
 	}
93
+
94
+	@Override
95
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
96
+		return new GetterMemberRef(type, this, mapper);
97
+	}
92 98
 }

+ 3
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/IDefinitionMember.java Parādīt failu

@@ -10,6 +10,7 @@ import org.openzen.zenscript.codemodel.GenericMapper;
10 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11 11
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
12 12
 import org.openzen.zenscript.codemodel.scope.TypeScope;
13
+import org.openzen.zenscript.codemodel.type.ITypeID;
13 14
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
14 15
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
15 16
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
@@ -46,4 +47,6 @@ public interface IDefinitionMember {
46 47
 	public void normalize(TypeScope scope);
47 48
 	
48 49
 	boolean isAbstract();
50
+	
51
+	DefinitionMemberRef ref(ITypeID type, GenericMapper mapper);
49 52
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ImplementationMember.java Parādīt failu

@@ -80,4 +80,9 @@ public class ImplementationMember extends DefinitionMember {
80 80
 	public boolean isAbstract() {
81 81
 		return false;
82 82
 	}
83
+
84
+	@Override
85
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
86
+		throw new UnsupportedOperationException("Cannot create an implementation reference");
87
+	}
83 88
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/InnerDefinitionMember.java Parādīt failu

@@ -13,6 +13,7 @@ import org.openzen.zenscript.codemodel.context.TypeResolutionContext;
13 13
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
14 14
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
15 15
 import org.openzen.zenscript.codemodel.scope.TypeScope;
16
+import org.openzen.zenscript.codemodel.type.ITypeID;
16 17
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
17 18
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
18 19
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
@@ -73,4 +74,9 @@ public class InnerDefinitionMember extends DefinitionMember {
73 74
 	public boolean isAbstract() {
74 75
 		return false;
75 76
 	}
77
+
78
+	@Override
79
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
80
+		throw new UnsupportedOperationException("Cannot create an inner definition reference");
81
+	}
76 82
 }

+ 7
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/SetterMember.java Parādīt failu

@@ -10,6 +10,7 @@ import org.openzen.zencode.shared.ConcatMap;
10 10
 import org.openzen.zenscript.codemodel.FunctionParameter;
11 11
 import org.openzen.zenscript.codemodel.GenericMapper;
12 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
13 14
 import org.openzen.zenscript.codemodel.member.ref.SetterMemberRef;
14 15
 import org.openzen.zenscript.codemodel.scope.TypeScope;
15 16
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
@@ -54,7 +55,7 @@ public class SetterMember extends PropertyMember {
54 55
 
55 56
 	@Override
56 57
 	public void registerTo(TypeMembers members, TypeMemberPriority priority, GenericMapper mapper) {
57
-		members.addSetter(new SetterMemberRef(this, mapper), priority);
58
+		members.addSetter(new SetterMemberRef(members.type, this, mapper), priority);
58 59
 	}
59 60
 
60 61
 	@Override
@@ -94,4 +95,9 @@ public class SetterMember extends PropertyMember {
94 95
 		if (type == BasicTypeID.UNDETERMINED)
95 96
 			type = overrides.getType();
96 97
 	}
98
+
99
+	@Override
100
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
101
+		return new SetterMemberRef(type, this, mapper);
102
+	}
97 103
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/StaticInitializerMember.java Parādīt failu

@@ -13,6 +13,7 @@ import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
13 13
 import org.openzen.zenscript.codemodel.scope.TypeScope;
14 14
 import org.openzen.zenscript.codemodel.statement.LoopStatement;
15 15
 import org.openzen.zenscript.codemodel.statement.Statement;
16
+import org.openzen.zenscript.codemodel.type.ITypeID;
16 17
 import org.openzen.zenscript.codemodel.type.member.BuiltinID;
17 18
 import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
18 19
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
@@ -67,4 +68,9 @@ public class StaticInitializerMember extends DefinitionMember {
67 68
 	public boolean isAbstract() {
68 69
 		return false;
69 70
 	}
71
+
72
+	@Override
73
+	public DefinitionMemberRef ref(ITypeID type, GenericMapper mapper) {
74
+		throw new UnsupportedOperationException("Cannot reference a static initializer");
75
+	}
70 76
 }

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/CasterMemberRef.java Parādīt failu

@@ -36,7 +36,7 @@ public class CasterMemberRef implements DefinitionMemberRef {
36 36
 	}
37 37
 	
38 38
 	@Override
39
-	public ITypeID getType() {
39
+	public ITypeID getOwnerType() {
40 40
 		return type;
41 41
 	}
42 42
 

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ConstMemberRef.java Parādīt failu

@@ -7,6 +7,7 @@ package org.openzen.zenscript.codemodel.member.ref;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.GenericMapper;
9 9
 import org.openzen.zenscript.codemodel.member.ConstMember;
10
+import org.openzen.zenscript.codemodel.type.ITypeID;
10 11
 
11 12
 /**
12 13
  *
@@ -15,8 +16,8 @@ import org.openzen.zenscript.codemodel.member.ConstMember;
15 16
 public class ConstMemberRef extends PropertyRef {
16 17
 	public final ConstMember member;
17 18
 	
18
-	public ConstMemberRef(ConstMember member, GenericMapper mapper) {
19
-		super(member, mapper);
19
+	public ConstMemberRef(ITypeID owner, ConstMember member, GenericMapper mapper) {
20
+		super(owner, member, mapper);
20 21
 		this.member = member;
21 22
 	}
22 23
 

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/DefinitionMemberRef.java Parādīt failu

@@ -22,7 +22,7 @@ public interface DefinitionMemberRef {
22 22
 	
23 23
 	<T> T getTag(Class<T> type);
24 24
 	
25
-	ITypeID getType();
25
+	ITypeID getOwnerType();
26 26
 	
27 27
 	DefinitionMemberRef getOverrides();
28 28
 	

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FieldMemberRef.java Parādīt failu

@@ -7,6 +7,7 @@ package org.openzen.zenscript.codemodel.member.ref;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.GenericMapper;
9 9
 import org.openzen.zenscript.codemodel.member.FieldMember;
10
+import org.openzen.zenscript.codemodel.type.ITypeID;
10 11
 
11 12
 /**
12 13
  *
@@ -15,8 +16,8 @@ import org.openzen.zenscript.codemodel.member.FieldMember;
15 16
 public class FieldMemberRef extends PropertyRef {
16 17
 	public final FieldMember member;
17 18
 	
18
-	public FieldMemberRef(FieldMember member, GenericMapper mapper) {
19
-		super(member, mapper);
19
+	public FieldMemberRef(ITypeID owner, FieldMember member, GenericMapper mapper) {
20
+		super(owner, member, mapper);
20 21
 		this.member = member;
21 22
 	}
22 23
 

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/FunctionalMemberRef.java Parādīt failu

@@ -71,7 +71,7 @@ public class FunctionalMemberRef implements DefinitionMemberRef {
71 71
 	}
72 72
 	
73 73
 	@Override
74
-	public ITypeID getType() {
74
+	public ITypeID getOwnerType() {
75 75
 		return type;
76 76
 	}
77 77
 	
@@ -142,7 +142,7 @@ public class FunctionalMemberRef implements DefinitionMemberRef {
142 142
 	}
143 143
 	
144 144
 	public Expression callWithComparator(CodePosition position, CompareType comparison, Expression target, FunctionHeader instancedHeader, CallArguments arguments, TypeScope scope) {
145
-		return new CompareExpression(position, target, arguments.arguments[0], this, comparison, scope);
145
+		return new CompareExpression(position, target, arguments.arguments[0], this, comparison);
146 146
 	}
147 147
 	
148 148
 	public Expression callStatic(CodePosition position, ITypeID target, FunctionHeader instancedHeader, CallArguments arguments, TypeScope scope) {

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/GetterMemberRef.java Parādīt failu

@@ -11,6 +11,7 @@ import org.openzen.zenscript.codemodel.expression.Expression;
11 11
 import org.openzen.zenscript.codemodel.expression.GetterExpression;
12 12
 import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
13 13
 import org.openzen.zenscript.codemodel.member.GetterMember;
14
+import org.openzen.zenscript.codemodel.type.ITypeID;
14 15
 
15 16
 /**
16 17
  *
@@ -19,8 +20,8 @@ import org.openzen.zenscript.codemodel.member.GetterMember;
19 20
 public class GetterMemberRef extends PropertyRef {
20 21
 	public final GetterMember member;
21 22
 	
22
-	public GetterMemberRef(GetterMember member, GenericMapper mapper) {
23
-		super(member, mapper);
23
+	public GetterMemberRef(ITypeID owner, GetterMember member, GenericMapper mapper) {
24
+		super(owner, member, mapper);
24 25
 		
25 26
 		this.member = member;
26 27
 	}

+ 3
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/ImplementationMemberRef.java Parādīt failu

@@ -21,9 +21,9 @@ public class ImplementationMemberRef implements DefinitionMemberRef {
21 21
 	private final ITypeID type;
22 22
 	public final ITypeID implementsType;
23 23
 	
24
-	public ImplementationMemberRef(ImplementationMember member, ITypeID type, ITypeID implementsType) {
24
+	public ImplementationMemberRef(ImplementationMember member, ITypeID owner, ITypeID implementsType) {
25 25
 		this.member = member;
26
-		this.type = type;
26
+		this.type = owner;
27 27
 		this.implementsType = implementsType;
28 28
 	}
29 29
 
@@ -33,7 +33,7 @@ public class ImplementationMemberRef implements DefinitionMemberRef {
33 33
 	}
34 34
 	
35 35
 	@Override
36
-	public ITypeID getType() {
36
+	public ITypeID getOwnerType() {
37 37
 		return type;
38 38
 	}
39 39
 

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/IteratorMemberRef.java Parādīt failu

@@ -33,7 +33,7 @@ public class IteratorMemberRef implements DefinitionMemberRef {
33 33
 	}
34 34
 	
35 35
 	@Override
36
-	public ITypeID getType() {
36
+	public ITypeID getOwnerType() {
37 37
 		return type;
38 38
 	}
39 39
 

+ 8
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/PropertyRef.java Parādīt failu

@@ -21,10 +21,12 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
21 21
 public abstract class PropertyRef implements DefinitionMemberRef {
22 22
 	private final PropertyMember member;
23 23
 	
24
+	private final ITypeID owner;
24 25
 	private ITypeID type;
25 26
 	private GenericMapper mapper;
26 27
 	
27
-	public PropertyRef(PropertyMember member, GenericMapper mapper) {
28
+	public PropertyRef(ITypeID owner, PropertyMember member, GenericMapper mapper) {
29
+		this.owner = owner;
28 30
 		this.member = member;
29 31
 		
30 32
 		if (member.getType() == BasicTypeID.UNDETERMINED) {
@@ -36,6 +38,11 @@ public abstract class PropertyRef implements DefinitionMemberRef {
36 38
 		}
37 39
 	}
38 40
 	
41
+	@Override
42
+	public final ITypeID getOwnerType() {
43
+		return owner;
44
+	}
45
+	
39 46
 	public final ITypeID getType() {
40 47
 		if (type == null) {
41 48
 			if (member.getType() == BasicTypeID.UNDETERMINED)

+ 3
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/ref/SetterMemberRef.java Parādīt failu

@@ -7,6 +7,7 @@ package org.openzen.zenscript.codemodel.member.ref;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.GenericMapper;
9 9
 import org.openzen.zenscript.codemodel.member.SetterMember;
10
+import org.openzen.zenscript.codemodel.type.ITypeID;
10 11
 
11 12
 /**
12 13
  *
@@ -15,8 +16,8 @@ import org.openzen.zenscript.codemodel.member.SetterMember;
15 16
 public class SetterMemberRef extends PropertyRef {
16 17
 	public final SetterMember member;
17 18
 	
18
-	public SetterMemberRef(SetterMember member, GenericMapper mapper) {
19
-		super(member, mapper);
19
+	public SetterMemberRef(ITypeID owner, SetterMember member, GenericMapper mapper) {
20
+		super(owner, member, mapper);
20 21
 		
21 22
 		this.member = member;
22 23
 	}

+ 0
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/scope/BaseScope.java Parādīt failu

@@ -5,7 +5,6 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.scope;
7 7
 
8
-import java.util.List;
9 8
 import java.util.function.Function;
10 9
 import org.openzen.zencode.shared.CodePosition;
11 10
 import org.openzen.zenscript.codemodel.FunctionHeader;
@@ -15,7 +14,6 @@ import org.openzen.zenscript.codemodel.statement.LoopStatement;
15 14
 import org.openzen.zenscript.codemodel.type.GenericName;
16 15
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
17 16
 import org.openzen.zenscript.codemodel.type.ITypeID;
18
-import org.openzen.zenscript.codemodel.type.member.TypeMemberPreparer;
19 17
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
20 18
 
21 19
 /**

+ 20
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/CodeSerializationInput.java Parādīt failu

@@ -8,6 +8,7 @@ package org.openzen.zenscript.codemodel.serialization;
8 8
 import org.openzen.zencode.shared.CodePosition;
9 9
 import org.openzen.zenscript.codemodel.FunctionHeader;
10 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11
+import org.openzen.zenscript.codemodel.context.ModuleContext;
11 12
 import org.openzen.zenscript.codemodel.expression.CallArguments;
12 13
 import org.openzen.zenscript.codemodel.expression.Expression;
13 14
 import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
@@ -15,6 +16,9 @@ import org.openzen.zenscript.codemodel.statement.Statement;
15 16
 import org.openzen.zenscript.codemodel.type.ITypeID;
16 17
 import org.openzen.zenscript.codemodel.context.StatementContext;
17 18
 import org.openzen.zenscript.codemodel.context.TypeContext;
19
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
20
+import org.openzen.zenscript.codemodel.member.EnumConstantMember;
21
+import org.openzen.zenscript.codemodel.member.ref.VariantOptionRef;
18 22
 
19 23
 /**
20 24
  *
@@ -39,6 +43,10 @@ public interface CodeSerializationInput {
39 43
 	
40 44
 	long readULong();
41 45
 	
46
+	float readFloat();
47
+	
48
+	double readDouble();
49
+	
42 50
 	char readChar();
43 51
 	
44 52
 	String readString();
@@ -47,6 +55,10 @@ public interface CodeSerializationInput {
47 55
 	
48 56
 	DefinitionMemberRef readMember(TypeContext context, ITypeID type);
49 57
 	
58
+	EnumConstantMember readEnumConstant(TypeContext context);
59
+	
60
+	VariantOptionRef readVariantOption(TypeContext context, ITypeID type);
61
+	
50 62
 	ITypeID deserializeType(TypeContext context);
51 63
 	
52 64
 	CodePosition deserializePosition();
@@ -58,4 +70,12 @@ public interface CodeSerializationInput {
58 70
 	Statement deserializeStatement(StatementContext context);
59 71
 	
60 72
 	Expression deserializeExpression(StatementContext context);
73
+	
74
+	TypeParameter deserializeTypeParameter(TypeContext context);
75
+	
76
+	TypeParameter[] deserializeTypeParameters(TypeContext context);
77
+	
78
+	void enqueueMembers(DecodingOperation operation);
79
+	
80
+	public void enqueueCode(DecodingOperation operation);
61 81
 }

+ 2
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/CodeSerializationOutput.java Parādīt failu

@@ -66,6 +66,8 @@ public interface CodeSerializationOutput {
66 66
 	
67 67
 	void serialize(TypeContext context, TypeParameter parameter);
68 68
 	
69
+	void serialize(TypeContext context, TypeParameter[] parameters);
70
+	
69 71
 	void serialize(CodePosition position);
70 72
 	
71 73
 	void serialize(TypeContext context, FunctionHeader header);

+ 14
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/serialization/DecodingOperation.java Parādīt failu

@@ -0,0 +1,14 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.codemodel.serialization;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface DecodingOperation {
13
+	void decode(CodeSerializationInput input);
14
+}

+ 15
- 15
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/BlockStatement.java Parādīt failu

@@ -5,8 +5,6 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.statement;
7 7
 
8
-import java.util.ArrayList;
9
-import java.util.List;
10 8
 import java.util.function.Consumer;
11 9
 import org.openzen.zencode.shared.CodePosition;
12 10
 import org.openzen.zencode.shared.ConcatMap;
@@ -20,9 +18,9 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
20 18
  * @author Hoofdgebruiker
21 19
  */
22 20
 public class BlockStatement extends Statement {
23
-	public final List<Statement> statements;
21
+	public final Statement[] statements;
24 22
 	
25
-	public BlockStatement(CodePosition position, List<Statement> statements) {
23
+	public BlockStatement(CodePosition position, Statement[] statements) {
26 24
 		super(position, getThrownType(statements));
27 25
 		
28 26
 		this.statements = statements;
@@ -48,29 +46,30 @@ public class BlockStatement extends Statement {
48 46
 	
49 47
 	@Override
50 48
 	public Statement transform(StatementTransformer transformer, ConcatMap<LoopStatement, LoopStatement> modified) {
51
-		List<Statement> tStatements = new ArrayList<>();
49
+		Statement[] tStatements = new Statement[statements.length];
52 50
 		boolean unchanged = true;
53
-		for (Statement statement : statements) {
51
+		for (int i = 0; i < statements.length; i++) {
52
+			Statement statement = statements[i];
54 53
 			Statement tStatement = statement.transform(transformer, modified);
55 54
 			unchanged &= statement == tStatement;
56
-			tStatements.add(statement);
55
+			tStatements[i] = statement;
57 56
 		}
58 57
 		return unchanged ? this : new BlockStatement(position, tStatements);
59 58
 	}
60 59
 
61 60
 	@Override
62 61
 	public Statement transform(ExpressionTransformer transformer, ConcatMap<LoopStatement, LoopStatement> modified) {
63
-		List<Statement> tStatements = new ArrayList<>();
62
+		Statement[] tStatements = new Statement[statements.length];
64 63
 		boolean unchanged = true;
65
-		for (Statement statement : statements) {
66
-			Statement tStatement = statement.transform(transformer, modified);
67
-			unchanged &= statement == tStatement;
68
-			tStatements.add(statement);
64
+		for (int i = 0; i < tStatements.length; i++) {
65
+			Statement tStatement = statements[i].transform(transformer, modified);
66
+			unchanged &= statements[i] == tStatement;
67
+			tStatements[i] = tStatement;
69 68
 		}
70 69
 		return unchanged ? this : new BlockStatement(position, tStatements);
71 70
 	}
72 71
 	
73
-	private static ITypeID getThrownType(List<Statement> statements) {
72
+	private static ITypeID getThrownType(Statement[] statements) {
74 73
 		ITypeID result = null;
75 74
 		for (Statement statement : statements)
76 75
 			result = Expression.binaryThrow(statement.position, result, statement.thrownType);
@@ -79,9 +78,10 @@ public class BlockStatement extends Statement {
79 78
 
80 79
 	@Override
81 80
 	public Statement normalize(TypeScope scope, ConcatMap<LoopStatement, LoopStatement> modified) {
82
-		List<Statement> normalized = new ArrayList<>();
81
+		Statement[] normalized = new Statement[statements.length];
82
+		int i = 0;
83 83
 		for (Statement statement : statements)
84
-			normalized.add(statement.normalize(scope, modified));
84
+			normalized[i++] = statement.normalize(scope, modified);
85 85
 		return new BlockStatement(position, normalized);
86 86
 	}
87 87
 }

+ 11
- 10
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/SwitchCase.java Parādīt failu

@@ -5,8 +5,6 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.statement;
7 7
 
8
-import java.util.ArrayList;
9
-import java.util.List;
10 8
 import org.openzen.zencode.shared.ConcatMap;
11 9
 import org.openzen.zenscript.codemodel.expression.ExpressionTransformer;
12 10
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
@@ -18,33 +16,36 @@ import org.openzen.zenscript.codemodel.scope.TypeScope;
18 16
  */
19 17
 public class SwitchCase {
20 18
 	public final SwitchValue value;
21
-	public final List<Statement> statements;
19
+	public final Statement[] statements;
22 20
 	
23
-	public SwitchCase(SwitchValue value, List<Statement> statements) {
21
+	public SwitchCase(SwitchValue value, Statement[] statements) {
24 22
 		this.value = value;
25 23
 		this.statements = statements;
26 24
 	}
27 25
 	
28 26
 	public SwitchCase transform(StatementTransformer transformer, ConcatMap<LoopStatement, LoopStatement> modified) {
29
-		List<Statement> tStatements = new ArrayList<>();
27
+		Statement[] tStatements = new Statement[statements.length];
28
+		int i = 0;
30 29
 		for (Statement statement : statements) {
31
-			tStatements.add(statement.transform(transformer, modified));
30
+			tStatements[i++] = statement.transform(transformer, modified);
32 31
 		}
33 32
 		return new SwitchCase(value, tStatements);
34 33
 	}
35 34
 	
36 35
 	public SwitchCase transform(ExpressionTransformer transformer, ConcatMap<LoopStatement, LoopStatement> modified) {
37
-		List<Statement> tStatements = new ArrayList<>();
36
+		Statement[] tStatements = new Statement[statements.length];
37
+		int i = 0;
38 38
 		for (Statement statement : statements) {
39
-			tStatements.add(statement.transform(transformer, modified));
39
+			tStatements[i++] = statement.transform(transformer, modified);
40 40
 		}
41 41
 		return new SwitchCase(value, tStatements);
42 42
 	}
43 43
 	
44 44
 	public SwitchCase normalize(TypeScope scope, ConcatMap<LoopStatement, LoopStatement> modified) {
45
-		List<Statement> tStatements = new ArrayList<>();
45
+		Statement[] tStatements = new Statement[statements.length];
46
+		int i = 0;
46 47
 		for (Statement statement : statements)
47
-			tStatements.add(statement.normalize(scope, modified));
48
+			tStatements[i++] = statement.normalize(scope, modified);
48 49
 		return new SwitchCase(value, tStatements);
49 50
 	}
50 51
 }

+ 6
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/BuiltinID.java Parādīt failu

@@ -526,5 +526,10 @@ public enum BuiltinID {
526 526
 	ITERATOR_ARRAY_KEY_VALUES,
527 527
 	ITERATOR_ASSOC_KEYS,
528 528
 	ITERATOR_ASSOC_KEY_VALUES,
529
-	ITERATOR_STRING_CHARS
529
+	ITERATOR_STRING_CHARS;
530
+	
531
+	private static final BuiltinID[] VALUES = values();
532
+	public static BuiltinID get(int ordinal) {
533
+		return VALUES[ordinal];
534
+	}
530 535
 }

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/DefinitionMemberGroup.java Parādīt failu

@@ -118,6 +118,10 @@ public class DefinitionMemberGroup {
118 118
 		return this.methods;
119 119
 	}
120 120
 	
121
+	public ConstMemberRef getConstant() {
122
+		return constant.member;
123
+	}
124
+	
121 125
 	public void setConst(ConstMemberRef constant, TypeMemberPriority priority) {
122 126
 		if (this.constant != null) {
123 127
 			this.constant = this.constant.resolve(new TypeMember<>(priority, constant));

+ 29
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMembers.java Parādīt failu

@@ -28,6 +28,7 @@ import org.openzen.zenscript.codemodel.member.IDefinitionMember;
28 28
 import org.openzen.zenscript.codemodel.member.InnerDefinition;
29 29
 import org.openzen.zenscript.codemodel.member.ref.CasterMemberRef;
30 30
 import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
31
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
31 32
 import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
32 33
 import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
33 34
 import org.openzen.zenscript.codemodel.member.ref.GetterMemberRef;
@@ -132,6 +133,34 @@ public final class TypeMembers {
132 133
 			other.getOrCreateGroup(entry.getKey()).merge(position, entry.getValue(), priority);
133 134
 	}
134 135
 	
136
+	public DefinitionMemberRef getBuiltin(BuiltinID builtin) {
137
+		for (DefinitionMemberGroup group : members.values()) {
138
+			if (group.getConstant() != null && group.getConstant().member.builtin == builtin)
139
+				return group.getConstant();
140
+			if (group.getField() != null && group.getField().member.builtin == builtin)
141
+				return group.getField();
142
+			
143
+			for (TypeMember<FunctionalMemberRef> member : group.getMethodMembers()) {
144
+				if (member.member.getBuiltin() == builtin)
145
+					return member.member;
146
+			}
147
+		}
148
+		
149
+		for (DefinitionMemberGroup group : operators.values()) {
150
+			if (group.getConstant() != null && group.getConstant().member.builtin == builtin)
151
+				return group.getConstant();
152
+			if (group.getField() != null && group.getField().member.builtin == builtin)
153
+				return group.getField();
154
+			
155
+			for (TypeMember<FunctionalMemberRef> member : group.getMethodMembers()) {
156
+				if (member.member.getBuiltin() == builtin)
157
+					return member.member;
158
+			}
159
+		}
160
+		
161
+		return null;
162
+	}
163
+	
135 164
 	public ITypeID union(ITypeID other) {
136 165
 		other = other.getNormalized();
137 166
 		if (type == other)

+ 5
- 0
CompilerShared/src/main/java/org/openzen/zenscript/compiler/SemanticModule.java Parādīt failu

@@ -16,6 +16,7 @@ import org.openzen.zenscript.codemodel.Module;
16 16
 import org.openzen.zenscript.codemodel.PackageDefinitions;
17 17
 import org.openzen.zenscript.codemodel.ScriptBlock;
18 18
 import org.openzen.zenscript.codemodel.annotations.AnnotationProcessor;
19
+import org.openzen.zenscript.codemodel.context.ModuleContext;
19 20
 import org.openzen.zenscript.codemodel.context.ModuleTypeResolutionContext;
20 21
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
21 22
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
@@ -137,6 +138,10 @@ public class SemanticModule {
137 138
 		}
138 139
 	}
139 140
 	
141
+	public ModuleContext getContext() {
142
+		return new ModuleContext(compilationUnit.globalTypeRegistry, module, expansions, rootPackage);
143
+	}
144
+	
140 145
 	public enum State {
141 146
 		INVALID,
142 147
 		ASSEMBLED,

+ 2
- 4
Constructor/src/main/java/org/openzen/zenscript/constructor/Project.java Parādīt failu

@@ -9,10 +9,8 @@ import java.io.File;
9 9
 import java.io.IOException;
10 10
 import java.util.ArrayList;
11 11
 import java.util.List;
12
-import java.util.function.Consumer;
13 12
 import org.json.JSONArray;
14 13
 import org.json.JSONObject;
15
-import org.openzen.zencode.shared.CompileException;
16 14
 import org.openzen.zenscript.constructor.module.DirectoryModuleReference;
17 15
 import org.openzen.zenscript.constructor.module.ModuleReference;
18 16
 import org.openzen.zenscript.compiler.Target;
@@ -38,9 +36,9 @@ public class Project {
38 36
 		if (!directory.isDirectory())
39 37
 			throw new ConstructorException("Project directory isn't a directory");
40 38
 		
41
-		File projectFile = new File(directory, "project.json");
39
+		File projectFile = new File(directory, "project.zcp");
42 40
 		if (!projectFile.exists())
43
-			throw new ConstructorException("Missing project.json file in project directory");
41
+			throw new ConstructorException("Missing project.zcp file in project directory");
44 42
 		
45 43
 		JSONObject json = JSONUtils.load(projectFile);
46 44
 		

+ 20
- 0
IDE/build.gradle Parādīt failu

@@ -17,3 +17,23 @@ dependencies {
17 17
 	compile project(':DrawableGui')
18 18
 	compile project(':Constructor')
19 19
 }
20
+
21
+task fatJar(type: Jar) {
22
+	manifest {
23
+        attributes 'Implementation-Title': 'ZenCode IDE',
24
+			'Class-Path': '.',
25
+        	'Main-Class': 'org.openzen.zenscript.ide.Main'
26
+    }
27
+    baseName = project.name + '-all'
28
+    from {
29
+		configurations.compile.collect {
30
+			it.isDirectory() ? it : zipTree(it)
31
+		}
32
+	} {
33
+		exclude "META-INF/INDEX.LIST"
34
+		exclude "META-INF/*.SF"
35
+        exclude "META-INF/*.DSA"
36
+        exclude "META-INF/*.RSA"
37
+	}
38
+    with jar
39
+}

+ 3
- 0
IDE/src/main/java/org/openzen/zenscript/ide/Arguments.java Parādīt failu

@@ -28,6 +28,9 @@ public class Arguments {
28 28
 			positional++;
29 29
 		}
30 30
 		
31
+		if (projectDir.isFile()) // means we're opening a project file instead of a directory
32
+			projectDir = projectDir.getParentFile();
33
+		
31 34
 		this.projectDirectory = projectDir;
32 35
 	}
33 36
 }

+ 1
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/BaseMemberCompiler.java Parādīt failu

@@ -5,7 +5,6 @@
5 5
  */
6 6
 package org.openzen.zenscript.javasource;
7 7
 
8
-import java.util.Collections;
9 8
 import org.openzen.zenscript.codemodel.FunctionHeader;
10 9
 import org.openzen.zenscript.codemodel.FunctionParameter;
11 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
@@ -94,7 +93,7 @@ public abstract class BaseMemberCompiler implements MemberVisitor<Void> {
94 93
 			output.append(";\n");
95 94
 		} else {
96 95
 			if (!(body instanceof BlockStatement))
97
-				body = new BlockStatement(body.position, Collections.singletonList(body));
96
+				body = new BlockStatement(body.position, new Statement[] { body });
98 97
 			
99 98
 			JavaSourceStatementScope scope = new JavaSourceStatementScope(this.scope, settings, header, indent + settings.indent, null, null, expansionTarget != null);
100 99
 			body.accept(new JavaSourceStatementCompiler(scope, output, true, false));

+ 2
- 2
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaDefinitionVisitor.java Parādīt failu

@@ -6,7 +6,6 @@
6 6
 package org.openzen.zenscript.javasource;
7 7
 
8 8
 import java.util.ArrayList;
9
-import java.util.Collections;
10 9
 import java.util.List;
11 10
 import java.util.Map;
12 11
 import org.openzen.zencode.shared.CodePosition;
@@ -27,6 +26,7 @@ import org.openzen.zenscript.codemodel.member.EnumConstantMember;
27 26
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
28 27
 import org.openzen.zenscript.codemodel.member.ImplementationMember;
29 28
 import org.openzen.zenscript.codemodel.statement.BlockStatement;
29
+import org.openzen.zenscript.codemodel.statement.Statement;
30 30
 import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
31 31
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
32 32
 import org.openzen.zenscript.codemodel.type.ITypeID;
@@ -385,7 +385,7 @@ public class JavaDefinitionVisitor implements DefinitionVisitor<Void> {
385 385
 			
386 386
 			if (definition.isDestructible() && !memberCompiler.hasDestructor) {
387 387
 				DestructorMember emptyDestructor = new DestructorMember(CodePosition.BUILTIN, definition, 0);
388
-				emptyDestructor.body = new BlockStatement(CodePosition.BUILTIN, Collections.emptyList());
388
+				emptyDestructor.body = new BlockStatement(CodePosition.BUILTIN, new Statement[0]);
389 389
 				memberCompiler.visitDestructor(emptyDestructor);
390 390
 			}
391 391
 			

+ 1
- 1
JavaSourceCompiler/src/main/java/org/openzen/zenscript/javasource/JavaMemberCompiler.java Parādīt failu

@@ -172,7 +172,7 @@ public class JavaMemberCompiler extends BaseMemberCompiler {
172 172
 		
173 173
 		Statement body = member.body;
174 174
 		if ((body == null || body instanceof EmptyStatement) && !(definition instanceof InterfaceDefinition))
175
-			body = new BlockStatement(member.position, Collections.emptyList());
175
+			body = new BlockStatement(member.position, new Statement[0]);
176 176
 		
177 177
 		compileBody(body, member.header);
178 178
 		return null;

+ 20
- 0
ModuleDeserializer/build.gradle Parādīt failu

@@ -0,0 +1,20 @@
1
+// Note: "common.gradle" in the root project contains additional initialization
2
+//   for this project. This initialization is applied in the "build.gradle"
3
+//   of the root project.
4
+
5
+// NetBeans will automatically add "run" and "debug" tasks relying on the
6
+// "mainClass" property. You may however define the property prior executing
7
+// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
8
+//
9
+// Note however, that you may define your own "run" and "debug" task if you
10
+// prefer. In this case NetBeans will not add these tasks but you may rely on
11
+// your own implementation.
12
+if (!hasProperty('mainClass')) {
13
+    ext.mainClass = ''
14
+}
15
+
16
+dependencies {
17
+    compile project(':CompilerShared')
18
+	compile project(':Constructor')
19
+	compile project(':ModuleSerializationShared')
20
+}

+ 969
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/CodeReader.java Parādīt failu

@@ -0,0 +1,969 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import org.openzen.zenscript.codemodel.serialization.DecodingOperation;
9
+import compactio.CompactDataInput;
10
+import java.util.ArrayList;
11
+import java.util.List;
12
+import org.openzen.zencode.shared.CodePosition;
13
+import org.openzen.zencode.shared.SourceFile;
14
+import org.openzen.zenscript.codemodel.CompareType;
15
+import org.openzen.zenscript.codemodel.FunctionHeader;
16
+import org.openzen.zenscript.codemodel.FunctionParameter;
17
+import org.openzen.zenscript.codemodel.GenericMapper;
18
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
19
+import org.openzen.zenscript.codemodel.context.StatementContext;
20
+import org.openzen.zenscript.codemodel.context.TypeContext;
21
+import org.openzen.zenscript.codemodel.definition.VariantDefinition;
22
+import org.openzen.zenscript.codemodel.expression.*;
23
+import org.openzen.zenscript.codemodel.expression.switchvalue.CharSwitchValue;
24
+import org.openzen.zenscript.codemodel.expression.switchvalue.EnumConstantSwitchValue;
25
+import org.openzen.zenscript.codemodel.expression.switchvalue.IntSwitchValue;
26
+import org.openzen.zenscript.codemodel.expression.switchvalue.StringSwitchValue;
27
+import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
28
+import org.openzen.zenscript.codemodel.expression.switchvalue.VariantOptionSwitchValue;
29
+import org.openzen.zenscript.codemodel.generic.ParameterSuperBound;
30
+import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
31
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
32
+import org.openzen.zenscript.codemodel.generic.TypeParameterBound;
33
+import org.openzen.zenscript.codemodel.member.EnumConstantMember;
34
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
35
+import org.openzen.zenscript.codemodel.member.ref.CasterMemberRef;
36
+import org.openzen.zenscript.codemodel.member.ref.ConstMemberRef;
37
+import org.openzen.zenscript.codemodel.member.ref.DefinitionMemberRef;
38
+import org.openzen.zenscript.codemodel.member.ref.FieldMemberRef;
39
+import org.openzen.zenscript.codemodel.member.ref.FunctionalMemberRef;
40
+import org.openzen.zenscript.codemodel.member.ref.GetterMemberRef;
41
+import org.openzen.zenscript.codemodel.member.ref.IteratorMemberRef;
42
+import org.openzen.zenscript.codemodel.member.ref.SetterMemberRef;
43
+import org.openzen.zenscript.codemodel.member.ref.VariantOptionRef;
44
+import org.openzen.zenscript.codemodel.serialization.CodeSerializationInput;
45
+import org.openzen.zenscript.codemodel.statement.BlockStatement;
46
+import org.openzen.zenscript.codemodel.statement.BreakStatement;
47
+import org.openzen.zenscript.codemodel.statement.ContinueStatement;
48
+import org.openzen.zenscript.codemodel.statement.DoWhileStatement;
49
+import org.openzen.zenscript.codemodel.statement.EmptyStatement;
50
+import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
51
+import org.openzen.zenscript.codemodel.statement.ForeachStatement;
52
+import org.openzen.zenscript.codemodel.statement.IfStatement;
53
+import org.openzen.zenscript.codemodel.statement.LockStatement;
54
+import org.openzen.zenscript.codemodel.statement.LoopStatement;
55
+import org.openzen.zenscript.codemodel.statement.ReturnStatement;
56
+import org.openzen.zenscript.codemodel.statement.Statement;
57
+import org.openzen.zenscript.codemodel.statement.SwitchCase;
58
+import org.openzen.zenscript.codemodel.statement.SwitchStatement;
59
+import org.openzen.zenscript.codemodel.statement.ThrowStatement;
60
+import org.openzen.zenscript.codemodel.statement.VarStatement;
61
+import org.openzen.zenscript.codemodel.statement.WhileStatement;
62
+import org.openzen.zenscript.codemodel.type.ArrayTypeID;
63
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
64
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
65
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
66
+import org.openzen.zenscript.codemodel.type.ITypeID;
67
+import org.openzen.zenscript.codemodel.type.ModifiedTypeID;
68
+import org.openzen.zenscript.codemodel.type.member.BuiltinID;
69
+import org.openzen.zenscript.codemodel.type.member.TypeMembers;
70
+import org.openzen.zenscript.moduleserialization.CodePositionEncoding;
71
+import org.openzen.zenscript.moduleserialization.ExpressionEncoding;
72
+import org.openzen.zenscript.moduleserialization.FunctionHeaderEncoding;
73
+import org.openzen.zenscript.moduleserialization.StatementEncoding;
74
+import org.openzen.zenscript.moduleserialization.SwitchValueEncoding;
75
+import org.openzen.zenscript.moduleserialization.TypeEncoding;
76
+import org.openzen.zenscript.moduleserialization.TypeParameterEncoding;
77
+
78
+/**
79
+ *
80
+ * @author Hoofdgebruiker
81
+ */
82
+public class CodeReader implements CodeSerializationInput {
83
+	private final CompactDataInput input;
84
+	
85
+	public final DecodingStage classes = new DecodingStage();
86
+	public final DecodingStage members = new DecodingStage();
87
+	public final DecodingStage code = new DecodingStage();
88
+	
89
+	private DecodingStage currentStage = null;
90
+	private CodePosition lastPosition = CodePosition.UNKNOWN;
91
+	
92
+	private final String[] strings;
93
+	private final SourceFile[] sourceFiles;
94
+	private final List<HighLevelDefinition> definitions = new ArrayList<>();
95
+	private final List<IDefinitionMember> memberList = new ArrayList<>();
96
+	private final List<EnumConstantMember> enumConstantMembers = new ArrayList<>();
97
+	private final List<VariantDefinition.Option> variantOptions = new ArrayList<>();
98
+	
99
+	private final GlobalTypeRegistry registry;
100
+	
101
+	public CodeReader(
102
+			CompactDataInput input,
103
+			String[] strings,
104
+			SourceFile[] sourceFiles,
105
+			GlobalTypeRegistry registry)
106
+	{
107
+		this.input = input;
108
+		
109
+		this.strings = strings;
110
+		this.sourceFiles = sourceFiles;
111
+		this.registry = registry;
112
+	}
113
+	
114
+	public void add(HighLevelDefinition definition) {
115
+		definitions.add(definition);
116
+	}
117
+	
118
+	public void add(IDefinitionMember member) {
119
+		memberList.add(member);
120
+	}
121
+	
122
+	public void startClasses() {
123
+		currentStage = classes;
124
+	}
125
+	
126
+	public void startMembers() {
127
+		currentStage = members;
128
+	}
129
+	
130
+	public void startCode() {
131
+		currentStage = code;
132
+	}
133
+
134
+	@Override
135
+	public boolean readBool() {
136
+		return input.readBool();
137
+	}
138
+
139
+	@Override
140
+	public int readByte() {
141
+		return input.readByte();
142
+	}
143
+
144
+	@Override
145
+	public byte readSByte() {
146
+		return input.readSByte();
147
+	}
148
+
149
+	@Override
150
+	public short readShort() {
151
+		return input.readShort();
152
+	}
153
+
154
+	@Override
155
+	public int readUShort() {
156
+		return input.readUShort();
157
+	}
158
+
159
+	@Override
160
+	public int readInt() {
161
+		return input.readInt();
162
+	}
163
+
164
+	@Override
165
+	public int readUInt() {
166
+		return input.readUInt();
167
+	}
168
+
169
+	@Override
170
+	public long readLong() {
171
+		return input.readLong();
172
+	}
173
+
174
+	@Override
175
+	public long readULong() {
176
+		return input.readULong();
177
+	}
178
+	
179
+	@Override
180
+	public float readFloat() {
181
+		return input.readFloat();
182
+	}
183
+	
184
+	@Override
185
+	public double readDouble() {
186
+		return input.readDouble();
187
+	}
188
+
189
+	@Override
190
+	public char readChar() {
191
+		return input.readChar();
192
+	}
193
+
194
+	@Override
195
+	public String readString() {
196
+		return strings[input.readUInt()];
197
+	}
198
+
199
+	@Override
200
+	public HighLevelDefinition readDefinition() {
201
+		return definitions.get(input.readUInt());
202
+	}
203
+
204
+	@Override
205
+	public DefinitionMemberRef readMember(TypeContext context, ITypeID type) {
206
+		int memberId = input.readVarUInt();
207
+		if (memberId == 0) {
208
+			return null;
209
+		} else if (memberId == 1) {
210
+			BuiltinID builtin = BuiltinID.get(input.readVarUInt());
211
+			TypeMembers members = context.getTypeMembers(type);
212
+			DefinitionMemberRef result = members.getBuiltin(builtin);
213
+			if (result == null)
214
+				throw new IllegalArgumentException("Could not find builtin member for " + builtin);
215
+			
216
+			return result;
217
+		} else {
218
+			return memberList.get(memberId - 2).ref(type, context.getMapper());
219
+		}
220
+	}
221
+	
222
+	@Override
223
+	public EnumConstantMember readEnumConstant(TypeContext context) {
224
+		return enumConstantMembers.get(readUInt());
225
+	}
226
+	
227
+	@Override
228
+	public VariantOptionRef readVariantOption(TypeContext context, ITypeID type) {
229
+		return variantOptions.get(readUInt()).instance(type, context.getMapper());
230
+	}
231
+
232
+	@Override
233
+	public ITypeID deserializeType(TypeContext context) {
234
+		int type = input.readVarUInt();
235
+		switch (type) {
236
+			case TypeEncoding.TYPE_NONE:
237
+				return null;
238
+			case TypeEncoding.TYPE_VOID:
239
+				return BasicTypeID.VOID;
240
+			case TypeEncoding.TYPE_BOOL:
241
+				return BasicTypeID.BOOL;
242
+			case TypeEncoding.TYPE_BYTE:
243
+				return BasicTypeID.BYTE;
244
+			case TypeEncoding.TYPE_SBYTE:
245
+				return BasicTypeID.SBYTE;
246
+			case TypeEncoding.TYPE_SHORT:
247
+				return BasicTypeID.SHORT;
248
+			case TypeEncoding.TYPE_USHORT:
249
+				return BasicTypeID.USHORT;
250
+			case TypeEncoding.TYPE_INT:
251
+				return BasicTypeID.INT;
252
+			case TypeEncoding.TYPE_UINT:
253
+				return BasicTypeID.UINT;
254
+			case TypeEncoding.TYPE_LONG:
255
+				return BasicTypeID.LONG;
256
+			case TypeEncoding.TYPE_ULONG:
257
+				return BasicTypeID.ULONG;
258
+			case TypeEncoding.TYPE_USIZE:
259
+				return BasicTypeID.USIZE;
260
+			case TypeEncoding.TYPE_FLOAT:
261
+				return BasicTypeID.FLOAT;
262
+			case TypeEncoding.TYPE_DOUBLE:
263
+				return BasicTypeID.DOUBLE;
264
+			case TypeEncoding.TYPE_CHAR:
265
+				return BasicTypeID.CHAR;
266
+			case TypeEncoding.TYPE_STRING:
267
+				return BasicTypeID.STRING;
268
+			case TypeEncoding.TYPE_UNDETERMINED:
269
+				return BasicTypeID.UNDETERMINED;
270
+			case TypeEncoding.TYPE_DEFINITION: {
271
+				HighLevelDefinition definition = readDefinition();
272
+				ITypeID[] arguments = new ITypeID[definition.typeParameters.length];
273
+				for (int i = 0; i < arguments.length; i++)
274
+					arguments[i] = deserializeType(context);
275
+				return registry.getForDefinition(definition, arguments);
276
+			}
277
+			case TypeEncoding.TYPE_DEFINITION_INNER: {
278
+				DefinitionTypeID outer = (DefinitionTypeID)deserializeType(context);
279
+				HighLevelDefinition definition = readDefinition();
280
+				ITypeID[] arguments = new ITypeID[definition.typeParameters.length];
281
+				for (int i = 0; i < arguments.length; i++)
282
+					arguments[i] = deserializeType(context);
283
+				return registry.getForDefinition(definition, arguments, outer);
284
+			}
285
+			case TypeEncoding.TYPE_GENERIC:
286
+				return registry.getGeneric(context.getTypeParameter(input.readVarUInt()));
287
+			case TypeEncoding.TYPE_FUNCTION:
288
+				return registry.getFunction(deserializeHeader(context));
289
+			case TypeEncoding.TYPE_ARRAY:
290
+				return registry.getArray(deserializeType(context), 1);
291
+			case TypeEncoding.TYPE_ARRAY_MULTIDIMENSIONAL: {
292
+				ITypeID baseType = deserializeType(context);
293
+				int dimension = input.readVarUInt();
294
+				return registry.getArray(baseType, dimension);
295
+			}
296
+			case TypeEncoding.TYPE_ASSOC: {
297
+				ITypeID key = deserializeType(context);
298
+				ITypeID value = deserializeType(context);
299
+				return registry.getAssociative(key, value);
300
+			}
301
+			case TypeEncoding.TYPE_GENERIC_MAP: {
302
+				TypeParameter parameter = deserializeTypeParameter(context);
303
+				TypeContext inner = new TypeContext(context, context.thisType, parameter);
304
+				ITypeID value = deserializeType(inner);
305
+				return registry.getGenericMap(value, parameter);
306
+			}
307
+			case TypeEncoding.TYPE_RANGE:
308
+				return registry.getRange(deserializeType(context));
309
+			case TypeEncoding.TYPE_ITERATOR: {
310
+				ITypeID[] values = new ITypeID[input.readVarUInt()];
311
+				for (int i = 0; i < values.length; i++)
312
+					values[i] = deserializeType(context);
313
+				return registry.getIterator(values);
314
+			}
315
+			case TypeEncoding.TYPE_OPTIONAL:
316
+				return registry.getOptional(deserializeType(context));
317
+			case TypeEncoding.TYPE_CONST:
318
+				return registry.getModified(ModifiedTypeID.MODIFIER_CONST, deserializeType(context));
319
+			case TypeEncoding.TYPE_IMMUTABLE:
320
+				return registry.getModified(ModifiedTypeID.MODIFIER_IMMUTABLE, deserializeType(context));
321
+			default:
322
+				throw new IllegalArgumentException("Unknown type: " + type);
323
+		}
324
+	}
325
+
326
+	@Override
327
+	public CodePosition deserializePosition() {
328
+		int flags = input.readVarUInt();
329
+		SourceFile file = lastPosition.file;
330
+		int fromLine = lastPosition.fromLine;
331
+		int fromLineOffset = lastPosition.fromLineOffset;
332
+		if ((flags & CodePositionEncoding.FLAG_FILE) > 0)
333
+			file = sourceFiles[input.readVarUInt()];
334
+		if ((flags & CodePositionEncoding.FLAG_FROM_LINE) > 0)
335
+			fromLine = input.readVarUInt();
336
+		if ((flags & CodePositionEncoding.FLAG_FROM_OFFSET) > 0)
337
+			fromLineOffset = input.readVarUInt();
338
+		int toLine = (flags & CodePositionEncoding.FLAG_TO_LINE) == 0 ? lastPosition.toLine : fromLine + input.readVarUInt();
339
+		int toLineOffset = (flags & CodePositionEncoding.FLAG_TO_OFFSET) == 0 ? lastPosition.toLineOffset : input.readVarUInt();
340
+		return lastPosition = new CodePosition(file, fromLine, fromLineOffset, toLine, toLineOffset);
341
+	}
342
+
343
+	@Override
344
+	public FunctionHeader deserializeHeader(TypeContext context) {
345
+		TypeParameter[] typeParameters = TypeParameter.NONE;
346
+		
347
+		int flags = input.readVarUInt();
348
+		TypeContext inner;
349
+		if ((flags & FunctionHeaderEncoding.FLAG_TYPE_PARAMETERS) > 0) {
350
+			typeParameters = deserializeTypeParameters(context);
351
+			inner = new TypeContext(context, context.thisType, typeParameters);
352
+		} else {
353
+			inner = context;
354
+		}
355
+		
356
+		ITypeID returnType = BasicTypeID.VOID;
357
+		if ((flags & FunctionHeaderEncoding.FLAG_RETURN_TYPE) > 0)
358
+			returnType = deserializeType(inner);
359
+		
360
+		ITypeID thrownType = null;
361
+		if ((flags & FunctionHeaderEncoding.FLAG_THROWS) > 0)
362
+			thrownType = deserializeType(inner);
363
+		
364
+		FunctionParameter[] parameters = FunctionParameter.NONE;
365
+		if ((flags & FunctionHeaderEncoding.FLAG_PARAMETERS) > 0) {
366
+			parameters = new FunctionParameter[readUInt()];
367
+			StatementContext statementContext = new StatementContext(context);
368
+			for (int i = 0; i < parameters.length; i++) {
369
+				ITypeID type = deserializeType(inner);
370
+				String name = readString();
371
+				FunctionParameter parameter = new FunctionParameter(type, name, null, (i == parameters.length - 1) && ((flags & FunctionHeaderEncoding.FLAG_VARIADIC) > 0));
372
+				parameters[i] = parameter;
373
+				
374
+				if ((flags & FunctionHeaderEncoding.FLAG_DEFAULT_VALUES) > 0) {
375
+					if (currentStage == code) {
376
+						parameters[i].defaultValue = deserializeExpression(statementContext);
377
+					} else {
378
+						enqueueCode(decoder -> parameter.defaultValue = deserializeExpression(statementContext));
379
+					}
380
+				}
381
+			}
382
+		}
383
+		return new FunctionHeader(typeParameters, returnType, thrownType, parameters);
384
+	}
385
+	
386
+	@Override
387
+	public TypeParameter deserializeTypeParameter(TypeContext context) {
388
+		int flags = readUInt();
389
+		CodePosition position = CodePosition.UNKNOWN;
390
+		String name = null;
391
+		if ((flags & TypeParameterEncoding.FLAG_POSITION) > 0)
392
+			position = deserializePosition();
393
+		if ((flags & TypeParameterEncoding.FLAG_NAME) > 0)
394
+			name = readString();
395
+		
396
+		TypeParameter result = new TypeParameter(position, name);
397
+		TypeContext inner = new TypeContext(context, context.thisType, result);
398
+		if ((flags & TypeParameterEncoding.FLAG_BOUNDS) > 0) {
399
+			int bounds = readUInt();
400
+			for (int i = 0; i < bounds; i++)
401
+				result.bounds.add(deserializeTypeParameterBound(inner));
402
+		}
403
+		
404
+		return result;
405
+	}
406
+	
407
+	@Override
408
+	public TypeParameter[] deserializeTypeParameters(TypeContext context) {
409
+		TypeParameter[] result = new TypeParameter[readUInt()];
410
+		int[] allflags = new int[result.length];
411
+		for (int i = 0; i < result.length; i++) {
412
+			int flags = readUInt();
413
+			allflags[i] = flags;
414
+			
415
+			CodePosition position = CodePosition.UNKNOWN;
416
+			String name = null;
417
+			if ((flags & TypeParameterEncoding.FLAG_POSITION) > 0)
418
+				position = deserializePosition();
419
+			if ((flags & TypeParameterEncoding.FLAG_NAME) > 0)
420
+				name = readString();
421
+
422
+			result[i] = new TypeParameter(position, name);
423
+		}
424
+		
425
+		TypeContext inner = new TypeContext(context, context.thisType, result);
426
+		for (int i = 0; i < result.length; i++) {
427
+			int flags = allflags[i];
428
+			if ((flags & TypeParameterEncoding.FLAG_BOUNDS) > 0) {
429
+				int bounds = readUInt();
430
+				for (int j = 0; j < bounds; j++)
431
+					result[i].bounds.add(deserializeTypeParameterBound(inner));
432
+			}
433
+		}
434
+		
435
+		return result;
436
+	}
437
+
438
+	@Override
439
+	public CallArguments deserializeArguments(StatementContext context) {
440
+		ITypeID[] typeArguments = new ITypeID[readUInt()];
441
+		for (int i = 0; i < typeArguments.length; i++)
442
+			typeArguments[i] = deserializeType(context);
443
+		
444
+		Expression[] arguments = new Expression[readUInt()];
445
+		for (int i = 0; i < arguments.length; i++)
446
+			arguments[i] = deserializeExpression(context);
447
+		
448
+		return new CallArguments(typeArguments, arguments);
449
+	}
450
+
451
+	@Override
452
+	public Statement deserializeStatement(StatementContext context) {
453
+		int type = readUInt();
454
+		int flags = type == StatementEncoding.TYPE_NULL ? 0 :  readUInt();
455
+		CodePosition position = (flags & StatementEncoding.FLAG_POSITION) > 0 ? deserializePosition() : CodePosition.UNKNOWN;
456
+		switch (type) {
457
+			case StatementEncoding.TYPE_NULL:
458
+				return null;
459
+			case StatementEncoding.TYPE_BLOCK: {
460
+				Statement[] statements = new Statement[readUInt()];
461
+				StatementContext inner = new StatementContext(context);
462
+				for (int i = 0; i < statements.length; i++)
463
+					statements[i] = deserializeStatement(inner);
464
+				return new BlockStatement(position, statements);
465
+			}
466
+			case StatementEncoding.TYPE_BREAK: {
467
+				LoopStatement loop = context.getLoop(readUInt());
468
+				return new BreakStatement(position, loop);
469
+			}
470
+			case StatementEncoding.TYPE_CONTINUE: {
471
+				LoopStatement loop = context.getLoop(readUInt());
472
+				return new ContinueStatement(position, loop);
473
+			}
474
+			case StatementEncoding.TYPE_DO_WHILE: {
475
+				Expression condition = deserializeExpression(context);
476
+				String label = null;
477
+				if ((flags & StatementEncoding.FLAG_LABEL) > 0)
478
+					label = readString();
479
+				DoWhileStatement loop = new DoWhileStatement(position, label, condition);
480
+				StatementContext inner = new StatementContext(context, loop);
481
+				loop.content = deserializeStatement(inner);
482
+				return loop;
483
+			}
484
+			case StatementEncoding.TYPE_EMPTY:
485
+				return new EmptyStatement(position);
486
+			case StatementEncoding.TYPE_EXPRESSION: {
487
+				Expression expression = deserializeExpression(context);
488
+				return new ExpressionStatement(position, expression);
489
+			}
490
+			case StatementEncoding.TYPE_FOREACH: {
491
+				Expression list = deserializeExpression(context);
492
+				IteratorMemberRef iterator = (IteratorMemberRef)readMember(context, list.type);
493
+				VarStatement[] loopVariables = new VarStatement[iterator.getLoopVariableCount()];
494
+				for (int i = 0; i < loopVariables.length; i++) {
495
+					String name = ((flags & StatementEncoding.FLAG_NAME) > 0) ? readString() : null;
496
+					loopVariables[i] = new VarStatement(position, name, iterator.types[i], null, true);
497
+				}
498
+				ForeachStatement loop = new ForeachStatement(position, loopVariables, iterator, list);
499
+				StatementContext inner = new StatementContext(context, loop);
500
+				for (VarStatement variable : loopVariables)
501
+					inner.add(variable);
502
+				
503
+				loop.content = deserializeStatement(inner);
504
+				return loop;
505
+			}
506
+			case StatementEncoding.TYPE_IF: {
507
+				Expression condition = deserializeExpression(context);
508
+				Statement onThen = deserializeStatement(context);
509
+				Statement onElse = deserializeStatement(context);
510
+				return new IfStatement(position, condition, onThen, onElse);
511
+			}
512
+			case StatementEncoding.TYPE_LOCK: {
513
+				Expression object = deserializeExpression(context);
514
+				Statement content = deserializeStatement(context);
515
+				return new LockStatement(position, object, content);
516
+			}
517
+			case StatementEncoding.TYPE_RETURN: {
518
+				Expression value = deserializeExpression(context);
519
+				return new ReturnStatement(position, value);
520
+			}
521
+			case StatementEncoding.TYPE_SWITCH: {
522
+				Expression value = deserializeExpression(context);
523
+				String label = ((flags & StatementEncoding.FLAG_LABEL) > 0) ? readString() : null;
524
+				SwitchStatement statement = new SwitchStatement(position, label, value);
525
+				StatementContext inner = new StatementContext(context, statement);
526
+				
527
+				int numberOfCases = readUInt();
528
+				for (int i = 0; i < numberOfCases; i++) {
529
+					SwitchValue switchValue = deserializeSwitchValue(inner, (flags & StatementEncoding.FLAG_NAME) > 0);
530
+					if (switchValue instanceof VariantOptionSwitchValue)
531
+						context.variantOptionSwitchValue = (VariantOptionSwitchValue)switchValue;
532
+					
533
+					Statement[] statements = new Statement[readUInt()];
534
+					for (int j = 0; j < statements.length; j++)
535
+						statements[j] = deserializeStatement(inner);
536
+					statement.cases.add(new SwitchCase(switchValue, statements));
537
+				}
538
+				
539
+				return statement;
540
+			}
541
+			case StatementEncoding.TYPE_THROW: {
542
+				Expression value = deserializeExpression(context);
543
+				return new ThrowStatement(position, value);
544
+			}
545
+			case StatementEncoding.TYPE_TRY_CATCH:
546
+				throw new UnsupportedOperationException("Not supported yet");
547
+			case StatementEncoding.TYPE_VAR: {
548
+				ITypeID varType = deserializeType(context);
549
+				String name = (flags & StatementEncoding.FLAG_NAME) > 0 ? readString() : null;
550
+				Expression initializer = deserializeExpression(context);
551
+				VarStatement result = new VarStatement(position, name, varType, initializer, (flags & StatementEncoding.FLAG_FINAL) > 0);
552
+				context.add(result);
553
+				return result;
554
+			}
555
+			case StatementEncoding.TYPE_WHILE: {
556
+				Expression condition = deserializeExpression(context);
557
+				String label = (flags & StatementEncoding.FLAG_LABEL) > 0 ? readString() : null;
558
+				WhileStatement loop = new WhileStatement(position, label, condition);
559
+				StatementContext inner = new StatementContext(context, loop);
560
+				loop.content = deserializeStatement(inner);
561
+				return loop;
562
+			}
563
+			default:
564
+				throw new IllegalArgumentException("Unknown statement type: " + type);
565
+		}
566
+	}
567
+
568
+	@Override
569
+	public Expression deserializeExpression(StatementContext context) {
570
+		int kind = readUInt();
571
+		int flags = kind == ExpressionEncoding.TYPE_NONE ? 0 : readUInt();
572
+		CodePosition position = (flags & ExpressionEncoding.FLAG_POSITION) > 0 ? deserializePosition() : CodePosition.UNKNOWN;
573
+		switch (kind) {
574
+			case ExpressionEncoding.TYPE_NONE:
575
+				return null;
576
+			case ExpressionEncoding.TYPE_AND_AND: {
577
+				Expression left = deserializeExpression(context);
578
+				Expression right = deserializeExpression(context);
579
+				return new AndAndExpression(position, left, right);
580
+			}
581
+			case ExpressionEncoding.TYPE_ARRAY: {
582
+				ITypeID type = deserializeType(context);
583
+				Expression[] expressions = new Expression[readUInt()];
584
+				return new ArrayExpression(position, expressions, (ArrayTypeID)type);
585
+			}
586
+			case ExpressionEncoding.TYPE_COMPARE: {
587
+				CompareType comparison = readComparator();
588
+				Expression left = deserializeExpression(context);
589
+				Expression right = deserializeExpression(context);
590
+				FunctionalMemberRef operator = (FunctionalMemberRef)readMember(context, left.type);
591
+				return new CompareExpression(position, left, right, operator, comparison);
592
+			}
593
+			case ExpressionEncoding.TYPE_CALL: {
594
+				Expression target = deserializeExpression(context);
595
+				FunctionalMemberRef member = (FunctionalMemberRef)readMember(context, target.type);
596
+				CallArguments arguments = deserializeArguments(context);
597
+				FunctionHeader instancedHeader = member.getHeader().instanceForCall(registry, arguments);
598
+				return new CallExpression(position, target, member, instancedHeader, arguments);
599
+			}
600
+			case ExpressionEncoding.TYPE_CALL_STATIC: {
601
+				ITypeID type = deserializeType(context);
602
+				FunctionalMemberRef member = (FunctionalMemberRef)readMember(context, type);
603
+				CallArguments arguments = deserializeArguments(context);
604
+				FunctionHeader instancedHeader = member.getHeader().instanceForCall(registry, arguments);
605
+				return new CallStaticExpression(position, type, member, instancedHeader, arguments);
606
+			}
607
+			case ExpressionEncoding.TYPE_CAPTURED_CLOSURE: {
608
+				Expression value = deserializeExpression(context.getLambdaOuter());
609
+				return new CapturedClosureExpression(position, (CapturedExpression)value, context.getLambdaClosure());
610
+			}
611
+			case ExpressionEncoding.TYPE_CAPTURED_DIRECT: {
612
+				Expression value = deserializeExpression(context.getLambdaOuter());
613
+				return new CapturedDirectExpression(position, context.getLambdaClosure(), value);
614
+			}
615
+			case ExpressionEncoding.TYPE_CAPTURED_LOCAL_VARIABLE: {
616
+				VarStatement var = context.getLambdaOuter().getVariable(readUInt());
617
+				return new CapturedLocalVariableExpression(position, var, context.getLambdaClosure());
618
+			}
619
+			case ExpressionEncoding.TYPE_CAPTURED_PARAMETER: {
620
+				FunctionParameter parameter = context.getLambdaOuter().getParameter(readUInt());
621
+				return new CapturedParameterExpression(position, parameter, context.getLambdaClosure());
622
+			}
623
+			case ExpressionEncoding.TYPE_CAPTURED_THIS: {
624
+				return new CapturedThisExpression(position, context.thisType, context.getLambdaClosure());
625
+			}
626
+			case ExpressionEncoding.TYPE_CAST: {
627
+				ITypeID toType = deserializeType(context);
628
+				Expression value = deserializeExpression(context);
629
+				CasterMemberRef member = (CasterMemberRef)readMember(context, toType);
630
+				return new CastExpression(position, value, member, (flags & ExpressionEncoding.FLAG_IMPLICIT) > 0);
631
+			}
632
+			case ExpressionEncoding.TYPE_CHECKNULL: {
633
+				Expression value = deserializeExpression(context);
634
+				return new CheckNullExpression(position, value);
635
+			}
636
+			case ExpressionEncoding.TYPE_COALESCE: {
637
+				Expression left = deserializeExpression(context);
638
+				Expression right = deserializeExpression(context);
639
+				return new CoalesceExpression(position, left, right);
640
+			}
641
+			case ExpressionEncoding.TYPE_CONDITIONAL: {
642
+				Expression condition = deserializeExpression(context);
643
+				ITypeID type = deserializeType(context);
644
+				Expression onThen = deserializeExpression(context);
645
+				Expression onElse = deserializeExpression(context);
646
+				return new ConditionalExpression(position, condition, onThen, onElse, type);
647
+			}
648
+			case ExpressionEncoding.TYPE_CONST: {
649
+				ITypeID type = deserializeType(context);
650
+				ConstMemberRef member = (ConstMemberRef)readMember(context, type);
651
+				return new ConstExpression(position, member);
652
+			}
653
+			case ExpressionEncoding.TYPE_CONSTANT_BOOL_TRUE:
654
+				return new ConstantBoolExpression(position, true);
655
+			case ExpressionEncoding.TYPE_CONSTANT_BOOL_FALSE:
656
+				return new ConstantBoolExpression(position, false);
657
+			case ExpressionEncoding.TYPE_CONSTANT_BYTE:
658
+				return new ConstantByteExpression(position, readByte());
659
+			case ExpressionEncoding.TYPE_CONSTANT_CHAR:
660
+				return new ConstantCharExpression(position, readChar());
661
+			case ExpressionEncoding.TYPE_CONSTANT_DOUBLE:
662
+				return new ConstantDoubleExpression(position, readDouble());
663
+			case ExpressionEncoding.TYPE_CONSTANT_FLOAT:
664
+				return new ConstantFloatExpression(position, readFloat());
665
+			case ExpressionEncoding.TYPE_CONSTANT_INT:
666
+				return new ConstantIntExpression(position, readInt());
667
+			case ExpressionEncoding.TYPE_CONSTANT_LONG:
668
+				return new ConstantLongExpression(position, readLong());
669
+			case ExpressionEncoding.TYPE_CONSTANT_SBYTE:
670
+				return new ConstantSByteExpression(position, readSByte());
671
+			case ExpressionEncoding.TYPE_CONSTANT_SHORT:
672
+				return new ConstantShortExpression(position, readShort());
673
+			case ExpressionEncoding.TYPE_CONSTANT_STRING:
674
+				return new ConstantStringExpression(position, readString());
675
+			case ExpressionEncoding.TYPE_CONSTANT_UINT:
676
+				return new ConstantUIntExpression(position, readUInt());
677
+			case ExpressionEncoding.TYPE_CONSTANT_ULONG:
678
+				return new ConstantULongExpression(position, readULong());
679
+			case ExpressionEncoding.TYPE_CONSTANT_USHORT:
680
+				return new ConstantUShortExpression(position, readUShort());
681
+			case ExpressionEncoding.TYPE_CONSTANT_USIZE:
682
+				return new ConstantUSizeExpression(position, readULong());
683
+			case ExpressionEncoding.TYPE_CONSTRUCTOR_THIS_CALL: {
684
+				FunctionalMemberRef constructor = (FunctionalMemberRef)readMember(context, context.thisType);
685
+				CallArguments arguments = deserializeArguments(context);
686
+				return new ConstructorThisCallExpression(position, context.thisType, constructor, arguments);
687
+			}
688
+			case ExpressionEncoding.TYPE_CONSTRUCTOR_SUPER_CALL: {
689
+				ITypeID superType = context.thisType.getSuperType(registry);
690
+				FunctionalMemberRef constructor = (FunctionalMemberRef)readMember(context, superType);
691
+				CallArguments arguments = deserializeArguments(context);
692
+				return new ConstructorSuperCallExpression(position, superType, constructor, arguments);
693
+			}
694
+			case ExpressionEncoding.TYPE_ENUM_CONSTANT: {
695
+				EnumConstantMember constant = readEnumConstant(context);
696
+				return new EnumConstantExpression(position, registry.getForDefinition(constant.definition), constant);
697
+			}
698
+			case ExpressionEncoding.TYPE_FUNCTION: {
699
+				FunctionHeader header = deserializeHeader(context);
700
+				LambdaClosure closure = new LambdaClosure();
701
+				StatementContext inner = new StatementContext(context, header, closure);
702
+				Statement body = deserializeStatement(inner);
703
+				return new FunctionExpression(position, registry.getFunction(header), closure, header, body);
704
+			}
705
+			case ExpressionEncoding.TYPE_GET_FIELD: {
706
+				Expression target = deserializeExpression(context);
707
+				FieldMemberRef field = (FieldMemberRef)readMember(context, target.type);
708
+				return new GetFieldExpression(position, target, field);
709
+			}
710
+			case ExpressionEncoding.TYPE_GET_FUNCTION_PARAMETER: {
711
+				FunctionParameter parameter = context.getParameter(readUInt());
712
+				return new GetFunctionParameterExpression(position, parameter);
713
+			}
714
+			case ExpressionEncoding.TYPE_GET_LOCAL_VARIABLE: {
715
+				VarStatement variable = context.getVariable(readUInt());
716
+				return new GetLocalVariableExpression(position, variable);
717
+			}
718
+			case ExpressionEncoding.TYPE_GET_MATCHING_VARIANT_FIELD: {
719
+				return new GetMatchingVariantField(position, context.variantOptionSwitchValue, kind);
720
+			}
721
+			case ExpressionEncoding.TYPE_GET_STATIC_FIELD: {
722
+				ITypeID type = deserializeType(context);
723
+				FieldMemberRef field = (FieldMemberRef)readMember(context, type);
724
+				return new GetStaticFieldExpression(position, field);
725
+			}
726
+			case ExpressionEncoding.TYPE_GETTER: {
727
+				Expression target = deserializeExpression(context);
728
+				GetterMemberRef getter = (GetterMemberRef)readMember(context, target.type);
729
+				return new GetterExpression(position, target, getter);
730
+			}
731
+			case ExpressionEncoding.TYPE_GLOBAL: {
732
+				String name = readString();
733
+				Expression resolution = deserializeExpression(context);
734
+				return new GlobalExpression(position, name, resolution);
735
+			}
736
+			case ExpressionEncoding.TYPE_GLOBAL_CALL: {
737
+				String name = readString();
738
+				CallArguments arguments = deserializeArguments(context);
739
+				Expression resolution = deserializeExpression(context);
740
+				return new GlobalCallExpression(position, name, arguments, resolution);
741
+			}
742
+			case ExpressionEncoding.TYPE_INTERFACE_CAST: {
743
+				Expression value = deserializeExpression(context);
744
+				ITypeID toType = deserializeType(context);
745
+				return new InterfaceCastExpression(position, value, toType);
746
+			}
747
+			case ExpressionEncoding.TYPE_IS: {
748
+				Expression value = deserializeExpression(context);
749
+				ITypeID type = deserializeType(context);
750
+				return new IsExpression(position, value, type);
751
+			}
752
+			case ExpressionEncoding.TYPE_MAKE_CONST: {
753
+				Expression value = deserializeExpression(context);
754
+				ITypeID constType = registry.getModified(ModifiedTypeID.MODIFIER_CONST, value.type);
755
+				return new MakeConstExpression(position, value, constType);
756
+			}
757
+			case ExpressionEncoding.TYPE_MAP: {
758
+				ITypeID type = deserializeType(context);
759
+				Expression[] keys = new Expression[readUInt()];
760
+				Expression[] values = new Expression[keys.length];
761
+				for (int i = 0; i < keys.length; i++) {
762
+					keys[i] = deserializeExpression(context);
763
+					values[i] = deserializeExpression(context);
764
+				}
765
+				return new MapExpression(position, keys, values, type);
766
+			}
767
+			case ExpressionEncoding.TYPE_MATCH: {
768
+				boolean localVariableNames = (flags & ExpressionEncoding.FLAG_NAMES) > 0;
769
+				
770
+				Expression value = deserializeExpression(context);
771
+				ITypeID type = deserializeType(context);
772
+				MatchExpression.Case[] cases = new MatchExpression.Case[readUInt()];
773
+				for (int i = 0; i < cases.length; i++) {
774
+					SwitchValue key = deserializeSwitchValue(context, localVariableNames);
775
+					Expression caseValue = deserializeExpression(context);
776
+					cases[i] = new MatchExpression.Case(key, caseValue);
777
+				}
778
+				return new MatchExpression(position, value, type, cases);
779
+			}
780
+			case ExpressionEncoding.TYPE_NEW: {
781
+				ITypeID type = deserializeType(context);
782
+				FunctionalMemberRef member = (FunctionalMemberRef)readMember(context, type);
783
+				CallArguments arguments = deserializeArguments(context);
784
+				return new NewExpression(position, type, member, arguments);
785
+			}
786
+			case ExpressionEncoding.TYPE_NULL: {
787
+				ITypeID type = deserializeType(context);
788
+				return new NullExpression(position, type);
789
+			}
790
+			case ExpressionEncoding.TYPE_OR_OR: {
791
+				Expression left = deserializeExpression(context);
792
+				Expression right = deserializeExpression(context);
793
+				return new OrOrExpression(position, left, right);
794
+			}
795
+			case ExpressionEncoding.TYPE_PANIC: {
796
+				Expression value = deserializeExpression(context);
797
+				ITypeID type = deserializeType(context);
798
+				return new PanicExpression(position, type, value);
799
+			}
800
+			case ExpressionEncoding.TYPE_POST_CALL: {
801
+				Expression target = deserializeExpression(context);
802
+				FunctionalMemberRef member = (FunctionalMemberRef)readMember(context, target.type);
803
+				return new PostCallExpression(position, target, member, member.getHeader());
804
+			}
805
+			case ExpressionEncoding.TYPE_RANGE: {
806
+				Expression from = deserializeExpression(context);
807
+				Expression to = deserializeExpression(context);
808
+				return new RangeExpression(position, registry.getRange(from.type), from, to);
809
+			}
810
+			case ExpressionEncoding.TYPE_SAME_OBJECT: {
811
+				Expression left = deserializeExpression(context);
812
+				Expression right = deserializeExpression(context);
813
+				boolean inverted = (flags & ExpressionEncoding.FLAG_INVERTED) > 0;
814
+				return new SameObjectExpression(position, left, right, inverted);
815
+			}
816
+			case ExpressionEncoding.TYPE_SET_FIELD: {
817
+				Expression target = deserializeExpression(context);
818
+				FieldMemberRef field = (FieldMemberRef)readMember(context, target.type);
819
+				Expression value = deserializeExpression(context);
820
+				return new SetFieldExpression(position, target, field, value);
821
+			}
822
+			case ExpressionEncoding.TYPE_SET_FUNCTION_PARAMETER: {
823
+				FunctionParameter parameter = context.getParameter(readUInt());
824
+				Expression value = deserializeExpression(context);
825
+				return new SetFunctionParameterExpression(position, parameter, value);
826
+			}
827
+			case ExpressionEncoding.TYPE_SET_LOCAL_VARIABLE: {
828
+				VarStatement variable = context.getVariable(readUInt());
829
+				Expression value = deserializeExpression(context);
830
+				return new SetLocalVariableExpression(position, variable, value);
831
+			}
832
+			case ExpressionEncoding.TYPE_SET_STATIC_FIELD: {
833
+				ITypeID type = deserializeType(context);
834
+				FieldMemberRef member = (FieldMemberRef)readMember(context, type);
835
+				Expression value = deserializeExpression(context);
836
+				return new SetStaticFieldExpression(position, member, value);
837
+			}
838
+			case ExpressionEncoding.TYPE_SETTER: {
839
+				Expression target = deserializeExpression(context);
840
+				SetterMemberRef setter = (SetterMemberRef)readMember(context, target.type);
841
+				Expression value = deserializeExpression(context);
842
+				return new SetterExpression(position, target, setter, value);
843
+			}
844
+			case ExpressionEncoding.TYPE_STATIC_GETTER: {
845
+				ITypeID target = deserializeType(context);
846
+				GetterMemberRef getter = (GetterMemberRef)readMember(context, target);
847
+				return new StaticGetterExpression(position, getter);
848
+			}
849
+			case ExpressionEncoding.TYPE_STATIC_SETTER: {
850
+				ITypeID target = deserializeType(context);
851
+				SetterMemberRef setter = (SetterMemberRef)readMember(context, target);
852
+				Expression value = deserializeExpression(context);
853
+				return new StaticSetterExpression(position, setter, value);
854
+			}
855
+			case ExpressionEncoding.TYPE_SUPERTYPE_CAST: {
856
+				ITypeID type = deserializeType(context);
857
+				Expression value = deserializeExpression(context);
858
+				return new SupertypeCastExpression(position, value, type);
859
+			}
860
+			case ExpressionEncoding.TYPE_THIS:
861
+				return new ThisExpression(position, context.thisType);
862
+			case ExpressionEncoding.TYPE_THROW: {
863
+				ITypeID type = deserializeType(context);
864
+				Expression value = deserializeExpression(context);
865
+				return new ThrowExpression(position, type, value);
866
+			}
867
+			case ExpressionEncoding.TYPE_TRY_CONVERT: {
868
+				ITypeID type = deserializeType(context);
869
+				Expression value = deserializeExpression(context);
870
+				return new TryConvertExpression(position, type, value);
871
+			}
872
+			case ExpressionEncoding.TYPE_TRY_RETHROW_AS_EXCEPTION: {
873
+				ITypeID type = deserializeType(context);
874
+				ITypeID thrownType = deserializeType(context);
875
+				Expression value = deserializeExpression(context);
876
+				return new TryRethrowAsExceptionExpression(position, type, value, thrownType);
877
+			}
878
+			case ExpressionEncoding.TYPE_TRY_RETHROW_AS_RESULT: {
879
+				ITypeID type = deserializeType(context);
880
+				Expression value = deserializeExpression(context);
881
+				return new TryRethrowAsResultExpression(position, type, value);
882
+			}
883
+			case ExpressionEncoding.TYPE_VARIANT_VALUE: {
884
+				ITypeID type = deserializeType(context);
885
+				VariantOptionRef option = readVariantOption(context, type);
886
+				Expression[] arguments = new Expression[option.types.length];
887
+				for (int i = 0; i < arguments.length; i++)
888
+					arguments[i] = deserializeExpression(context);
889
+				return new VariantValueExpression(position, type, option, arguments);
890
+			}
891
+			case ExpressionEncoding.TYPE_WRAP_OPTIONAL: {
892
+				Expression value = deserializeExpression(context);
893
+				ITypeID optionalType = registry.getOptional(value.type);
894
+				return new WrapOptionalExpression(position, value, optionalType);
895
+			}
896
+			default:
897
+				throw new IllegalArgumentException("Not a valid expression type: " + kind);
898
+		}
899
+	}
900
+	
901
+	private CompareType readComparator() {
902
+		int type = readUInt();
903
+		switch (type) {
904
+			case ExpressionEncoding.COMPARATOR_LT:
905
+				return CompareType.LT;
906
+			case ExpressionEncoding.COMPARATOR_GT:
907
+				return CompareType.GT;
908
+			case ExpressionEncoding.COMPARATOR_EQ:
909
+				return CompareType.EQ;
910
+			case ExpressionEncoding.COMPARATOR_NE:
911
+				return CompareType.NE;
912
+			case ExpressionEncoding.COMPARATOR_LE:
913
+				return CompareType.LE;
914
+			case ExpressionEncoding.COMPARATOR_GE:
915
+				return CompareType.GE;
916
+			default:
917
+				throw new IllegalArgumentException("Invalid comparator: " + type);
918
+		}
919
+	}
920
+	
921
+	private SwitchValue deserializeSwitchValue(StatementContext context, boolean localVariableNames) {
922
+		int type = readUInt();
923
+		switch (type) {
924
+			case SwitchValueEncoding.TYPE_NULL:
925
+				return null;
926
+			case SwitchValueEncoding.TYPE_INT:
927
+				return new IntSwitchValue(readInt());
928
+			case SwitchValueEncoding.TYPE_CHAR:
929
+				return new CharSwitchValue(readChar());
930
+			case SwitchValueEncoding.TYPE_STRING:
931
+				return new StringSwitchValue(readString());
932
+			case SwitchValueEncoding.TYPE_ENUM:
933
+				return new EnumConstantSwitchValue(readEnumConstant(context));
934
+			case SwitchValueEncoding.TYPE_VARIANT_OPTION: {
935
+				ITypeID t = deserializeType(context);
936
+				VariantOptionRef option = readVariantOption(context, t);
937
+				String[] names = new String[option.types.length];
938
+				if (localVariableNames)
939
+					for (int i = 0; i < names.length; i++)
940
+						names[i] = readString();
941
+				return new VariantOptionSwitchValue(option, names);
942
+			}
943
+			default:
944
+				throw new IllegalArgumentException("Unknown switch value type: " + type);
945
+		}
946
+	}
947
+	
948
+	@Override
949
+	public void enqueueMembers(DecodingOperation operation) {
950
+		members.enqueue(operation);
951
+	}
952
+	
953
+	@Override
954
+	public void enqueueCode(DecodingOperation operation) {
955
+		code.enqueue(operation);
956
+	}
957
+	
958
+	private TypeParameterBound deserializeTypeParameterBound(TypeContext context) {
959
+		int type = readUInt();
960
+		switch (type) {
961
+			case TypeParameterEncoding.TYPE_SUPER_BOUND:
962
+				return new ParameterSuperBound(deserializeType(context));
963
+			case TypeParameterEncoding.TYPE_TYPE_BOUND:
964
+				return new ParameterTypeBound(CodePosition.UNKNOWN, deserializeType(context));
965
+			default:
966
+				throw new IllegalArgumentException("Not a valid parameter bound type: " + type);
967
+		}
968
+	}
969
+}

+ 33
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DecodingStage.java Parādīt failu

@@ -0,0 +1,33 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import org.openzen.zenscript.codemodel.serialization.DecodingOperation;
9
+import java.util.ArrayList;
10
+import java.util.List;
11
+
12
+/**
13
+ *
14
+ * @author Hoofdgebruiker
15
+ */
16
+public class DecodingStage {
17
+	private final List<DecodingOperation> operations = new ArrayList<>();
18
+	private boolean locked = false;
19
+	
20
+	public void decode(CodeReader encoder) {
21
+		locked = true;
22
+		for (DecodingOperation operation : operations) {
23
+			operation.decode(encoder);
24
+		}
25
+	}
26
+	
27
+	public void enqueue(DecodingOperation operation) {
28
+		if (locked)
29
+			throw new IllegalStateException("Encoding stage is locked!");
30
+		
31
+		operations.add(operation);
32
+	}
33
+}

+ 69
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DefinitionMemberDeserializer.java Parādīt failu

@@ -0,0 +1,69 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import org.openzen.zenscript.codemodel.context.TypeContext;
9
+import org.openzen.zenscript.codemodel.definition.AliasDefinition;
10
+import org.openzen.zenscript.codemodel.definition.ClassDefinition;
11
+import org.openzen.zenscript.codemodel.definition.DefinitionVisitorWithContext;
12
+import org.openzen.zenscript.codemodel.definition.EnumDefinition;
13
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
14
+import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
15
+import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
16
+import org.openzen.zenscript.codemodel.definition.StructDefinition;
17
+import org.openzen.zenscript.codemodel.definition.VariantDefinition;
18
+
19
+/**
20
+ *
21
+ * @author Hoofdgebruiker
22
+ */
23
+public class DefinitionMemberDeserializer implements DefinitionVisitorWithContext<TypeContext, Void> {
24
+	private final CodeReader reader;
25
+	
26
+	public DefinitionMemberDeserializer(CodeReader reader) {
27
+		this.reader = reader;
28
+	}
29
+
30
+	@Override
31
+	public Void visitClass(TypeContext context, ClassDefinition definition) {
32
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
33
+	}
34
+
35
+	@Override
36
+	public Void visitInterface(TypeContext context, InterfaceDefinition definition) {
37
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
38
+	}
39
+
40
+	@Override
41
+	public Void visitEnum(TypeContext context, EnumDefinition definition) {
42
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
43
+	}
44
+
45
+	@Override
46
+	public Void visitStruct(TypeContext context, StructDefinition definition) {
47
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
48
+	}
49
+
50
+	@Override
51
+	public Void visitFunction(TypeContext context, FunctionDefinition definition) {
52
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
53
+	}
54
+
55
+	@Override
56
+	public Void visitExpansion(TypeContext context, ExpansionDefinition definition) {
57
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
58
+	}
59
+
60
+	@Override
61
+	public Void visitAlias(TypeContext context, AliasDefinition definition) {
62
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
63
+	}
64
+
65
+	@Override
66
+	public Void visitVariant(TypeContext context, VariantDefinition variant) {
67
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
68
+	}
69
+}

+ 16
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DeserializationException.java Parādīt failu

@@ -0,0 +1,16 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class DeserializationException extends Exception {
13
+	public DeserializationException(String reason) {
14
+		super(reason);
15
+	}
16
+}

+ 99
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/DeserializingModule.java Parādīt failu

@@ -0,0 +1,99 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import org.openzen.zenscript.codemodel.Module;
11
+import org.openzen.zenscript.codemodel.PackageDefinitions;
12
+import org.openzen.zenscript.codemodel.ScriptBlock;
13
+import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
14
+import org.openzen.zenscript.codemodel.context.ModuleContext;
15
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
16
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
17
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
18
+import org.openzen.zenscript.compiler.CompilationUnit;
19
+import org.openzen.zenscript.compiler.SemanticModule;
20
+
21
+/**
22
+ *
23
+ * @author Hoofdgebruiker
24
+ */
25
+public class DeserializingModule {
26
+	public final String name;
27
+	public final Module module;
28
+	
29
+	private final SemanticModule loaded;
30
+	private final DeserializingModule[] dependencies;
31
+	private final ZSPackage rootPackage;
32
+	private final ZSPackage modulePackage;
33
+	
34
+	private final PackageDefinitions definitions = new PackageDefinitions();
35
+	private final List<ScriptBlock> scripts = new ArrayList<>();
36
+	private final AnnotationDefinition[] annotations;
37
+	
38
+	private final List<ExpansionDefinition> expansions;
39
+	public final ModuleContext context;
40
+	
41
+	public DeserializingModule(
42
+			String name,
43
+			GlobalTypeRegistry registry,
44
+			DeserializingModule[] dependencies,
45
+			ZSPackage rootPackage,
46
+			ZSPackage modulePackage,
47
+			AnnotationDefinition[] annotations)
48
+	{
49
+		this.name = name;
50
+		this.module = new Module(name);
51
+		this.loaded = null;
52
+		this.dependencies = dependencies;
53
+		this.rootPackage = rootPackage;
54
+		this.modulePackage = modulePackage;
55
+		this.annotations = annotations;
56
+		
57
+		expansions = new ArrayList<>();
58
+		context = new ModuleContext(registry, expansions, rootPackage);
59
+	}
60
+	
61
+	public DeserializingModule(SemanticModule module) 
62
+	{
63
+		this.name = module.name;
64
+		this.module = module.module;
65
+		this.loaded = module;
66
+		this.dependencies = null;
67
+		this.rootPackage = module.rootPackage;
68
+		this.modulePackage = module.modulePackage;
69
+		this.annotations = module.annotations;
70
+		
71
+		expansions = module.expansions;
72
+		context = module.getContext();
73
+	}
74
+	
75
+	public boolean hasCode() {
76
+		return module == null;
77
+	}
78
+	
79
+	public SemanticModule load(CompilationUnit compilationUnit) {
80
+		if (loaded != null)
81
+			return loaded;
82
+		
83
+		SemanticModule[] dependencies = new SemanticModule[this.dependencies.length];
84
+		List<ExpansionDefinition> expansions = new ArrayList<>();
85
+		
86
+		return new SemanticModule(
87
+				name,
88
+				module,
89
+				dependencies,
90
+				SemanticModule.State.ASSEMBLED,
91
+				rootPackage,
92
+				modulePackage,
93
+				definitions,
94
+				scripts,
95
+				compilationUnit,
96
+				expansions,
97
+				annotations);
98
+	}
99
+}

+ 38
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/Main.java Parādīt failu

@@ -0,0 +1,38 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import java.io.File;
9
+import java.io.IOException;
10
+import java.nio.file.Files;
11
+import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
12
+import org.openzen.zenscript.codemodel.annotations.NativeAnnotationDefinition;
13
+import org.openzen.zenscript.codemodel.annotations.PreconditionAnnotationDefinition;
14
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
15
+import org.openzen.zenscript.compiler.CompilationUnit;
16
+import org.openzen.zenscript.compiler.ModuleRegistry;
17
+import org.openzen.zenscript.compiler.SemanticModule;
18
+
19
+/**
20
+ *
21
+ * @author Hoofdgebruiker
22
+ */
23
+public class Main {
24
+	public static void main(String[] args) throws IOException, DeserializationException {
25
+		byte[] data = Files.readAllBytes(new File("../ModuleSerializer/stdlib.bzm").toPath());
26
+		
27
+		ZSPackage rootPackage = ZSPackage.createRoot();
28
+		AnnotationDefinition[] annotations = new AnnotationDefinition[] {
29
+			NativeAnnotationDefinition.INSTANCE,
30
+			PreconditionAnnotationDefinition.INSTANCE
31
+		};
32
+		CompilationUnit compilationUnit = new CompilationUnit();
33
+		ModuleRegistry modules = new ModuleRegistry();
34
+		ModuleDeserializer deserializer = new ModuleDeserializer(modules, compilationUnit, annotations, rootPackage);
35
+		SemanticModule[] libs = deserializer.deserialize(data);
36
+		
37
+	}
38
+}

+ 203
- 0
ModuleDeserializer/src/main/java/org/openzen/zenscript/moduledeserializer/ModuleDeserializer.java Parādīt failu

@@ -0,0 +1,203 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.moduledeserializer;
7
+
8
+import compactio.CompactBytesDataInput;
9
+import compactio.CompactDataInput;
10
+import java.util.Arrays;
11
+import org.openzen.zencode.shared.CodePosition;
12
+import org.openzen.zencode.shared.SourceFile;
13
+import org.openzen.zencode.shared.VirtualSourceFile;
14
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
15
+import org.openzen.zenscript.codemodel.annotations.AnnotationDefinition;
16
+import org.openzen.zenscript.codemodel.context.ModuleContext;
17
+import org.openzen.zenscript.codemodel.context.TypeContext;
18
+import org.openzen.zenscript.codemodel.definition.ClassDefinition;
19
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
20
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
21
+import org.openzen.zenscript.codemodel.serialization.CodeSerializationInput;
22
+import org.openzen.zenscript.codemodel.serialization.DecodingOperation;
23
+import org.openzen.zenscript.compiler.CompilationUnit;
24
+import org.openzen.zenscript.compiler.ModuleRegistry;
25
+import org.openzen.zenscript.compiler.SemanticModule;
26
+import org.openzen.zenscript.moduleserialization.DefinitionEncoding;
27
+
28
+/**
29
+ *
30
+ * @author Hoofdgebruiker
31
+ */
32
+public class ModuleDeserializer {
33
+	private final ModuleRegistry modules;
34
+	private final CompilationUnit compilationUnit;
35
+	private final AnnotationDefinition[] annotations;
36
+	private final ZSPackage rootPackage;
37
+	
38
+	public ModuleDeserializer(
39
+			ModuleRegistry modules,
40
+			CompilationUnit compilationUnit,
41
+			AnnotationDefinition[] annotations,
42
+			ZSPackage rootPackage)
43
+	{
44
+		this.modules = modules;
45
+		this.compilationUnit = compilationUnit;
46
+		this.annotations = annotations;
47
+		this.rootPackage = rootPackage;
48
+	}
49
+	
50
+	public SemanticModule[] deserialize(byte[] data) throws DeserializationException {
51
+		CompactDataInput input = new CompactBytesDataInput(data);
52
+		if (input.readInt() != 0x5A43424D)
53
+			throw new DeserializationException("Invalid marker; not a binary module");
54
+		
55
+		int version = input.readVarUInt();
56
+		if (version != 0)
57
+			throw new DeserializationException("Unsupported version: " + version);
58
+		
59
+		String[] stringTable = input.readStringArray();
60
+		SourceFile[] sourceFiles = new SourceFile[input.readVarUInt()];
61
+		for (int i = 0; i < sourceFiles.length; i++)
62
+			sourceFiles[i] = new VirtualSourceFile(input.readString());
63
+		
64
+		CodeReader decoder = new CodeReader(
65
+				input,
66
+				stringTable,
67
+				sourceFiles,
68
+				compilationUnit.globalTypeRegistry);
69
+		
70
+		DeserializingModule[] packagedModules = new DeserializingModule[input.readVarUInt()];
71
+		String[][] dependencyNames = new String[packagedModules.length][];
72
+		for (int i = 0; i < packagedModules.length; i++) {
73
+			int flags = input.readVarUInt();
74
+			String name = input.readString();
75
+			
76
+			ZSPackage modulePackage = rootPackage;
77
+			int packageNameParts = input.readVarUInt();
78
+			for (int j = 0; j < packageNameParts; j++)
79
+				modulePackage = modulePackage.getOrCreatePackage(input.readString());
80
+
81
+			DeserializingModule[] dependencies = new DeserializingModule[input.readVarUInt()];
82
+			String[] dependencyNames2 = new String[dependencies.length];
83
+			dependencyNames[i] = dependencyNames2;
84
+			for (int j = 0; j < dependencyNames2.length; j++)
85
+				dependencyNames2[j] = input.readString();
86
+			
87
+			packagedModules[i] = new DeserializingModule(
88
+					name,
89
+					compilationUnit.globalTypeRegistry,
90
+					dependencies,
91
+					rootPackage,
92
+					modulePackage,
93
+					annotations);
94
+			decoder.code.enqueue(new ModuleDecodeScriptsOperation(packagedModules[i]));
95
+			decoder.classes.enqueue(new ModuleDecodeClassesOperation(packagedModules[i], decoder));
96
+		}
97
+		
98
+		DeserializingModule[] allModules = Arrays.copyOf(packagedModules, input.readVarUInt());
99
+		for (int i = packagedModules.length; i < allModules.length; i++) {
100
+			int flags = input.readVarUInt();
101
+			String name = input.readString();
102
+			allModules[i] = new DeserializingModule(modules.load(name));
103
+			decoder.classes.enqueue(new ModuleDecodeClassesOperation(allModules[i], decoder));
104
+		}
105
+		
106
+		System.out.println("Decoding classes");
107
+		decoder.startClasses();
108
+		decoder.classes.decode(decoder);
109
+		System.out.println("Decoding members");
110
+		decoder.startMembers();
111
+		decoder.members.decode(decoder);
112
+		System.out.println("Decoding code");
113
+		decoder.startCode();
114
+		decoder.code.decode(decoder);
115
+		
116
+		SemanticModule[] results = new SemanticModule[packagedModules.length];
117
+		for (int i = 0; i < results.length; i++) {
118
+			DeserializingModule module = packagedModules[i];
119
+			results[i] = module.load(compilationUnit);
120
+		}
121
+		return results;
122
+	}
123
+	
124
+	private class ModuleDecodeClassesOperation implements DecodingOperation {
125
+		private final DeserializingModule module;
126
+		private final CodeReader reader;
127
+		
128
+		public ModuleDecodeClassesOperation(DeserializingModule module, CodeReader reader) {
129
+			this.module = module;
130
+			this.reader = reader;
131
+		}
132
+
133
+		@Override
134
+		public void decode(CodeSerializationInput input) {
135
+			int numDefinitions = input.readUInt();
136
+			DefinitionMemberDeserializer memberDeserializer = new DefinitionMemberDeserializer(reader);
137
+			for (int i = 0; i < numDefinitions; i++) {
138
+				HighLevelDefinition definition = deserializeDefinition(reader, module.context);
139
+				reader.add(definition);
140
+				
141
+				TypeContext typeContext = new TypeContext(module.context, definition.typeParameters, module.context.registry.getForMyDefinition(definition));
142
+				reader.members.enqueue(in -> definition.accept(typeContext, memberDeserializer));
143
+			}
144
+		}
145
+		
146
+		private HighLevelDefinition deserializeDefinition(CodeReader reader, ModuleContext context) {
147
+			int type = reader.readUInt();
148
+			int flags = reader.readUInt();
149
+			CodePosition position = CodePosition.UNKNOWN;
150
+			String name = null;
151
+			TypeParameter[] typeParameters = TypeParameter.NONE;
152
+			if ((flags & DefinitionEncoding.FLAG_POSITION) > 0)
153
+				position = reader.deserializePosition();
154
+			ZSPackage pkg = context.root.getRecursive(reader.readString());
155
+			if ((flags & DefinitionEncoding.FLAG_NAME) > 0)
156
+				name = reader.readString();
157
+			if ((flags & DefinitionEncoding.FLAG_TYPE_PARAMETERS) > 0)
158
+				typeParameters = reader.deserializeTypeParameters(new TypeContext(context, TypeParameter.NONE, null));
159
+			
160
+			switch (type) { 
161
+				case DefinitionEncoding.TYPE_CLASS: {
162
+					ClassDefinition result = new ClassDefinition(position, context.module, pkg, name, flags);
163
+					decodeMembers(reader, context, result);
164
+					return result;
165
+				}
166
+				case DefinitionEncoding.TYPE_STRUCT:
167
+				case DefinitionEncoding.TYPE_INTERFACE:
168
+				case DefinitionEncoding.TYPE_ENUM:
169
+				case DefinitionEncoding.TYPE_VARIANT:
170
+				case DefinitionEncoding.TYPE_FUNCTION:
171
+				case DefinitionEncoding.TYPE_ALIAS:
172
+				case DefinitionEncoding.TYPE_EXPANSION:
173
+			}
174
+		}
175
+	}
176
+	
177
+	private void decodeMembers(
178
+			CodeReader reader,
179
+			ModuleContext moduleContext,
180
+			HighLevelDefinition definition)
181
+	{
182
+		reader.enqueueMembers(input -> {
183
+			TypeContext context = new TypeContext(moduleContext, definition.typeParameters, moduleContext.registry.getForMyDefinition(definition));
184
+			definition.addMember(reader.deserializeMember(context));
185
+		});
186
+		
187
+		int innerMembers = reader.readUInt();
188
+		
189
+	}
190
+	
191
+	private class ModuleDecodeScriptsOperation implements DecodingOperation {
192
+		private final DeserializingModule module;
193
+		
194
+		public ModuleDecodeScriptsOperation(DeserializingModule module) {
195
+			this.module = module;
196
+		}
197
+
198
+		@Override
199
+		public void decode(CodeSerializationInput input) {
200
+			
201
+		}
202
+	}
203
+}

+ 2
- 0
ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/ExpressionEncoding.java Parādīt failu

@@ -14,6 +14,8 @@ public class ExpressionEncoding {
14 14
 	
15 15
 	public static final int FLAG_POSITION = 1;
16 16
 	public static final int FLAG_IMPLICIT = 2; // for cast expressions
17
+	public static final int FLAG_NAMES = 4;
18
+	public static final int FLAG_INVERTED = 8;
17 19
 	
18 20
 	public static final int TYPE_NONE = 0;
19 21
 	public static final int TYPE_AND_AND = 1;

+ 4
- 3
ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/FunctionHeaderEncoding.java Parādīt failu

@@ -14,7 +14,8 @@ public class FunctionHeaderEncoding {
14 14
 	
15 15
 	public static final int FLAG_TYPE_PARAMETERS = 1;
16 16
 	public static final int FLAG_RETURN_TYPE = 2;
17
-	public static final int FLAG_PARAMETERS = 4;
18
-	public static final int FLAG_VARIADIC = 8;
19
-	public static final int FLAG_DEFAULT_VALUES = 16;
17
+	public static final int FLAG_THROWS = 4;
18
+	public static final int FLAG_PARAMETERS = 8;
19
+	public static final int FLAG_VARIADIC = 16;
20
+	public static final int FLAG_DEFAULT_VALUES = 32;
20 21
 }

+ 2
- 0
ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/StatementEncoding.java Parādīt failu

@@ -15,6 +15,8 @@ public class StatementEncoding {
15 15
 	public static final int FLAG_POSITION = 1;
16 16
 	public static final int FLAG_ANNOTATIONS = 2;
17 17
 	public static final int FLAG_FINAL = 4;
18
+	public static final int FLAG_NAME = 8;
19
+	public static final int FLAG_LABEL = 16;
18 20
 	
19 21
 	public static final int TYPE_NULL = 0;
20 22
 	public static final int TYPE_BLOCK = 1;

+ 12
- 11
ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/TypeEncoding.java Parādīt failu

@@ -31,15 +31,16 @@ public class TypeEncoding {
31 31
 	public static final int TYPE_STRING = 16;
32 32
 	public static final int TYPE_UNDETERMINED = 17;
33 33
 	public static final int TYPE_DEFINITION = 18;
34
-	public static final int TYPE_GENERIC = 19;
35
-	public static final int TYPE_FUNCTION = 20;
36
-	public static final int TYPE_ARRAY = 21;
37
-	public static final int TYPE_ARRAY_MULTIDIMENSIONAL = 22;
38
-	public static final int TYPE_ASSOC = 23;
39
-	public static final int TYPE_GENERIC_MAP = 24;
40
-	public static final int TYPE_RANGE = 25;
41
-	public static final int TYPE_ITERATOR = 26;
42
-	public static final int TYPE_OPTIONAL = 27;
43
-	public static final int TYPE_CONST = 28;
44
-	public static final int TYPE_IMMUTABLE = 29;
34
+	public static final int TYPE_DEFINITION_INNER = 19;
35
+	public static final int TYPE_GENERIC = 20;
36
+	public static final int TYPE_FUNCTION = 21;
37
+	public static final int TYPE_ARRAY = 22;
38
+	public static final int TYPE_ARRAY_MULTIDIMENSIONAL = 23;
39
+	public static final int TYPE_ASSOC = 24;
40
+	public static final int TYPE_GENERIC_MAP = 25;
41
+	public static final int TYPE_RANGE = 26;
42
+	public static final int TYPE_ITERATOR = 27;
43
+	public static final int TYPE_OPTIONAL = 28;
44
+	public static final int TYPE_CONST = 29;
45
+	public static final int TYPE_IMMUTABLE = 30;
45 46
 }

+ 1
- 0
ModuleSerializationShared/src/main/java/org/openzen/zenscript/moduleserialization/TypeParameterEncoding.java Parādīt failu

@@ -14,6 +14,7 @@ public class TypeParameterEncoding {
14 14
 	
15 15
 	public static final int FLAG_POSITION = 1;
16 16
 	public static final int FLAG_NAME = 2;
17
+	public static final int FLAG_BOUNDS = 4;
17 18
 	
18 19
 	public static final int TYPE_TYPE_BOUND = 1;
19 20
 	public static final int TYPE_SUPER_BOUND = 2;

ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/ModuleEncoder.java → ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/CodeWriter.java Parādīt failu

@@ -47,7 +47,7 @@ import org.openzen.zenscript.moduleserializer.encoder.TypeSerializer;
47 47
  *
48 48
  * @author Hoofdgebruiker
49 49
  */
50
-public class ModuleEncoder implements CodeSerializationOutput {
50
+public class CodeWriter implements CodeSerializationOutput {
51 51
 	private final Map<String, Integer> stringMap = new HashMap<>();
52 52
 	private final Map<SourceFile, Integer> sourceFileMap = new HashMap<>();
53 53
 	
@@ -71,7 +71,7 @@ public class ModuleEncoder implements CodeSerializationOutput {
71 71
 	
72 72
 	private CodePosition lastPosition = CodePosition.UNKNOWN;
73 73
 	
74
-	public ModuleEncoder(
74
+	public CodeWriter(
75 75
 			CompactDataOutput output,
76 76
 			SerializationOptions options,
77 77
 			String[] strings,
@@ -82,9 +82,9 @@ public class ModuleEncoder implements CodeSerializationOutput {
82 82
 		typeParameterEncoder = new TypeParameterBoundSerializer(this);
83 83
 		memberSerializer = new MemberSerializer(this, options);
84 84
 		typeSerializer = new TypeSerializer(this);
85
-		statementSerializer = new StatementSerializer(this, options.positions);
86
-		expressionSerializer = new ExpressionSerializer(this, options.expressionPositions);
87
-		switchValueSerializer = new SwitchValueSerializer(this);
85
+		statementSerializer = new StatementSerializer(this, options.positions, options.localVariableNames);
86
+		expressionSerializer = new ExpressionSerializer(this, options.expressionPositions, options.localVariableNames);
87
+		switchValueSerializer = new SwitchValueSerializer(this, options.localVariableNames);
88 88
 		
89 89
 		for (String string : strings)
90 90
 			stringMap.put(string, stringMap.size());
@@ -206,13 +206,11 @@ public class ModuleEncoder implements CodeSerializationOutput {
206 206
 			return;
207 207
 		} else if (member.getTarget().getBuiltin() != null) {
208 208
 			writeUInt(1);
209
-			serialize(context, member.getType());
210 209
 			writeUInt(member.getTarget().getBuiltin().ordinal()); // TODO: use something else?
211 210
 			return;
212 211
 		}
213 212
 		
214 213
 		IDefinitionMember member_ = member.getTarget();
215
-		write(member_.getDefinition());
216 214
 		EncodingDefinition definition = member_.getDefinition().getTag(EncodingDefinition.class);
217 215
 		
218 216
 		int index = definition.members.indexOf(member_);
@@ -246,18 +244,37 @@ public class ModuleEncoder implements CodeSerializationOutput {
246 244
 	
247 245
 	@Override
248 246
 	public void serialize(TypeContext context, TypeParameter parameter) {
249
-		int typeParameterFlags = 0;
247
+		int flags = serializeInitial(parameter);
248
+		serializeRemaining(flags, context, parameter);
249
+	}
250
+	
251
+	@Override
252
+	public void serialize(TypeContext context, TypeParameter[] parameters) {
253
+		output.writeVarUInt(parameters.length);
254
+		int[] flags = new int[parameters.length];
255
+		for (int i = 0; i < parameters.length; i++)
256
+			flags[i] = serializeInitial(parameters[i]);
257
+		for (int i = 0; i < parameters.length; i++)
258
+			serializeRemaining(flags[i], context, parameters[i]);
259
+	}
260
+	
261
+	private int serializeInitial(TypeParameter parameter) {
262
+		int flags = 0;
250 263
 		if (parameter.position != CodePosition.UNKNOWN && options.positions)
251
-			typeParameterFlags |= TypeParameterEncoding.FLAG_POSITION;
264
+			flags |= TypeParameterEncoding.FLAG_POSITION;
252 265
 		if (parameter.name != null && options.typeParameterNames)
253
-			typeParameterFlags |= TypeParameterEncoding.FLAG_NAME;
266
+			flags |= TypeParameterEncoding.FLAG_NAME;
254 267
 
255
-		output.writeVarUInt(typeParameterFlags);
256
-		if ((typeParameterFlags & TypeParameterEncoding.FLAG_POSITION) > 0)
268
+		output.writeVarUInt(flags);
269
+		if ((flags & TypeParameterEncoding.FLAG_POSITION) > 0)
257 270
 			serialize(parameter.position);
258
-		if ((typeParameterFlags & TypeParameterEncoding.FLAG_NAME) > 0)
271
+		if ((flags & TypeParameterEncoding.FLAG_NAME) > 0)
259 272
 			output.writeString(parameter.name);
260 273
 		
274
+		return flags;
275
+	}
276
+	
277
+	private void serializeRemaining(int flags, TypeContext context, TypeParameter parameter) {
261 278
 		if (currentStage == code || currentStage == members) {
262 279
 			output.writeVarUInt(parameter.bounds.size());
263 280
 			for (TypeParameterBound bound : parameter.bounds)
@@ -335,24 +352,25 @@ public class ModuleEncoder implements CodeSerializationOutput {
335 352
 		if (header.getReturnType() != BasicTypeID.VOID)
336 353
 			flags |= FunctionHeaderEncoding.FLAG_RETURN_TYPE;
337 354
 		if (header.parameters.length > 0)
355
+			flags |= FunctionHeaderEncoding.FLAG_THROWS;
356
+		if (header.hasAnyDefaultValues())
338 357
 			flags |= FunctionHeaderEncoding.FLAG_PARAMETERS;
339 358
 		if (header.parameters.length > 0 && header.parameters[header.parameters.length - 1].variadic)
340 359
 			flags |= FunctionHeaderEncoding.FLAG_VARIADIC;
341
-		if (header.hasAnyDefaultValues())
360
+		if (header.thrownType != null)
342 361
 			flags |= FunctionHeaderEncoding.FLAG_DEFAULT_VALUES;
343 362
 		
344 363
 		writeUInt(flags);
345
-		if ((flags & FunctionHeaderEncoding.FLAG_TYPE_PARAMETERS) > 0) {
346
-			writeUInt(header.typeParameters.length);
347
-			for (TypeParameter parameter : header.typeParameters) {
348
-				serialize(context, parameter);
349
-			}
350
-		}
364
+		if ((flags & FunctionHeaderEncoding.FLAG_TYPE_PARAMETERS) > 0)
365
+			serialize(context, header.typeParameters);
351 366
 		if ((flags & FunctionHeaderEncoding.FLAG_RETURN_TYPE) > 0)
352 367
 			serialize(context, header.getReturnType());
368
+		if ((flags & FunctionHeaderEncoding.FLAG_THROWS) > 0)
369
+			serialize(context, header.thrownType);
353 370
 		
354 371
 		if ((flags & FunctionHeaderEncoding.FLAG_PARAMETERS) > 0) {
355
-			StatementContext statementContext = new StatementContext(header);
372
+			StatementContext statementContext = new StatementContext(context);
373
+			writeUInt(header.parameters.length);
356 374
 			for (FunctionParameter parameter : header.parameters) {
357 375
 				// TODO: annotations
358 376
 				serialize(context, parameter.type);

+ 4
- 1
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/EncodingModule.java Parādīt failu

@@ -8,6 +8,7 @@ package org.openzen.zenscript.moduleserializer;
8 8
 import java.util.ArrayList;
9 9
 import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.Module;
11
+import org.openzen.zenscript.codemodel.context.ModuleContext;
11 12
 
12 13
 /**
13 14
  *
@@ -15,11 +16,13 @@ import org.openzen.zenscript.codemodel.Module;
15 16
  */
16 17
 public class EncodingModule {
17 18
 	private final Module module;
19
+	public final ModuleContext context;
18 20
 	public final List<EncodingDefinition> definitions = new ArrayList<>();
19 21
 	public final boolean withCode;
20 22
 	
21
-	public EncodingModule(Module module, boolean withCode) {
23
+	public EncodingModule(Module module, ModuleContext context, boolean withCode) {
22 24
 		this.module = module;
25
+		this.context = context;
23 26
 		this.withCode = withCode;
24 27
 	}
25 28
 	

+ 1
- 1
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/EncodingStage.java Parādīt failu

@@ -17,7 +17,7 @@ public class EncodingStage {
17 17
 	private final List<EncodingOperation> operations = new ArrayList<>();
18 18
 	private boolean locked = false;
19 19
 	
20
-	public void encode(ModuleEncoder encoder) {
20
+	public void encode(CodeWriter encoder) {
21 21
 		locked = true;
22 22
 		for (EncodingOperation operation : operations) {
23 23
 			operation.encode(encoder);

+ 2
- 1
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/Main.java Parādīt failu

@@ -29,7 +29,8 @@ public class Main {
29 29
 				true,
30 30
 				false,
31 31
 				false,
32
-				true));
32
+				true,
33
+				false));
33 34
 		byte[] encoded = serializer.serialize(Collections.singletonList(module));
34 35
 		System.out.println("stdlib encoded as " + encoded.length + " bytes");
35 36
 		Files.write(new File("stdlib.bzm").toPath(), encoded);

+ 19
- 11
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/ModuleSerializer.java Parādīt failu

@@ -12,6 +12,7 @@ import java.util.List;
12 12
 import org.openzen.zencode.shared.SourceFile;
13 13
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
14 14
 import org.openzen.zenscript.codemodel.ScriptBlock;
15
+import org.openzen.zenscript.codemodel.context.ModuleContext;
15 16
 import org.openzen.zenscript.codemodel.statement.Statement;
16 17
 import org.openzen.zenscript.compiler.SemanticModule;
17 18
 import org.openzen.zenscript.moduleserialization.ModuleEncoding;
@@ -36,7 +37,7 @@ public class ModuleSerializer {
36 37
 		
37 38
 		List<EncodingModule> encodingModules = new ArrayList<>();
38 39
 		for (SemanticModule module : modules) {
39
-			encodingModules.add(tableBuilder.register(module.module));
40
+			encodingModules.add(tableBuilder.register(module.module, module.getContext()));
40 41
 			
41 42
 			tableBuilder.writeString(module.name);
42 43
 			for (String part : getPackageName(module.modulePackage))
@@ -44,14 +45,15 @@ public class ModuleSerializer {
44 45
 		}
45 46
 		
46 47
 		for (SemanticModule module : modules) {
47
-			EncodingModule encodedModule = tableBuilder.register(module.module);
48
+			EncodingModule encodedModule = tableBuilder.register(module.module, module.getContext());
49
+			ModuleContext moduleContext = new ModuleContext(module.compilationUnit.globalTypeRegistry, module.expansions, module.rootPackage);
48 50
 			for (HighLevelDefinition definition : module.definitions.getAll()) {
49 51
 				encodedModule.add(EncodingDefinition.complete(definition));
50
-				tableBuilder.serialize(definition);
52
+				tableBuilder.serialize(moduleContext, definition);
51 53
 			}
52 54
 			
53 55
 			for (ScriptBlock script : module.scripts) {
54
-				StatementContext context = new StatementContext();
56
+				StatementContext context = new StatementContext(moduleContext, null);
55 57
 				for (Statement statement : script.statements) {
56 58
 					tableBuilder.serialize(context, statement);
57 59
 				}
@@ -62,7 +64,7 @@ public class ModuleSerializer {
62 64
 		String[] strings = tableBuilder.getStrings();
63 65
 		
64 66
 		CompactBytesDataOutput output = new CompactBytesDataOutput();
65
-		ModuleEncoder encoder = new ModuleEncoder(
67
+		CodeWriter encoder = new CodeWriter(
66 68
 				output,
67 69
 				options,
68 70
 				strings,
@@ -91,7 +93,11 @@ public class ModuleSerializer {
91 93
 			for (SemanticModule dependency : module.dependencies)
92 94
 				output.writeString(dependency.name);
93 95
 			
94
-			encoder.code.enqueue(new ModuleEncodeScriptsOperation(module.scripts));
96
+			ModuleContext moduleContext = new ModuleContext(
97
+					module.compilationUnit.globalTypeRegistry,
98
+					module.expansions,
99
+					module.rootPackage);
100
+			encoder.code.enqueue(new ModuleEncodeScriptsOperation(moduleContext, module.scripts));
95 101
 			encoder.classes.enqueue(new ModuleEncodeClassesOperation(options, encodingModule, encoder));
96 102
 		}
97 103
 		
@@ -133,12 +139,12 @@ public class ModuleSerializer {
133 139
 	private static class ModuleEncodeClassesOperation implements EncodingOperation {
134 140
 		private final SerializationOptions options;
135 141
 		private final EncodingModule module;
136
-		private final ModuleEncoder encoder;
142
+		private final CodeWriter encoder;
137 143
 		
138 144
 		public ModuleEncodeClassesOperation(
139 145
 				SerializationOptions options,
140 146
 				EncodingModule module,
141
-				ModuleEncoder encoder)
147
+				CodeWriter encoder)
142 148
 		{
143 149
 			this.options = options;
144 150
 			this.module = module;
@@ -152,16 +158,18 @@ public class ModuleSerializer {
152 158
 			DefinitionSerializer definitionEncoder = new DefinitionSerializer(options, output);
153 159
 			for (EncodingDefinition definition : module.definitions) {
154 160
 				System.out.println("Encoding definition " + definition.definition.name);
155
-				definition.definition.accept(definitionEncoder);
161
+				definition.definition.accept(module.context, definitionEncoder);
156 162
 				encoder.add(definition.definition);
157 163
 			}
158 164
 		}
159 165
 	}
160 166
 	
161 167
 	private static class ModuleEncodeScriptsOperation implements EncodingOperation {
168
+		private final ModuleContext module;
162 169
 		private final List<ScriptBlock> scripts;
163 170
 		
164
-		public ModuleEncodeScriptsOperation(List<ScriptBlock> scripts) {
171
+		public ModuleEncodeScriptsOperation(ModuleContext module, List<ScriptBlock> scripts) {
172
+			this.module = module;
165 173
 			this.scripts = scripts;
166 174
 		}
167 175
 
@@ -170,7 +178,7 @@ public class ModuleSerializer {
170 178
 			output.writeUInt(scripts.size());
171 179
 			for (ScriptBlock script : scripts) {
172 180
 				output.writeUInt(script.statements.size());
173
-				StatementContext context = new StatementContext();
181
+				StatementContext context = new StatementContext(module, null);
174 182
 				for (Statement statement : script.statements) {
175 183
 					output.serialize(context, statement);
176 184
 				}

+ 4
- 1
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/SerializationOptions.java Parādīt failu

@@ -14,15 +14,18 @@ public class SerializationOptions {
14 14
 	public final boolean expressionPositions;
15 15
 	public final boolean positionOffsets;
16 16
 	public final boolean typeParameterNames;
17
+	public final boolean localVariableNames;
17 18
 	
18 19
 	public SerializationOptions(
19 20
 			boolean positions,
20 21
 			boolean expressionPositions,
21 22
 			boolean positionOffsets,
22
-			boolean typeParameterNames) {
23
+			boolean typeParameterNames,
24
+			boolean localVariableNames) {
23 25
 		this.positions = positions;
24 26
 		this.expressionPositions = expressionPositions;
25 27
 		this.positionOffsets = positionOffsets;
26 28
 		this.typeParameterNames = typeParameterNames;
29
+		this.localVariableNames = localVariableNames;
27 30
 	}
28 31
 }

+ 25
- 18
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/TableBuilder.java Parādīt failu

@@ -18,6 +18,7 @@ import org.openzen.zenscript.codemodel.FunctionHeader;
18 18
 import org.openzen.zenscript.codemodel.FunctionParameter;
19 19
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
20 20
 import org.openzen.zenscript.codemodel.Module;
21
+import org.openzen.zenscript.codemodel.context.ModuleContext;
21 22
 import org.openzen.zenscript.codemodel.context.StatementContext;
22 23
 import org.openzen.zenscript.codemodel.context.TypeContext;
23 24
 import org.openzen.zenscript.codemodel.expression.CallArguments;
@@ -32,9 +33,7 @@ import org.openzen.zenscript.codemodel.member.ref.VariantOptionRef;
32 33
 import org.openzen.zenscript.codemodel.serialization.CodeSerializationOutput;
33 34
 import org.openzen.zenscript.codemodel.serialization.EncodingOperation;
34 35
 import org.openzen.zenscript.codemodel.statement.Statement;
35
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
36 36
 import org.openzen.zenscript.codemodel.type.ITypeID;
37
-import org.openzen.zenscript.moduleserialization.FunctionHeaderEncoding;
38 37
 import org.openzen.zenscript.moduleserialization.TypeParameterEncoding;
39 38
 import org.openzen.zenscript.moduleserializer.encoder.DefinitionMemberSerializer;
40 39
 import org.openzen.zenscript.moduleserializer.encoder.DefinitionSerializer;
@@ -73,16 +72,16 @@ public class TableBuilder implements CodeSerializationOutput {
73 72
 		definitionSerializer = new DefinitionSerializer(options, this);
74 73
 		definitionMemberSerializer = new DefinitionMemberSerializer(options, this);
75 74
 		memberSerializer = new MemberSerializer(this, options);
76
-		switchValues = new SwitchValueSerializer(this);
77
-		statements = new StatementSerializer(this, true);
78
-		expressions = new ExpressionSerializer(this, true);
75
+		switchValues = new SwitchValueSerializer(this, options.localVariableNames);
76
+		statements = new StatementSerializer(this, options.positions, options.localVariableNames);
77
+		expressions = new ExpressionSerializer(this, options.positions, options.localVariableNames);
79 78
 		typeSerializer = new TypeSerializer(this);
80 79
 		typeParameterBoundSerializer = new TypeParameterBoundSerializer(this);
81 80
 	}
82 81
 	
83
-	public EncodingModule register(Module module) {
82
+	public EncodingModule register(Module module, ModuleContext context) {
84 83
 		if (moduleSet.add(module)) {
85
-			EncodingModule encodedModule = new EncodingModule(module, true);
84
+			EncodingModule encodedModule = new EncodingModule(module, context, true);
86 85
 			module.setTag(EncodingModule.class, encodedModule);
87 86
 			modules.add(encodedModule);
88 87
 			return encodedModule;
@@ -106,7 +105,7 @@ public class TableBuilder implements CodeSerializationOutput {
106 105
 	}
107 106
 	
108 107
 	private EncodingDefinition prepare(HighLevelDefinition definition) {
109
-		register(definition.module);
108
+		register(definition.module, null);
110 109
 		
111 110
 		if (definitions.add(definition)) {
112 111
 			EncodingDefinition result = new EncodingDefinition(definition);
@@ -120,9 +119,10 @@ public class TableBuilder implements CodeSerializationOutput {
120 119
 		}
121 120
 	}
122 121
 	
123
-	public void serialize(HighLevelDefinition definition) {
124
-		definition.accept(definitionSerializer);
125
-		definition.accept(new TypeContext(definition.typeParameters), definitionMemberSerializer);
122
+	public void serialize(ModuleContext context, HighLevelDefinition definition) {
123
+		definition.accept(context, definitionSerializer);
124
+		ITypeID thisType = context.registry.getForMyDefinition(definition);
125
+		definition.accept(new TypeContext(context, definition.typeParameters, thisType), definitionMemberSerializer);
126 126
 	}
127 127
 
128 128
 	@Override
@@ -206,14 +206,23 @@ public class TableBuilder implements CodeSerializationOutput {
206 206
 			typeParameterFlags |= TypeParameterEncoding.FLAG_POSITION;
207 207
 		if (parameter.name != null && options.typeParameterNames)
208 208
 			typeParameterFlags |= TypeParameterEncoding.FLAG_NAME;
209
+		if (!parameter.bounds.isEmpty())
210
+			typeParameterFlags |= TypeParameterEncoding.FLAG_BOUNDS;
209 211
 
210 212
 		if ((typeParameterFlags & TypeParameterEncoding.FLAG_POSITION) > 0)
211 213
 			serialize(parameter.position);
212 214
 		if ((typeParameterFlags & TypeParameterEncoding.FLAG_NAME) > 0)
213 215
 			writeString(parameter.name);
214
-		
215
-		for (TypeParameterBound bound : parameter.bounds)
216
-			bound.accept(context, typeParameterBoundSerializer);
216
+		if ((typeParameterFlags & TypeParameterEncoding.FLAG_BOUNDS) > 0) {
217
+			for (TypeParameterBound bound : parameter.bounds)
218
+				bound.accept(context, typeParameterBoundSerializer);
219
+		}
220
+	}
221
+	
222
+	@Override
223
+	public void serialize(TypeContext context, TypeParameter[] parameters) {
224
+		for (TypeParameter parameter : parameters)
225
+			serialize(context, parameter);
217 226
 	}
218 227
 
219 228
 	@Override
@@ -223,12 +232,10 @@ public class TableBuilder implements CodeSerializationOutput {
223 232
 
224 233
 	@Override
225 234
 	public void serialize(TypeContext context, FunctionHeader header) {
226
-		for (TypeParameter parameter : header.typeParameters)
227
-			serialize(context, parameter);
228
-			
235
+		serialize(context, header.typeParameters);
229 236
 		serialize(context, header.getReturnType());
230 237
 		
231
-		StatementContext statementContext = new StatementContext(header);
238
+		StatementContext statementContext = new StatementContext(context, header);
232 239
 		for (FunctionParameter parameter : header.parameters) {
233 240
 			// TODO: annotations
234 241
 			serialize(context, parameter.type);

+ 0
- 2
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/DefinitionMemberSerializer.java Parādīt failu

@@ -25,8 +25,6 @@ import org.openzen.zenscript.codemodel.serialization.CodeSerializationOutput;
25 25
 import org.openzen.zenscript.codemodel.type.ITypeID;
26 26
 import org.openzen.zenscript.moduleserialization.DefinitionEncoding;
27 27
 import org.openzen.zenscript.moduleserializer.EncodingDefinition;
28
-import org.openzen.zenscript.moduleserializer.EncodingModule;
29
-import org.openzen.zenscript.moduleserializer.ModuleEncoder;
30 28
 import org.openzen.zenscript.moduleserializer.SerializationOptions;
31 29
 
32 30
 /**

+ 44
- 42
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/DefinitionSerializer.java Parādīt failu

@@ -5,12 +5,15 @@
5 5
  */
6 6
 package org.openzen.zenscript.moduleserializer.encoder;
7 7
 
8
+import java.util.ArrayList;
9
+import java.util.List;
8 10
 import org.openzen.zenscript.codemodel.context.TypeContext;
9 11
 import org.openzen.zencode.shared.CodePosition;
10 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.context.ModuleContext;
11 14
 import org.openzen.zenscript.codemodel.definition.AliasDefinition;
12 15
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
13
-import org.openzen.zenscript.codemodel.definition.DefinitionVisitor;
16
+import org.openzen.zenscript.codemodel.definition.DefinitionVisitorWithContext;
14 17
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
15 18
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
16 19
 import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
@@ -18,16 +21,17 @@ import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
18 21
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
19 22
 import org.openzen.zenscript.codemodel.definition.VariantDefinition;
20 23
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
24
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
25
+import org.openzen.zenscript.codemodel.member.InnerDefinitionMember;
21 26
 import org.openzen.zenscript.codemodel.serialization.CodeSerializationOutput;
22 27
 import org.openzen.zenscript.moduleserialization.DefinitionEncoding;
23
-import org.openzen.zenscript.moduleserialization.TypeParameterEncoding;
24 28
 import org.openzen.zenscript.moduleserializer.SerializationOptions;
25 29
 
26 30
 /**
27 31
  *
28 32
  * @author Hoofdgebruiker
29 33
  */
30
-public class DefinitionSerializer implements DefinitionVisitor<Void> {
34
+public class DefinitionSerializer implements DefinitionVisitorWithContext<ModuleContext, Void> {
31 35
 	private final SerializationOptions options;
32 36
 	private final CodeSerializationOutput output;
33 37
 	
@@ -36,7 +40,7 @@ public class DefinitionSerializer implements DefinitionVisitor<Void> {
36 40
 		this.output = output;
37 41
 	}
38 42
 	
39
-	private void visit(HighLevelDefinition definition) {
43
+	private void visit(ModuleContext context, HighLevelDefinition definition) {
40 44
 		int flags = 0;
41 45
 		if (definition.position != null && options.positions)
42 46
 			flags |= DefinitionEncoding.FLAG_POSITION;
@@ -54,90 +58,88 @@ public class DefinitionSerializer implements DefinitionVisitor<Void> {
54 58
 			output.writeString(definition.name);
55 59
 		if (definition.typeParameters.length > 0) {
56 60
 			output.writeUInt(definition.typeParameters.length);
57
-			for (TypeParameter parameter : definition.typeParameters) {
58
-				int typeParameterFlags = 0;
59
-				if (parameter.position != CodePosition.UNKNOWN && options.positions)
60
-					typeParameterFlags |= TypeParameterEncoding.FLAG_POSITION;
61
-				if (parameter.name != null && options.typeParameterNames)
62
-					typeParameterFlags |= TypeParameterEncoding.FLAG_NAME;
63
-				
64
-				output.writeUInt(typeParameterFlags);
65
-				if ((typeParameterFlags & TypeParameterEncoding.FLAG_POSITION) > 0)
66
-					output.serialize(parameter.position);
67
-				if ((typeParameterFlags & TypeParameterEncoding.FLAG_NAME) > 0)
68
-					output.writeString(parameter.name);
69
-			}
61
+			TypeContext typeContext = new TypeContext(context, TypeParameter.NONE, null);
62
+			output.serialize(typeContext, definition.typeParameters);
70 63
 		}
71 64
 	}
72 65
 	
73
-	private void queueEncodeMembers(HighLevelDefinition definition) {
66
+	private void encodeMembers(ModuleContext moduleContext, HighLevelDefinition definition) {
67
+		List<InnerDefinitionMember> innerDefinitions = new ArrayList<>();
68
+		for (IDefinitionMember member : definition.members)
69
+			if ((member instanceof InnerDefinitionMember))
70
+				innerDefinitions.add((InnerDefinitionMember)member);
71
+		
74 72
 		output.enqueueMembers(output -> {
75 73
 			DefinitionMemberSerializer memberEncoder = new DefinitionMemberSerializer(options, output);
76
-			TypeContext context = new TypeContext(definition.typeParameters);
74
+			TypeContext context = new TypeContext(moduleContext, definition.typeParameters, moduleContext.registry.getForMyDefinition(definition));
77 75
 			definition.accept(context, memberEncoder);
78 76
 		});
77
+		
78
+		output.writeUInt(innerDefinitions.size());
79
+		for (InnerDefinitionMember innerDefinition : innerDefinitions)
80
+			innerDefinition.innerDefinition.accept(moduleContext, this);
79 81
 	}
80 82
 	
81 83
 	@Override
82
-	public Void visitClass(ClassDefinition definition) {
84
+	public Void visitClass(ModuleContext context, ClassDefinition definition) {
83 85
 		output.writeUInt(DefinitionEncoding.TYPE_CLASS);
84
-		visit(definition);
85
-		queueEncodeMembers(definition);
86
+		visit(context, definition);
87
+		encodeMembers(context, definition);
86 88
 		return null;
87 89
 	}
88 90
 
89 91
 	@Override
90
-	public Void visitInterface(InterfaceDefinition definition) {
92
+	public Void visitInterface(ModuleContext context, InterfaceDefinition definition) {
91 93
 		output.writeUInt(DefinitionEncoding.TYPE_INTERFACE);
92
-		visit(definition);
93
-		queueEncodeMembers(definition);
94
+		visit(context, definition);
95
+		encodeMembers(context, definition);
94 96
 		return null;
95 97
 	}
96 98
 
97 99
 	@Override
98
-	public Void visitEnum(EnumDefinition definition) {
100
+	public Void visitEnum(ModuleContext context, EnumDefinition definition) {
99 101
 		output.writeUInt(DefinitionEncoding.TYPE_ENUM);
100
-		visit(definition);
101
-		queueEncodeMembers(definition);
102
+		visit(context, definition);
103
+		encodeMembers(context, definition);
102 104
 		return null;
103 105
 	}
104 106
 
105 107
 	@Override
106
-	public Void visitStruct(StructDefinition definition) {
108
+	public Void visitStruct(ModuleContext context, StructDefinition definition) {
107 109
 		output.writeUInt(DefinitionEncoding.TYPE_STRUCT);
108
-		visit(definition);
109
-		queueEncodeMembers(definition);
110
+		visit(context, definition);
111
+		encodeMembers(context, definition);
110 112
 		return null;
111 113
 	}
112 114
 
113 115
 	@Override
114
-	public Void visitFunction(FunctionDefinition definition) {
116
+	public Void visitFunction(ModuleContext context, FunctionDefinition definition) {
115 117
 		output.writeUInt(DefinitionEncoding.TYPE_FUNCTION);
116
-		visit(definition);
117
-		queueEncodeMembers(definition);
118
+		visit(context, definition);
119
+		encodeMembers(context, definition);
118 120
 		return null;
119 121
 	}
120 122
 
121 123
 	@Override
122
-	public Void visitExpansion(ExpansionDefinition definition) {
124
+	public Void visitExpansion(ModuleContext context, ExpansionDefinition definition) {
123 125
 		output.writeUInt(DefinitionEncoding.TYPE_EXPANSION);
124
-		visit(definition);
125
-		queueEncodeMembers(definition);
126
+		visit(context, definition);
127
+		encodeMembers(context, definition);
126 128
 		return null;
127 129
 	}
128 130
 
129 131
 	@Override
130
-	public Void visitAlias(AliasDefinition definition) {
132
+	public Void visitAlias(ModuleContext context, AliasDefinition definition) {
131 133
 		output.writeUInt(DefinitionEncoding.TYPE_ALIAS);
132
-		visit(definition);
134
+		visit(context, definition);
133 135
 		return null;
134 136
 	}
135 137
 
136 138
 	@Override
137
-	public Void visitVariant(VariantDefinition variant) {
139
+	public Void visitVariant(ModuleContext context, VariantDefinition variant) {
138 140
 		output.writeUInt(DefinitionEncoding.TYPE_VARIANT);
139
-		visit(variant);
140
-		queueEncodeMembers(variant);
141
+		visit(context, variant);
142
+		encodeMembers(context, variant);
141 143
 		return null;
142 144
 	}
143 145
 }

+ 47
- 7
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/ExpressionSerializer.java Parādīt failu

@@ -19,10 +19,16 @@ import org.openzen.zenscript.moduleserialization.ExpressionEncoding;
19 19
 public class ExpressionSerializer implements ExpressionVisitorWithContext<StatementContext, Void> {
20 20
 	private final CodeSerializationOutput output;
21 21
 	private final boolean positions;
22
+	private final boolean localVariableNames;
22 23
 	
23
-	public ExpressionSerializer(CodeSerializationOutput output, boolean positions) {
24
+	public ExpressionSerializer(
25
+			CodeSerializationOutput output,
26
+			boolean positions,
27
+			boolean localVariableNames)
28
+	{
24 29
 		this.output = output;
25 30
 		this.positions = positions;
31
+		this.localVariableNames = localVariableNames;
26 32
 	}
27 33
 	
28 34
 	private int getFlags(Expression expression) {
@@ -71,6 +77,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
71 77
 		output.writeUInt(getCompareType(expression.comparison));
72 78
 		output.serialize(context, expression.left);
73 79
 		output.serialize(context, expression.right);
80
+		output.write(context, expression.operator);
74 81
 		return null;
75 82
 	}
76 83
 
@@ -80,8 +87,8 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
80 87
 		int flags = getFlags(expression);
81 88
 		serialize(flags, expression);
82 89
 		
83
-		output.write(context, expression.member);
84 90
 		output.serialize(context, expression.target);
91
+		output.write(context, expression.member);
85 92
 		output.serialize(context, expression.arguments);
86 93
 		return null;
87 94
 	}
@@ -92,24 +99,30 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
92 99
 		int flags = getFlags(expression);
93 100
 		serialize(flags, expression);
94 101
 		
95
-		output.write(context, expression.member);
96 102
 		output.serialize(context, expression.target);
103
+		output.write(context, expression.member);
97 104
 		output.serialize(context, expression.arguments);
98 105
 		return null;
99 106
 	}
100 107
 
101 108
 	@Override
102 109
 	public Void visitCapturedClosure(StatementContext context, CapturedClosureExpression expression) {
110
+		if (expression.closure != context.getLambdaClosure())
111
+			throw new AssertionError("Closure invalid");
112
+		
103 113
 		output.writeUInt(ExpressionEncoding.TYPE_CAPTURED_CLOSURE);
104 114
 		int flags = getFlags(expression);
105 115
 		serialize(flags, expression);
106 116
 		
107
-		output.serialize(context, expression.value);
117
+		output.serialize(context.getLambdaOuter(), expression.value);
108 118
 		return null;
109 119
 	}
110 120
 
111 121
 	@Override
112 122
 	public Void visitCapturedDirect(StatementContext context, CapturedDirectExpression expression) {
123
+		if (expression.closure != context.getLambdaClosure())
124
+			throw new AssertionError("Closure invalid");
125
+		
113 126
 		output.writeUInt(ExpressionEncoding.TYPE_CAPTURED_DIRECT);
114 127
 		int flags = getFlags(expression);
115 128
 		serialize(flags, expression);
@@ -120,16 +133,22 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
120 133
 
121 134
 	@Override
122 135
 	public Void visitCapturedLocalVariable(StatementContext context, CapturedLocalVariableExpression expression) {
136
+		if (expression.closure != context.getLambdaClosure())
137
+			throw new AssertionError("Closure invalid");
138
+		
123 139
 		output.writeUInt(ExpressionEncoding.TYPE_CAPTURED_LOCAL_VARIABLE);
124 140
 		int flags = getFlags(expression);
125 141
 		serialize(flags, expression);
126 142
 		
127
-		output.writeUInt(context.getVariableId(expression.variable));
143
+		output.writeUInt(context.getLambdaOuter().getVariableId(expression.variable));
128 144
 		return null;
129 145
 	}
130 146
 
131 147
 	@Override
132 148
 	public Void visitCapturedParameter(StatementContext context, CapturedParameterExpression expression) {
149
+		if (expression.closure != context.getLambdaClosure())
150
+			throw new AssertionError("Closure invalid");
151
+		
133 152
 		output.writeUInt(ExpressionEncoding.TYPE_CAPTURED_PARAMETER);
134 153
 		int flags = getFlags(expression);
135 154
 		serialize(flags, expression);
@@ -140,6 +159,9 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
140 159
 
141 160
 	@Override
142 161
 	public Void visitCapturedThis(StatementContext context, CapturedThisExpression expression) {
162
+		if (expression.closure != context.getLambdaClosure())
163
+			throw new AssertionError("Closure invalid");
164
+		
143 165
 		output.writeUInt(ExpressionEncoding.TYPE_CAPTURED_THIS);
144 166
 		int flags = getFlags(expression);
145 167
 		serialize(flags, expression);
@@ -188,6 +210,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
188 210
 		serialize(flags, expression);
189 211
 		
190 212
 		output.serialize(context, expression.condition);
213
+		output.serialize(context, expression.type);
191 214
 		output.serialize(context, expression.ifThen);
192 215
 		output.serialize(context, expression.ifElse);
193 216
 		return null;
@@ -199,6 +222,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
199 222
 		int flags = getFlags(expression);
200 223
 		serialize(flags, expression);
201 224
 		
225
+		output.serialize(context, expression.constant.getType());
202 226
 		output.write(context, expression.constant);
203 227
 		return null;
204 228
 	}
@@ -432,6 +456,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
432 456
 		int flags = getFlags(expression);
433 457
 		serialize(flags, expression);
434 458
 		
459
+		output.serialize(context, expression.field.getType());
435 460
 		output.write(context, expression.field);
436 461
 		return null;
437 462
 	}
@@ -454,6 +479,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
454 479
 		serialize(flags, expression);
455 480
 		
456 481
 		output.writeString(expression.name);
482
+		output.serialize(context, expression.resolution);
457 483
 		return null;
458 484
 	}
459 485
 
@@ -465,6 +491,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
465 491
 		
466 492
 		output.writeString(expression.name);
467 493
 		output.serialize(context, expression.arguments);
494
+		output.serialize(context, expression.resolution);
468 495
 		return null;
469 496
 	}
470 497
 
@@ -519,9 +546,12 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
519 546
 	public Void visitMatch(StatementContext context, MatchExpression expression) {
520 547
 		output.writeUInt(ExpressionEncoding.TYPE_MATCH);
521 548
 		int flags = getFlags(expression);
549
+		if (localVariableNames)
550
+			flags |= ExpressionEncoding.FLAG_NAMES;
522 551
 		serialize(flags, expression);
523 552
 		
524 553
 		output.serialize(context, expression.value);
554
+		output.serialize(context, expression.type);
525 555
 		output.writeUInt(expression.cases.length);
526 556
 		for (MatchExpression.Case case_ : expression.cases) {
527 557
 			output.serialize(context, case_.key);
@@ -536,6 +566,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
536 566
 		int flags = getFlags(expression);
537 567
 		serialize(flags, expression);
538 568
 		
569
+		output.serialize(context, expression.constructor.getOwnerType());
539 570
 		output.write(context, expression.constructor);
540 571
 		output.serialize(context, expression.arguments);
541 572
 		return null;
@@ -546,6 +577,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
546 577
 		output.writeUInt(ExpressionEncoding.TYPE_NULL);
547 578
 		int flags = getFlags(expression);
548 579
 		serialize(flags, expression);
580
+		output.serialize(context, expression.type);
549 581
 		return null;
550 582
 	}
551 583
 
@@ -567,6 +599,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
567 599
 		serialize(flags, expression);
568 600
 		
569 601
 		output.serialize(context, expression.value);
602
+		output.serialize(context, expression.type);
570 603
 		return null;
571 604
 	}
572 605
 
@@ -596,6 +629,8 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
596 629
 	public Void visitSameObject(StatementContext context, SameObjectExpression expression) {
597 630
 		output.writeUInt(ExpressionEncoding.TYPE_SAME_OBJECT);
598 631
 		int flags = getFlags(expression);
632
+		if (expression.inverted)
633
+			flags |= ExpressionEncoding.FLAG_INVERTED;
599 634
 		serialize(flags, expression);
600 635
 		
601 636
 		output.serialize(context, expression.left);
@@ -687,8 +722,8 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
687 722
 		int flags = getFlags(expression);
688 723
 		serialize(flags, expression);
689 724
 		
690
-		output.serialize(context, expression.value);
691 725
 		output.serialize(context, expression.type);
726
+		output.serialize(context, expression.value);
692 727
 		return null;
693 728
 	}
694 729
 
@@ -706,6 +741,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
706 741
 		int flags = getFlags(expression);
707 742
 		serialize(flags, expression);
708 743
 		
744
+		output.serialize(context, expression.type);
709 745
 		output.serialize(context, expression.value);
710 746
 		return null;
711 747
 	}
@@ -716,6 +752,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
716 752
 		int flags = getFlags(expression);
717 753
 		serialize(flags, expression);
718 754
 		
755
+		output.serialize(context, expression.type);
719 756
 		output.serialize(context, expression.value);
720 757
 		return null;
721 758
 	}
@@ -726,6 +763,8 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
726 763
 		int flags = getFlags(expression);
727 764
 		serialize(flags, expression);
728 765
 		
766
+		output.serialize(context, expression.type);
767
+		output.serialize(context, expression.thrownType);
729 768
 		output.serialize(context, expression.value);
730 769
 		return null;
731 770
 	}
@@ -736,6 +775,7 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
736 775
 		int flags = getFlags(expression);
737 776
 		serialize(flags, expression);
738 777
 		
778
+		output.serialize(context, expression.type);
739 779
 		output.serialize(context, expression.value);
740 780
 		return null;
741 781
 	}
@@ -746,8 +786,8 @@ public class ExpressionSerializer implements ExpressionVisitorWithContext<Statem
746 786
 		int flags = getFlags(expression);
747 787
 		serialize(flags, expression);
748 788
 		
789
+		output.serialize(context, expression.option.variant);
749 790
 		output.write(expression.option);
750
-		output.writeUInt(expression.arguments.length);
751 791
 		for (Expression argument : expression.arguments)
752 792
 			output.serialize(context, argument);
753 793
 		return null;

+ 20
- 19
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/MemberSerializer.java Parādīt failu

@@ -102,10 +102,12 @@ public class MemberSerializer implements MemberVisitorWithContext<TypeContext, V
102 102
 		
103 103
 		int flags = getFlags(member);
104 104
 		serialize(flags, member);
105
-		output.serialize(context, member.header);
105
+		
106
+		StatementContext inner = new StatementContext(context, member.header);
107
+		output.serialize(inner, member.header);
106 108
 		
107 109
 		output.enqueueCode(encoder -> {
108
-			encoder.serialize(new StatementContext(context, member.header), member.body);
110
+			encoder.serialize(inner, member.body);
109 111
 		});
110 112
 		return null;
111 113
 	}
@@ -128,11 +130,13 @@ public class MemberSerializer implements MemberVisitorWithContext<TypeContext, V
128 130
 		output.writeUInt(MemberEncoding.TYPE_METHOD);
129 131
 		int flags = getFlags(member);
130 132
 		serialize(flags, member);
131
-		output.serialize(context, member.header);
133
+		
134
+		StatementContext inner = new StatementContext(context, member.header);
135
+		output.serialize(inner, member.header);
132 136
 		
133 137
 		output.enqueueCode(encoder -> {
134
-			encoder.write(context, member.getOverrides());
135
-			encoder.serialize(new StatementContext(context, member.header), member.body);
138
+			encoder.write(inner, member.getOverrides());
139
+			encoder.serialize(inner, member.body);
136 140
 		});
137 141
 		return null;
138 142
 	}
@@ -174,11 +178,13 @@ public class MemberSerializer implements MemberVisitorWithContext<TypeContext, V
174 178
 		int flags = getFlags(member);
175 179
 		serialize(flags, member);
176 180
 		output.writeUInt(getId(member.operator));
177
-		output.serialize(context, member.header);
181
+		
182
+		StatementContext inner = new StatementContext(context, member.header);
183
+		output.serialize(inner, member.header);
178 184
 		
179 185
 		output.enqueueCode(encoder -> {
180
-			encoder.write(context, member.getOverrides());
181
-			encoder.serialize(new StatementContext(context, member.header), member.body);
186
+			encoder.write(inner, member.getOverrides());
187
+			encoder.serialize(inner, member.body);
182 188
 		});
183 189
 		return null;
184 190
 	}
@@ -222,11 +228,13 @@ public class MemberSerializer implements MemberVisitorWithContext<TypeContext, V
222 228
 		output.writeUInt(MemberEncoding.TYPE_CALLER);
223 229
 		int flags = getFlags(member);
224 230
 		serialize(flags, member);
225
-		output.serialize(context, member.header);
231
+		
232
+		StatementContext inner = new StatementContext(context, member.header);
233
+		output.serialize(inner, member.header);
226 234
 		
227 235
 		output.enqueueCode(encoder -> {
228
-			encoder.write(context, member.getOverrides());
229
-			encoder.serialize(new StatementContext(context, member.header), member.body);
236
+			encoder.write(inner, member.getOverrides());
237
+			encoder.serialize(inner, member.body);
230 238
 		});
231 239
 		return null;
232 240
 	}
@@ -247,14 +255,7 @@ public class MemberSerializer implements MemberVisitorWithContext<TypeContext, V
247 255
 
248 256
 	@Override
249 257
 	public Void visitInnerDefinition(TypeContext context, InnerDefinitionMember member) {
250
-		output.writeUInt(MemberEncoding.TYPE_INNER_DEFINITION);
251
-		int flags = getFlags(member);
252
-		serialize(flags, member);
253
-		
254
-		output.writeUInt(member.innerDefinition.members.size());
255
-		for (IDefinitionMember innerMember : member.innerDefinition.members) {
256
-			innerMember.accept(context, this);
257
-		}
258
+		// already serialized
258 259
 		return null;
259 260
 	}
260 261
 

+ 37
- 7
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/StatementSerializer.java Parādīt failu

@@ -35,10 +35,16 @@ import org.openzen.zenscript.moduleserialization.StatementEncoding;
35 35
 public class StatementSerializer implements StatementVisitorWithContext<StatementContext, Void> {
36 36
 	private final CodeSerializationOutput output;
37 37
 	private final boolean positions;
38
+	private final boolean localVariableNames;
38 39
 	
39
-	public StatementSerializer(CodeSerializationOutput output, boolean positions) {
40
+	public StatementSerializer(
41
+			CodeSerializationOutput output,
42
+			boolean positions,
43
+			boolean localVariableNames)
44
+	{
40 45
 		this.output = output;
41 46
 		this.positions = positions;
47
+		this.localVariableNames = localVariableNames;
42 48
 	}
43 49
 	
44 50
 	private int getFlags(Statement statement) {
@@ -60,7 +66,7 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
60 66
 		int flags = getFlags(statement);
61 67
 		encode(flags, statement);
62 68
 		
63
-		output.writeUInt(statement.statements.size());
69
+		output.writeUInt(statement.statements.length);
64 70
 		StatementContext inner = new StatementContext(context);
65 71
 		for (Statement s : statement.statements)
66 72
 			output.serialize(inner, s);
@@ -91,9 +97,13 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
91 97
 	public Void visitDoWhile(StatementContext context, DoWhileStatement statement) {
92 98
 		output.writeUInt(StatementEncoding.TYPE_DO_WHILE);
93 99
 		int flags = getFlags(statement);
100
+		if (statement.label != null)
101
+			flags |= StatementEncoding.FLAG_LABEL;
94 102
 		encode(flags, statement);
95 103
 		
96 104
 		output.serialize(context, statement.condition);
105
+		if ((flags & StatementEncoding.FLAG_LABEL) > 0)
106
+			output.writeString(statement.label);
97 107
 		StatementContext inner = new StatementContext(context, statement);
98 108
 		output.serialize(inner, statement.content);
99 109
 		return null;
@@ -120,13 +130,19 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
120 130
 	public Void visitForeach(StatementContext context, ForeachStatement statement) {
121 131
 		output.writeUInt(StatementEncoding.TYPE_FOREACH);
122 132
 		int flags = getFlags(statement);
133
+		if (localVariableNames)
134
+			flags |= StatementEncoding.FLAG_NAME;
123 135
 		encode(flags, statement);
124 136
 		output.serialize(context, statement.list);
125 137
 		output.write(context, statement.iterator);
126
-		output.writeUInt(statement.loopVariables.length);
127
-		for (VarStatement loopVariable : statement.loopVariables)
128
-			output.writeString(loopVariable.name);
138
+		if ((flags & StatementEncoding.FLAG_NAME) > 0) {
139
+			for (VarStatement loopVariable : statement.loopVariables)
140
+				output.writeString(loopVariable.name);
141
+		}
129 142
 		StatementContext inner = new StatementContext(context, statement);
143
+		for (VarStatement variable : statement.loopVariables)
144
+			inner.add(variable);
145
+		
130 146
 		output.serialize(inner, statement.content);
131 147
 		return null;
132 148
 	}
@@ -165,14 +181,21 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
165 181
 	public Void visitSwitch(StatementContext context, SwitchStatement statement) {
166 182
 		output.writeUInt(StatementEncoding.TYPE_RETURN);
167 183
 		int flags = getFlags(statement);
184
+		if (statement.label != null)
185
+			flags |= StatementEncoding.FLAG_LABEL;
186
+		if (localVariableNames)
187
+			flags |= StatementEncoding.FLAG_NAME;
168 188
 		encode(flags, statement);
189
+		
169 190
 		output.serialize(context, statement.value);
191
+		if ((flags & StatementEncoding.FLAG_LABEL) > 0)
192
+			output.writeString(statement.label);
170 193
 		output.writeUInt(statement.cases.size());
171 194
 		
172 195
 		StatementContext inner = new StatementContext(context, statement);
173 196
 		for (SwitchCase case_ : statement.cases) {
174 197
 			output.serialize(context, case_.value);
175
-			output.writeUInt(case_.statements.size());
198
+			output.writeUInt(case_.statements.length);
176 199
 			for (Statement s : case_.statements) {
177 200
 				output.serialize(inner, s);
178 201
 			}
@@ -204,10 +227,13 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
204 227
 		int flags = getFlags(statement);
205 228
 		if (statement.isFinal)
206 229
 			flags |= StatementEncoding.FLAG_FINAL;
230
+		if (statement.name != null && localVariableNames)
231
+			flags |= StatementEncoding.FLAG_NAME;
207 232
 		encode(flags, statement);
208 233
 		
209 234
 		output.serialize(context, statement.type);
210
-		output.writeString(statement.name);
235
+		if ((flags & StatementEncoding.FLAG_NAME) > 0)
236
+			output.writeString(statement.name);
211 237
 		output.serialize(context, statement.initializer);
212 238
 		
213 239
 		context.add(statement);
@@ -218,9 +244,13 @@ public class StatementSerializer implements StatementVisitorWithContext<Statemen
218 244
 	public Void visitWhile(StatementContext context, WhileStatement statement) {
219 245
 		output.writeUInt(StatementEncoding.TYPE_WHILE);
220 246
 		int flags = getFlags(statement);
247
+		if (statement.label != null)
248
+			flags |= StatementEncoding.FLAG_LABEL;
221 249
 		encode(flags, statement);
222 250
 		
223 251
 		output.serialize(context, statement.condition);
252
+		if ((flags & StatementEncoding.FLAG_LABEL) > 0)
253
+			output.writeString(statement.label);
224 254
 		output.serialize(new StatementContext(context, statement), statement.content);
225 255
 		return null;
226 256
 	}

+ 7
- 4
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/SwitchValueSerializer.java Parādīt failu

@@ -21,9 +21,11 @@ import org.openzen.zenscript.moduleserialization.SwitchValueEncoding;
21 21
  */
22 22
 public class SwitchValueSerializer implements SwitchValueVisitorWithContext<TypeContext, Void> {
23 23
 	private final CodeSerializationOutput output;
24
+	private final boolean localVariableNames;
24 25
 	
25
-	public SwitchValueSerializer(CodeSerializationOutput output) {
26
+	public SwitchValueSerializer(CodeSerializationOutput output, boolean localVariableNames) {
26 27
 		this.output = output;
28
+		this.localVariableNames = localVariableNames;
27 29
 	}
28 30
 
29 31
 	@Override
@@ -58,9 +60,10 @@ public class SwitchValueSerializer implements SwitchValueVisitorWithContext<Type
58 60
 	public Void acceptVariantOption(TypeContext context, VariantOptionSwitchValue value) {
59 61
 		output.writeUInt(SwitchValueEncoding.TYPE_VARIANT_OPTION);
60 62
 		output.write(value.option);
61
-		output.writeUInt(value.parameters.length);
62
-		for (String parameter : value.parameters)
63
-			output.writeString(parameter);
63
+		if (localVariableNames) {
64
+			for (String parameter : value.parameters)
65
+				output.writeString(parameter);
66
+		}
64 67
 		return null;
65 68
 	}
66 69
 }

+ 6
- 2
ModuleSerializer/src/main/java/org/openzen/zenscript/moduleserializer/encoder/TypeSerializer.java Parādīt failu

@@ -52,6 +52,7 @@ public class TypeSerializer implements TypeVisitorWithContext<TypeContext, Void>
52 52
 			case CHAR: output.writeUInt(TypeEncoding.TYPE_CHAR); break;
53 53
 			case STRING: output.writeUInt(TypeEncoding.TYPE_STRING); break;
54 54
 			case UNDETERMINED: output.writeUInt(TypeEncoding.TYPE_UNDETERMINED); break;
55
+			case NULL: output.writeUInt(TypeEncoding.TYPE_NULL); break;
55 56
 			default: throw new IllegalArgumentException("Unknown basic type: " + basic);
56 57
 		}
57 58
 		return null;
@@ -81,7 +82,7 @@ public class TypeSerializer implements TypeVisitorWithContext<TypeContext, Void>
81 82
 	public Void visitGenericMap(TypeContext context, GenericMapTypeID map) {
82 83
 		output.writeUInt(TypeEncoding.TYPE_GENERIC_MAP);
83 84
 		output.serialize(context, map.key);
84
-		map.value.accept(new TypeContext(context, new TypeParameter[] { map.key }), this);
85
+		map.value.accept(new TypeContext(context, context.thisType, new TypeParameter[] { map.key }), this);
85 86
 		return null;
86 87
 	}
87 88
 
@@ -113,7 +114,10 @@ public class TypeSerializer implements TypeVisitorWithContext<TypeContext, Void>
113 114
 	@Override
114 115
 	public Void visitGeneric(TypeContext context, GenericTypeID generic) {
115 116
 		output.writeUInt(TypeEncoding.TYPE_GENERIC);
116
-		output.writeUInt(context.getId(generic.parameter));
117
+		int id = context.getId(generic.parameter);
118
+		if (id < 0)
119
+			throw new IllegalStateException("Type parameter not in scope: " + generic.parameter);
120
+		output.writeUInt(id);
117 121
 		return null;
118 122
 	}
119 123
 

+ 3
- 4
Parser/src/main/java/org/openzen/zenscript/parser/statements/ParsedStatementBlock.java Parādīt failu

@@ -1,6 +1,5 @@
1 1
 package org.openzen.zenscript.parser.statements;
2 2
 
3
-import java.util.ArrayList;
4 3
 import java.util.List;
5 4
 import org.openzen.zencode.shared.CodePosition;
6 5
 import org.openzen.zenscript.codemodel.WhitespaceInfo;
@@ -10,7 +9,6 @@ import org.openzen.zenscript.codemodel.statement.Statement;
10 9
 import org.openzen.zenscript.codemodel.scope.BlockScope;
11 10
 import org.openzen.zenscript.codemodel.scope.StatementScope;
12 11
 import org.openzen.zenscript.parser.ParsedAnnotation;
13
-import org.openzen.zenscript.parser.PrecompilationState;
14 12
 
15 13
 public class ParsedStatementBlock extends ParsedStatement {
16 14
 	private final List<ParsedStatement> statements;
@@ -26,9 +24,10 @@ public class ParsedStatementBlock extends ParsedStatement {
26 24
 	@Override
27 25
 	public Statement compile(StatementScope scope) {
28 26
 		StatementScope blockScope = new BlockScope(scope);
29
-		List<Statement> compiled = new ArrayList<>();
27
+		Statement[] compiled = new Statement[statements.size()];
28
+		int i = 0;
30 29
 		for (ParsedStatement statement : statements) {
31
-			compiled.add(statement.compile(blockScope));
30
+			compiled[i++] = statement.compile(blockScope);
32 31
 		}
33 32
 		BlockStatement block = new BlockStatement(position, compiled);
34 33
 		result(block, scope);

+ 3
- 3
Parser/src/main/java/org/openzen/zenscript/parser/statements/ParsedSwitchCase.java Parādīt failu

@@ -13,7 +13,6 @@ import org.openzen.zenscript.codemodel.statement.SwitchCase;
13 13
 import org.openzen.zenscript.codemodel.type.ITypeID;
14 14
 import org.openzen.zenscript.codemodel.scope.ExpressionScope;
15 15
 import org.openzen.zenscript.codemodel.scope.StatementScope;
16
-import org.openzen.zenscript.parser.PrecompilationState;
17 16
 import org.openzen.zenscript.parser.expression.ParsedExpression;
18 17
 
19 18
 /**
@@ -30,9 +29,10 @@ public class ParsedSwitchCase {
30 29
 	
31 30
 	public SwitchCase compile(ITypeID type, StatementScope scope) {
32 31
 		SwitchValue cValue = value == null ? null : value.compileToSwitchValue(type, new ExpressionScope(scope));
33
-		List<Statement> cStatements = new ArrayList<>();
32
+		Statement[] cStatements = new Statement[statements.size()];
33
+		int i = 0;
34 34
 		for (ParsedStatement statement : statements) {
35
-			cStatements.add(statement.compile(scope));
35
+			cStatements[i++] = statement.compile(scope);
36 36
 		}
37 37
 		return new SwitchCase(cValue, cStatements);
38 38
 	}

+ 2
- 2
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java Parādīt failu

@@ -114,9 +114,9 @@ public class GlobalRegistry {
114 114
 			return new PartialMemberGroupExpression(
115 115
 					position,
116 116
 					scope,
117
-					new GetStaticFieldExpression(position, new FieldMemberRef(SYSTEM_OUT, GenericMapper.EMPTY)),
117
+					new GetStaticFieldExpression(position, new FieldMemberRef(scope.getTypeRegistry().getForMyDefinition(SYSTEM), SYSTEM_OUT, GenericMapper.EMPTY)),
118 118
 					"println",
119
-					PRINTSTREAM_PRINTLN.ref(GenericMapper.EMPTY),
119
+					PRINTSTREAM_PRINTLN.ref(scope.getTypeRegistry().getForDefinition(PRINTSTREAM), GenericMapper.EMPTY),
120 120
 					null,
121 121
 					false);
122 122
 		}

+ 4
- 4
Shared/src/main/java/compactio/CompactBytesDataInput.java Parādīt failu

@@ -43,12 +43,12 @@ public final class CompactBytesDataInput implements CompactDataInput, AutoClosea
43 43
     }
44 44
     
45 45
     @Override
46
-    public byte readByte() {
47
-        return (byte)(data[offset++]);
46
+    public int readByte() {
47
+        return (data[offset++]) & 0xFF;
48 48
     }
49 49
     
50 50
     @Override
51
-    public int readSByte() {
51
+    public byte readSByte() {
52 52
         return data[offset++];
53 53
     }
54 54
     
@@ -187,7 +187,7 @@ public final class CompactBytesDataInput implements CompactDataInput, AutoClosea
187 187
         boolean[] result = new boolean[size];
188 188
         int limitForI = (size + 7) / 8;
189 189
         for (int i = 0; i < limitForI; i++) {
190
-            byte bvalue = this.readByte();
190
+            int bvalue = this.readByte();
191 191
             int remainingBits = result.length - 8 * i;
192 192
             if (remainingBits > 0)
193 193
                 result[i * 8 + 0] = (bvalue & (byte)1) > (byte)0;

+ 2
- 2
Shared/src/main/java/compactio/CompactDataInput.java Parādīt failu

@@ -3,9 +3,9 @@ package compactio;
3 3
 public interface CompactDataInput extends AutoCloseable {
4 4
     boolean readBool();
5 5
     
6
-    byte readByte();
6
+    int readByte();
7 7
     
8
-    int readSByte();
8
+    byte readSByte();
9 9
     
10 10
     short readShort();
11 11
     

Notiek ielāde…
Atcelt
Saglabāt