Browse Source

- Added package info to definitions

- Added parameter index to function parameters
- Removed unnecessary expression types; these can be implemented differently
- WIP on a code (re)formatter
Stan Hebben 6 years ago
parent
commit
8b327ec7f7
43 changed files with 1736 additions and 262 deletions
  1. 18
    0
      CodeFormatter/build.gradle
  2. 617
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java
  3. 33
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionString.java
  4. 91
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FileFormatter.java
  5. 272
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FormattingSettings.java
  6. 16
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/Importer.java
  7. 73
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java
  8. 21
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ParentStatementType.java
  9. 296
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/StatementFormatter.java
  10. 171
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java
  11. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java
  12. 1
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java
  13. 4
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java
  14. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/AliasDefinition.java
  15. 4
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java
  16. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/EnumDefinition.java
  17. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java
  18. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java
  19. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/InterfaceDefinition.java
  20. 2
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/StructDefinition.java
  21. 11
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java
  22. 0
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  23. 0
    31
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/StringConcatExpression.java
  24. 0
    29
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SubstringExpression.java
  25. 0
    91
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/SubstringMember.java
  26. 2
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java
  27. 1
    14
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java
  28. 0
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachVisitor.java
  29. 0
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java
  30. 1
    1
      JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java
  31. 9
    8
      Parser/src/main/java/org/openzen/zenscript/parser/ParsedDefinition.java
  32. 6
    6
      Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java
  33. 5
    4
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedAlias.java
  34. 5
    4
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java
  35. 5
    4
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnum.java
  36. 5
    4
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedExpansion.java
  37. 5
    6
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedFunction.java
  38. 5
    6
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedInterface.java
  39. 5
    6
      Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedStruct.java
  40. 2
    1
      Parser/src/main/java/org/openzen/zenscript/parser/member/ParsedDefinitionMember.java
  41. 1
    0
      ScriptingExample/build.gradle
  42. 17
    16
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java
  43. 16
    4
      ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java

+ 18
- 0
CodeFormatter/build.gradle View File

@@ -0,0 +1,18 @@
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(':CodeModel')
18
+}

+ 617
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java View File

@@ -0,0 +1,617 @@
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.formatter;
7
+
8
+import org.openzen.zenscript.codemodel.expression.AndAndExpression;
9
+import org.openzen.zenscript.codemodel.expression.ArrayExpression;
10
+import org.openzen.zenscript.codemodel.expression.BasicCompareExpression;
11
+import org.openzen.zenscript.codemodel.expression.CallArguments;
12
+import org.openzen.zenscript.codemodel.expression.CallExpression;
13
+import org.openzen.zenscript.codemodel.expression.CallStaticExpression;
14
+import org.openzen.zenscript.codemodel.expression.CapturedClosureExpression;
15
+import org.openzen.zenscript.codemodel.expression.CapturedDirectExpression;
16
+import org.openzen.zenscript.codemodel.expression.CapturedLocalVariableExpression;
17
+import org.openzen.zenscript.codemodel.expression.CapturedParameterExpression;
18
+import org.openzen.zenscript.codemodel.expression.CapturedThisExpression;
19
+import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
20
+import org.openzen.zenscript.codemodel.expression.CoalesceExpression;
21
+import org.openzen.zenscript.codemodel.expression.ConditionalExpression;
22
+import org.openzen.zenscript.codemodel.expression.ConstantBoolExpression;
23
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
24
+import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
25
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
26
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
27
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
28
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
29
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
30
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
31
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
32
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
33
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
34
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
35
+import org.openzen.zenscript.codemodel.expression.ConstructorSuperCallExpression;
36
+import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
37
+import org.openzen.zenscript.codemodel.expression.EnumConstantExpression;
38
+import org.openzen.zenscript.codemodel.expression.EqualsExpression;
39
+import org.openzen.zenscript.codemodel.expression.Expression;
40
+import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
41
+import org.openzen.zenscript.codemodel.expression.FunctionExpression;
42
+import org.openzen.zenscript.codemodel.expression.GenericCompareExpression;
43
+import org.openzen.zenscript.codemodel.expression.GetFieldExpression;
44
+import org.openzen.zenscript.codemodel.expression.GetFunctionParameterExpression;
45
+import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
46
+import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
47
+import org.openzen.zenscript.codemodel.expression.GetterExpression;
48
+import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
49
+import org.openzen.zenscript.codemodel.expression.IsExpression;
50
+import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
51
+import org.openzen.zenscript.codemodel.expression.MapExpression;
52
+import org.openzen.zenscript.codemodel.expression.NewExpression;
53
+import org.openzen.zenscript.codemodel.expression.NotExpression;
54
+import org.openzen.zenscript.codemodel.expression.NullExpression;
55
+import org.openzen.zenscript.codemodel.expression.OrOrExpression;
56
+import org.openzen.zenscript.codemodel.expression.RangeExpression;
57
+import org.openzen.zenscript.codemodel.expression.SetFieldExpression;
58
+import org.openzen.zenscript.codemodel.expression.SetFunctionParameterExpression;
59
+import org.openzen.zenscript.codemodel.expression.SetLocalVariableExpression;
60
+import org.openzen.zenscript.codemodel.expression.SetStaticFieldExpression;
61
+import org.openzen.zenscript.codemodel.expression.SetterExpression;
62
+import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
63
+import org.openzen.zenscript.codemodel.expression.StaticSetterExpression;
64
+import org.openzen.zenscript.codemodel.expression.ThisExpression;
65
+import org.openzen.zenscript.codemodel.expression.WrapOptionalExpression;
66
+import org.openzen.zenscript.codemodel.member.CasterMember;
67
+import org.openzen.zenscript.codemodel.member.OperatorMember;
68
+import org.openzen.zenscript.codemodel.type.ITypeID;
69
+import org.openzen.zenscript.shared.StringUtils;
70
+
71
+/**
72
+ *
73
+ * @author Hoofdgebruiker
74
+ */
75
+public class ExpressionFormatter implements ExpressionVisitor<ExpressionString> {
76
+	private final FormattingSettings settings;
77
+	public final TypeFormatter typeFormatter;
78
+	
79
+	public ExpressionFormatter(FormattingSettings settings, TypeFormatter typeFormatter) {
80
+		this.settings = settings;
81
+		this.typeFormatter = typeFormatter;
82
+	}
83
+
84
+	@Override
85
+	public ExpressionString visitAndAnd(AndAndExpression expression) {
86
+		return binary(expression.left, expression.right, OperatorPriority.ANDAND, " && ");
87
+	}
88
+
89
+	@Override
90
+	public ExpressionString visitArray(ArrayExpression expression) {
91
+		StringBuilder result = new StringBuilder();
92
+		result.append("[");
93
+		int index = 0;
94
+		for (Expression element : expression.expressions) {
95
+			if (index > 0)
96
+				result.append(", ");
97
+			
98
+			result.append(element.accept(this).value);
99
+			index++;
100
+		}
101
+		result.append("]");
102
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
103
+	}
104
+
105
+	@Override
106
+	public ExpressionString visitCompare(BasicCompareExpression expression) {
107
+		return binary(
108
+				expression.left,
109
+				expression.right,
110
+				OperatorPriority.COMPARE,
111
+				" " + expression.operator.str + " ");
112
+	}
113
+
114
+	@Override
115
+	public ExpressionString visitCall(CallExpression expression) {
116
+		if (expression.member instanceof OperatorMember) {
117
+			OperatorMember operator = (OperatorMember) expression.member;
118
+			switch (operator.operator) {
119
+				case NOT:
120
+					return unaryPrefix(expression.target, OperatorPriority.NOT, "!");
121
+				case NEG:
122
+					return unaryPrefix(expression.target, OperatorPriority.NEG, "-");
123
+				case CAT:
124
+					if (expression.arguments.arguments.length == 0) {
125
+						return unaryPrefix(expression.target, OperatorPriority.INVERT, "~");
126
+					} else {
127
+						return binary(expression.target, expression.getFirstArgument(), OperatorPriority.CAT, " ~ ");
128
+					}
129
+				case ADD:
130
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.ADD, " + ");
131
+				case SUB:
132
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.SUB, " - ");
133
+				case MUL:
134
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.MUL, " * ");
135
+				case DIV:
136
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.DIV, " / ");
137
+				case MOD:
138
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.MOD, " % ");
139
+				case AND:
140
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.AND, " & ");
141
+				case OR:
142
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.OR, " | ");
143
+				case XOR:
144
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XOR, " ^ ");
145
+				case CONTAINS:
146
+					return binary(expression.getFirstArgument(), expression.target, OperatorPriority.CONTAINS, " in ");
147
+				case EQUALS:
148
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.COMPARE, " == ");
149
+				case INDEXSET: {
150
+					StringBuilder result = new StringBuilder();
151
+					result.append(expression.target);
152
+					result.append("[");
153
+					for (int i = 0; i < expression.arguments.arguments.length - 1; i++) {
154
+						if (i > 0)
155
+							result.append(", ");
156
+						
157
+						result.append(expression.arguments.arguments[i].accept(this).value);
158
+					}
159
+					result.append("] = ");
160
+					result.append(expression.arguments.arguments[expression.arguments.arguments.length - 1].accept(this).value);
161
+					return new ExpressionString(result.toString(), OperatorPriority.ASSIGN);
162
+				}
163
+				case INDEXGET: {
164
+					StringBuilder result = new StringBuilder();
165
+					result.append(expression.target);
166
+					result.append("[");
167
+					for (int i = 0; i < expression.arguments.arguments.length - 1; i++) {
168
+						if (i > 0)
169
+							result.append(", ");
170
+						
171
+						result.append(expression.arguments.arguments[i].accept(this));
172
+					}
173
+					result.append("]");
174
+					return new ExpressionString(result.toString(), OperatorPriority.INDEX);
175
+				}
176
+				case MEMBERGETTER: {
177
+					StringBuilder result = new StringBuilder();
178
+					result.append(expression.target);
179
+					result.append(".get(");
180
+					result.append(expression.getFirstArgument().accept(this));
181
+					result.append(")");
182
+					return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
183
+				}
184
+				case MEMBERSETTER: {
185
+					StringBuilder result = new StringBuilder();
186
+					result.append(expression.target);
187
+					result.append(".set(");
188
+					result.append(expression.getFirstArgument().accept(this));
189
+					result.append(", ");
190
+					result.append(expression.arguments.arguments[1].accept(this));
191
+					result.append(")");
192
+					return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
193
+				}
194
+				case ADDASSIGN:
195
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " += ");
196
+				case SUBASSIGN:
197
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " -= ");
198
+				case MULASSIGN:
199
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " *= ");
200
+				case DIVASSIGN:
201
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " /= ");
202
+				case MODASSIGN:
203
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " %= ");
204
+				case CATASSIGN:
205
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " ~= ");
206
+				case ORASSIGN:
207
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " |= ");
208
+				case ANDASSIGN:
209
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " &= ");
210
+				case XORASSIGN:
211
+					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " ^= ");
212
+				case POST_INCREMENT:
213
+					return unaryPostfix(expression.target, OperatorPriority.DECREMENT, "++");
214
+				case POST_DECREMENT:
215
+					return unaryPostfix(expression.target, OperatorPriority.DECREMENT, "--");
216
+				case PRE_INCREMENT:
217
+					return unaryPrefix(expression.target, OperatorPriority.DECREMENT, "++");
218
+				case PRE_DECREMENT:
219
+					return unaryPrefix(expression.target, OperatorPriority.DECREMENT, "--");
220
+				case CALL: {
221
+					StringBuilder result = new StringBuilder();
222
+					result.append(".");
223
+					format(result, expression.arguments);
224
+					return new ExpressionString(result.toString(), OperatorPriority.CALL);
225
+				}
226
+				case CAST: {
227
+					StringBuilder result = new StringBuilder();
228
+					result.append(" as ");
229
+					result.append(expression.arguments.typeArguments[0].accept(typeFormatter));
230
+					return new ExpressionString(result.toString(), OperatorPriority.CAST);
231
+				}
232
+				default:
233
+					throw new UnsupportedOperationException("Unknown operator: " + operator.operator);
234
+			}
235
+		} else if (expression.member instanceof CasterMember) {
236
+			CasterMember casterMember = (CasterMember) expression.member;
237
+			
238
+			StringBuilder result = new StringBuilder();
239
+			result.append(expression.target.accept(this).value);
240
+			if (!casterMember.isImplicit()) {
241
+				result.append(" as ");
242
+				result.append(casterMember.toType.accept(typeFormatter));
243
+			}
244
+			return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
245
+		} else {
246
+			StringBuilder result = new StringBuilder();
247
+			result.append(expression.target.accept(this).value);
248
+			result.append(".");
249
+			result.append(expression.member.name);
250
+			format(result, expression.arguments);
251
+			return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
252
+		}
253
+	}
254
+
255
+	@Override
256
+	public ExpressionString visitCallStatic(CallStaticExpression expression) {
257
+		StringBuilder result = new StringBuilder();
258
+		result.append(expression.type.accept(typeFormatter));
259
+		result.append(".");
260
+		result.append(expression.member.name);
261
+		format(result, expression.arguments);
262
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
263
+	}
264
+	
265
+	private void format(StringBuilder result, CallArguments arguments) {
266
+		if (arguments.typeArguments.length > 0) {
267
+			result.append("<");
268
+			
269
+			int index = 0;
270
+			for (ITypeID typeArgument : arguments.typeArguments) {
271
+				if (index > 0)
272
+					result.append(", ");
273
+				result.append(typeArgument.accept(typeFormatter));
274
+				index++;
275
+			}
276
+			result.append(">");
277
+		}
278
+		result.append("(");
279
+		int index = 0;
280
+		for (Expression argument : arguments.arguments) {
281
+			if (index > 0)
282
+				result.append(", ");
283
+			result.append(argument.accept(this).value);
284
+			index++;
285
+		}
286
+		result.append(")");
287
+	}
288
+
289
+	@Override
290
+	public ExpressionString visitCapturedClosure(CapturedClosureExpression expression) {
291
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
292
+	}
293
+
294
+	@Override
295
+	public ExpressionString visitCapturedDirect(CapturedDirectExpression expression) {
296
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
297
+	}
298
+
299
+	@Override
300
+	public ExpressionString visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
301
+		return new ExpressionString(expression.variable.name, OperatorPriority.PRIMARY);
302
+	}
303
+
304
+	@Override
305
+	public ExpressionString visitCapturedParameter(CapturedParameterExpression expression) {
306
+		return new ExpressionString(expression.parameter.name, OperatorPriority.PRIMARY);
307
+	}
308
+
309
+	@Override
310
+	public ExpressionString visitCapturedThis(CapturedThisExpression expression) {
311
+		return new ExpressionString("this", OperatorPriority.PRIMARY);
312
+	}
313
+
314
+	@Override
315
+	public ExpressionString visitCheckNull(CheckNullExpression expression) {
316
+		return expression.value.accept(this);
317
+	}
318
+
319
+	@Override
320
+	public ExpressionString visitCoalesce(CoalesceExpression expression) {
321
+		return binary(expression.left, expression.right, OperatorPriority.COALESCE, " ?? ");
322
+	}
323
+
324
+	@Override
325
+	public ExpressionString visitConditional(ConditionalExpression expression) {
326
+		StringBuilder result = new StringBuilder();
327
+		result.append(expression.condition.accept(this));
328
+		result.append(" ? ");
329
+		result.append(expression.ifThen.accept(this));
330
+		result.append(" : ");
331
+		result.append(expression.ifElse.accept(this));
332
+		return new ExpressionString(result.toString(), OperatorPriority.TERNARY);
333
+	}
334
+
335
+	@Override
336
+	public ExpressionString visitConstantBool(ConstantBoolExpression expression) {
337
+		return new ExpressionString(expression.value ? "true" : "false", OperatorPriority.PRIMARY);
338
+	}
339
+
340
+	@Override
341
+	public ExpressionString visitConstantByte(ConstantByteExpression expression) {
342
+		return new ExpressionString(Byte.toString(expression.value) + " as byte", OperatorPriority.CAST);
343
+	}
344
+
345
+	@Override
346
+	public ExpressionString visitConstantChar(ConstantCharExpression expression) {
347
+		return new ExpressionString(
348
+				StringUtils.escape(Character.toString(expression.value), '\'', true),
349
+				OperatorPriority.PRIMARY);
350
+	}
351
+
352
+	@Override
353
+	public ExpressionString visitConstantDouble(ConstantDoubleExpression expression) {
354
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
355
+	}
356
+
357
+	@Override
358
+	public ExpressionString visitConstantFloat(ConstantFloatExpression expression) {
359
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
360
+	}
361
+
362
+	@Override
363
+	public ExpressionString visitConstantInt(ConstantIntExpression expression) {
364
+		return new ExpressionString(Integer.toString(expression.value), OperatorPriority.PRIMARY);
365
+	}
366
+
367
+	@Override
368
+	public ExpressionString visitConstantLong(ConstantLongExpression expression) {
369
+		return new ExpressionString(Long.toString(expression.value), OperatorPriority.PRIMARY);
370
+	}
371
+
372
+	@Override
373
+	public ExpressionString visitConstantSByte(ConstantSByteExpression expression) {
374
+		return new ExpressionString(Byte.toString(expression.value) + " as sbyte", OperatorPriority.CAST);
375
+	}
376
+
377
+	@Override
378
+	public ExpressionString visitConstantShort(ConstantShortExpression expression) {
379
+		return new ExpressionString(Integer.toString(expression.value) + " as short", OperatorPriority.CAST);
380
+	}
381
+
382
+	@Override
383
+	public ExpressionString visitConstantString(ConstantStringExpression expression) {
384
+		return new ExpressionString(StringUtils.escape(
385
+				expression.value,
386
+				settings.useSingleQuotesForStrings ? '\'' : '"',
387
+				true), OperatorPriority.CAST);
388
+	}
389
+
390
+	@Override
391
+	public ExpressionString visitConstantUInt(ConstantUIntExpression expression) {
392
+		return new ExpressionString(Integer.toString(expression.value) + " as uint", OperatorPriority.CAST);
393
+	}
394
+
395
+	@Override
396
+	public ExpressionString visitConstantULong(ConstantULongExpression expression) {
397
+		return new ExpressionString(Long.toString(expression.value) + " as ulong", OperatorPriority.CAST);
398
+	}
399
+
400
+	@Override
401
+	public ExpressionString visitConstantUShort(ConstantUShortExpression expression) {
402
+		return new ExpressionString(Integer.toString(expression.value) + " as ushort", OperatorPriority.CAST);
403
+	}
404
+
405
+	@Override
406
+	public ExpressionString visitConstructorThisCall(ConstructorThisCallExpression expression) {
407
+		StringBuilder result = new StringBuilder();
408
+		result.append("this");
409
+		format(result, expression.arguments);
410
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
411
+	}
412
+
413
+	@Override
414
+	public ExpressionString visitConstructorSuperCall(ConstructorSuperCallExpression expression) {
415
+		StringBuilder result = new StringBuilder();
416
+		result.append("super");
417
+		format(result, expression.arguments);
418
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
419
+	}
420
+
421
+	@Override
422
+	public ExpressionString visitEnumConstant(EnumConstantExpression expression) {
423
+		return new ExpressionString(expression.type.accept(typeFormatter) + "." + expression.value.name, OperatorPriority.MEMBER);
424
+	}
425
+
426
+	@Override
427
+	public ExpressionString visitEquals(EqualsExpression expression) {
428
+		return binary(expression.left, expression.right, OperatorPriority.COMPARE, " === ");
429
+	}
430
+
431
+	@Override
432
+	public ExpressionString visitFunction(FunctionExpression expression) {
433
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
434
+	}
435
+
436
+	@Override
437
+	public ExpressionString visitGenericCompare(GenericCompareExpression expression) {
438
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
439
+	}
440
+
441
+	@Override
442
+	public ExpressionString visitGetField(GetFieldExpression expression) {
443
+		StringBuilder result = new StringBuilder();
444
+		result.append(expression.target.accept(this));
445
+		result.append('.');
446
+		result.append(expression.field.name);
447
+		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
448
+	}
449
+
450
+	@Override
451
+	public ExpressionString visitGetFunctionParameter(GetFunctionParameterExpression expression) {
452
+		return new ExpressionString(expression.parameter.name, OperatorPriority.PRIMARY);
453
+	}
454
+
455
+	@Override
456
+	public ExpressionString visitGetLocalVariable(GetLocalVariableExpression expression) {
457
+		return new ExpressionString(expression.variable.name, OperatorPriority.PRIMARY);
458
+	}
459
+
460
+	@Override
461
+	public ExpressionString visitGetStaticField(GetStaticFieldExpression expression) {
462
+		StringBuilder result = new StringBuilder();
463
+		result.append(expression.type.accept(typeFormatter));
464
+		result.append('.');
465
+		result.append(expression.field.name);
466
+		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
467
+	}
468
+
469
+	@Override
470
+	public ExpressionString visitGetter(GetterExpression expression) {
471
+		StringBuilder result = new StringBuilder();
472
+		result.append(expression.target.accept(this));
473
+		result.append('.');
474
+		result.append(expression.getter.name);
475
+		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
476
+	}
477
+
478
+	@Override
479
+	public ExpressionString visitInterfaceCast(InterfaceCastExpression expression) {
480
+		StringBuilder result = new StringBuilder();
481
+		result.append(expression.value.accept(this).value);
482
+		result.append(" as ");
483
+		result.append(expression.type.accept(typeFormatter));
484
+		return new ExpressionString(result.toString(), OperatorPriority.CAST);
485
+	}
486
+
487
+	@Override
488
+	public ExpressionString visitIs(IsExpression expression) {
489
+		StringBuilder result = new StringBuilder();
490
+		result.append(expression.value.accept(this).value);
491
+		result.append(" is ");
492
+		result.append(expression.type.accept(typeFormatter));
493
+		return new ExpressionString(result.toString(), OperatorPriority.COMPARE);
494
+	}
495
+
496
+	@Override
497
+	public ExpressionString visitMakeConst(MakeConstExpression expression) {
498
+		return expression.accept(this);
499
+	}
500
+
501
+	@Override
502
+	public ExpressionString visitMap(MapExpression expression) {
503
+		StringBuilder result = new StringBuilder();
504
+		result.append("{");
505
+		for (int i = 0; i < expression.keys.length; i++) {
506
+			if (i > 0)
507
+				result.append(", ");
508
+			result.append(expression.keys[i].accept(this));
509
+			result.append(": ");
510
+			result.append(expression.values[i].accept(this));
511
+		}
512
+		result.append("}");
513
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
514
+	}
515
+
516
+	@Override
517
+	public ExpressionString visitNew(NewExpression expression) {
518
+		StringBuilder result = new StringBuilder();
519
+		result.append("new ");
520
+		result.append(expression.type.accept(typeFormatter));
521
+		format(result, expression.arguments);
522
+		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
523
+	}
524
+
525
+	@Override
526
+	public ExpressionString visitNot(NotExpression expression) {
527
+		return unaryPrefix(expression.value, OperatorPriority.NOT, "!");
528
+	}
529
+
530
+	@Override
531
+	public ExpressionString visitNull(NullExpression expression) {
532
+		return new ExpressionString("null", OperatorPriority.PRIMARY);
533
+	}
534
+
535
+	@Override
536
+	public ExpressionString visitOrOr(OrOrExpression expression) {
537
+		return binary(expression.left, expression.right, OperatorPriority.OROR, " || ");
538
+	}
539
+
540
+	@Override
541
+	public ExpressionString visitRange(RangeExpression expression) {
542
+		return binary(expression.from, expression.to, OperatorPriority.RANGE, " .. ");
543
+	}
544
+
545
+	@Override
546
+	public ExpressionString visitSetField(SetFieldExpression expression) {
547
+		return new ExpressionString(
548
+				expression.target.accept(this) + "." + expression.field.name + " = " + expression.value.accept(this).value, 
549
+				OperatorPriority.ASSIGN);
550
+	}
551
+
552
+	@Override
553
+	public ExpressionString visitSetFunctionParameter(SetFunctionParameterExpression expression) {
554
+		return new ExpressionString(
555
+				expression.parameter.name + " = " + expression.value.accept(this).value,
556
+				OperatorPriority.ASSIGN);
557
+	}
558
+
559
+	@Override
560
+	public ExpressionString visitSetLocalVariable(SetLocalVariableExpression expression) {
561
+		return new ExpressionString(
562
+				expression.variable.name + " = " + expression.value.accept(this).value,
563
+				OperatorPriority.ASSIGN);
564
+	}
565
+
566
+	@Override
567
+	public ExpressionString visitSetStaticField(SetStaticFieldExpression expression) {
568
+		return new ExpressionString(
569
+				expression.type.accept(typeFormatter) + "." + expression.field.name + " = " + expression.value.accept(this).value,
570
+				OperatorPriority.ASSIGN);
571
+	}
572
+
573
+	@Override
574
+	public ExpressionString visitSetter(SetterExpression expression) {
575
+		return new ExpressionString(
576
+				expression.target.accept(this) + "." + expression.setter.name + " = " + expression.value.accept(this),
577
+				OperatorPriority.ASSIGN);
578
+	}
579
+
580
+	@Override
581
+	public ExpressionString visitStaticGetter(StaticGetterExpression expression) {
582
+		return new ExpressionString(
583
+				expression.type.accept(typeFormatter) + "." + expression.getter.name, 
584
+				OperatorPriority.MEMBER);
585
+	}
586
+
587
+	@Override
588
+	public ExpressionString visitStaticSetter(StaticSetterExpression expression) {
589
+		return new ExpressionString(
590
+				expression.type.accept(typeFormatter) + "." + expression.setter.name + " = " + expression.setter.name,
591
+				OperatorPriority.ASSIGN);
592
+	}
593
+
594
+	@Override
595
+	public ExpressionString visitThis(ThisExpression expression) {
596
+		return new ExpressionString("this", OperatorPriority.PRIMARY);
597
+	}
598
+
599
+	@Override
600
+	public ExpressionString visitWrapOptional(WrapOptionalExpression expression) {
601
+		return expression.value.accept(this);
602
+	}
603
+	
604
+	private ExpressionString unaryPrefix(Expression value, OperatorPriority operator, String operatorString) {
605
+		return new ExpressionString(operatorString + value.accept(this).value, operator);
606
+	}
607
+	
608
+	private ExpressionString unaryPostfix(Expression value, OperatorPriority operator, String operatorString) {
609
+		return new ExpressionString(value.accept(this).value + operatorString, operator);
610
+	}
611
+	
612
+	private ExpressionString binary(Expression left, Expression right, OperatorPriority operator, String operatorString) {
613
+		String value = left.accept(this).wrapLeft(operator)
614
+				+ operatorString + right.accept(this).wrapRight(operator);
615
+		return new ExpressionString(value, operator);
616
+	}
617
+}

+ 33
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionString.java View File

@@ -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.formatter;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class ExpressionString {
13
+	public final String value;
14
+	public final OperatorPriority priority;
15
+	
16
+	public ExpressionString(String value, OperatorPriority priority) {
17
+		this.value = value;
18
+		this.priority = priority;
19
+	}
20
+	
21
+	public String wrapLeft(OperatorPriority outer) {
22
+		return OperatorPriority.shouldWrapLeft(priority, outer) ? "(" + value + ")" : value;
23
+	}
24
+	
25
+	public String wrapRight(OperatorPriority outer) {
26
+		return OperatorPriority.shouldWrapRight(priority, outer) ? "(" + value + ")" : value;
27
+	}
28
+	
29
+	@Override
30
+	public String toString() {
31
+		return value;
32
+	}
33
+}

+ 91
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FileFormatter.java View File

@@ -0,0 +1,91 @@
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.formatter;
7
+
8
+import java.util.ArrayList;
9
+import java.util.Collections;
10
+import java.util.HashMap;
11
+import java.util.List;
12
+import java.util.Map;
13
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
+import org.openzen.zenscript.codemodel.ScriptBlock;
15
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
16
+import org.openzen.zenscript.codemodel.statement.Statement;
17
+
18
+/**
19
+ *
20
+ * @author Hoofdgebruiker
21
+ */
22
+public class FileFormatter {
23
+	private final FormattingSettings settings;
24
+	
25
+	public FileFormatter(FormattingSettings settings) {
26
+		this.settings = settings;
27
+	}
28
+	
29
+	// Formats the given scripts and definitions into a file
30
+	public String format(ZSPackage pkg, ScriptBlock script, List<HighLevelDefinition> definitions) {
31
+		FileImporter importer = new FileImporter(pkg);
32
+		
33
+		TypeFormatter typeFormatter = new TypeFormatter(settings, importer);
34
+		ExpressionFormatter expressionFormatter = new ExpressionFormatter(settings, typeFormatter);
35
+		
36
+		StatementFormatter scriptFormatter = new StatementFormatter("", settings, expressionFormatter);
37
+		for (Statement statement : script.statements) {
38
+			statement.accept(scriptFormatter);
39
+		}
40
+		
41
+		StringBuilder output = new StringBuilder();
42
+		importer.write(output);
43
+		output.append(scriptFormatter.toString().trim());
44
+		return output.toString();
45
+	}
46
+	
47
+	private class FileImporter implements Importer {
48
+		private final ZSPackage pkg;
49
+		private final Map<String, HighLevelDefinition> imports = new HashMap<>();
50
+		
51
+		public FileImporter(ZSPackage pkg) {
52
+			this.pkg = pkg;
53
+		}
54
+		
55
+		public void write(StringBuilder output) {
56
+			Map<ZSPackage, List<HighLevelDefinition>> importsByPackage = new HashMap<>();
57
+			for (HighLevelDefinition definition : imports.values()) {
58
+				if (!importsByPackage.containsKey(definition.pkg))
59
+					importsByPackage.put(definition.pkg, new ArrayList<>());
60
+				
61
+				importsByPackage.get(definition.pkg).add(definition);
62
+			}
63
+			
64
+			List<ZSPackage> sortedPackages = new ArrayList<>(importsByPackage.keySet());
65
+			Collections.sort(sortedPackages, (a, b) -> a.fullName.compareTo(b.fullName));
66
+			for (ZSPackage pkg : sortedPackages) {
67
+				for (HighLevelDefinition definition : importsByPackage.get(pkg))
68
+					output.append("import ").append(pkg.fullName).append('.').append(definition.name).append(";\n");
69
+			}
70
+			
71
+			if (imports.size() > 0)
72
+				output.append("\n");
73
+		}
74
+		
75
+		@Override
76
+		public String importDefinition(HighLevelDefinition definition) {
77
+			if (definition.pkg == pkg)
78
+				return definition.name;
79
+			
80
+			if (imports.get(definition.name) == definition)
81
+				return definition.name;
82
+			
83
+			if (imports.containsKey(definition.name) || pkg.contains(definition.name)) {
84
+				return definition.pkg.fullName + '.' + definition.name;
85
+			} else {
86
+				imports.put(definition.name, definition);
87
+				return definition.name;
88
+			}
89
+		}
90
+	}
91
+}

+ 272
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FormattingSettings.java View File

@@ -0,0 +1,272 @@
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.formatter;
7
+
8
+import org.openzen.zenscript.shared.StringUtils;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public class FormattingSettings {
15
+	public final boolean showAnyInFunctionHeaders;
16
+	public final boolean useSingleQuotesForStrings;
17
+	public final boolean useTabs; // use tabs instead of spaces
18
+	public final int spacesPerTab; // number of spaces per tab
19
+	public final boolean spaceBeforeLabelColon;
20
+	public final boolean spaceAfterLabelColon;
21
+	public final boolean bracketsAroundConditions;
22
+	public final boolean ifElseForceBrackets;
23
+	public final boolean ifElseAvoidBrackets;
24
+	public final boolean loopForceBrackets;
25
+	public final boolean loopAvoidBrackets;
26
+	public final boolean tryCatchForceBrackets;
27
+	public final boolean tryCatchAvoidBrackets;
28
+	public final boolean ifSingleLineOnSameLine;
29
+	public final boolean elseSingleLineOnSameLine;
30
+	public final boolean loopSingleLineOnSameLine;
31
+	public final boolean ifBracketOnSameLine;
32
+	public final boolean elseBracketOnSameLine;
33
+	public final boolean loopBracketOnSameLine;
34
+	public final boolean tryCatchNewLine; // new line between } and catch or } and finally
35
+	public final boolean tryCatchBracketOnSameLine;
36
+	
37
+	public final String indent;
38
+	
39
+	private FormattingSettings(Builder builder) {
40
+		showAnyInFunctionHeaders = builder.showAnyInFunctionHeaders;
41
+		useSingleQuotesForStrings = builder.useSingleQuotesForStrings;
42
+		useTabs = builder.useTabs;
43
+		spacesPerTab = builder.spacesPerTab;
44
+		spaceBeforeLabelColon = builder.spaceBeforeLabelColon;
45
+		spaceAfterLabelColon = builder.spaceAfterLabelColon;
46
+		bracketsAroundConditions = builder.bracketsAroundConditions;
47
+		ifElseForceBrackets = builder.ifElseForceBrackets;
48
+		ifElseAvoidBrackets = builder.ifElseAvoidBrackets;
49
+		loopForceBrackets = builder.loopForceBrackets;
50
+		loopAvoidBrackets = builder.loopAvoidBrackets;
51
+		tryCatchForceBrackets = builder.tryCatchForceBrackets;
52
+		tryCatchAvoidBrackets = builder.tryCatchAvoidBrackets;
53
+		ifSingleLineOnSameLine = builder.ifSingleLineOnSameLine;
54
+		elseSingleLineOnSameLine = builder.elseSingleLineOnSameLine;
55
+		loopSingleLineOnSameLine = builder.loopSingleLineOnSameLine;
56
+		ifBracketOnSameLine = builder.ifBracketOnSameLine;
57
+		elseBracketOnSameLine = builder.elseBracketOnSameLine;
58
+		loopBracketOnSameLine = builder.loopBracketOnSameLine;
59
+		tryCatchNewLine = builder.tryCatchNewLine;
60
+		tryCatchBracketOnSameLine = builder.tryCatchBracketOnSameLine;
61
+		
62
+		if (useTabs) {
63
+			indent = "\t";
64
+		} else {
65
+			indent = StringUtils.times(' ', spacesPerTab);
66
+		}
67
+	}
68
+	
69
+	public String getSingleLineSeparator(String indent, ParentStatementType position) {
70
+		switch (position) {
71
+			case NONE:
72
+				return "\n" + indent;
73
+				
74
+			case IF:
75
+			case IF_WITH_ELSE:
76
+				if (ifSingleLineOnSameLine)
77
+					return " ";
78
+				else
79
+					return "\n" + indent + this.indent;
80
+				
81
+			case ELSE:
82
+				if (elseSingleLineOnSameLine)
83
+					return " ";
84
+				else
85
+					return "\n" + indent + this.indent;
86
+				
87
+			case LOOP:
88
+				if (loopSingleLineOnSameLine)
89
+					return " ";
90
+				else
91
+					return "\n" + indent + this.indent;
92
+				
93
+			case TRY:
94
+			case CATCH:
95
+			case FINALLY:
96
+				return "\n" + indent + this.indent;
97
+			
98
+			default:
99
+				return "\n" + indent + this.indent;
100
+		}
101
+	}
102
+	
103
+	public String getBlockSeparator(String indent, ParentStatementType position) {
104
+		switch (position) {
105
+			case NONE:
106
+				return "\n" + indent + "{";
107
+				
108
+			case IF:
109
+			case IF_WITH_ELSE:
110
+				if (ifBracketOnSameLine)
111
+					return " {";
112
+				else
113
+					return "\n" + indent + "{";
114
+				
115
+			case ELSE:
116
+				if (elseBracketOnSameLine)
117
+					return " {";
118
+				else
119
+					return "\n" + indent + "{";
120
+				
121
+			case LOOP:
122
+				if (loopBracketOnSameLine)
123
+					return " {";
124
+				else
125
+					return "\n" + indent + "{";
126
+			
127
+			case TRY:
128
+			case CATCH:
129
+			case FINALLY:
130
+				if (tryCatchBracketOnSameLine)
131
+					return " {";
132
+				else
133
+					return "\n" + indent + "{";
134
+			
135
+			default:
136
+				return "\n" + indent + this.indent;
137
+		}
138
+	}
139
+	
140
+	public static class Builder {
141
+		private boolean showAnyInFunctionHeaders = false;
142
+		private boolean useSingleQuotesForStrings = true;
143
+		private boolean useTabs = false;
144
+		private int spacesPerTab = 4;
145
+		private boolean spaceBeforeLabelColon = true;
146
+		private boolean spaceAfterLabelColon = false;
147
+		private boolean bracketsAroundConditions = false;
148
+		private boolean ifElseForceBrackets = false;
149
+		private boolean ifElseAvoidBrackets = true;
150
+		private boolean loopForceBrackets = false;
151
+		private boolean loopAvoidBrackets = true;
152
+		private boolean tryCatchForceBrackets = true;
153
+		private boolean tryCatchAvoidBrackets = false;
154
+		private boolean ifSingleLineOnSameLine = false;
155
+		private boolean elseSingleLineOnSameLine = false;
156
+		private boolean loopSingleLineOnSameLine = false;
157
+		private boolean ifBracketOnSameLine = true;
158
+		private boolean elseBracketOnSameLine = true;
159
+		private boolean loopBracketOnSameLine = true;
160
+		private boolean tryCatchNewLine = true;
161
+		private boolean tryCatchBracketOnSameLine = true;
162
+		
163
+		public Builder showAnyInFunctionHeaders(boolean show) {
164
+			showAnyInFunctionHeaders = show;
165
+			return this;
166
+		}
167
+		
168
+		public Builder useSingleQuotesForStrings(boolean single) {
169
+			useSingleQuotesForStrings = single;
170
+			return this;
171
+		}
172
+		
173
+		public Builder useTabs(boolean tabs) {
174
+			useTabs = tabs;
175
+			return this;
176
+		}
177
+		
178
+		public Builder spacesPerTabs(int spaces) {
179
+			spacesPerTab = spaces;
180
+			return this;
181
+		}
182
+		
183
+		public Builder spaceBeforeLabelColon(boolean space) {
184
+			spaceBeforeLabelColon = space;
185
+			return this;
186
+		}
187
+		
188
+		public Builder spaceAfterLabelColon(boolean space) {
189
+			spaceAfterLabelColon = space;
190
+			return this;
191
+		}
192
+		
193
+		public Builder bracketsAroundConditions(boolean brackets) {
194
+			bracketsAroundConditions = brackets;
195
+			return this;
196
+		}
197
+		
198
+		public Builder ifElseForceBrackets(boolean force) {
199
+			ifElseForceBrackets = force;
200
+			return this;
201
+		}
202
+		
203
+		public Builder ifElseAvoidBrackets(boolean avoid) {
204
+			ifElseAvoidBrackets = avoid;
205
+			return this;
206
+		}
207
+		
208
+		public Builder loopForceBrackets(boolean force) {
209
+			loopForceBrackets = force;
210
+			return this;
211
+		}
212
+		
213
+		public Builder loopAvoidBrackets(boolean avoid) {
214
+			loopAvoidBrackets = avoid;
215
+			return this;
216
+		}
217
+		
218
+		public Builder tryCatchForceBrackets(boolean force) {
219
+			tryCatchForceBrackets = force;
220
+			return this;
221
+		}
222
+		
223
+		public Builder tryCatchAvoidBrackets(boolean avoid) {
224
+			tryCatchAvoidBrackets = avoid;
225
+			return this;
226
+		}
227
+		
228
+		public Builder ifSingleLineOnSameLine(boolean sameLine) {
229
+			ifSingleLineOnSameLine = sameLine;
230
+			return this;
231
+		}
232
+		
233
+		public Builder elseSingleLineOnSameLine(boolean sameLine) {
234
+			elseSingleLineOnSameLine = sameLine;
235
+			return this;
236
+		}
237
+		
238
+		public Builder loopSingleLineOnSameLine(boolean sameLine) {
239
+			loopSingleLineOnSameLine = sameLine;
240
+			return this;
241
+		}
242
+		
243
+		public Builder ifBracketOnSameLine(boolean sameLine) {
244
+			ifBracketOnSameLine = sameLine;
245
+			return this;
246
+		}
247
+		
248
+		public Builder elseBracketOnSameLine(boolean sameLine) {
249
+			elseBracketOnSameLine = sameLine;
250
+			return this;
251
+		}
252
+		
253
+		public Builder loopBracketOnSameLine(boolean sameLine) {
254
+			loopBracketOnSameLine = sameLine;
255
+			return this;
256
+		}
257
+		
258
+		public Builder tryCatchNewLine(boolean newLine) {
259
+			tryCatchNewLine = newLine;
260
+			return this;
261
+		}
262
+		
263
+		public Builder tryCatchBracketOnSameLine(boolean sameLine) {
264
+			tryCatchBracketOnSameLine = sameLine;
265
+			return this;
266
+		}
267
+		
268
+		public FormattingSettings build() {
269
+			return new FormattingSettings(this);
270
+		}
271
+	}
272
+}

+ 16
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/Importer.java View File

@@ -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.formatter;
7
+
8
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public interface Importer {
15
+	String importDefinition(HighLevelDefinition definition);
16
+}

+ 73
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java View File

@@ -0,0 +1,73 @@
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.formatter;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public enum OperatorPriority {
13
+	ADD(6),
14
+	SUB(6),
15
+	MUL(7),
16
+	DIV(7),
17
+	MOD(7),
18
+	CAT(6),
19
+	OR(4),
20
+	AND(4),
21
+	XOR(4),
22
+	NEG(8),
23
+	NOT(8),
24
+	INVERT(8),
25
+	CONTAINS(5),
26
+	COMPARE(5),
27
+	ASSIGN(0),
28
+	ADDASSIGN(0),
29
+	SUBASSIGN(0),
30
+	MULASSIGN(0),
31
+	DIVASSIGN(0),
32
+	MODASSIGN(0),
33
+	CATASSIGN(0),
34
+	ORASSIGN(0),
35
+	ANDASSIGN(0),
36
+	XORASSIGN(0),
37
+	
38
+	ANDAND(3),
39
+	OROR(2),
40
+	
41
+	TERNARY(1),
42
+	COALESCE(2),
43
+	
44
+	INCREMENT(8),
45
+	DECREMENT(8),
46
+	MEMBER(9),
47
+	RANGE(9),
48
+	INDEX(9),
49
+	CALL(9),
50
+	CAST(9),
51
+	
52
+	PRIMARY(10);
53
+	
54
+	private final int priority;
55
+	private final boolean isCommutative;
56
+	
57
+	private OperatorPriority(int priority) {
58
+		this(priority, false);
59
+	}
60
+	
61
+	private OperatorPriority(int priority, boolean isCommutative) {
62
+		this.priority = priority;
63
+		this.isCommutative = isCommutative;
64
+	}
65
+	
66
+	public static boolean shouldWrapLeft(OperatorPriority inner, OperatorPriority outer) {
67
+		return inner == outer || inner.priority > outer.priority;
68
+	}
69
+	
70
+	public static boolean shouldWrapRight(OperatorPriority inner, OperatorPriority outer) {
71
+		return (inner == outer && inner.isCommutative) || (inner.priority > outer.priority);
72
+	}
73
+}

+ 21
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ParentStatementType.java View File

@@ -0,0 +1,21 @@
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.formatter;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public enum ParentStatementType {
13
+	NONE,
14
+	IF,
15
+	IF_WITH_ELSE,
16
+	ELSE,
17
+	LOOP,
18
+	TRY,
19
+	CATCH,
20
+	FINALLY;
21
+}

+ 296
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/StatementFormatter.java View File

@@ -0,0 +1,296 @@
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.formatter;
7
+
8
+import org.openzen.zenscript.codemodel.expression.Expression;
9
+import org.openzen.zenscript.codemodel.statement.BlockStatement;
10
+import org.openzen.zenscript.codemodel.statement.BreakStatement;
11
+import org.openzen.zenscript.codemodel.statement.CatchClause;
12
+import org.openzen.zenscript.codemodel.statement.ContinueStatement;
13
+import org.openzen.zenscript.codemodel.statement.DoWhileStatement;
14
+import org.openzen.zenscript.codemodel.statement.EmptyStatement;
15
+import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
16
+import org.openzen.zenscript.codemodel.statement.ForeachStatement;
17
+import org.openzen.zenscript.codemodel.statement.IfStatement;
18
+import org.openzen.zenscript.codemodel.statement.LockStatement;
19
+import org.openzen.zenscript.codemodel.statement.ReturnStatement;
20
+import org.openzen.zenscript.codemodel.statement.Statement;
21
+import org.openzen.zenscript.codemodel.statement.StatementVisitor;
22
+import org.openzen.zenscript.codemodel.statement.ThrowStatement;
23
+import org.openzen.zenscript.codemodel.statement.TryCatchStatement;
24
+import org.openzen.zenscript.codemodel.statement.VarStatement;
25
+import org.openzen.zenscript.codemodel.statement.WhileStatement;
26
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
27
+
28
+/**
29
+ *
30
+ * @author Hoofdgebruiker
31
+ */
32
+public class StatementFormatter implements StatementVisitor<Void> {
33
+	private final FormattingSettings settings;
34
+	private final StringBuilder result;
35
+	private final ExpressionFormatter expressionFormatter;
36
+	
37
+	private String indent;
38
+	private ParentStatementType position = ParentStatementType.NONE;
39
+	
40
+	public StatementFormatter(String indent, FormattingSettings settings, ExpressionFormatter expressionFormatter) {
41
+		this.indent = indent;
42
+		this.settings = settings;
43
+		result = new StringBuilder();
44
+		this.expressionFormatter = expressionFormatter;
45
+	}
46
+	
47
+	@Override
48
+	public String toString() {
49
+		return result.toString();
50
+	}
51
+
52
+	@Override
53
+	public Void visitBlock(BlockStatement statement) {
54
+		beginBlock();
55
+		
56
+		String oldIndent = indent;
57
+		indent = oldIndent + settings.indent;
58
+		for (Statement subStatement : statement.statements) {
59
+			format(ParentStatementType.NONE, subStatement);
60
+		}
61
+		indent = oldIndent;
62
+		endBlock();
63
+		return null;
64
+	}
65
+
66
+	@Override
67
+	public Void visitBreak(BreakStatement statement) {
68
+		beginSingleLine();
69
+		result.append("break");
70
+		if (statement.target != null)
71
+			result.append(' ').append(statement.target.label);
72
+		result.append(";");
73
+		endSingleLine();
74
+		return null;
75
+	}
76
+
77
+	@Override
78
+	public Void visitContinue(ContinueStatement statement) {
79
+		beginSingleLine();
80
+		result.append("continue");
81
+		if (statement.target != null)
82
+			result.append(' ').append(statement.target.label);
83
+		result.append(";");
84
+		endSingleLine();
85
+		return null;
86
+	}
87
+
88
+	@Override
89
+	public Void visitDoWhile(DoWhileStatement statement) {
90
+		beginSingleLine();
91
+		result.append("do");
92
+		if (statement.label != null) {
93
+			if (settings.spaceBeforeLabelColon)
94
+				result.append(' ');
95
+			result.append(':');
96
+			if (settings.spaceAfterLabelColon)
97
+				result.append(' ');
98
+			result.append(statement.label);
99
+		}
100
+		format(ParentStatementType.LOOP, statement.content);
101
+		result.append(" while ");
102
+		appendCondition(statement.condition);
103
+		result.append(";");
104
+		endSingleLine();
105
+		return null;
106
+	}
107
+
108
+	@Override
109
+	public Void visitEmpty(EmptyStatement statement) {
110
+		beginSingleLine();
111
+		result.append(";\n");
112
+		endSingleLine();
113
+		return null;
114
+	}
115
+
116
+	@Override
117
+	public Void visitExpression(ExpressionStatement statement) {
118
+		beginSingleLine();
119
+		result.append(statement.expression.accept(expressionFormatter).value)
120
+			  .append(";");
121
+		endSingleLine();
122
+		return null;
123
+	}
124
+
125
+	@Override
126
+	public Void visitForeach(ForeachStatement statement) {
127
+		beginSingleLine();
128
+		result.append("for ");
129
+		for (int i = 0; i < statement.loopVariables.length; i++) {
130
+			if (i > 0)
131
+				result.append(", ");
132
+			
133
+			result.append(statement.loopVariables[i].name);
134
+		}
135
+		result.append(" in ");
136
+		result.append(statement.list.accept(expressionFormatter).value);
137
+		format(ParentStatementType.LOOP, statement.content);
138
+		endSingleLine();
139
+		return null;
140
+	}
141
+
142
+	@Override
143
+	public Void visitIf(IfStatement statement) {
144
+		ParentStatementType position = this.position;
145
+		beginSingleLine();
146
+		result.append("if ");
147
+		appendCondition(statement.condition);
148
+		format(statement.onElse == null ? ParentStatementType.IF : ParentStatementType.IF_WITH_ELSE, statement.onThen);
149
+		if (statement.onElse != null) {
150
+			result.append("else");
151
+			format(ParentStatementType.ELSE, statement.onElse);
152
+		}
153
+		endSingleLine();
154
+		return null;
155
+	}
156
+
157
+	@Override
158
+	public Void visitLock(LockStatement statement) {
159
+		beginSingleLine();
160
+		result.append("lock ");
161
+		result.append(statement.object.accept(expressionFormatter).value);
162
+		statement.content.accept(this);
163
+		endSingleLine();
164
+		return null;
165
+	}
166
+
167
+	@Override
168
+	public Void visitReturn(ReturnStatement statement) {
169
+		beginSingleLine();
170
+		result.append("return");
171
+		if (statement.value != null) {
172
+			result.append(' ');
173
+			result.append(statement.value.accept(expressionFormatter).value);
174
+		}
175
+		result.append(";");
176
+		endSingleLine();
177
+		return null;
178
+	}
179
+
180
+	@Override
181
+	public Void visitThrow(ThrowStatement statement) {
182
+		beginSingleLine();
183
+		result.append("throw ").append(statement.value.accept(expressionFormatter));
184
+		endSingleLine();
185
+		return null;
186
+	}
187
+
188
+	@Override
189
+	public Void visitTryCatch(TryCatchStatement statement) {
190
+		beginSingleLine();
191
+		result.append("try");
192
+		if (statement.resourceName != null) {
193
+			result.append(' ').append(statement.resourceName);
194
+			result.append(" = ");
195
+			result.append(statement.resourceInitializer.accept(expressionFormatter).value);
196
+		}
197
+		
198
+		format(ParentStatementType.TRY, statement.content);
199
+		
200
+		for (CatchClause catchClause : statement.catchClauses) {
201
+			result.append(indent).append("catch ");
202
+			if (catchClause.exceptionName != null)
203
+				result.append(catchClause.exceptionName);
204
+			if (catchClause.exceptionType != BasicTypeID.ANY) {
205
+				result.append(" as ");
206
+				catchClause.exceptionType.accept(expressionFormatter.typeFormatter);
207
+			}
208
+			
209
+			format(ParentStatementType.CATCH, catchClause.content);
210
+		}
211
+		if (statement.finallyClause != null) {
212
+			result.append(indent).append("finally ");
213
+			
214
+			format(ParentStatementType.FINALLY, statement.finallyClause);
215
+		}
216
+		endSingleLine();
217
+		return null;
218
+	}
219
+
220
+	@Override
221
+	public Void visitVar(VarStatement statement) {
222
+		beginSingleLine();
223
+		result.append(statement.isFinal ? "val " : "var ");
224
+		result.append(statement.name);
225
+		
226
+		if (statement.initializer == null || statement.initializer.type != statement.type) {
227
+			result.append(" as ");
228
+			result.append(statement.type.accept(expressionFormatter.typeFormatter));
229
+		}
230
+		if (statement.initializer != null) {
231
+			result.append(" = ");
232
+			result.append(statement.initializer.accept(expressionFormatter).value);
233
+		}
234
+		result.append(";");
235
+		endSingleLine();
236
+		return null;
237
+	}
238
+
239
+	@Override
240
+	public Void visitWhile(WhileStatement statement) {
241
+		beginSingleLine();
242
+		result.append("while");
243
+		if (statement.label != null) {
244
+			if (settings.spaceBeforeLabelColon)
245
+				result.append(' ');
246
+			result.append(':');
247
+			if (settings.spaceAfterLabelColon)
248
+				result.append(' ');
249
+			result.append(statement.label);
250
+		}
251
+		result.append(' ');
252
+		appendCondition(statement.condition);
253
+		
254
+		format(ParentStatementType.LOOP, statement.content);
255
+		endSingleLine();
256
+		return null;
257
+	}
258
+	
259
+	private void format(ParentStatementType position, Statement statement) {
260
+		ParentStatementType oldPosition = this.position;
261
+		this.position = position;
262
+		statement.accept(this);
263
+		this.position = oldPosition;
264
+	}
265
+	
266
+	private void appendCondition(Expression condition) {
267
+		if (settings.bracketsAroundConditions)
268
+			result.append('(');
269
+		result.append(condition.accept(expressionFormatter).value);
270
+		if (settings.bracketsAroundConditions)
271
+			result.append(')');
272
+	}
273
+	
274
+	private void beginBlock() {
275
+		result.append(settings.getBlockSeparator(indent, position));
276
+	}
277
+	
278
+	private void endBlock() {
279
+		result.append("\n").append(indent).append("}");
280
+		if (position == ParentStatementType.IF_WITH_ELSE) {
281
+			if (settings.elseBracketOnSameLine)
282
+				result.append(" ");
283
+			else
284
+				result.append("\n").append(indent);
285
+		}
286
+	}
287
+	
288
+	private void beginSingleLine() {
289
+		result.append(settings.getSingleLineSeparator(indent, position));
290
+	}
291
+	
292
+	private void endSingleLine() {
293
+		if (position == ParentStatementType.IF_WITH_ELSE)
294
+			result.append("\n").append(indent);
295
+	}
296
+}

+ 171
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java View File

@@ -0,0 +1,171 @@
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.formatter;
7
+
8
+import org.openzen.zenscript.codemodel.FunctionHeader;
9
+import org.openzen.zenscript.codemodel.FunctionParameter;
10
+import org.openzen.zenscript.codemodel.generic.GenericParameterBound;
11
+import org.openzen.zenscript.codemodel.generic.GenericParameterBoundVisitor;
12
+import org.openzen.zenscript.codemodel.generic.ParameterSuperBound;
13
+import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
14
+import org.openzen.zenscript.codemodel.generic.TypeParameter;
15
+import org.openzen.zenscript.codemodel.type.ArrayTypeID;
16
+import org.openzen.zenscript.codemodel.type.AssocTypeID;
17
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
18
+import org.openzen.zenscript.codemodel.type.ConstTypeID;
19
+import org.openzen.zenscript.codemodel.type.DefinitionTypeID;
20
+import org.openzen.zenscript.codemodel.type.FunctionTypeID;
21
+import org.openzen.zenscript.codemodel.type.GenericTypeID;
22
+import org.openzen.zenscript.codemodel.type.ITypeID;
23
+import org.openzen.zenscript.codemodel.type.ITypeVisitor;
24
+import org.openzen.zenscript.codemodel.type.IteratorTypeID;
25
+import org.openzen.zenscript.codemodel.type.OptionalTypeID;
26
+import org.openzen.zenscript.codemodel.type.RangeTypeID;
27
+import org.openzen.zenscript.shared.StringUtils;
28
+
29
+/**
30
+ *
31
+ * @author Hoofdgebruiker
32
+ */
33
+public class TypeFormatter implements ITypeVisitor<String>, GenericParameterBoundVisitor<String> {
34
+	private final FormattingSettings settings;
35
+	private final Importer importer;
36
+	
37
+	public TypeFormatter(FormattingSettings settings, Importer importer) {
38
+		this.settings = settings;
39
+		this.importer = importer;
40
+	}
41
+
42
+	@Override
43
+	public String visitBasic(BasicTypeID basic) {
44
+		return basic.name;
45
+	}
46
+
47
+	@Override
48
+	public String visitArray(ArrayTypeID array) {
49
+		String element = array.elementType.accept(this);
50
+		if (array.dimension == 1) {
51
+			return element + "[]";
52
+		} else {
53
+			return element + "[" + StringUtils.times(',', array.dimension - 1) + "]";
54
+		}
55
+	}
56
+
57
+	@Override
58
+	public String visitAssoc(AssocTypeID assoc) {
59
+		return assoc.valueType.accept(this) + "[" + assoc.keyType.accept(this) + "]";
60
+	}
61
+
62
+	@Override
63
+	public String visitIterator(IteratorTypeID iterator) {
64
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
65
+	}
66
+
67
+	@Override
68
+	public String visitFunction(FunctionTypeID function) {
69
+		StringBuilder result = new StringBuilder();
70
+		result.append("function");
71
+		
72
+		FunctionHeader header = function.header;
73
+		formatTypeParameters(result, header.typeParameters);
74
+		result.append("(");
75
+		int parameterIndex = 0;
76
+		for (FunctionParameter parameter : header.parameters) {
77
+			if (parameterIndex > 0)
78
+				result.append(", ");
79
+			
80
+			result.append(parameter.name);
81
+			if (parameter.variadic)
82
+				result.append("...");
83
+			
84
+			if (!settings.showAnyInFunctionHeaders || parameter.type != BasicTypeID.ANY) {
85
+				result.append(" as ");
86
+				result.append(header.returnType.accept(this));
87
+			}
88
+			
89
+			parameterIndex++;
90
+		}
91
+		result.append(")");
92
+		if (!settings.showAnyInFunctionHeaders || header.returnType != BasicTypeID.ANY) {
93
+			result.append(" as ");
94
+			result.append(header.returnType.accept(this));
95
+		}
96
+		return result.toString();
97
+	}
98
+
99
+	@Override
100
+	public String visitDefinition(DefinitionTypeID definition) {
101
+		String importedName = importer.importDefinition(definition.definition);
102
+		if (definition.typeParameters.length == 0)
103
+			return importedName;
104
+		
105
+		StringBuilder result = new StringBuilder();
106
+		result.append(importedName);
107
+		result.append("<");
108
+		int index = 0;
109
+		for (ITypeID typeParameter : definition.typeParameters) {
110
+			if (index > 0)
111
+				result.append(", ");
112
+			
113
+			result.append(typeParameter.accept(this));
114
+		}
115
+		result.append(">");
116
+		return result.toString();
117
+	}
118
+
119
+	@Override
120
+	public String visitGeneric(GenericTypeID generic) {
121
+		return generic.parameter.name;
122
+	}
123
+
124
+	@Override
125
+	public String visitRange(RangeTypeID range) {
126
+		return range.from.accept(this) + " .. " + range.to.accept(this);
127
+	}
128
+
129
+	@Override
130
+	public String visitConst(ConstTypeID type) {
131
+		return "const " + type.accept(this);
132
+	}
133
+
134
+	@Override
135
+	public String visitOptional(OptionalTypeID optional) {
136
+		return optional.accept(this) + "?";
137
+	}
138
+	
139
+	private void formatTypeParameters(StringBuilder result, TypeParameter[] parameters) {
140
+		if (parameters.length > 0) {
141
+			result.append("<");
142
+			int index = 0;
143
+			for (TypeParameter parameter : parameters) {
144
+				if (index > 0)
145
+					result.append(", ");
146
+				
147
+				result.append(parameter.name);
148
+				
149
+				if (parameter.bounds.size() > 0) {
150
+					for (GenericParameterBound bound : parameter.bounds) {
151
+						result.append(": ");
152
+						result.append(bound.accept(this));
153
+					}
154
+				}
155
+				
156
+				index++;
157
+			}
158
+			result.append(">");
159
+		}
160
+	}
161
+
162
+	@Override
163
+	public String visitSuper(ParameterSuperBound bound) {
164
+		return "super " + bound.type.accept(this);
165
+	}
166
+
167
+	@Override
168
+	public String visitType(ParameterTypeBound bound) {
169
+		return bound.type.accept(this);
170
+	}
171
+}

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionHeader.java View File

@@ -38,12 +38,18 @@ public class FunctionHeader {
38 38
 		this.typeParameters = NO_GENERIC_PARAMETERS;
39 39
 		this.returnType = returnType;
40 40
 		this.parameters = parameters;
41
+		
42
+		for (int i = 0; i < parameters.length; i++)
43
+			parameters[i].index = i;
41 44
 	}
42 45
 	
43 46
 	public FunctionHeader(TypeParameter[] genericParameters, ITypeID returnType, FunctionParameter[] parameters) {
44 47
 		this.typeParameters = genericParameters;
45 48
 		this.returnType = returnType;
46 49
 		this.parameters = parameters;
50
+		
51
+		for (int i = 0; i < parameters.length; i++)
52
+			parameters[i].index = i;
47 53
 	}
48 54
 	
49 55
 	public FunctionHeader instance(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> mapping) {

+ 1
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java View File

@@ -17,6 +17,7 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
17 17
  * @author Hoofdgebruiker
18 18
  */
19 19
 public class FunctionParameter {
20
+	public int index = -1;
20 21
 	public final ITypeID type;
21 22
 	public final String name;
22 23
 	public final Expression defaultValue;

+ 4
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.codemodel;
8 8
 import java.util.ArrayList;
9 9
 import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.definition.DefinitionVisitor;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
11 12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 13
 import org.openzen.zenscript.codemodel.member.FieldMember;
13 14
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
@@ -19,6 +20,7 @@ import org.openzen.zenscript.shared.Taggable;
19 20
  * @author Hoofdgebruiker
20 21
  */
21 22
 public abstract class HighLevelDefinition extends Taggable {
23
+	public final ZSPackage pkg;
22 24
 	public final String name;
23 25
 	public final int modifiers;
24 26
 	public final List<IDefinitionMember> members = new ArrayList<>();
@@ -27,7 +29,8 @@ public abstract class HighLevelDefinition extends Taggable {
27 29
 	public HighLevelDefinition outerDefinition;
28 30
 	public ITypeID superType;
29 31
 	
30
-	public HighLevelDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
32
+	public HighLevelDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
33
+		this.pkg = pkg;
31 34
 		this.name = name;
32 35
 		this.modifiers = modifiers;
33 36
 		this.outerDefinition = outerDefinition;

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/AliasDefinition.java View File

@@ -15,8 +15,8 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
15 15
 public class AliasDefinition extends HighLevelDefinition {
16 16
 	public ITypeID type;
17 17
 	
18
-	public AliasDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
19
-		super(name, modifiers, outerDefinition);
18
+	public AliasDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
19
+		super(pkg, name, modifiers, outerDefinition);
20 20
 	}
21 21
 	
22 22
 	public void setType(ITypeID type) {

+ 4
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ClassDefinition.java View File

@@ -13,12 +13,12 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
13 13
  * @author Hoofdgebruiker
14 14
  */
15 15
 public class ClassDefinition extends HighLevelDefinition {
16
-	public ClassDefinition(String name, int modifiers) {
17
-		this(name, modifiers, null);
16
+	public ClassDefinition(ZSPackage pkg, String name, int modifiers) {
17
+		this(pkg, name, modifiers, null);
18 18
 	}
19 19
 	
20
-	public ClassDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
21
-		super(name, modifiers, outerDefinition);
20
+	public ClassDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
21
+		super(pkg, name, modifiers, outerDefinition);
22 22
 	}
23 23
 	
24 24
 	public void setSuperclass(ITypeID superclass) {

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/EnumDefinition.java View File

@@ -12,8 +12,8 @@ import org.openzen.zenscript.codemodel.HighLevelDefinition;
12 12
  * @author Hoofdgebruiker
13 13
  */
14 14
 public class EnumDefinition extends HighLevelDefinition {
15
-	public EnumDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
16
-		super(name, modifiers, outerDefinition);
15
+	public EnumDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
16
+		super(pkg, name, modifiers, outerDefinition);
17 17
 	}
18 18
 
19 19
 	@Override

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ExpansionDefinition.java View File

@@ -15,8 +15,8 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
15 15
 public class ExpansionDefinition extends HighLevelDefinition {
16 16
 	public ITypeID target;
17 17
 	
18
-	public ExpansionDefinition(int modifiers, HighLevelDefinition outerDefinition) {
19
-		super(null, modifiers, outerDefinition);
18
+	public ExpansionDefinition(ZSPackage pkg, int modifiers, HighLevelDefinition outerDefinition) {
19
+		super(pkg, null, modifiers, outerDefinition);
20 20
 	}
21 21
 
22 22
 	@Override

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java View File

@@ -18,8 +18,8 @@ public class FunctionDefinition extends HighLevelDefinition {
18 18
 	public FunctionHeader header;
19 19
 	public List<Statement> statements;
20 20
 	
21
-	public FunctionDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
22
-		super(name, modifiers, outerDefinition);
21
+	public FunctionDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
22
+		super(pkg, name, modifiers, outerDefinition);
23 23
 	}
24 24
 	
25 25
 	public void setHeader(FunctionHeader header) {

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/InterfaceDefinition.java View File

@@ -17,8 +17,8 @@ import org.openzen.zenscript.codemodel.type.ITypeID;
17 17
 public class InterfaceDefinition extends HighLevelDefinition {
18 18
 	private final List<ITypeID> baseInterfaces = new ArrayList<>();
19 19
 	
20
-	public InterfaceDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
21
-		super(name, modifiers, outerDefinition);
20
+	public InterfaceDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
21
+		super(pkg, name, modifiers, outerDefinition);
22 22
 	}
23 23
 	
24 24
 	public void addBaseInterface(ITypeID baseInterface) {

+ 2
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/StructDefinition.java View File

@@ -12,8 +12,8 @@ import org.openzen.zenscript.codemodel.HighLevelDefinition;
12 12
  * @author Hoofdgebruiker
13 13
  */
14 14
 public class StructDefinition extends HighLevelDefinition {
15
-	public StructDefinition(String name, int modifiers, HighLevelDefinition outerDefinition) {
16
-		super(name, modifiers, outerDefinition);
15
+	public StructDefinition(ZSPackage pkg, String name, int modifiers, HighLevelDefinition outerDefinition) {
16
+		super(pkg, name, modifiers, outerDefinition);
17 17
 	}
18 18
 
19 19
 	@Override

+ 11
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/ZSPackage.java View File

@@ -25,6 +25,12 @@ import org.openzen.zenscript.codemodel.scope.TypeScope;
25 25
  * @author Hoofdgebruiker
26 26
  */
27 27
 public class ZSPackage {
28
+	public final String fullName;
29
+	
30
+	public ZSPackage(String fullName) {
31
+		this.fullName = fullName;
32
+	}
33
+	
28 34
 	private final Map<String, ZSPackage> subPackages = new HashMap<>();
29 35
 	private final Map<String, HighLevelDefinition> types = new HashMap<>();
30 36
 	
@@ -42,6 +48,10 @@ public class ZSPackage {
42 48
 		return null;
43 49
 	}
44 50
 	
51
+	public boolean contains(String name) {
52
+		return types.containsKey(name) || subPackages.containsKey(name);
53
+	}
54
+	
45 55
 	public HighLevelDefinition getImport(List<String> name, int depth) {
46 56
 		if (depth >= name.size())
47 57
 			return null;
@@ -83,7 +93,7 @@ public class ZSPackage {
83 93
 		if (subPackages.containsKey(name))
84 94
 			return subPackages.get(name);
85 95
 		
86
-		ZSPackage result = new ZSPackage();
96
+		ZSPackage result = new ZSPackage(fullName.isEmpty() ? name : fullName + '.' + name);
87 97
 		subPackages.put(name, result);
88 98
 		return result;
89 99
 	}

+ 0
- 4
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java View File

@@ -116,10 +116,6 @@ public interface ExpressionVisitor<T> {
116 116
 	
117 117
 	public T visitStaticSetter(StaticSetterExpression expression);
118 118
 	
119
-	public T visitStringConcat(StringConcatExpression expression);
120
-	
121
-	public T visitSubstring(SubstringExpression expression);
122
-	
123 119
 	public T visitThis(ThisExpression expression);
124 120
 	
125 121
 	public T visitWrapOptional(WrapOptionalExpression expression);

+ 0
- 31
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/StringConcatExpression.java View File

@@ -1,31 +0,0 @@
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.expression;
7
-
8
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
9
-import org.openzen.zenscript.shared.CodePosition;
10
-
11
-/**
12
- *
13
- * @author Hoofdgebruiker
14
- */
15
-public class StringConcatExpression extends Expression {
16
-	// either of these must be a basic type
17
-	public final Expression left;
18
-	public final Expression right;
19
-	
20
-	public StringConcatExpression(CodePosition position, Expression left, Expression right) {
21
-		super(position, BasicTypeID.STRING);
22
-		
23
-		this.left = left;
24
-		this.right = right;
25
-	}
26
-
27
-	@Override
28
-	public <T> T accept(ExpressionVisitor<T> visitor) {
29
-		return visitor.visitStringConcat(this);
30
-	}
31
-}

+ 0
- 29
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SubstringExpression.java View File

@@ -1,29 +0,0 @@
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.expression;
7
-
8
-import org.openzen.zenscript.shared.CodePosition;
9
-
10
-/**
11
- *
12
- * @author Hoofdgebruiker
13
- */
14
-public class SubstringExpression extends Expression {
15
-	public final Expression value;
16
-	public final Expression range;
17
-	
18
-	public SubstringExpression(CodePosition position, Expression value, Expression range) {
19
-		super(position, value.type);
20
-		
21
-		this.value = value;
22
-		this.range = range;
23
-	}
24
-
25
-	@Override
26
-	public <T> T accept(ExpressionVisitor<T> visitor) {
27
-		return visitor.visitSubstring(this);
28
-	}
29
-}

+ 0
- 91
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/builtin/SubstringMember.java View File

@@ -1,91 +0,0 @@
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.member.builtin;
7
-
8
-import java.util.Map;
9
-import org.openzen.zenscript.codemodel.CompareType;
10
-import org.openzen.zenscript.codemodel.FunctionHeader;
11
-import org.openzen.zenscript.codemodel.OperatorType;
12
-import org.openzen.zenscript.codemodel.expression.CallArguments;
13
-import org.openzen.zenscript.codemodel.expression.Expression;
14
-import org.openzen.zenscript.codemodel.expression.SubstringExpression;
15
-import org.openzen.zenscript.codemodel.generic.TypeParameter;
16
-import org.openzen.zenscript.codemodel.member.ICallableMember;
17
-import org.openzen.zenscript.codemodel.member.IDefinitionMember;
18
-import org.openzen.zenscript.codemodel.member.MemberVisitor;
19
-import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
20
-import org.openzen.zenscript.codemodel.type.ITypeID;
21
-import org.openzen.zenscript.codemodel.type.member.TypeMemberPriority;
22
-import org.openzen.zenscript.codemodel.type.member.TypeMembers;
23
-import org.openzen.zenscript.shared.CodePosition;
24
-import org.openzen.zenscript.shared.Taggable;
25
-
26
-/**
27
- *
28
- * @author Hoofdgebruiker
29
- */
30
-public class SubstringMember extends Taggable implements ICallableMember {
31
-	private final FunctionHeader header;
32
-	
33
-	public SubstringMember(FunctionHeader header) {
34
-		this.header = header;
35
-	}
36
-
37
-	@Override
38
-	public boolean isStatic() {
39
-		return false;
40
-	}
41
-
42
-	@Override
43
-	public FunctionHeader getHeader() {
44
-		return header;
45
-	}
46
-
47
-	@Override
48
-	public Expression call(CodePosition position, Expression target, FunctionHeader instancedHeader, CallArguments arguments) {
49
-		return new SubstringExpression(position, target, arguments.arguments[0]);
50
-	}
51
-
52
-	@Override
53
-	public Expression callWithComparator(CodePosition position, CompareType operator, Expression target, FunctionHeader instancedHeader, CallArguments arguments) {
54
-		throw new UnsupportedOperationException("Not comparable");
55
-	}
56
-
57
-	@Override
58
-	public Expression callStatic(CodePosition position, FunctionHeader instancedHeader, CallArguments arguments) {
59
-		throw new UnsupportedOperationException("Not a static member");
60
-	}
61
-
62
-	@Override
63
-	public Expression callStaticWithComparator(CodePosition position, CompareType operator, FunctionHeader instancedHeader, CallArguments arguments) {
64
-		throw new UnsupportedOperationException("Not comparable");
65
-	}
66
-
67
-	@Override
68
-	public CodePosition getPosition() {
69
-		return CodePosition.BUILTIN;
70
-	}
71
-
72
-	@Override
73
-	public String describe() {
74
-		return "substring";
75
-	}
76
-
77
-	@Override
78
-	public void registerTo(TypeMembers type, TypeMemberPriority priority) {
79
-		type.addOperator(OperatorType.INDEXGET, this, priority);
80
-	}
81
-
82
-	@Override
83
-	public IDefinitionMember instance(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> mapping) {
84
-		return this; // string member; no instancing
85
-	}
86
-
87
-	@Override
88
-	public <T> T accept(MemberVisitor<T> visitor) {
89
-		throw new UnsupportedOperationException("Not a compilable member");
90
-	}
91
-}

+ 2
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java View File

@@ -38,7 +38,6 @@ import org.openzen.zenscript.codemodel.member.builtin.ArrayIteratorValues;
38 38
 import org.openzen.zenscript.codemodel.member.builtin.ComparatorMember;
39 39
 import org.openzen.zenscript.codemodel.member.builtin.ConstantGetterMember;
40 40
 import org.openzen.zenscript.codemodel.member.builtin.RangeIterator;
41
-import org.openzen.zenscript.codemodel.member.builtin.SubstringMember;
42 41
 import org.openzen.zenscript.codemodel.type.ArrayTypeID;
43 42
 import org.openzen.zenscript.codemodel.type.AssocTypeID;
44 43
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
@@ -411,8 +410,8 @@ public class TypeMemberBuilder implements ITypeVisitor<Void> {
411 410
 
412 411
 		members.addGetter(new GetterMember(BUILTIN, 0, "length", INT), TypeMemberPriority.SPECIFIED);
413 412
 
414
-		FunctionHeader substringHeader = new FunctionHeader(STRING, new FunctionParameter(cache.getRegistry().getRange(INT, INT)));
415
-		members.addOperator(OperatorType.INDEXGET, new SubstringMember(substringHeader), TypeMemberPriority.SPECIFIED);
413
+		//FunctionHeader substringHeader = new FunctionHeader(STRING, new FunctionParameter(cache.getRegistry().getRange(INT, INT)));
414
+		//members.addOperator(OperatorType.INDEXGET, new SubstringMember(substringHeader), TypeMemberPriority.SPECIFIED);
416 415
 		
417 416
 		members.addConstructor(new ConstructorMember(BUILTIN, 0, new FunctionHeader(VOID, new FunctionParameter(cache.getRegistry().getArray(CHAR, 1), "characters"))), TypeMemberPriority.SPECIFIED);
418 417
 		

+ 1
- 14
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java View File

@@ -1,8 +1,6 @@
1 1
 package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3
-import com.sun.javafx.image.IntPixelGetter;
4 3
 import org.objectweb.asm.Type;
5
-import org.openzen.zenscript.codemodel.definition.ZSPackage;
6 4
 import org.openzen.zenscript.codemodel.expression.*;
7 5
 import org.openzen.zenscript.codemodel.member.DefinitionMember;
8 6
 import org.openzen.zenscript.implementations.IntRange;
@@ -16,8 +14,7 @@ import org.openzen.zenscript.shared.CompileExceptionCode;
16 14
 import java.util.Map;
17 15
 
18 16
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
19
-
20
-    private JavaWriter javaWriter;
17
+    private final JavaWriter javaWriter;
21 18
 
22 19
     public JavaExpressionVisitor(final JavaWriter javaWriter) {
23 20
         this.javaWriter = javaWriter;
@@ -379,16 +376,6 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
379 376
         return null;
380 377
     }
381 378
 
382
-    @Override
383
-    public Void visitStringConcat(StringConcatExpression expression) {
384
-        return null;
385
-    }
386
-
387
-    @Override
388
-    public Void visitSubstring(SubstringExpression expression) {
389
-        return null;
390
-    }
391
-
392 379
     @Override
393 380
     public Void visitThis(ThisExpression expression) {
394 381
         return null;

+ 0
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaForeachVisitor.java View File

@@ -2,7 +2,6 @@ package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3 3
 import org.objectweb.asm.Label;
4 4
 import org.openzen.zenscript.codemodel.iterator.ForeachIteratorVisitor;
5
-import org.openzen.zenscript.codemodel.member.CustomIteratorMember;
6 5
 import org.openzen.zenscript.codemodel.statement.Statement;
7 6
 import org.openzen.zenscript.codemodel.statement.VarStatement;
8 7
 import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;

+ 0
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java View File

@@ -10,7 +10,6 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
10 10
     private final JavaExpressionVisitor expressionVisitor;
11 11
 
12 12
     public JavaStatementVisitor(final JavaWriter javaWriter) {
13
-
14 13
         this.javaWriter = javaWriter;
15 14
         this.expressionVisitor = new JavaExpressionVisitor(javaWriter);
16 15
     }

+ 1
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

@@ -12,7 +12,7 @@ import static org.objectweb.asm.Opcodes.*;
12 12
 
13 13
 public class JavaWriter {
14 14
     final LocalVariablesSorter visitor;
15
-    private boolean debug = true;
15
+    private boolean debug = false;
16 16
     private int labelIndex = 1;
17 17
     private Map<Label, String> labelNames = new HashMap<>();
18 18
 

+ 9
- 8
Parser/src/main/java/org/openzen/zenscript/parser/ParsedDefinition.java View File

@@ -6,6 +6,7 @@
6 6
 package org.openzen.zenscript.parser;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
9 10
 import org.openzen.zenscript.shared.CodePosition;
10 11
 import org.openzen.zenscript.lexer.ZSTokenStream;
11 12
 import static org.openzen.zenscript.lexer.ZSTokenType.K_ALIAS;
@@ -29,21 +30,21 @@ import org.openzen.zenscript.parser.definitions.ParsedStruct;
29 30
  * @author Hoofdgebruiker
30 31
  */
31 32
 public abstract class ParsedDefinition {
32
-	public static ParsedDefinition parse(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
33
+	public static ParsedDefinition parse(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
33 34
 		if (tokens.optional(K_CLASS) != null) {
34
-			return ParsedClass.parseClass(position, modifiers, tokens, outerDefinition);
35
+			return ParsedClass.parseClass(pkg, position, modifiers, tokens, outerDefinition);
35 36
 		} else if (tokens.optional(K_INTERFACE) != null) {
36
-			return ParsedInterface.parseInterface(position, modifiers, tokens, outerDefinition);
37
+			return ParsedInterface.parseInterface(pkg, position, modifiers, tokens, outerDefinition);
37 38
 		} else if (tokens.optional(K_ENUM) != null) {
38
-			return ParsedEnum.parseEnum(position, modifiers, tokens, outerDefinition);
39
+			return ParsedEnum.parseEnum(pkg, position, modifiers, tokens, outerDefinition);
39 40
 		} else if (tokens.optional(K_STRUCT) != null) {
40
-			return ParsedStruct.parse(position, modifiers, tokens, outerDefinition);
41
+			return ParsedStruct.parse(pkg, position, modifiers, tokens, outerDefinition);
41 42
 		} else if (tokens.optional(K_ALIAS) != null) {
42
-			return ParsedAlias.parseAlias(position, modifiers, tokens, outerDefinition);
43
+			return ParsedAlias.parseAlias(pkg, position, modifiers, tokens, outerDefinition);
43 44
 		} else if (tokens.optional(K_FUNCTION) != null) {
44
-			return ParsedFunction.parseFunction(position, modifiers, tokens, outerDefinition);
45
+			return ParsedFunction.parseFunction(pkg, position, modifiers, tokens, outerDefinition);
45 46
 		} else if (tokens.optional(K_EXPAND) != null) {
46
-			return ParsedExpansion.parseExpansion(position, modifiers, tokens, outerDefinition);
47
+			return ParsedExpansion.parseExpansion(pkg, position, modifiers, tokens, outerDefinition);
47 48
 		} else {
48 49
 			//tokens.required(EOF, "An import, class, interface, enum, struct, function or alias expected.");
49 50
 			return null;

+ 6
- 6
Parser/src/main/java/org/openzen/zenscript/parser/ParsedFile.java View File

@@ -39,22 +39,22 @@ import org.openzen.zenscript.shared.SourceFile;
39 39
  * @author Hoofdgebruiker
40 40
  */
41 41
 public class ParsedFile {
42
-	public static ParsedFile parse(File file) throws IOException {
42
+	public static ParsedFile parse(ZSPackage pkg, File file) throws IOException {
43 43
 		String filename = file.toString();
44 44
 		try (FileReader reader = new FileReader(file)) {
45
-			return parse(filename, reader);
45
+			return parse(pkg, filename, reader);
46 46
 		}
47 47
 	}
48 48
 	
49
-	public static ParsedFile parse(String filename, String content) {
49
+	public static ParsedFile parse(ZSPackage pkg, String filename, String content) {
50 50
 		try (StringReader reader = new StringReader(content)) {
51
-			return parse(filename, reader);
51
+			return parse(pkg, filename, reader);
52 52
 		} catch (IOException ex) {
53 53
 			throw new AssertionError(); // supposed to never happen in a StringReader
54 54
 		}
55 55
 	}
56 56
 	
57
-	public static ParsedFile parse(String filename, Reader reader) throws IOException {
57
+	public static ParsedFile parse(ZSPackage pkg, String filename, Reader reader) throws IOException {
58 58
 		ParsedFile result = new ParsedFile(filename);
59 59
 		
60 60
 		ZSTokenStream tokens = new ZSTokenStream(filename, reader);
@@ -87,7 +87,7 @@ public class ParsedFile {
87 87
 			} else if (tokens.optional(EOF) != null) {
88 88
 				break;
89 89
 			} else {
90
-				ParsedDefinition definition = ParsedDefinition.parse(position, modifiers, tokens, null);
90
+				ParsedDefinition definition = ParsedDefinition.parse(pkg, position, modifiers, tokens, null);
91 91
 				if (definition == null) {
92 92
 					result.statements.add(ParsedStatement.parse(tokens));
93 93
 				} else {

+ 5
- 4
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedAlias.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.parser.definitions;
8 8
 import java.util.List;
9 9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10 10
 import org.openzen.zenscript.codemodel.definition.AliasDefinition;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
11 12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.lexer.ZSTokenType;
@@ -22,11 +23,11 @@ import org.openzen.zenscript.shared.CodePosition;
22 23
  * @author Hoofdgebruiker
23 24
  */
24 25
 public class ParsedAlias extends ParsedDefinition {
25
-	public static ParsedAlias parseAlias(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26
+	public static ParsedAlias parseAlias(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26 27
 		String name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected").content;
27 28
 		List<ParsedGenericParameter> parameters = ParsedGenericParameter.parseAll(tokens);
28 29
 		IParsedType type = IParsedType.parse(tokens);
29
-		return new ParsedAlias(position, modifiers, name, parameters, type, outerDefinition);
30
+		return new ParsedAlias(pkg, position, modifiers, name, parameters, type, outerDefinition);
30 31
 	}
31 32
 	
32 33
 	private final String name;
@@ -35,14 +36,14 @@ public class ParsedAlias extends ParsedDefinition {
35 36
 	
36 37
 	private final AliasDefinition compiled;
37 38
 	
38
-	public ParsedAlias(CodePosition position, int modifiers, String name, List<ParsedGenericParameter> parameters, IParsedType type, HighLevelDefinition outerDefinition) {
39
+	public ParsedAlias(ZSPackage pkg, CodePosition position, int modifiers, String name, List<ParsedGenericParameter> parameters, IParsedType type, HighLevelDefinition outerDefinition) {
39 40
 		super(position, modifiers);
40 41
 		
41 42
 		this.name = name;
42 43
 		this.parameters = parameters;
43 44
 		this.type = type;
44 45
 		
45
-		compiled = new AliasDefinition(name, modifiers, outerDefinition);
46
+		compiled = new AliasDefinition(pkg, name, modifiers, outerDefinition);
46 47
 	}
47 48
 	
48 49
 	@Override

+ 5
- 4
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedClass.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.parser.definitions;
8 8
 import java.util.List;
9 9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10 10
 import org.openzen.zenscript.codemodel.definition.ClassDefinition;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
11 12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.lexer.ZSTokenType;
@@ -22,7 +23,7 @@ import org.openzen.zenscript.shared.CodePosition;
22 23
  * @author Stan Hebben
23 24
  */
24 25
 public class ParsedClass extends BaseParsedDefinition {
25
-	public static ParsedClass parseClass(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26
+	public static ParsedClass parseClass(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26 27
 		String name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected").content;
27 28
 		List<ParsedGenericParameter> genericParameters = ParsedGenericParameter.parseAll(tokens);
28 29
 		
@@ -33,7 +34,7 @@ public class ParsedClass extends BaseParsedDefinition {
33 34
 		
34 35
 		tokens.required(ZSTokenType.T_AOPEN, "{ expected");
35 36
 		
36
-		ParsedClass result = new ParsedClass(position, modifiers, name, genericParameters, superclass, outerDefinition);
37
+		ParsedClass result = new ParsedClass(pkg, position, modifiers, name, genericParameters, superclass, outerDefinition);
37 38
 		while (tokens.optional(ZSTokenType.T_ACLOSE) == null) {
38 39
 			result.addMember(ParsedDefinitionMember.parse(tokens, result.compiled));
39 40
 		}
@@ -45,13 +46,13 @@ public class ParsedClass extends BaseParsedDefinition {
45 46
 	
46 47
 	private final ClassDefinition compiled;
47 48
 	
48
-	public ParsedClass(CodePosition position, int modifiers, String name, List<ParsedGenericParameter> genericParameters, IParsedType superclass, HighLevelDefinition outerDefinition) {
49
+	public ParsedClass(ZSPackage pkg, CodePosition position, int modifiers, String name, List<ParsedGenericParameter> genericParameters, IParsedType superclass, HighLevelDefinition outerDefinition) {
49 50
 		super(position, modifiers);
50 51
 		
51 52
 		this.genericParameters = genericParameters;
52 53
 		this.superclass = superclass;
53 54
 		
54
-		compiled = new ClassDefinition(name, modifiers, outerDefinition);
55
+		compiled = new ClassDefinition(pkg, name, modifiers, outerDefinition);
55 56
 		for (ParsedGenericParameter parameter : genericParameters)
56 57
 			compiled.addGenericParameter(parameter.compiled);
57 58
 	}

+ 5
- 4
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedEnum.java View File

@@ -9,6 +9,7 @@ import java.util.ArrayList;
9 9
 import java.util.List;
10 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11 11
 import org.openzen.zenscript.codemodel.definition.EnumDefinition;
12
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.lexer.ZSTokenType;
14 15
 import org.openzen.zenscript.parser.member.ParsedDefinitionMember;
@@ -19,7 +20,7 @@ import org.openzen.zenscript.shared.CodePosition;
19 20
  * @author Hoofdgebruiker
20 21
  */
21 22
 public class ParsedEnum extends BaseParsedDefinition {
22
-	public static ParsedEnum parseEnum(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
23
+	public static ParsedEnum parseEnum(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
23 24
 		String name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected").content;
24 25
 		tokens.required(ZSTokenType.T_AOPEN, "{ expected");
25 26
 		
@@ -30,7 +31,7 @@ public class ParsedEnum extends BaseParsedDefinition {
30 31
 				break;
31 32
 		}
32 33
 		
33
-		ParsedEnum result = new ParsedEnum(position, modifiers, name, enumValues, outerDefinition);
34
+		ParsedEnum result = new ParsedEnum(pkg, position, modifiers, name, enumValues, outerDefinition);
34 35
 		if (tokens.optional(ZSTokenType.T_SEMICOLON) != null) {
35 36
 			while (tokens.optional(ZSTokenType.T_ACLOSE) == null) {
36 37
 				result.addMember(ParsedDefinitionMember.parse(tokens, result.compiled));
@@ -45,12 +46,12 @@ public class ParsedEnum extends BaseParsedDefinition {
45 46
 	
46 47
 	private final EnumDefinition compiled;
47 48
 	
48
-	public ParsedEnum(CodePosition position, int modifiers, String name, List<ParsedEnumConstant> enumValues, HighLevelDefinition outerDefinition) {
49
+	public ParsedEnum(ZSPackage pkg, CodePosition position, int modifiers, String name, List<ParsedEnumConstant> enumValues, HighLevelDefinition outerDefinition) {
49 50
 		super(position, modifiers);
50 51
 		
51 52
 		this.enumValues = enumValues;
52 53
 		
53
-		compiled = new EnumDefinition(name, modifiers, outerDefinition);
54
+		compiled = new EnumDefinition(pkg, name, modifiers, outerDefinition);
54 55
 	}
55 56
 
56 57
 	@Override

+ 5
- 4
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedExpansion.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.parser.definitions;
8 8
 import java.util.List;
9 9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10 10
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
11 12
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.lexer.ZSTokenType;
@@ -22,12 +23,12 @@ import org.openzen.zenscript.shared.CodePosition;
22 23
  * @author Hoofdgebruiker
23 24
  */
24 25
 public class ParsedExpansion extends BaseParsedDefinition {
25
-	public static ParsedExpansion parseExpansion(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26
+	public static ParsedExpansion parseExpansion(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26 27
 		List<ParsedGenericParameter> parameters = ParsedGenericParameter.parseAll(tokens);
27 28
 		IParsedType target = IParsedType.parse(tokens);
28 29
 		tokens.required(ZSTokenType.T_AOPEN, "{ expected");
29 30
 		
30
-		ParsedExpansion result = new ParsedExpansion(position, modifiers, parameters, target, outerDefinition);
31
+		ParsedExpansion result = new ParsedExpansion(pkg, position, modifiers, parameters, target, outerDefinition);
31 32
 		while (tokens.optional(ZSTokenType.T_ACLOSE) == null) {
32 33
 			result.addMember(ParsedDefinitionMember.parse(tokens, result.compiled));
33 34
 		}
@@ -38,13 +39,13 @@ public class ParsedExpansion extends BaseParsedDefinition {
38 39
 	private final IParsedType target;
39 40
 	private final ExpansionDefinition compiled;
40 41
 	
41
-	public ParsedExpansion(CodePosition position, int modifiers, List<ParsedGenericParameter> parameters, IParsedType target, HighLevelDefinition outerDefinition) {
42
+	public ParsedExpansion(ZSPackage pkg, CodePosition position, int modifiers, List<ParsedGenericParameter> parameters, IParsedType target, HighLevelDefinition outerDefinition) {
42 43
 		super(position, modifiers);
43 44
 		
44 45
 		this.parameters = parameters;
45 46
 		this.target = target;
46 47
 		
47
-		this.compiled = new ExpansionDefinition(modifiers, outerDefinition);
48
+		this.compiled = new ExpansionDefinition(pkg, modifiers, outerDefinition);
48 49
 		for (ParsedGenericParameter parameter : parameters)
49 50
 			compiled.addGenericParameter(parameter.compiled);
50 51
 	}

+ 5
- 6
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedFunction.java View File

@@ -9,6 +9,7 @@ package org.openzen.zenscript.parser.definitions;
9 9
 import static org.openzen.zenscript.lexer.ZSTokenType.*;
10 10
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
11 11
 import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
12
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
12 13
 import org.openzen.zenscript.lexer.ZSTokenStream;
13 14
 import org.openzen.zenscript.linker.BaseScope;
14 15
 import org.openzen.zenscript.linker.FunctionScope;
@@ -22,27 +23,25 @@ import org.openzen.zenscript.shared.CodePosition;
22 23
  * @author Stanneke
23 24
  */
24 25
 public class ParsedFunction extends ParsedDefinition {
25
-	public static ParsedFunction parseFunction(CodePosition position, int modifiers, ZSTokenStream parser, HighLevelDefinition outerDefinition) {
26
+	public static ParsedFunction parseFunction(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream parser, HighLevelDefinition outerDefinition) {
26 27
 		String name = parser.required(T_IDENTIFIER, "identifier expected").content;
27 28
 		ParsedFunctionHeader header = ParsedFunctionHeader.parse(parser);
28 29
 		ParsedFunctionBody body = ParsedStatement.parseFunctionBody(parser);
29
-		return new ParsedFunction(position, modifiers, name, header, body, outerDefinition);
30
+		return new ParsedFunction(pkg, position, modifiers, name, header, body, outerDefinition);
30 31
 	}
31 32
 	
32
-	private final String name;
33 33
 	private final ParsedFunctionHeader header;
34 34
 	private final ParsedFunctionBody body;
35 35
 
36 36
 	private final FunctionDefinition compiled;
37 37
 	
38
-	private ParsedFunction(CodePosition position, int modifiers, String name, ParsedFunctionHeader header, ParsedFunctionBody body, HighLevelDefinition outerDefinition) {
38
+	private ParsedFunction(ZSPackage pkg, CodePosition position, int modifiers, String name, ParsedFunctionHeader header, ParsedFunctionBody body, HighLevelDefinition outerDefinition) {
39 39
 		super(position, modifiers);
40 40
 		
41
-		this.name = name;
42 41
 		this.header = header;
43 42
 		this.body = body;
44 43
 		
45
-		compiled = new FunctionDefinition(name, modifiers, outerDefinition);
44
+		compiled = new FunctionDefinition(pkg, name, modifiers, outerDefinition);
46 45
 	}
47 46
 
48 47
 	@Override

+ 5
- 6
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedInterface.java View File

@@ -10,6 +10,7 @@ import java.util.Collections;
10 10
 import java.util.List;
11 11
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
12 12
 import org.openzen.zenscript.codemodel.definition.InterfaceDefinition;
13
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
13 14
 import org.openzen.zenscript.lexer.ZSTokenStream;
14 15
 import org.openzen.zenscript.lexer.ZSTokenType;
15 16
 import org.openzen.zenscript.linker.BaseScope;
@@ -22,7 +23,7 @@ import org.openzen.zenscript.shared.CodePosition;
22 23
  * @author Hoofdgebruiker
23 24
  */
24 25
 public class ParsedInterface extends BaseParsedDefinition {
25
-	public static ParsedInterface parseInterface(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26
+	public static ParsedInterface parseInterface(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
26 27
 		String name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected").content;
27 28
 		List<ParsedGenericParameter> genericParameters = ParsedGenericParameter.parseAll(tokens);
28 29
 		List<IParsedType> superInterfaces = Collections.emptyList();
@@ -35,27 +36,25 @@ public class ParsedInterface extends BaseParsedDefinition {
35 36
 		
36 37
 		tokens.required(ZSTokenType.T_AOPEN, "{ expected");
37 38
 		
38
-		ParsedInterface result = new ParsedInterface(position, modifiers, name, genericParameters, superInterfaces, outerDefinition);
39
+		ParsedInterface result = new ParsedInterface(pkg, position, modifiers, name, genericParameters, superInterfaces, outerDefinition);
39 40
 		while (tokens.optional(ZSTokenType.T_ACLOSE) == null) {
40 41
 			result.addMember(ParsedDefinitionMember.parse(tokens, result.compiled));
41 42
 		}
42 43
 		return result;
43 44
 	}
44 45
 	
45
-	private final String name;
46 46
 	private final List<ParsedGenericParameter> genericParameters;
47 47
 	private final List<IParsedType> superInterfaces;
48 48
 	
49 49
 	private final InterfaceDefinition compiled;
50 50
 	
51
-	public ParsedInterface(CodePosition position, int modifiers, String name, List<ParsedGenericParameter> genericParameters, List<IParsedType> superInterfaces, HighLevelDefinition outerDefinition) {
51
+	public ParsedInterface(ZSPackage pkg, CodePosition position, int modifiers, String name, List<ParsedGenericParameter> genericParameters, List<IParsedType> superInterfaces, HighLevelDefinition outerDefinition) {
52 52
 		super(position, modifiers);
53 53
 		
54
-		this.name = name;
55 54
 		this.genericParameters = genericParameters;
56 55
 		this.superInterfaces = superInterfaces;
57 56
 		
58
-		compiled = new InterfaceDefinition(name, modifiers, outerDefinition);
57
+		compiled = new InterfaceDefinition(pkg, name, modifiers, outerDefinition);
59 58
 	}
60 59
 
61 60
 	@Override

+ 5
- 6
Parser/src/main/java/org/openzen/zenscript/parser/definitions/ParsedStruct.java View File

@@ -8,6 +8,7 @@ package org.openzen.zenscript.parser.definitions;
8 8
 import java.util.List;
9 9
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
10 10
 import org.openzen.zenscript.codemodel.definition.StructDefinition;
11
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
11 12
 import org.openzen.zenscript.lexer.ZSTokenStream;
12 13
 import org.openzen.zenscript.lexer.ZSTokenType;
13 14
 import org.openzen.zenscript.linker.BaseScope;
@@ -19,31 +20,29 @@ import org.openzen.zenscript.shared.CodePosition;
19 20
  * @author Hoofdgebruiker
20 21
  */
21 22
 public class ParsedStruct extends BaseParsedDefinition {
22
-	public static ParsedStruct parseStruct(CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
23
+	public static ParsedStruct parseStruct(ZSPackage pkg, CodePosition position, int modifiers, ZSTokenStream tokens, HighLevelDefinition outerDefinition) {
23 24
 		String name = tokens.required(ZSTokenType.T_IDENTIFIER, "identifier expected").content;
24 25
 		List<ParsedGenericParameter> parameters = ParsedGenericParameter.parseAll(tokens);
25 26
 		
26 27
 		tokens.required(ZSTokenType.T_AOPEN, "{");
27 28
 		
28
-		ParsedStruct result = new ParsedStruct(position, modifiers, name, parameters, outerDefinition);
29
+		ParsedStruct result = new ParsedStruct(pkg, position, modifiers, name, parameters, outerDefinition);
29 30
 		while (tokens.optional(ZSTokenType.T_ACLOSE) == null) {
30 31
 			result.addMember(ParsedDefinitionMember.parse(tokens, result.compiled));
31 32
 		}
32 33
 		return result;
33 34
 	}
34 35
 	
35
-	private final String name;
36 36
 	private final List<ParsedGenericParameter> parameters;
37 37
 	
38 38
 	private final StructDefinition compiled;
39 39
 	
40
-	public ParsedStruct(CodePosition position, int modifiers, String name, List<ParsedGenericParameter> parameters, HighLevelDefinition outerDefinition) {
40
+	public ParsedStruct(ZSPackage pkg, CodePosition position, int modifiers, String name, List<ParsedGenericParameter> parameters, HighLevelDefinition outerDefinition) {
41 41
 		super(position, modifiers);
42 42
 		
43
-		this.name = name;
44 43
 		this.parameters = parameters;
45 44
 		
46
-		compiled = new StructDefinition(name, modifiers, outerDefinition);
45
+		compiled = new StructDefinition(pkg, name, modifiers, outerDefinition);
47 46
 		for (ParsedGenericParameter parameter : parameters)
48 47
 			compiled.addGenericParameter(parameter.compiled);
49 48
 	}

+ 2
- 1
Parser/src/main/java/org/openzen/zenscript/parser/member/ParsedDefinitionMember.java View File

@@ -15,6 +15,7 @@ import org.openzen.zenscript.parser.statements.ParsedFunctionBody;
15 15
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
16 16
 import org.openzen.zenscript.codemodel.Modifiers;
17 17
 import org.openzen.zenscript.codemodel.OperatorType;
18
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
18 19
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
19 20
 import org.openzen.zenscript.lexer.ZSToken;
20 21
 import org.openzen.zenscript.lexer.ZSTokenStream;
@@ -189,7 +190,7 @@ public abstract class ParsedDefinitionMember {
189 190
 			case K_ALIAS:
190 191
 			case K_STRUCT:
191 192
 			case K_ENUM:
192
-				return new ParsedInnerDefinition(ParsedDefinition.parse(start, modifiers, tokens, forDefinition));
193
+				return new ParsedInnerDefinition(ParsedDefinition.parse(forDefinition.pkg, start, modifiers, tokens, forDefinition));
193 194
 			case K_FOR: {
194 195
 				tokens.next();
195 196
 				ParsedFunctionHeader header = ParsedFunctionHeader.parse(tokens);

+ 1
- 0
ScriptingExample/build.gradle View File

@@ -16,4 +16,5 @@ if (!hasProperty('mainClass')) {
16 16
 dependencies {
17 17
 	compile project(':Parser')
18 18
 	compile project(':JavaBytecodeCompiler')
19
+	compile project(':CodeFormatter')
19 20
 }

+ 17
- 16
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java View File

@@ -36,24 +36,32 @@ import org.openzen.zenscript.shared.CodePosition;
36 36
  * @author Hoofdgebruiker
37 37
  */
38 38
 public class GlobalRegistry {
39
+	private final ZSPackage rootPackage = new ZSPackage("");
40
+	private final ZSPackage javaIo = rootPackage.getOrCreatePackage("java").getOrCreatePackage("io");
41
+	private final ZSPackage javaLang = rootPackage.getOrCreatePackage("java").getOrCreatePackage("lang");
39 42
 	
43
+	public GlobalRegistry() {
44
+		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
45
+		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
46
+		
47
+		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
48
+		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
49
+	}
40 50
 	
41 51
 	public ZSPackage collectPackages() {
42
-		ZSPackage rootPackage = new ZSPackage();
43
-		
44 52
 		// register packages here
45 53
 		
46 54
 		{
47 55
 			// eg. package my.package with a class MyClass with a single native method test() returning a string
48 56
 			// the visitors can then during compilation check if a method is an instance of NativeMethodMember and treat it accordingly
49
-			ClassDefinition myClassDefinition = new ClassDefinition("MyClass", Modifiers.MODIFIER_PUBLIC, null);
57
+			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("package");
58
+			ClassDefinition myClassDefinition = new ClassDefinition(packageMyPackage, "MyClass", Modifiers.MODIFIER_PUBLIC, null);
50 59
 			JavaClassInfo myClassInfo = new JavaClassInfo("my/test/MyClass");
51 60
 			
52 61
 			MethodMember member = new MethodMember(CodePosition.NATIVE, Modifiers.MODIFIER_PUBLIC, "test", new FunctionHeader(BasicTypeID.STRING));
53 62
 			member.setTag(JavaMethodInfo.class, new JavaMethodInfo(myClassInfo, "test", "()Ljava/lang/String;"));
54 63
 			myClassDefinition.addMember(member);
55 64
 			
56
-			ZSPackage packageMyPackage = rootPackage.getOrCreatePackage("my").getOrCreatePackage("package");
57 65
 			packageMyPackage.register(myClassDefinition);
58 66
 		}
59 67
 		
@@ -75,28 +83,21 @@ public class GlobalRegistry {
75 83
 		return globals;
76 84
 	}
77 85
 	
78
-	private static final ClassDefinition PRINTSTREAM = new ClassDefinition("PrintStream", Modifiers.MODIFIER_EXPORT);
79
-	private static final MethodMember PRINTSTREAM_PRINTLN = new MethodMember(
86
+	private final ClassDefinition PRINTSTREAM = new ClassDefinition(javaIo, "PrintStream", Modifiers.MODIFIER_EXPORT);
87
+	private final ClassDefinition SYSTEM = new ClassDefinition(javaLang, "System", Modifiers.MODIFIER_EXPORT);
88
+	private final MethodMember PRINTSTREAM_PRINTLN = new MethodMember(
80 89
 			CodePosition.NATIVE,
81 90
 			Modifiers.MODIFIER_EXPORT,
82 91
 			"println",
83 92
 			new FunctionHeader(BasicTypeID.VOID, new FunctionParameter(BasicTypeID.STRING)));
84 93
 	
85
-	private static final FieldMember SYSTEM_OUT = new FieldMember(
94
+	private final FieldMember SYSTEM_OUT = new FieldMember(
86 95
 			CodePosition.NATIVE,
87 96
 			Modifiers.MODIFIER_EXPORT,
88 97
 			"out",
89
-			DefinitionTypeID.forType(PRINTSTREAM),
98
+			DefinitionTypeID.forType(SYSTEM),
90 99
 			true);
91 100
 	
92
-	static {
93
-		JavaClassInfo jPrintStream = new JavaClassInfo("java/io/PrintStream");
94
-		PRINTSTREAM_PRINTLN.setTag(JavaMethodInfo.class, new JavaMethodInfo(jPrintStream, "println", "(Ljava/lang/String;)V"));
95
-		
96
-		JavaClassInfo jSystem = new JavaClassInfo("java/lang/System");
97
-		SYSTEM_OUT.setTag(JavaFieldInfo.class, new JavaFieldInfo(jSystem, "out", "Ljava/io/PrintStream;"));
98
-	}
99
-	
100 101
 	private class PrintlnSymbol implements ISymbol {
101 102
 
102 103
 		@Override

+ 16
- 4
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java View File

@@ -3,6 +3,7 @@ package org.openzen.zenscript.scriptingexample;
3 3
 import java.io.File;
4 4
 import java.io.IOException;
5 5
 import java.util.ArrayList;
6
+import java.util.Collections;
6 7
 import java.util.List;
7 8
 import java.util.Map;
8 9
 import java.util.Optional;
@@ -13,10 +14,13 @@ import org.openzen.zenscript.codemodel.ScriptBlock;
13 14
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
14 15
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
15 16
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
17
+import org.openzen.zenscript.formatter.FileFormatter;
18
+import org.openzen.zenscript.formatter.FormattingSettings;
16 19
 import org.openzen.zenscript.javabytecode.JavaCompiler;
17 20
 import org.openzen.zenscript.javabytecode.JavaModule;
18 21
 import org.openzen.zenscript.linker.symbol.ISymbol;
19 22
 import org.openzen.zenscript.parser.ParsedFile;
23
+import org.openzen.zenscript.shared.SourceFile;
20 24
 
21 25
 public class Main {
22 26
     /**
@@ -27,19 +31,27 @@ public class Main {
27 31
 		File inputDirectory = new File("scripts");
28 32
 		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);
29 33
 		
30
-		ParsedFile[] parsedFiles = parse(inputFiles);
34
+		ZSPackage pkg = new ZSPackage("");
35
+		ParsedFile[] parsedFiles = parse(pkg, inputFiles);
31 36
 		
32 37
 		GlobalRegistry registry = new GlobalRegistry();
33 38
 		SemanticModule module = compileSyntaxToSemantic(parsedFiles, registry);
34 39
 		
40
+		FormattingSettings settings = new FormattingSettings.Builder().build();
41
+		for (ScriptBlock block : module.scripts) {
42
+			FileFormatter formatter = new FileFormatter(settings);
43
+			System.out.println("== " + block.getTag(SourceFile.class).filename + " ==");
44
+			System.out.println(formatter.format(pkg, block, Collections.emptyList()));
45
+		}
46
+		
35 47
 		JavaModule javaModule = compileSemanticToJava(module);
36 48
 		javaModule.execute();
37 49
     }
38 50
 	
39
-	private static ParsedFile[] parse(File[] files) throws IOException {
51
+	private static ParsedFile[] parse(ZSPackage pkg, File[] files) throws IOException {
40 52
 		ParsedFile[] parsedFiles = new ParsedFile[files.length];
41 53
 		for (int i = 0; i < files.length; i++) {
42
-			parsedFiles[i] = ParsedFile.parse(files[i]);
54
+			parsedFiles[i] = ParsedFile.parse(pkg, files[i]);
43 55
 		}
44 56
 		return parsedFiles;
45 57
 	}
@@ -81,7 +93,7 @@ public class Main {
81 93
 	}
82 94
 	
83 95
 	private static JavaModule compileSemanticToJava(SemanticModule module) {
84
-		JavaCompiler compiler = new JavaCompiler(true);
96
+		JavaCompiler compiler = new JavaCompiler(false);
85 97
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
86 98
 			compiler.addDefinition(definition);
87 99
 		}

Loading…
Cancel
Save