Selaa lähdekoodia

Merge remote-tracking branch 'Stan/development' into development

kindlich 6 vuotta sitten
vanhempi
commit
91dd3066f7
No known key found for this signature in database
100 muutettua tiedostoa jossa 2184 lisäystä ja 330 poistoa
  1. 1
    0
      CodeFormatter/build.gradle
  2. 6
    6
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/CommentFormatter.java
  3. 2
    2
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/DefinitionFormatter.java
  4. 109
    100
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java
  5. 0
    33
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionString.java
  6. 3
    2
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FileFormatter.java
  7. 4
    4
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FormattingUtils.java
  8. 18
    4
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/MemberFormatter.java
  9. 0
    73
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java
  10. 15
    28
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ScriptFormattingSettings.java
  11. 2
    2
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/StatementFormatter.java
  12. 2
    2
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/SwitchValueFormatter.java
  13. 6
    8
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java
  14. 106
    0
      CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ZenScriptOperator.java
  15. 0
    1
      CodeFormatterShared/build.gradle
  16. 14
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/CommentFormatter.java
  17. 55
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/ExpressionString.java
  18. 26
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/FormattableOperator.java
  19. 56
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/FormattingSettings.java
  20. 1
    1
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/Importer.java
  21. 311
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormatter.java
  22. 25
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingSubBlock.java
  23. 29
    0
      CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingTarget.java
  24. 1
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/CompareType.java
  25. 8
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java
  26. 3
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java
  27. 16
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/ModuleProcessor.java
  28. 4
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/OperatorType.java
  29. 9
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/ScriptBlock.java
  30. 16
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/Annotation.java
  31. 44
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/AnnotationDefinition.java
  32. 219
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/AnnotationProcessor.java
  33. 21
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/DefinitionAnnotation.java
  34. 22
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/MemberAnnotation.java
  35. 88
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeAnnotationDefinition.java
  36. 31
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeDefinitionAnnotation.java
  37. 32
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeMemberAnnotation.java
  38. 18
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeTag.java
  39. 107
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionAnnotationDefinition.java
  40. 67
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionForMethod.java
  41. 19
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/StatementAnnotation.java
  42. 1
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java
  43. 9
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/AndAndExpression.java
  44. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ArrayExpression.java
  45. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallArguments.java
  46. 23
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallExpression.java
  47. 7
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallStaticExpression.java
  48. 10
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedClosureExpression.java
  49. 8
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedDirectExpression.java
  50. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedLocalVariableExpression.java
  51. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedParameterExpression.java
  52. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedThisExpression.java
  53. 8
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CastExpression.java
  54. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CheckNullExpression.java
  55. 7
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CoalesceExpression.java
  56. 15
    4
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CompareExpression.java
  57. 10
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConditionalExpression.java
  58. 44
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstExpression.java
  59. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantBoolExpression.java
  60. 7
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantByteExpression.java
  61. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantCharExpression.java
  62. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantDoubleExpression.java
  63. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantFloatExpression.java
  64. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantIntExpression.java
  65. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantLongExpression.java
  66. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantSByteExpression.java
  67. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantShortExpression.java
  68. 10
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantStringExpression.java
  69. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantUIntExpression.java
  70. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantULongExpression.java
  71. 7
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantUShortExpression.java
  72. 7
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java
  73. 7
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorThisCallExpression.java
  74. 10
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/EnumConstantExpression.java
  75. 53
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java
  76. 72
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionBuilder.java
  77. 14
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionTransformer.java
  78. 5
    3
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java
  79. 59
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/FunctionExpression.java
  80. 0
    31
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GenericCompareExpression.java
  81. 6
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetFieldExpression.java
  82. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetFunctionParameterExpression.java
  83. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetLocalVariableExpression.java
  84. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetStaticFieldExpression.java
  85. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetterExpression.java
  86. 7
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalCallExpression.java
  87. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalExpression.java
  88. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/InterfaceCastExpression.java
  89. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/IsExpression.java
  90. 6
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MakeConstExpression.java
  91. 7
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MapExpression.java
  92. 45
    2
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MatchExpression.java
  93. 31
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/NewExpression.java
  94. 5
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/NullExpression.java
  95. 7
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/OrOrExpression.java
  96. 9
    1
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/PostCallExpression.java
  97. 15
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/RangeExpression.java
  98. 7
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SameObjectExpression.java
  99. 9
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SetFieldExpression.java
  100. 0
    0
      CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SetFunctionParameterExpression.java

+ 1
- 0
CodeFormatter/build.gradle Näytä tiedosto

@@ -15,4 +15,5 @@ if (!hasProperty('mainClass')) {
15 15
 
16 16
 dependencies {
17 17
 	compile project(':CodeModel')
18
+	compile project(':CodeFormatterShared')
18 19
 }

+ 6
- 6
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/CommentFormatter.java Näytä tiedosto

@@ -12,14 +12,14 @@ package org.openzen.zenscript.formatter;
12 12
 public class CommentFormatter {
13 13
 	private CommentFormatter() {}
14 14
 	
15
-	public static String[] format(String[] comments) {
16
-		if (comments.length == 0)
17
-			return comments;
15
+	public static String[] format(String[] commentLines) {
16
+		if (commentLines.length == 0)
17
+			return commentLines;
18 18
 		
19 19
 		boolean isInMultilineComment = false;
20
-		String[] result = new String[comments.length];
21
-		for (int i = 0; i < comments.length; i++) {
22
-			String comment = comments[i];
20
+		String[] result = new String[commentLines.length];
21
+		for (int i = 0; i < commentLines.length; i++) {
22
+			String comment = commentLines[i];
23 23
 			if (isInMultilineComment) {
24 24
 				if (!comment.startsWith("*"))
25 25
 					comment = "* " + comment;

+ 2
- 2
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/DefinitionFormatter.java Näytä tiedosto

@@ -24,12 +24,12 @@ import org.openzen.zenscript.codemodel.member.IDefinitionMember;
24 24
  * @author Hoofdgebruiker
25 25
  */
26 26
 public class DefinitionFormatter implements DefinitionVisitor<Void> {
27
-	private final FormattingSettings settings;
27
+	private final ScriptFormattingSettings settings;
28 28
 	private final TypeFormatter typeFormatter;
29 29
 	private final StringBuilder output = new StringBuilder();
30 30
 	private final String indent;
31 31
 	
32
-	public DefinitionFormatter(FormattingSettings settings, TypeFormatter typeFormatter, String indent) {
32
+	public DefinitionFormatter(ScriptFormattingSettings settings, TypeFormatter typeFormatter, String indent) {
33 33
 		this.settings = settings;
34 34
 		this.typeFormatter = typeFormatter;
35 35
 		this.indent = indent;

+ 109
- 100
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionFormatter.java Näytä tiedosto

@@ -5,10 +5,11 @@
5 5
  */
6 6
 package org.openzen.zenscript.formatter;
7 7
 
8
+import org.openzen.zenscript.formattershared.ExpressionString;
8 9
 import org.openzen.zenscript.codemodel.OperatorType;
9 10
 import org.openzen.zenscript.codemodel.expression.AndAndExpression;
10 11
 import org.openzen.zenscript.codemodel.expression.ArrayExpression;
11
-import org.openzen.zenscript.codemodel.expression.BasicCompareExpression;
12
+import org.openzen.zenscript.codemodel.expression.CompareExpression;
12 13
 import org.openzen.zenscript.codemodel.expression.CallExpression;
13 14
 import org.openzen.zenscript.codemodel.expression.CallStaticExpression;
14 15
 import org.openzen.zenscript.codemodel.expression.CapturedClosureExpression;
@@ -20,6 +21,7 @@ import org.openzen.zenscript.codemodel.expression.CastExpression;
20 21
 import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
21 22
 import org.openzen.zenscript.codemodel.expression.CoalesceExpression;
22 23
 import org.openzen.zenscript.codemodel.expression.ConditionalExpression;
24
+import org.openzen.zenscript.codemodel.expression.ConstExpression;
23 25
 import org.openzen.zenscript.codemodel.expression.ConstantBoolExpression;
24 26
 import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
25 27
 import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
@@ -39,7 +41,6 @@ import org.openzen.zenscript.codemodel.expression.EnumConstantExpression;
39 41
 import org.openzen.zenscript.codemodel.expression.Expression;
40 42
 import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
41 43
 import org.openzen.zenscript.codemodel.expression.FunctionExpression;
42
-import org.openzen.zenscript.codemodel.expression.GenericCompareExpression;
43 44
 import org.openzen.zenscript.codemodel.expression.GetFieldExpression;
44 45
 import org.openzen.zenscript.codemodel.expression.GetFunctionParameterExpression;
45 46
 import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
@@ -67,6 +68,7 @@ import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
67 68
 import org.openzen.zenscript.codemodel.expression.StaticSetterExpression;
68 69
 import org.openzen.zenscript.codemodel.expression.SupertypeCastExpression;
69 70
 import org.openzen.zenscript.codemodel.expression.ThisExpression;
71
+import org.openzen.zenscript.codemodel.expression.ThrowExpression;
70 72
 import org.openzen.zenscript.codemodel.expression.TryConvertExpression;
71 73
 import org.openzen.zenscript.codemodel.expression.TryRethrowAsExceptionExpression;
72 74
 import org.openzen.zenscript.codemodel.expression.TryRethrowAsResultExpression;
@@ -80,17 +82,17 @@ import org.openzen.zenscript.shared.StringUtils;
80 82
  * @author Hoofdgebruiker
81 83
  */
82 84
 public class ExpressionFormatter implements ExpressionVisitor<ExpressionString> {
83
-	private final FormattingSettings settings;
85
+	private final ScriptFormattingSettings settings;
84 86
 	public final TypeFormatter typeFormatter;
85 87
 	
86
-	public ExpressionFormatter(FormattingSettings settings, TypeFormatter typeFormatter) {
88
+	public ExpressionFormatter(ScriptFormattingSettings settings, TypeFormatter typeFormatter) {
87 89
 		this.settings = settings;
88 90
 		this.typeFormatter = typeFormatter;
89 91
 	}
90 92
 
91 93
 	@Override
92 94
 	public ExpressionString visitAndAnd(AndAndExpression expression) {
93
-		return binary(expression.left, expression.right, OperatorPriority.ANDAND, " && ");
95
+		return binary(expression.left, expression.right, ZenScriptOperator.ANDAND);
94 96
 	}
95 97
 
96 98
 	@Override
@@ -106,16 +108,15 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
106 108
 			index++;
107 109
 		}
108 110
 		result.append("]");
109
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
111
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
110 112
 	}
111 113
 
112 114
 	@Override
113
-	public ExpressionString visitCompare(BasicCompareExpression expression) {
115
+	public ExpressionString visitCompare(CompareExpression expression) {
114 116
 		return binary(
115 117
 				expression.left,
116 118
 				expression.right,
117
-				OperatorPriority.COMPARE,
118
-				" " + expression.operator.str + " ");
119
+				ZenScriptOperator.getComparison(expression.comparison));
119 120
 	}
120 121
 
121 122
 	@Override
@@ -124,35 +125,37 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
124 125
 			OperatorMember operator = (OperatorMember) expression.member;
125 126
 			switch (operator.operator) {
126 127
 				case NOT:
127
-					return unaryPrefix(expression.target, OperatorPriority.NOT, "!");
128
+					return unaryPrefix(expression.target, ZenScriptOperator.NOT, "!");
128 129
 				case NEG:
129
-					return unaryPrefix(expression.target, OperatorPriority.NEG, "-");
130
+					return unaryPrefix(expression.target, ZenScriptOperator.NEG, "-");
130 131
 				case CAT:
131 132
 					if (expression.arguments.arguments.length == 0) {
132
-						return unaryPrefix(expression.target, OperatorPriority.INVERT, "~");
133
+						return unaryPrefix(expression.target, ZenScriptOperator.INVERT, "~");
133 134
 					} else {
134
-						return binary(expression.target, expression.getFirstArgument(), OperatorPriority.CAT, " ~ ");
135
+						return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.CAT);
135 136
 					}
136 137
 				case ADD:
137
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.ADD, " + ");
138
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.ADD);
138 139
 				case SUB:
139
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.SUB, " - ");
140
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.SUB);
140 141
 				case MUL:
141
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.MUL, " * ");
142
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.MUL);
142 143
 				case DIV:
143
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.DIV, " / ");
144
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.DIV);
144 145
 				case MOD:
145
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.MOD, " % ");
146
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.MOD);
146 147
 				case AND:
147
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.AND, " & ");
148
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.AND);
148 149
 				case OR:
149
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.OR, " | ");
150
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.OR);
150 151
 				case XOR:
151
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XOR, " ^ ");
152
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.XOR);
152 153
 				case CONTAINS:
153
-					return binary(expression.getFirstArgument(), expression.target, OperatorPriority.CONTAINS, " in ");
154
+					return binary(expression.getFirstArgument(), expression.target, ZenScriptOperator.CONTAINS);
154 155
 				case EQUALS:
155
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.COMPARE, " == ");
156
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.EQUALS);
157
+				case NOTEQUALS:
158
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.NOTEQUALS);
156 159
 				case INDEXSET: {
157 160
 					StringBuilder result = new StringBuilder();
158 161
 					result.append(expression.target);
@@ -165,7 +168,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
165 168
 					}
166 169
 					result.append("] = ");
167 170
 					result.append(expression.arguments.arguments[expression.arguments.arguments.length - 1].accept(this).value);
168
-					return new ExpressionString(result.toString(), OperatorPriority.ASSIGN);
171
+					return new ExpressionString(result.toString(), ZenScriptOperator.ASSIGN);
169 172
 				}
170 173
 				case INDEXGET: {
171 174
 					StringBuilder result = new StringBuilder();
@@ -178,7 +181,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
178 181
 						result.append(expression.arguments.arguments[i].accept(this));
179 182
 					}
180 183
 					result.append("]");
181
-					return new ExpressionString(result.toString(), OperatorPriority.INDEX);
184
+					return new ExpressionString(result.toString(), ZenScriptOperator.INDEX);
182 185
 				}
183 186
 				case MEMBERGETTER: {
184 187
 					StringBuilder result = new StringBuilder();
@@ -186,7 +189,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
186 189
 					result.append(".get(");
187 190
 					result.append(expression.getFirstArgument().accept(this));
188 191
 					result.append(")");
189
-					return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
192
+					return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
190 193
 				}
191 194
 				case MEMBERSETTER: {
192 195
 					StringBuilder result = new StringBuilder();
@@ -196,41 +199,41 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
196 199
 					result.append(", ");
197 200
 					result.append(expression.arguments.arguments[1].accept(this));
198 201
 					result.append(")");
199
-					return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
202
+					return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
200 203
 				}
201 204
 				case ADDASSIGN:
202
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " += ");
205
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.ADDASSIGN);
203 206
 				case SUBASSIGN:
204
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " -= ");
207
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.SUBASSIGN);
205 208
 				case MULASSIGN:
206
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " *= ");
209
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.MULASSIGN);
207 210
 				case DIVASSIGN:
208
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " /= ");
211
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.DIVASSIGN);
209 212
 				case MODASSIGN:
210
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " %= ");
213
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.MODASSIGN);
211 214
 				case CATASSIGN:
212
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " ~= ");
215
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.CATASSIGN);
213 216
 				case ORASSIGN:
214
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " |= ");
217
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.ORASSIGN);
215 218
 				case ANDASSIGN:
216
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " &= ");
219
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.ANDASSIGN);
217 220
 				case XORASSIGN:
218
-					return binary(expression.target, expression.getFirstArgument(), OperatorPriority.XORASSIGN, " ^= ");
221
+					return binary(expression.target, expression.getFirstArgument(), ZenScriptOperator.XORASSIGN);
219 222
 				case INCREMENT:
220
-					return unaryPrefix(expression.target, OperatorPriority.DECREMENT, "++");
223
+					return unaryPrefix(expression.target, ZenScriptOperator.DECREMENT, "++");
221 224
 				case DECREMENT:
222
-					return unaryPrefix(expression.target, OperatorPriority.DECREMENT, "--");
225
+					return unaryPrefix(expression.target, ZenScriptOperator.DECREMENT, "--");
223 226
 				case CALL: {
224 227
 					StringBuilder result = new StringBuilder();
225 228
 					result.append(".");
226 229
 					FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
227
-					return new ExpressionString(result.toString(), OperatorPriority.CALL);
230
+					return new ExpressionString(result.toString(), ZenScriptOperator.CALL);
228 231
 				}
229 232
 				case CAST: {
230 233
 					StringBuilder result = new StringBuilder();
231 234
 					result.append(" as ");
232 235
 					result.append(expression.arguments.typeArguments[0].accept(typeFormatter));
233
-					return new ExpressionString(result.toString(), OperatorPriority.CAST);
236
+					return new ExpressionString(result.toString(), ZenScriptOperator.CAST);
234 237
 				}
235 238
 				default:
236 239
 					throw new UnsupportedOperationException("Unknown operator: " + operator.operator);
@@ -241,7 +244,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
241 244
 			result.append(".");
242 245
 			result.append(expression.member.name);
243 246
 			FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
244
-			return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
247
+			return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
245 248
 		}
246 249
 	}
247 250
 
@@ -262,7 +265,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
262 265
 			result.append(expression.member.name);
263 266
 		}
264 267
 		FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
265
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
268
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
266 269
 	}
267 270
 
268 271
 	@Override
@@ -277,17 +280,17 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
277 280
 
278 281
 	@Override
279 282
 	public ExpressionString visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
280
-		return new ExpressionString(expression.variable.name, OperatorPriority.PRIMARY);
283
+		return new ExpressionString(expression.variable.name, ZenScriptOperator.PRIMARY);
281 284
 	}
282 285
 
283 286
 	@Override
284 287
 	public ExpressionString visitCapturedParameter(CapturedParameterExpression expression) {
285
-		return new ExpressionString(expression.parameter.name, OperatorPriority.PRIMARY);
288
+		return new ExpressionString(expression.parameter.name, ZenScriptOperator.PRIMARY);
286 289
 	}
287 290
 
288 291
 	@Override
289 292
 	public ExpressionString visitCapturedThis(CapturedThisExpression expression) {
290
-		return new ExpressionString("this", OperatorPriority.PRIMARY);
293
+		return new ExpressionString("this", ZenScriptOperator.PRIMARY);
291 294
 	}
292 295
 	
293 296
 	@Override
@@ -298,7 +301,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
298 301
 			result.append(" as ");
299 302
 			result.append(expression.member.toType.accept(typeFormatter));
300 303
 		}
301
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
304
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
302 305
 	}
303 306
 
304 307
 	@Override
@@ -308,7 +311,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
308 311
 
309 312
 	@Override
310 313
 	public ExpressionString visitCoalesce(CoalesceExpression expression) {
311
-		return binary(expression.left, expression.right, OperatorPriority.COALESCE, " ?? ");
314
+		return binary(expression.left, expression.right, ZenScriptOperator.COALESCE);
312 315
 	}
313 316
 
314 317
 	@Override
@@ -319,24 +322,33 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
319 322
 		result.append(expression.ifThen.accept(this));
320 323
 		result.append(" : ");
321 324
 		result.append(expression.ifElse.accept(this));
322
-		return new ExpressionString(result.toString(), OperatorPriority.TERNARY);
325
+		return new ExpressionString(result.toString(), ZenScriptOperator.TERNARY);
326
+	}
327
+	
328
+	@Override
329
+	public ExpressionString visitConst(ConstExpression expression) {
330
+		StringBuilder result = new StringBuilder();
331
+		result.append(expression.type.accept(typeFormatter));
332
+		result.append('.');
333
+		result.append(expression.constant.name);
334
+		return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
323 335
 	}
324 336
 
325 337
 	@Override
326 338
 	public ExpressionString visitConstantBool(ConstantBoolExpression expression) {
327
-		return new ExpressionString(expression.value ? "true" : "false", OperatorPriority.PRIMARY);
339
+		return new ExpressionString(expression.value ? "true" : "false", ZenScriptOperator.PRIMARY);
328 340
 	}
329 341
 
330 342
 	@Override
331 343
 	public ExpressionString visitConstantByte(ConstantByteExpression expression) {
332
-		return new ExpressionString(Byte.toString(expression.value) + " as byte", OperatorPriority.CAST);
344
+		return new ExpressionString(Integer.toString(expression.value) + " as byte", ZenScriptOperator.CAST);
333 345
 	}
334 346
 
335 347
 	@Override
336 348
 	public ExpressionString visitConstantChar(ConstantCharExpression expression) {
337 349
 		return new ExpressionString(
338 350
 				StringUtils.escape(Character.toString(expression.value), '\'', true),
339
-				OperatorPriority.PRIMARY);
351
+				ZenScriptOperator.PRIMARY);
340 352
 	}
341 353
 
342 354
 	@Override
@@ -351,22 +363,22 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
351 363
 
352 364
 	@Override
353 365
 	public ExpressionString visitConstantInt(ConstantIntExpression expression) {
354
-		return new ExpressionString(Integer.toString(expression.value), OperatorPriority.PRIMARY);
366
+		return new ExpressionString(Integer.toString(expression.value), ZenScriptOperator.PRIMARY);
355 367
 	}
356 368
 
357 369
 	@Override
358 370
 	public ExpressionString visitConstantLong(ConstantLongExpression expression) {
359
-		return new ExpressionString(Long.toString(expression.value), OperatorPriority.PRIMARY);
371
+		return new ExpressionString(Long.toString(expression.value), ZenScriptOperator.PRIMARY);
360 372
 	}
361 373
 
362 374
 	@Override
363 375
 	public ExpressionString visitConstantSByte(ConstantSByteExpression expression) {
364
-		return new ExpressionString(Byte.toString(expression.value) + " as sbyte", OperatorPriority.CAST);
376
+		return new ExpressionString(Byte.toString(expression.value) + " as sbyte", ZenScriptOperator.CAST);
365 377
 	}
366 378
 
367 379
 	@Override
368 380
 	public ExpressionString visitConstantShort(ConstantShortExpression expression) {
369
-		return new ExpressionString(Integer.toString(expression.value) + " as short", OperatorPriority.CAST);
381
+		return new ExpressionString(Integer.toString(expression.value) + " as short", ZenScriptOperator.CAST);
370 382
 	}
371 383
 
372 384
 	@Override
@@ -374,22 +386,22 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
374 386
 		return new ExpressionString(StringUtils.escape(
375 387
 				expression.value,
376 388
 				settings.useSingleQuotesForStrings ? '\'' : '"',
377
-				true), OperatorPriority.CAST);
389
+				true), ZenScriptOperator.CAST);
378 390
 	}
379 391
 
380 392
 	@Override
381 393
 	public ExpressionString visitConstantUInt(ConstantUIntExpression expression) {
382
-		return new ExpressionString(Integer.toString(expression.value) + " as uint", OperatorPriority.CAST);
394
+		return new ExpressionString(Integer.toString(expression.value) + " as uint", ZenScriptOperator.CAST);
383 395
 	}
384 396
 
385 397
 	@Override
386 398
 	public ExpressionString visitConstantULong(ConstantULongExpression expression) {
387
-		return new ExpressionString(Long.toString(expression.value) + " as ulong", OperatorPriority.CAST);
399
+		return new ExpressionString(Long.toString(expression.value) + " as ulong", ZenScriptOperator.CAST);
388 400
 	}
389 401
 
390 402
 	@Override
391 403
 	public ExpressionString visitConstantUShort(ConstantUShortExpression expression) {
392
-		return new ExpressionString(Integer.toString(expression.value) + " as ushort", OperatorPriority.CAST);
404
+		return new ExpressionString(Integer.toString(expression.value) + " as ushort", ZenScriptOperator.CAST);
393 405
 	}
394 406
 
395 407
 	@Override
@@ -397,7 +409,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
397 409
 		StringBuilder result = new StringBuilder();
398 410
 		result.append("this");
399 411
 		FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
400
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
412
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
401 413
 	}
402 414
 
403 415
 	@Override
@@ -405,12 +417,12 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
405 417
 		StringBuilder result = new StringBuilder();
406 418
 		result.append("super");
407 419
 		FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
408
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
420
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
409 421
 	}
410 422
 
411 423
 	@Override
412 424
 	public ExpressionString visitEnumConstant(EnumConstantExpression expression) {
413
-		return new ExpressionString(expression.type.accept(typeFormatter) + "." + expression.value.name, OperatorPriority.MEMBER);
425
+		return new ExpressionString(expression.type.accept(typeFormatter) + "." + expression.value.name, ZenScriptOperator.MEMBER);
414 426
 	}
415 427
 
416 428
 	@Override
@@ -418,28 +430,23 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
418 430
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
419 431
 	}
420 432
 
421
-	@Override
422
-	public ExpressionString visitGenericCompare(GenericCompareExpression expression) {
423
-		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
424
-	}
425
-
426 433
 	@Override
427 434
 	public ExpressionString visitGetField(GetFieldExpression expression) {
428 435
 		StringBuilder result = new StringBuilder();
429 436
 		result.append(expression.target.accept(this));
430 437
 		result.append('.');
431 438
 		result.append(expression.field.name);
432
-		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
439
+		return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
433 440
 	}
434 441
 
435 442
 	@Override
436 443
 	public ExpressionString visitGetFunctionParameter(GetFunctionParameterExpression expression) {
437
-		return new ExpressionString(expression.parameter.name, OperatorPriority.PRIMARY);
444
+		return new ExpressionString(expression.parameter.name, ZenScriptOperator.PRIMARY);
438 445
 	}
439 446
 
440 447
 	@Override
441 448
 	public ExpressionString visitGetLocalVariable(GetLocalVariableExpression expression) {
442
-		return new ExpressionString(expression.variable.name, OperatorPriority.PRIMARY);
449
+		return new ExpressionString(expression.variable.name, ZenScriptOperator.PRIMARY);
443 450
 	}
444 451
 
445 452
 	@Override
@@ -448,7 +455,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
448 455
 		result.append(expression.type.accept(typeFormatter));
449 456
 		result.append('.');
450 457
 		result.append(expression.field.name);
451
-		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
458
+		return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
452 459
 	}
453 460
 
454 461
 	@Override
@@ -457,12 +464,12 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
457 464
 		result.append(expression.target.accept(this));
458 465
 		result.append('.');
459 466
 		result.append(expression.getter.name);
460
-		return new ExpressionString(result.toString(), OperatorPriority.MEMBER);
467
+		return new ExpressionString(result.toString(), ZenScriptOperator.MEMBER);
461 468
 	}
462 469
 	
463 470
 	@Override
464 471
 	public ExpressionString visitGlobal(GlobalExpression expression) {
465
-		return new ExpressionString(expression.name, OperatorPriority.PRIMARY);
472
+		return new ExpressionString(expression.name, ZenScriptOperator.PRIMARY);
466 473
 	}
467 474
 	
468 475
 	@Override
@@ -470,7 +477,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
470 477
 		StringBuilder result = new StringBuilder();
471 478
 		result.append(expression.name);
472 479
 		FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
473
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
480
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
474 481
 	}
475 482
 
476 483
 	@Override
@@ -479,7 +486,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
479 486
 		result.append(expression.value.accept(this).value);
480 487
 		result.append(" as ");
481 488
 		result.append(expression.type.accept(typeFormatter));
482
-		return new ExpressionString(result.toString(), OperatorPriority.CAST);
489
+		return new ExpressionString(result.toString(), ZenScriptOperator.CAST);
483 490
 	}
484 491
 
485 492
 	@Override
@@ -488,7 +495,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
488 495
 		result.append(expression.value.accept(this).value);
489 496
 		result.append(" is ");
490 497
 		result.append(expression.type.accept(typeFormatter));
491
-		return new ExpressionString(result.toString(), OperatorPriority.COMPARE);
498
+		return new ExpressionString(result.toString(), ZenScriptOperator.IS);
492 499
 	}
493 500
 
494 501
 	@Override
@@ -508,7 +515,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
508 515
 			result.append(expression.values[i].accept(this));
509 516
 		}
510 517
 		result.append("}");
511
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
518
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
512 519
 	}
513 520
 	
514 521
 	@Override
@@ -518,8 +525,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
518 525
 		result.append(expression.value.accept(this));
519 526
 		result.append(" {\n");
520 527
 
521
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
522
-//		throw new UnsupportedOperationException();
528
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
523 529
 	}
524 530
 
525 531
 	@Override
@@ -528,81 +534,81 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
528 534
 		result.append("new ");
529 535
 		result.append(expression.type.accept(typeFormatter));
530 536
 		FormattingUtils.formatCall(result, typeFormatter, this, expression.arguments);
531
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
537
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
532 538
 	}
533 539
 
534 540
 	@Override
535 541
 	public ExpressionString visitNull(NullExpression expression) {
536
-		return new ExpressionString("null", OperatorPriority.PRIMARY);
542
+		return new ExpressionString("null", ZenScriptOperator.PRIMARY);
537 543
 	}
538 544
 
539 545
 	@Override
540 546
 	public ExpressionString visitOrOr(OrOrExpression expression) {
541
-		return binary(expression.left, expression.right, OperatorPriority.OROR, " || ");
547
+		return binary(expression.left, expression.right, ZenScriptOperator.OROR);
542 548
 	}
543 549
 	
544 550
 	@Override
545 551
 	public ExpressionString visitPostCall(PostCallExpression expression) {
546
-		return unaryPostfix(expression.target, OperatorPriority.INCREMENT, expression.member.operator == OperatorType.INCREMENT ? "++" : "--");
552
+		return unaryPostfix(expression.target, ZenScriptOperator.INCREMENT, expression.member.operator == OperatorType.INCREMENT ? "++" : "--");
547 553
 	}
548 554
 
549 555
 	@Override
550 556
 	public ExpressionString visitRange(RangeExpression expression) {
551
-		return binary(expression.from, expression.to, OperatorPriority.RANGE, " .. ");
557
+		return binary(expression.from, expression.to, ZenScriptOperator.RANGE);
552 558
 	}
553 559
 	
554 560
 	@Override
555 561
 	public ExpressionString visitSameObject(SameObjectExpression expression) {
556
-		return binary(expression.left, expression.right, OperatorPriority.COMPARE, expression.inverted ? " !== " : " === ");
562
+		return binary(expression.left, expression.right, expression.inverted ? ZenScriptOperator.NOTSAME : ZenScriptOperator.SAME);
557 563
 	}
558 564
 
559 565
 	@Override
560 566
 	public ExpressionString visitSetField(SetFieldExpression expression) {
561 567
 		return new ExpressionString(
562 568
 				expression.target.accept(this) + "." + expression.field.name + " = " + expression.value.accept(this).value, 
563
-				OperatorPriority.ASSIGN);
569
+				ZenScriptOperator.ASSIGN);
564 570
 	}
565 571
 
566 572
 	@Override
567 573
 	public ExpressionString visitSetFunctionParameter(SetFunctionParameterExpression expression) {
568 574
 		return new ExpressionString(
569 575
 				expression.parameter.name + " = " + expression.value.accept(this).value,
570
-				OperatorPriority.ASSIGN);
576
+				ZenScriptOperator.ASSIGN);
571 577
 	}
572 578
 
573 579
 	@Override
574 580
 	public ExpressionString visitSetLocalVariable(SetLocalVariableExpression expression) {
575 581
 		return new ExpressionString(
576 582
 				expression.variable.name + " = " + expression.value.accept(this).value,
577
-				OperatorPriority.ASSIGN);
583
+				ZenScriptOperator.ASSIGN);
578 584
 	}
579 585
 
580 586
 	@Override
581 587
 	public ExpressionString visitSetStaticField(SetStaticFieldExpression expression) {
582 588
 		return new ExpressionString(
583 589
 				expression.type.accept(typeFormatter) + "." + expression.field.name + " = " + expression.value.accept(this).value,
584
-				OperatorPriority.ASSIGN);
590
+				ZenScriptOperator.ASSIGN);
585 591
 	}
586 592
 
587 593
 	@Override
588 594
 	public ExpressionString visitSetter(SetterExpression expression) {
589 595
 		return new ExpressionString(
590 596
 				expression.target.accept(this) + "." + expression.setter.name + " = " + expression.value.accept(this),
591
-				OperatorPriority.ASSIGN);
597
+				ZenScriptOperator.ASSIGN);
592 598
 	}
593 599
 
594 600
 	@Override
595 601
 	public ExpressionString visitStaticGetter(StaticGetterExpression expression) {
596 602
 		return new ExpressionString(
597 603
 				expression.type.accept(typeFormatter) + "." + expression.getter.name, 
598
-				OperatorPriority.MEMBER);
604
+				ZenScriptOperator.MEMBER);
599 605
 	}
600 606
 
601 607
 	@Override
602 608
 	public ExpressionString visitStaticSetter(StaticSetterExpression expression) {
603 609
 		return new ExpressionString(
604 610
 				expression.type.accept(typeFormatter) + "." + expression.setter.name + " = " + expression.setter.name,
605
-				OperatorPriority.ASSIGN);
611
+				ZenScriptOperator.ASSIGN);
606 612
 	}
607 613
 	
608 614
 	@Override
@@ -612,7 +618,12 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
612 618
 
613 619
 	@Override
614 620
 	public ExpressionString visitThis(ThisExpression expression) {
615
-		return new ExpressionString("this", OperatorPriority.PRIMARY);
621
+		return new ExpressionString("this", ZenScriptOperator.PRIMARY);
622
+	}
623
+
624
+	@Override
625
+	public ExpressionString visitThrow(ThrowExpression expression) {
626
+		return new ExpressionString("throw " + expression.value.accept(this), ZenScriptOperator.PRIMARY);
616 627
 	}
617 628
 
618 629
 	@Override
@@ -638,7 +649,7 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
638 649
 		StringBuilder result = new StringBuilder();
639 650
 		result.append("new ");
640 651
 		// TODO: finish this
641
-		return new ExpressionString(result.toString(), OperatorPriority.PRIMARY);
652
+		return new ExpressionString(result.toString(), ZenScriptOperator.PRIMARY);
642 653
 	}
643 654
 
644 655
 	@Override
@@ -646,17 +657,15 @@ public class ExpressionFormatter implements ExpressionVisitor<ExpressionString>
646 657
 		return expression.value.accept(this);
647 658
 	}
648 659
 	
649
-	private ExpressionString unaryPrefix(Expression value, OperatorPriority operator, String operatorString) {
660
+	private ExpressionString unaryPrefix(Expression value, ZenScriptOperator operator, String operatorString) {
650 661
 		return new ExpressionString(operatorString + value.accept(this).value, operator);
651 662
 	}
652 663
 	
653
-	private ExpressionString unaryPostfix(Expression value, OperatorPriority operator, String operatorString) {
664
+	private ExpressionString unaryPostfix(Expression value, ZenScriptOperator operator, String operatorString) {
654 665
 		return new ExpressionString(value.accept(this).value + operatorString, operator);
655 666
 	}
656 667
 	
657
-	private ExpressionString binary(Expression left, Expression right, OperatorPriority operator, String operatorString) {
658
-		String value = left.accept(this).wrapLeft(operator)
659
-				+ operatorString + right.accept(this).wrapRight(operator);
660
-		return new ExpressionString(value, operator);
668
+	private ExpressionString binary(Expression left, Expression right, ZenScriptOperator operator) {
669
+		return ExpressionString.binary(left.accept(this), right.accept(this), operator);
661 670
 	}
662 671
 }

+ 0
- 33
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ExpressionString.java Näytä tiedosto

@@ -1,33 +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.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
-}

+ 3
- 2
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FileFormatter.java Näytä tiedosto

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.formatter;
7 7
 
8
+import org.openzen.zenscript.formattershared.Importer;
8 9
 import java.util.ArrayList;
9 10
 import java.util.Collections;
10 11
 import java.util.HashMap;
@@ -21,9 +22,9 @@ import org.openzen.zenscript.codemodel.statement.Statement;
21 22
  * @author Hoofdgebruiker
22 23
  */
23 24
 public class FileFormatter {
24
-	private final FormattingSettings settings;
25
+	private final ScriptFormattingSettings settings;
25 26
 	
26
-	public FileFormatter(FormattingSettings settings) {
27
+	public FileFormatter(ScriptFormattingSettings settings) {
27 28
 		this.settings = settings;
28 29
 	}
29 30
 	

+ 4
- 4
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FormattingUtils.java Näytä tiedosto

@@ -66,7 +66,7 @@ public class FormattingUtils {
66 66
 			output.append("const? ");
67 67
 	}
68 68
 	
69
-	public static void formatHeader(StringBuilder result, FormattingSettings settings, FunctionHeader header, TypeFormatter typeFormatter) {
69
+	public static void formatHeader(StringBuilder result, ScriptFormattingSettings settings, FunctionHeader header, TypeFormatter typeFormatter) {
70 70
 		FormattingUtils.formatTypeParameters(result, header.typeParameters, typeFormatter);
71 71
 		result.append("(");
72 72
 		int parameterIndex = 0;
@@ -115,7 +115,7 @@ public class FormattingUtils {
115 115
 		}
116 116
 	}
117 117
 	
118
-	public static void formatBody(StringBuilder output, FormattingSettings settings, String indent, TypeFormatter typeFormatter, Statement body) {
118
+	public static void formatBody(StringBuilder output, ScriptFormattingSettings settings, String indent, TypeFormatter typeFormatter, Statement body) {
119 119
 		body.accept(new BodyFormatter(output, settings, indent, typeFormatter));
120 120
 		output.append("\n");
121 121
 	}
@@ -149,12 +149,12 @@ public class FormattingUtils {
149 149
 	
150 150
 	private static class BodyFormatter implements StatementVisitor<Void> {
151 151
 		private final StringBuilder output;
152
-		private final FormattingSettings settings;
152
+		private final ScriptFormattingSettings settings;
153 153
 		private final StatementFormatter statementFormatter;
154 154
 		private final String indent;
155 155
 		private final TypeFormatter typeFormatter;
156 156
 		
157
-		public BodyFormatter(StringBuilder output, FormattingSettings settings, String indent, TypeFormatter typeFormatter) {
157
+		public BodyFormatter(StringBuilder output, ScriptFormattingSettings settings, String indent, TypeFormatter typeFormatter) {
158 158
 			this.output = output;
159 159
 			this.settings = settings;
160 160
 			this.indent = indent;

+ 18
- 4
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/MemberFormatter.java Näytä tiedosto

@@ -8,10 +8,10 @@ package org.openzen.zenscript.formatter;
8 8
 import org.openzen.zenscript.codemodel.Modifiers;
9 9
 import org.openzen.zenscript.codemodel.member.CallerMember;
10 10
 import org.openzen.zenscript.codemodel.member.CasterMember;
11
+import org.openzen.zenscript.codemodel.member.ConstMember;
11 12
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
12 13
 import org.openzen.zenscript.codemodel.member.CustomIteratorMember;
13 14
 import org.openzen.zenscript.codemodel.member.DestructorMember;
14
-import org.openzen.zenscript.codemodel.member.EnumConstantMember;
15 15
 import org.openzen.zenscript.codemodel.member.FieldMember;
16 16
 import org.openzen.zenscript.codemodel.member.GetterMember;
17 17
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
@@ -29,14 +29,14 @@ import org.openzen.zenscript.codemodel.statement.Statement;
29 29
  * @author Hoofdgebruiker
30 30
  */
31 31
 public class MemberFormatter implements MemberVisitor<Void> {
32
-	private final FormattingSettings settings;
32
+	private final ScriptFormattingSettings settings;
33 33
 	private final StringBuilder output;
34 34
 	private final String indent;
35 35
 	private final TypeFormatter typeFormatter;
36 36
 	private boolean isFirst = true;
37 37
 	private boolean wasField = false;
38 38
 	
39
-	public MemberFormatter(FormattingSettings settings, StringBuilder output, String indent, TypeFormatter typeFormatter) {
39
+	public MemberFormatter(ScriptFormattingSettings settings, StringBuilder output, String indent, TypeFormatter typeFormatter) {
40 40
 		this.settings = settings;
41 41
 		this.output = output;
42 42
 		this.indent = indent;
@@ -54,6 +54,20 @@ public class MemberFormatter implements MemberVisitor<Void> {
54 54
 		
55 55
 		wasField = field;
56 56
 	}
57
+	
58
+	@Override
59
+	public Void visitConst(ConstMember member) {
60
+		visit(true);
61
+		FormattingUtils.formatModifiers(output, member.modifiers & ~Modifiers.FINAL);
62
+		output.append("const")
63
+				.append(member.name)
64
+				.append(" as ")
65
+				.append(member.type.accept(typeFormatter))
66
+				.append(" = ")
67
+				.append(member.value.accept(new ExpressionFormatter(settings, typeFormatter)))
68
+				.append(";\n");
69
+		return null;
70
+	}
57 71
 
58 72
 	@Override
59 73
 	public Void visitField(FieldMember member) {
@@ -68,7 +82,7 @@ public class MemberFormatter implements MemberVisitor<Void> {
68 82
 			output.append(" = ")
69 83
 					.append(member.initializer.accept(new ExpressionFormatter(settings, typeFormatter)));
70 84
 		}
71
-		output.append(";").append("\n");
85
+		output.append(";\n");
72 86
 		return null;
73 87
 	}
74 88
 

+ 0
- 73
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/OperatorPriority.java Näytä tiedosto

@@ -1,73 +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.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
-}

CodeFormatter/src/main/java/org/openzen/zenscript/formatter/FormattingSettings.java → CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ScriptFormattingSettings.java Näytä tiedosto

@@ -5,17 +5,15 @@
5 5
  */
6 6
 package org.openzen.zenscript.formatter;
7 7
 
8
-import org.openzen.zenscript.shared.StringUtils;
8
+import org.openzen.zenscript.formattershared.FormattingSettings;
9 9
 
10 10
 /**
11 11
  *
12 12
  * @author Hoofdgebruiker
13 13
  */
14
-public class FormattingSettings {
14
+public class ScriptFormattingSettings extends FormattingSettings {
15 15
 	public final boolean showAnyInFunctionHeaders;
16 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 17
 	public final boolean spaceBeforeLabelColon;
20 18
 	public final boolean spaceAfterLabelColon;
21 19
 	public final boolean bracketsAroundConditions;
@@ -37,13 +35,11 @@ public class FormattingSettings {
37 35
 	public final boolean functionBracketOnSameLine;
38 36
 	public final boolean lambdaMethodOnSameLine;
39 37
 	
40
-	public final String indent;
41
-	
42
-	private FormattingSettings(Builder builder) {
38
+	private ScriptFormattingSettings(Builder builder) {
39
+		super(builder);
40
+		
43 41
 		showAnyInFunctionHeaders = builder.showAnyInFunctionHeaders;
44 42
 		useSingleQuotesForStrings = builder.useSingleQuotesForStrings;
45
-		useTabs = builder.useTabs;
46
-		spacesPerTab = builder.spacesPerTab;
47 43
 		spaceBeforeLabelColon = builder.spaceBeforeLabelColon;
48 44
 		spaceAfterLabelColon = builder.spaceAfterLabelColon;
49 45
 		bracketsAroundConditions = builder.bracketsAroundConditions;
@@ -64,12 +60,6 @@ public class FormattingSettings {
64 60
 		classBracketOnSameLine = builder.classBracketOnSameLine;
65 61
 		functionBracketOnSameLine = builder.functionBracketOnSameLine;
66 62
 		lambdaMethodOnSameLine = builder.lambdaMethodOnSameLine;
67
-		
68
-		if (useTabs) {
69
-			indent = "\t";
70
-		} else {
71
-			indent = StringUtils.times(' ', spacesPerTab);
72
-		}
73 63
 	}
74 64
 	
75 65
 	public String getSingleLineSeparator(String indent, ParentStatementType position) {
@@ -143,7 +133,7 @@ public class FormattingSettings {
143 133
 		}
144 134
 	}
145 135
 	
146
-	public static class Builder {
136
+	public static class Builder extends FormattingSettings.Builder<Builder> {
147 137
 		private boolean showAnyInFunctionHeaders = false;
148 138
 		private boolean useSingleQuotesForStrings = true;
149 139
 		private boolean useTabs = false;
@@ -169,6 +159,12 @@ public class FormattingSettings {
169 159
 		private boolean functionBracketOnSameLine = false;
170 160
 		private boolean lambdaMethodOnSameLine = false;
171 161
 		
162
+		public Builder() {
163
+			super(CommentFormatter::format);
164
+			
165
+			instance = this;
166
+		}
167
+		
172 168
 		public Builder showAnyInFunctionHeaders(boolean show) {
173 169
 			showAnyInFunctionHeaders = show;
174 170
 			return this;
@@ -179,16 +175,6 @@ public class FormattingSettings {
179 175
 			return this;
180 176
 		}
181 177
 		
182
-		public Builder useTabs(boolean tabs) {
183
-			useTabs = tabs;
184
-			return this;
185
-		}
186
-		
187
-		public Builder spacesPerTabs(int spaces) {
188
-			spacesPerTab = spaces;
189
-			return this;
190
-		}
191
-		
192 178
 		public Builder spaceBeforeLabelColon(boolean space) {
193 179
 			spaceBeforeLabelColon = space;
194 180
 			return this;
@@ -289,8 +275,9 @@ public class FormattingSettings {
289 275
 			return this;
290 276
 		}
291 277
 		
292
-		public FormattingSettings build() {
293
-			return new FormattingSettings(this);
278
+		@Override
279
+		public ScriptFormattingSettings build() {
280
+			return new ScriptFormattingSettings(this);
294 281
 		}
295 282
 	}
296 283
 }

+ 2
- 2
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/StatementFormatter.java Näytä tiedosto

@@ -33,14 +33,14 @@ import org.openzen.zenscript.codemodel.statement.WhileStatement;
33 33
  * @author Hoofdgebruiker
34 34
  */
35 35
 public class StatementFormatter implements StatementVisitor<Void> {
36
-	private final FormattingSettings settings;
36
+	private final ScriptFormattingSettings settings;
37 37
 	private final StringBuilder output;
38 38
 	private final ExpressionFormatter expressionFormatter;
39 39
 	
40 40
 	private String indent;
41 41
 	private ParentStatementType position = ParentStatementType.NONE;
42 42
 	
43
-	public StatementFormatter(StringBuilder output, String indent, FormattingSettings settings, ExpressionFormatter expressionFormatter) {
43
+	public StatementFormatter(StringBuilder output, String indent, ScriptFormattingSettings settings, ExpressionFormatter expressionFormatter) {
44 44
 		this.output = output;
45 45
 		this.indent = indent;
46 46
 		this.settings = settings;

+ 2
- 2
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/SwitchValueFormatter.java Näytä tiedosto

@@ -18,9 +18,9 @@ import org.openzen.zenscript.shared.StringUtils;
18 18
  * @author Hoofdgebruiker
19 19
  */
20 20
 public class SwitchValueFormatter implements SwitchValueVisitor<String> {
21
-	private final FormattingSettings settings;
21
+	private final ScriptFormattingSettings settings;
22 22
 	
23
-	public SwitchValueFormatter(FormattingSettings settings) {
23
+	public SwitchValueFormatter(ScriptFormattingSettings settings) {
24 24
 		this.settings = settings;
25 25
 	}
26 26
 

+ 6
- 8
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/TypeFormatter.java Näytä tiedosto

@@ -5,9 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.formatter;
7 7
 
8
-import org.openzen.zenscript.codemodel.FunctionHeader;
9
-import org.openzen.zenscript.codemodel.FunctionParameter;
10
-import org.openzen.zenscript.codemodel.generic.GenericParameterBound;
8
+import org.openzen.zenscript.formattershared.Importer;
11 9
 import org.openzen.zenscript.codemodel.generic.GenericParameterBoundVisitor;
12 10
 import org.openzen.zenscript.codemodel.generic.ParameterSuperBound;
13 11
 import org.openzen.zenscript.codemodel.generic.ParameterTypeBound;
@@ -32,10 +30,10 @@ import org.openzen.zenscript.shared.StringUtils;
32 30
  * @author Hoofdgebruiker
33 31
  */
34 32
 public class TypeFormatter implements ITypeVisitor<String>, GenericParameterBoundVisitor<String> {
35
-	private final FormattingSettings settings;
33
+	private final ScriptFormattingSettings settings;
36 34
 	private final Importer importer;
37 35
 	
38
-	public TypeFormatter(FormattingSettings settings, Importer importer) {
36
+	public TypeFormatter(ScriptFormattingSettings settings, Importer importer) {
39 37
 		this.settings = settings;
40 38
 		this.importer = importer;
41 39
 	}
@@ -127,9 +125,9 @@ public class TypeFormatter implements ITypeVisitor<String>, GenericParameterBoun
127 125
 	public String visitGenericMap(GenericMapTypeID map) {
128 126
 		StringBuilder result = new StringBuilder();
129 127
 		result.append(map.value.accept(this));
130
-		result.append("[");
131
-		FormattingUtils.formatTypeParameters(result, map.keys, this);
132
-		result.append("]");
128
+		result.append("[<");
129
+		FormattingUtils.formatTypeParameters(result, new TypeParameter[] { map.key }, this);
130
+		result.append("]>");
133 131
 		return result.toString();
134 132
 	}
135 133
 }

+ 106
- 0
CodeFormatter/src/main/java/org/openzen/zenscript/formatter/ZenScriptOperator.java Näytä tiedosto

@@ -0,0 +1,106 @@
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.CompareType;
9
+import org.openzen.zenscript.formattershared.FormattableOperator;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public enum ZenScriptOperator implements FormattableOperator {
16
+	ADD(6, " + "),
17
+	SUB(6, " - "),
18
+	MUL(7, " * "),
19
+	DIV(7, " / "),
20
+	MOD(7, " % "),
21
+	CAT(6, " ~ "),
22
+	OR(4, " | "),
23
+	AND(4, " & "),
24
+	XOR(4, " ^ "),
25
+	NEG(8, "-"),
26
+	NOT(8, "!"),
27
+	INVERT(8, "~"),
28
+	CONTAINS(5, " in "),
29
+	EQUALS(6, " == "),
30
+	NOTEQUALS(6, " != "),
31
+	GREATER(6, " > "),
32
+	LESS(6, " < "),
33
+	GREATER_EQUALS(6, " >= "),
34
+	LESS_EQUALS(6, " <= "),
35
+	IS(6, " is "),
36
+	SAME(6, " === "),
37
+	NOTSAME(6, " !== "),
38
+	ASSIGN(0, " = "),
39
+	ADDASSIGN(0, " += "),
40
+	SUBASSIGN(0, " -= "),
41
+	MULASSIGN(0, " *= "),
42
+	DIVASSIGN(0, " /= "),
43
+	MODASSIGN(0, " %= "),
44
+	CATASSIGN(0, " ~= "),
45
+	ORASSIGN(0, " |= "),
46
+	ANDASSIGN(0, " &= "),
47
+	XORASSIGN(0, " ^= "),
48
+	
49
+	ANDAND(3, " && "),
50
+	OROR(2, " || "),
51
+	
52
+	TERNARY(1, null),
53
+	COALESCE(2, " ?? "),
54
+	
55
+	INCREMENT(8, "++"),
56
+	DECREMENT(8, "--"),
57
+	MEMBER(9, null),
58
+	RANGE(9, " .. "),
59
+	INDEX(9, null),
60
+	CALL(9, null),
61
+	CAST(9, null),
62
+	
63
+	PRIMARY(10, null);
64
+	
65
+	private final int priority;
66
+	private final boolean isCommutative;
67
+	private final String operatorString;
68
+	
69
+	private ZenScriptOperator(int priority, String operatorString) {
70
+		this(priority, false, operatorString);
71
+	}
72
+	
73
+	private ZenScriptOperator(int priority, boolean isCommutative, String operatorString) {
74
+		this.priority = priority;
75
+		this.isCommutative = isCommutative;
76
+		this.operatorString = operatorString;
77
+	}
78
+
79
+	@Override
80
+	public int getPriority() {
81
+		return priority;
82
+	}
83
+
84
+	@Override
85
+	public boolean isCommutative() {
86
+		return isCommutative;
87
+	}
88
+	
89
+	@Override
90
+	public String getOperatorString() {
91
+		return operatorString;
92
+	}
93
+	
94
+	public static ZenScriptOperator getComparison(CompareType compare) {
95
+		switch (compare) {
96
+			case EQ: return EQUALS;
97
+			case NE: return NOTEQUALS;
98
+			case LT: return LESS;
99
+			case GT: return GREATER;
100
+			case LE: return LESS_EQUALS;
101
+			case GE: return GREATER_EQUALS;
102
+			default:
103
+				return null;
104
+		}
105
+	}
106
+}

Linker/build.gradle → CodeFormatterShared/build.gradle Näytä tiedosto

@@ -14,6 +14,5 @@ if (!hasProperty('mainClass')) {
14 14
 }
15 15
 
16 16
 dependencies {
17
-	compile project(':Shared')
18 17
 	compile project(':CodeModel')
19 18
 }

+ 14
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/CommentFormatter.java Näytä tiedosto

@@ -0,0 +1,14 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.formattershared;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface CommentFormatter {
13
+	String[] format(String[] lines);
14
+}

+ 55
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/ExpressionString.java Näytä tiedosto

@@ -0,0 +1,55 @@
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.formattershared;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class ExpressionString {
13
+	public final String value;
14
+	public final FormattableOperator priority;
15
+	
16
+	public ExpressionString(String value, FormattableOperator priority) {
17
+		this.value = value;
18
+		this.priority = priority;
19
+	}
20
+	
21
+	public String wrapLeft(FormattableOperator outer) {
22
+		return FormattableOperator.shouldWrapLeft(priority, outer) ? "(" + value + ")" : value;
23
+	}
24
+	
25
+	public String wrapRight(FormattableOperator outer) {
26
+		return FormattableOperator.shouldWrapRight(priority, outer) ? "(" + value + ")" : value;
27
+	}
28
+	
29
+	@Override
30
+	public String toString() {
31
+		return value;
32
+	}
33
+	
34
+	public static ExpressionString binary(ExpressionString left, ExpressionString right, FormattableOperator operator) {
35
+		String value = left.wrapLeft(operator)
36
+				+ operator.getOperatorString() + right.wrapRight(operator);
37
+		return new ExpressionString(value, operator);
38
+	}
39
+	
40
+	public ExpressionString unaryPrefix(FormattableOperator operator) {
41
+		return new ExpressionString(operator.getOperatorString() + value, operator);
42
+	}
43
+	
44
+	public ExpressionString unaryPostfix(FormattableOperator operator) {
45
+		return new ExpressionString(value + operator.getOperatorString(), operator);
46
+	}
47
+	
48
+	public ExpressionString unaryPrefix(FormattableOperator operator, String operatorString) {
49
+		return new ExpressionString(operatorString + value, operator);
50
+	}
51
+	
52
+	public ExpressionString unaryPostfix(FormattableOperator operator, String operatorString) {
53
+		return new ExpressionString(value + operatorString, operator);
54
+	}
55
+}

+ 26
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/FormattableOperator.java Näytä tiedosto

@@ -0,0 +1,26 @@
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.formattershared;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface FormattableOperator {
13
+	int getPriority();
14
+	
15
+	boolean isCommutative();
16
+	
17
+	String getOperatorString();
18
+	
19
+	public static boolean shouldWrapLeft(FormattableOperator inner, FormattableOperator outer) {
20
+		return inner != outer && inner.getPriority() <= outer.getPriority();
21
+	}
22
+	
23
+	public static boolean shouldWrapRight(FormattableOperator inner, FormattableOperator outer) {
24
+		return (inner == outer && inner.isCommutative()) || (inner.getPriority() <= outer.getPriority());
25
+	}
26
+}

+ 56
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/FormattingSettings.java Näytä tiedosto

@@ -0,0 +1,56 @@
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.formattershared;
7
+
8
+import org.openzen.zenscript.shared.StringUtils;
9
+
10
+/**
11
+ *
12
+ * @author Hoofdgebruiker
13
+ */
14
+public class FormattingSettings {
15
+	public final boolean useTabs; // use tabs instead of spaces
16
+	public final int spacesPerTab; // number of spaces per tab
17
+	public final String indent;
18
+	public final CommentFormatter commentFormatter;
19
+	
20
+	protected FormattingSettings(Builder builder) {
21
+		useTabs = builder.useTabs;
22
+		spacesPerTab = builder.spacesPerTab;
23
+		commentFormatter = builder.commentFormatter;
24
+		
25
+		if (useTabs) {
26
+			indent = "\t";
27
+		} else {
28
+			indent = StringUtils.times(' ', spacesPerTab);
29
+		}
30
+	}
31
+	
32
+	public static class Builder<T extends Builder<T>> {
33
+		private final CommentFormatter commentFormatter;
34
+		private boolean useTabs = false;
35
+		private int spacesPerTab = 4;
36
+		protected T instance;
37
+		
38
+		public Builder(CommentFormatter commentFormatter) {
39
+			this.commentFormatter = commentFormatter;
40
+		}
41
+		
42
+		public T useTabs(boolean tabs) {
43
+			useTabs = tabs;
44
+			return instance;
45
+		}
46
+		
47
+		public T spacesPerTabs(int spaces) {
48
+			spacesPerTab = spaces;
49
+			return instance;
50
+		}
51
+		
52
+		public FormattingSettings build() {
53
+			return new FormattingSettings(this);
54
+		}
55
+	}
56
+}

CodeFormatter/src/main/java/org/openzen/zenscript/formatter/Importer.java → CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/Importer.java Näytä tiedosto

@@ -3,7 +3,7 @@
3 3
  * To change this template file, choose Tools | Templates
4 4
  * and open the template in the editor.
5 5
  */
6
-package org.openzen.zenscript.formatter;
6
+package org.openzen.zenscript.formattershared;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9 9
 

+ 311
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormatter.java Näytä tiedosto

@@ -0,0 +1,311 @@
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.formattershared;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.WhitespaceInfo;
10
+import org.openzen.zenscript.codemodel.statement.BlockStatement;
11
+import org.openzen.zenscript.codemodel.statement.BreakStatement;
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.LoopStatement;
20
+import org.openzen.zenscript.codemodel.statement.ReturnStatement;
21
+import org.openzen.zenscript.codemodel.statement.Statement;
22
+import org.openzen.zenscript.codemodel.statement.StatementVisitor;
23
+import org.openzen.zenscript.codemodel.statement.SwitchStatement;
24
+import org.openzen.zenscript.codemodel.statement.ThrowStatement;
25
+import org.openzen.zenscript.codemodel.statement.TryCatchStatement;
26
+import org.openzen.zenscript.codemodel.statement.VarStatement;
27
+import org.openzen.zenscript.codemodel.statement.WhileStatement;
28
+
29
+/**
30
+ *
31
+ * @author Hoofdgebruiker
32
+ */
33
+public class StatementFormatter implements StatementVisitor<Void>, StatementFormattingTarget {
34
+	private final StringBuilder output;
35
+	private final Formatter formatter;
36
+	private final String indent;
37
+	private final FormattingSettings settings;
38
+	private final LoopStatement innerLoop;
39
+	
40
+	public StatementFormatter(StringBuilder output, FormattingSettings settings, Formatter formatter, String indent, LoopStatement innerLoop) {
41
+		this.output = output;
42
+		this.formatter = formatter;
43
+		this.indent = indent;
44
+		this.settings = settings;
45
+		this.innerLoop = innerLoop;
46
+	}
47
+
48
+	@Override
49
+	public Void visitBlock(BlockStatement statement) {
50
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
51
+		beginBlock(whitespace);
52
+		formatter.formatBlock(this, statement);
53
+		endBlock(whitespace);
54
+		return null;
55
+	}
56
+
57
+	@Override
58
+	public Void visitBreak(BreakStatement statement) {
59
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
60
+		beginSingleLine(whitespace);
61
+		formatter.formatBreak(this, statement);
62
+		endSingleLine(whitespace);
63
+		return null;
64
+	}
65
+
66
+	@Override
67
+	public Void visitContinue(ContinueStatement statement) {
68
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
69
+		beginSingleLine(whitespace);
70
+		formatter.formatContinue(this, statement);
71
+		endSingleLine(whitespace);
72
+		return null;
73
+	}
74
+
75
+	@Override
76
+	public Void visitDoWhile(DoWhileStatement statement) {
77
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
78
+		beginSingleLine(whitespace);
79
+		formatter.formatDoWhile(this, statement);
80
+		endSingleLine(whitespace);
81
+		return null;
82
+	}
83
+
84
+	@Override
85
+	public Void visitEmpty(EmptyStatement statement) {
86
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
87
+		beginSingleLine(whitespace);
88
+		formatter.formatEmpty(this, statement);
89
+		endSingleLine(whitespace);
90
+		return null;
91
+	}
92
+
93
+	@Override
94
+	public Void visitExpression(ExpressionStatement statement) {
95
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
96
+		beginSingleLine(whitespace);
97
+		formatter.formatExpression(this, statement);
98
+		endSingleLine(whitespace);
99
+		return null;
100
+	}
101
+
102
+	@Override
103
+	public Void visitForeach(ForeachStatement statement) {
104
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
105
+		beginSingleLine(whitespace);
106
+		formatter.formatForeach(this, statement);
107
+		endSingleLine(whitespace);
108
+		return null;
109
+	}
110
+
111
+	@Override
112
+	public Void visitIf(IfStatement statement) {
113
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
114
+		beginSingleLine(whitespace);
115
+		formatter.formatIf(this, statement);
116
+		endSingleLine(whitespace);
117
+		return null;
118
+	}
119
+
120
+	@Override
121
+	public Void visitLock(LockStatement statement) {
122
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
123
+		beginSingleLine(whitespace);
124
+		formatter.formatLock(this, statement);
125
+		endSingleLine(whitespace);
126
+		return null;
127
+	}
128
+
129
+	@Override
130
+	public Void visitReturn(ReturnStatement statement) {
131
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
132
+		beginSingleLine(whitespace);
133
+		formatter.formatReturn(this, statement);
134
+		endSingleLine(whitespace);
135
+		return null;
136
+	}
137
+
138
+	@Override
139
+	public Void visitSwitch(SwitchStatement statement) {
140
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
141
+		beginSingleLine(whitespace);
142
+		formatter.formatSwitch(this, statement);
143
+		endSingleLine(whitespace);
144
+		return null;
145
+	}
146
+
147
+	@Override
148
+	public Void visitThrow(ThrowStatement statement) {
149
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
150
+		beginSingleLine(whitespace);
151
+		formatter.formatThrow(this, statement);
152
+		endSingleLine(whitespace);
153
+		return null;
154
+	}
155
+
156
+	@Override
157
+	public Void visitTryCatch(TryCatchStatement statement) {
158
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
159
+		beginSingleLine(whitespace);
160
+		formatter.formatTryCatch(this, statement);
161
+		endSingleLine(whitespace);
162
+		return null;
163
+	}
164
+
165
+	@Override
166
+	public Void visitVar(VarStatement statement) {
167
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
168
+		beginSingleLine(whitespace);
169
+		formatter.formatVar(this, statement);
170
+		endSingleLine(whitespace);
171
+		return null;
172
+	}
173
+
174
+	@Override
175
+	public Void visitWhile(WhileStatement statement) {
176
+		WhitespaceInfo whitespace = statement.getTag(WhitespaceInfo.class);
177
+		beginSingleLine(whitespace);
178
+		formatter.formatWhile(this, statement);
179
+		endSingleLine(whitespace);
180
+		return null;
181
+	}
182
+	
183
+	// == StatementFormattingTarget implementation ==
184
+	
185
+	@Override
186
+	public LoopStatement getInnerLoop() {
187
+		return innerLoop;
188
+	}
189
+
190
+	@Override
191
+	public String getIndent() {
192
+		return indent;
193
+	}
194
+	
195
+	@Override
196
+	public void writeLine(String line) {
197
+		output.append('\n').append(indent).append(line);
198
+	}
199
+
200
+	@Override
201
+	public void writeInner(String lineBefore, Statement contents, LoopStatement loop, String lineAfter) {
202
+		output.append('\n').append(indent).append(lineBefore);
203
+		contents.accept(new StatementFormatter(output, settings, formatter, indent + settings.indent, loop == null ? innerLoop : loop));
204
+		
205
+		if (!lineAfter.isEmpty())
206
+			output.append('\n').append(indent).append(lineAfter);
207
+	}
208
+
209
+	@Override
210
+	public void writeInnerMulti(String lineBefore, List<StatementFormattingSubBlock> contents, LoopStatement loop, String lineAfter) {
211
+		output.append('\n').append(indent).append(lineBefore);
212
+		
213
+		String newIndent = indent + settings.indent + settings.indent;
214
+		StatementFormatter inner = new StatementFormatter(output, settings, formatter, newIndent, innerLoop);
215
+		
216
+		for (StatementFormattingSubBlock subBlock : contents) {
217
+			output.append('\n').append(indent).append(settings.indent).append(subBlock.header);
218
+			for (String literal : subBlock.literalStatements)
219
+				output.append('\n').append(newIndent).append(literal);
220
+			for (Statement statement : subBlock.statements)
221
+				statement.accept(inner);
222
+		}
223
+		
224
+		if (!lineAfter.isEmpty())
225
+			output.append('\n').append(indent).append(lineAfter);
226
+	}
227
+
228
+	@Override
229
+	public void writeBlock(String lineBefore, BlockStatement contents, String lineAfter) {
230
+		output.append(' ').append(lineBefore);
231
+		
232
+		StatementFormatter inner = new StatementFormatter(output, settings, formatter, indent, innerLoop);
233
+		for (Statement statement : contents.statements)
234
+			statement.accept(inner);
235
+		
236
+		if (!lineAfter.isEmpty())
237
+			output.append('\n').append(indent.substring(0, indent.length() - settings.indent.length())).append(lineAfter);
238
+	}
239
+	
240
+	private void beginBlock(WhitespaceInfo whitespace) {
241
+		if (whitespace != null && whitespace.emptyLine) {
242
+			output.append("\n").append(indent);
243
+		}
244
+		
245
+		if (whitespace != null)
246
+			writeComments(whitespace.commentsBefore);
247
+	}
248
+	
249
+	private void endBlock(WhitespaceInfo whitespace) {
250
+		if (whitespace != null && !whitespace.commentsAfter.isEmpty())
251
+			output.append(' ').append(whitespace.commentsAfter);
252
+	}
253
+	
254
+	private void beginSingleLine(WhitespaceInfo whitespace) {
255
+		if (whitespace != null) {
256
+			if (whitespace.emptyLine) {
257
+				output.append("\n").append(indent);
258
+			}
259
+			writeComments(whitespace.commentsBefore);
260
+		}
261
+	}
262
+	
263
+	private void endSingleLine(WhitespaceInfo whitespace) {
264
+		if (whitespace != null && !whitespace.commentsAfter.isEmpty())
265
+			output.append(' ').append(whitespace.commentsAfter);
266
+	}
267
+	
268
+	private void writeComments(String[] comments) {
269
+		for (String comment : settings.commentFormatter.format(comments)) {
270
+			output.append(comment).append("\n").append(indent);
271
+		}
272
+	}
273
+	
274
+	private void writePostComments(String[] comments) {
275
+		for (String comment : settings.commentFormatter.format(comments)) {
276
+			output.append("\n").append(indent).append(comment);
277
+		}
278
+	}
279
+	
280
+	public interface Formatter {
281
+		public void formatBlock(StatementFormattingTarget target, BlockStatement statement);
282
+	
283
+		public void formatBreak(StatementFormattingTarget target, BreakStatement statement);
284
+
285
+		public void formatContinue(StatementFormattingTarget target, ContinueStatement statement);
286
+
287
+		public void formatDoWhile(StatementFormattingTarget target, DoWhileStatement statement);
288
+
289
+		public void formatEmpty(StatementFormattingTarget target, EmptyStatement statement);
290
+
291
+		public void formatExpression(StatementFormattingTarget target, ExpressionStatement statement);
292
+
293
+		public void formatForeach(StatementFormattingTarget target, ForeachStatement statement);
294
+
295
+		public void formatIf(StatementFormattingTarget target, IfStatement statement);
296
+
297
+		public void formatLock(StatementFormattingTarget target, LockStatement statement);
298
+
299
+		public void formatReturn(StatementFormattingTarget target, ReturnStatement statement);
300
+
301
+		public void formatSwitch(StatementFormattingTarget target, SwitchStatement statement);
302
+
303
+		public void formatThrow(StatementFormattingTarget target, ThrowStatement statement);
304
+
305
+		public void formatTryCatch(StatementFormattingTarget target, TryCatchStatement statement);
306
+
307
+		public void formatVar(StatementFormattingTarget target, VarStatement statement);
308
+
309
+		public void formatWhile(StatementFormattingTarget target, WhileStatement statement);
310
+	}
311
+}

+ 25
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingSubBlock.java Näytä tiedosto

@@ -0,0 +1,25 @@
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.formattershared;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.statement.Statement;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public class StatementFormattingSubBlock {
16
+	public final String header;
17
+	public final List<String> literalStatements;
18
+	public final List<Statement> statements;
19
+	
20
+	public StatementFormattingSubBlock(String header, List<String> literalStatements, List<Statement> statements) {
21
+		this.header = header;
22
+		this.literalStatements = literalStatements;
23
+		this.statements = statements;
24
+	}
25
+}

+ 29
- 0
CodeFormatterShared/src/main/java/org/openzen/zenscript/formattershared/StatementFormattingTarget.java Näytä tiedosto

@@ -0,0 +1,29 @@
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.formattershared;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.statement.BlockStatement;
10
+import org.openzen.zenscript.codemodel.statement.LoopStatement;
11
+import org.openzen.zenscript.codemodel.statement.Statement;
12
+
13
+/**
14
+ *
15
+ * @author Hoofdgebruiker
16
+ */
17
+public interface StatementFormattingTarget {
18
+	LoopStatement getInnerLoop();
19
+	
20
+	String getIndent();
21
+	
22
+	void writeLine(String line);
23
+	
24
+	void writeInner(String lineBefore, Statement contents, LoopStatement loop, String lineAfter);
25
+	
26
+	void writeInnerMulti(String lineBefore, List<StatementFormattingSubBlock> contents, LoopStatement loop, String lineAfter);
27
+	
28
+	void writeBlock(String lineBefore, BlockStatement contents, String lineAfter);
29
+}

+ 1
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/CompareType.java Näytä tiedosto

@@ -11,9 +11,7 @@ public enum CompareType {
11 11
 	EQ("=="),
12 12
 	NE("!="),
13 13
 	LE("<="),
14
-	GE(">="),
15
-	SAME("==="),
16
-	NOTSAME("!==");
14
+	GE(">=");
17 15
 	
18 16
 	public final String str;
19 17
 	

+ 8
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/FunctionParameter.java Näytä tiedosto

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel;
7 7
 
8
+import org.openzen.zenscript.codemodel.annotations.Annotation;
8 9
 import java.util.Map;
9 10
 import java.util.Objects;
10 11
 import org.openzen.zenscript.codemodel.expression.Expression;
@@ -20,12 +21,14 @@ import org.openzen.zenscript.shared.Taggable;
20 21
 public class FunctionParameter extends Taggable {
21 22
 	public static final FunctionParameter[] NONE = new FunctionParameter[0];
22 23
 	
24
+	public Annotation[] annotations;
23 25
 	public final ITypeID type;
24 26
 	public final String name;
25 27
 	public final Expression defaultValue;
26 28
 	public final boolean variadic;
27 29
 	
28 30
 	public FunctionParameter(ITypeID type) {
31
+		this.annotations = Annotation.NONE;
29 32
 		this.type = type;
30 33
 		this.name = "";
31 34
 		this.defaultValue = null;
@@ -33,6 +36,7 @@ public class FunctionParameter extends Taggable {
33 36
 	}
34 37
 	
35 38
 	public FunctionParameter(ITypeID type, String name) {
39
+		this.annotations = Annotation.NONE;
36 40
 		this.type = type;
37 41
 		this.name = name;
38 42
 		this.defaultValue = null;
@@ -40,6 +44,7 @@ public class FunctionParameter extends Taggable {
40 44
 	}
41 45
 	
42 46
 	public FunctionParameter(ITypeID type, String name, Expression defaultValue, boolean variadic) {
47
+		this.annotations = Annotation.NONE;
43 48
 		this.type = type;
44 49
 		this.name = name;
45 50
 		this.defaultValue = defaultValue;
@@ -47,7 +52,9 @@ public class FunctionParameter extends Taggable {
47 52
 	}
48 53
 	
49 54
 	public FunctionParameter withGenericArguments(GlobalTypeRegistry registry, Map<TypeParameter, ITypeID> arguments) {
50
-		return new FunctionParameter(type.withGenericArguments(registry, arguments), name, defaultValue, variadic);
55
+		FunctionParameter result = new FunctionParameter(type.withGenericArguments(registry, arguments), name, defaultValue, variadic);
56
+		result.annotations = annotations;
57
+		return result;
51 58
 	}
52 59
 	
53 60
 	@Override

+ 3
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/HighLevelDefinition.java Näytä tiedosto

@@ -7,11 +7,11 @@ package org.openzen.zenscript.codemodel;
7 7
 
8 8
 import java.util.ArrayList;
9 9
 import java.util.List;
10
+import org.openzen.zenscript.codemodel.annotations.DefinitionAnnotation;
10 11
 import org.openzen.zenscript.codemodel.definition.DefinitionVisitor;
11 12
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
12 13
 import org.openzen.zenscript.codemodel.generic.TypeParameter;
13 14
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
14
-import org.openzen.zenscript.codemodel.member.EnumConstantMember;
15 15
 import org.openzen.zenscript.codemodel.member.FieldMember;
16 16
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
17 17
 import org.openzen.zenscript.codemodel.type.ITypeID;
@@ -28,7 +28,9 @@ public abstract class HighLevelDefinition extends Taggable {
28 28
 	public final String name;
29 29
 	public final int modifiers;
30 30
 	public final List<IDefinitionMember> members = new ArrayList<>();
31
+	public final AccessScope access = new AccessScope();
31 32
 	public TypeParameter[] genericParameters = null;
33
+	public DefinitionAnnotation[] annotations = DefinitionAnnotation.NONE;
32 34
 	
33 35
 	public HighLevelDefinition outerDefinition;
34 36
 	public ITypeID superType;

+ 16
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/ModuleProcessor.java Näytä tiedosto

@@ -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.codemodel;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface ModuleProcessor {
13
+	public ScriptBlock process(ScriptBlock block);
14
+	
15
+	public void process(HighLevelDefinition definition);
16
+}

+ 4
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/OperatorType.java Näytä tiedosto

@@ -16,6 +16,7 @@ public enum OperatorType {
16 16
 	AND("&", "and"),
17 17
 	XOR("^", "xor"),
18 18
 	NEG("-", "negate"),
19
+	INVERT("~", "invert"),
19 20
 	NOT("!", "not"),
20 21
 	INDEXSET("[]=", "setElement"),
21 22
 	INDEXGET("[]", "getElement"),
@@ -24,6 +25,9 @@ public enum OperatorType {
24 25
 	MEMBERGETTER(".", "getMember"),
25 26
 	MEMBERSETTER(".=", "setMember"),
26 27
 	EQUALS("==", "equals"),
28
+	NOTEQUALS("!=", "notEquals"),
29
+	SAME("===", "same"),
30
+	NOTSAME("!==", "notSame"),
27 31
 	SHL("<<", "shl"),
28 32
 	SHR(">>", "shr"),
29 33
 	USHR(">>>", "ushr"),

+ 9
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/ScriptBlock.java Näytä tiedosto

@@ -14,9 +14,17 @@ import org.openzen.zenscript.shared.Taggable;
14 14
  * @author Hoofdgebruiker
15 15
  */
16 16
 public class ScriptBlock extends Taggable {
17
+	public final AccessScope access;
17 18
 	public final List<Statement> statements;
18 19
 	
19
-	public ScriptBlock(List<Statement> statements) {
20
+	public ScriptBlock(AccessScope access, List<Statement> statements) {
21
+		this.access = access;
20 22
 		this.statements = statements;
21 23
 	}
24
+	
25
+	public ScriptBlock withStatements(List<Statement> newStatements) {
26
+		ScriptBlock result = new ScriptBlock(access, newStatements);
27
+		result.addAllTagsFrom(this);
28
+		return result;
29
+	}
22 30
 }

+ 16
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/Annotation.java Näytä tiedosto

@@ -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.codemodel.annotations;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public interface Annotation {
13
+	public static final Annotation[] NONE = new Annotation[0];
14
+	
15
+	public void apply();
16
+}

+ 44
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/AnnotationDefinition.java Näytä tiedosto

@@ -0,0 +1,44 @@
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.annotations;
7
+
8
+import java.util.List;
9
+import org.openzen.zenscript.codemodel.FunctionHeader;
10
+import org.openzen.zenscript.codemodel.FunctionParameter;
11
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
12
+import org.openzen.zenscript.codemodel.expression.CallArguments;
13
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
14
+import org.openzen.zenscript.codemodel.scope.BaseScope;
15
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
16
+import org.openzen.zenscript.codemodel.scope.StatementScope;
17
+import org.openzen.zenscript.codemodel.statement.Statement;
18
+import org.openzen.zenscript.shared.CodePosition;
19
+
20
+/**
21
+ *
22
+ * @author Hoofdgebruiker
23
+ */
24
+public interface AnnotationDefinition {
25
+	public String getAnnotationName();
26
+	
27
+	public List<FunctionHeader> getInitializers(BaseScope scope);
28
+	
29
+	public ExpressionScope getScopeForMember(IDefinitionMember member, BaseScope scope);
30
+	
31
+	public ExpressionScope getScopeForType(HighLevelDefinition definition, BaseScope scope);
32
+	
33
+	public ExpressionScope getScopeForStatement(Statement statement, StatementScope scope);
34
+	
35
+	public ExpressionScope getScopeForParameter(FunctionHeader header, FunctionParameter parameter, BaseScope scope);
36
+	
37
+	public MemberAnnotation createForMember(CodePosition position, CallArguments arguments);
38
+	
39
+	public DefinitionAnnotation createForDefinition(CodePosition position, CallArguments arguments);
40
+	
41
+	public StatementAnnotation createForStatement(CodePosition position, CallArguments arguments);
42
+	
43
+	public Annotation createForParameter(CodePosition position, CallArguments arguments);
44
+}

+ 219
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/AnnotationProcessor.java Näytä tiedosto

@@ -0,0 +1,219 @@
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.annotations;
7
+
8
+import java.util.ArrayList;
9
+import java.util.HashMap;
10
+import java.util.List;
11
+import org.openzen.zenscript.codemodel.FunctionHeader;
12
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.ModuleProcessor;
14
+import org.openzen.zenscript.codemodel.PackageDefinitions;
15
+import org.openzen.zenscript.codemodel.ScriptBlock;
16
+import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
17
+import org.openzen.zenscript.codemodel.definition.ZSPackage;
18
+import org.openzen.zenscript.codemodel.member.CallerMember;
19
+import org.openzen.zenscript.codemodel.member.CasterMember;
20
+import org.openzen.zenscript.codemodel.member.ConstMember;
21
+import org.openzen.zenscript.codemodel.member.ConstructorMember;
22
+import org.openzen.zenscript.codemodel.member.CustomIteratorMember;
23
+import org.openzen.zenscript.codemodel.member.DestructorMember;
24
+import org.openzen.zenscript.codemodel.member.FieldMember;
25
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
26
+import org.openzen.zenscript.codemodel.member.GetterMember;
27
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
28
+import org.openzen.zenscript.codemodel.member.ImplementationMember;
29
+import org.openzen.zenscript.codemodel.member.InnerDefinitionMember;
30
+import org.openzen.zenscript.codemodel.member.MemberVisitor;
31
+import org.openzen.zenscript.codemodel.member.MethodMember;
32
+import org.openzen.zenscript.codemodel.member.OperatorMember;
33
+import org.openzen.zenscript.codemodel.member.SetterMember;
34
+import org.openzen.zenscript.codemodel.member.StaticInitializerMember;
35
+import org.openzen.zenscript.codemodel.scope.DefinitionScope;
36
+import org.openzen.zenscript.codemodel.scope.FileScope;
37
+import org.openzen.zenscript.codemodel.scope.FunctionScope;
38
+import org.openzen.zenscript.codemodel.scope.GlobalScriptScope;
39
+import org.openzen.zenscript.codemodel.scope.StatementScope;
40
+import org.openzen.zenscript.codemodel.statement.Statement;
41
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
42
+import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
43
+
44
+/**
45
+ *
46
+ * @author Hoofdgebruiker
47
+ */
48
+public class AnnotationProcessor implements ModuleProcessor {
49
+	private final ZSPackage rootPackage;
50
+	private final PackageDefinitions packageDefinitions;
51
+	private final GlobalTypeRegistry globalRegistry;
52
+	private final List<ExpansionDefinition> expansions;
53
+	private final List<AnnotationDefinition> annotations;
54
+	
55
+	public AnnotationProcessor(
56
+			ZSPackage rootPackage,
57
+			PackageDefinitions packageDefinitions,
58
+			GlobalTypeRegistry globalRegistry,
59
+			List<ExpansionDefinition> expansions,
60
+			List<AnnotationDefinition> annotations) {
61
+		this.rootPackage = rootPackage;
62
+		this.packageDefinitions = packageDefinitions;
63
+		this.globalRegistry = globalRegistry;
64
+		this.expansions = expansions;
65
+		this.annotations = annotations;
66
+	}
67
+	
68
+	@Override
69
+	public ScriptBlock process(ScriptBlock block) {
70
+		FileScope fileScope = new FileScope(block.access, rootPackage, packageDefinitions, globalRegistry, expansions, new HashMap<>(), annotations);
71
+		StatementScope scope = new GlobalScriptScope(fileScope);
72
+		List<Statement> transformed = new ArrayList<>();
73
+		boolean unchanged = true;
74
+		for (Statement statement : block.statements) {
75
+			Statement newStatement = process(statement, scope);
76
+			transformed.add(newStatement);
77
+			unchanged &= statement == newStatement;
78
+		}
79
+		return unchanged ? block : block.withStatements(transformed);
80
+	}
81
+	
82
+	@Override
83
+	public void process(HighLevelDefinition definition) {
84
+		FileScope fileScope = new FileScope(definition.access, rootPackage, packageDefinitions, globalRegistry, expansions, new HashMap<>(), annotations);
85
+		DefinitionScope scope = new DefinitionScope(fileScope, definition);
86
+		for (DefinitionAnnotation annotation : definition.annotations) {
87
+			annotation.apply(definition, scope);
88
+		}
89
+		
90
+		MemberAnnotationVisitor visitor = new MemberAnnotationVisitor(scope);
91
+		for (IDefinitionMember member : definition.members) {
92
+			member.accept(visitor);
93
+		}
94
+	}
95
+	
96
+	private Statement process(Statement statement, StatementScope scope) {
97
+		return statement.transform(s -> {
98
+			for (StatementAnnotation annotation : s.annotations) {
99
+				s = annotation.apply(s, scope);
100
+			}
101
+			return s;
102
+		});
103
+	}
104
+	
105
+	private class MemberAnnotationVisitor implements MemberVisitor<Void> {
106
+		private final DefinitionScope scope;
107
+		
108
+		public MemberAnnotationVisitor(DefinitionScope scope) {
109
+			this.scope = scope;
110
+		}
111
+		
112
+		@Override
113
+		public Void visitConst(ConstMember member) {
114
+			for (MemberAnnotation annotation : member.annotations)
115
+				annotation.apply(member, scope);
116
+			
117
+			return null;
118
+		}
119
+
120
+		@Override
121
+		public Void visitField(FieldMember member) {
122
+			for (MemberAnnotation annotation : member.annotations)
123
+				annotation.apply(member, scope);
124
+			
125
+			return null;
126
+		}
127
+
128
+		@Override
129
+		public Void visitConstructor(ConstructorMember member) {
130
+			return functional(member);
131
+		}
132
+
133
+		@Override
134
+		public Void visitDestructor(DestructorMember member) {
135
+			return functional(member);
136
+		}
137
+
138
+		@Override
139
+		public Void visitMethod(MethodMember member) {
140
+			return functional(member);
141
+		}
142
+
143
+		@Override
144
+		public Void visitGetter(GetterMember member) {
145
+			return functional(member);
146
+		}
147
+
148
+		@Override
149
+		public Void visitSetter(SetterMember member) {
150
+			return functional(member);
151
+		}
152
+
153
+		@Override
154
+		public Void visitOperator(OperatorMember member) {
155
+			return functional(member);
156
+		}
157
+
158
+		@Override
159
+		public Void visitCaster(CasterMember member) {
160
+			return functional(member);
161
+		}
162
+
163
+		@Override
164
+		public Void visitCustomIterator(CustomIteratorMember member) {
165
+			throw new UnsupportedOperationException("Not supported yet!");
166
+		}
167
+
168
+		@Override
169
+		public Void visitCaller(CallerMember member) {
170
+			return functional(member);
171
+		}
172
+
173
+		@Override
174
+		public Void visitImplementation(ImplementationMember implementation) {
175
+			for (IDefinitionMember member : implementation.members) {
176
+				member.accept(this);
177
+			}
178
+			return null;
179
+		}
180
+
181
+		@Override
182
+		public Void visitInnerDefinition(InnerDefinitionMember member) {
183
+			process(member.innerDefinition);
184
+			return null;
185
+		}
186
+
187
+		@Override
188
+		public Void visitStaticInitializer(StaticInitializerMember member) {
189
+			StatementScope scope = new FunctionScope(this.scope, new FunctionHeader(BasicTypeID.VOID));
190
+			member.body = process(member.body, scope);
191
+			return null;
192
+		}
193
+		
194
+		private Void functional(FunctionalMember member) {
195
+			for (MemberAnnotation annotation : member.annotations)
196
+				annotation.apply(member, scope);
197
+			
198
+			if (member.overrides != null) {
199
+				functional(member, member.overrides);
200
+			}
201
+			
202
+			if (member.body == null)
203
+				return null;
204
+			
205
+			StatementScope scope = new FunctionScope(this.scope, member.header);
206
+			member.body = process(member.body, scope);
207
+			return null;
208
+		}
209
+		
210
+		private void functional(FunctionalMember member, FunctionalMember overrides) {
211
+			for (MemberAnnotation annotation : overrides.annotations)
212
+				annotation.applyOnOverridingMethod(member, scope);
213
+			
214
+			if (overrides.overrides != null) {
215
+				functional(member, overrides.overrides);
216
+			}
217
+		}
218
+	}
219
+}

+ 21
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/DefinitionAnnotation.java Näytä tiedosto

@@ -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.codemodel.annotations;
7
+
8
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.codemodel.scope.BaseScope;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public interface DefinitionAnnotation {
16
+	public static final DefinitionAnnotation[] NONE = new DefinitionAnnotation[0];
17
+	
18
+	public void apply(HighLevelDefinition definition, BaseScope scope);
19
+	
20
+	public void applyOnSubtype(HighLevelDefinition definition, BaseScope scope);
21
+}

+ 22
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/MemberAnnotation.java Näytä tiedosto

@@ -0,0 +1,22 @@
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.annotations;
7
+
8
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
9
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
10
+import org.openzen.zenscript.codemodel.scope.BaseScope;
11
+
12
+/**
13
+ *
14
+ * @author Hoofdgebruiker
15
+ */
16
+public interface MemberAnnotation {
17
+	public static final MemberAnnotation[] NONE = new MemberAnnotation[0];
18
+	
19
+	public void apply(IDefinitionMember member, BaseScope scope);
20
+	
21
+	public void applyOnOverridingMethod(FunctionalMember member, BaseScope scope);
22
+}

+ 88
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeAnnotationDefinition.java Näytä tiedosto

@@ -0,0 +1,88 @@
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.annotations;
7
+
8
+import java.util.Collections;
9
+import java.util.List;
10
+import org.openzen.zenscript.codemodel.FunctionHeader;
11
+import org.openzen.zenscript.codemodel.FunctionParameter;
12
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
13
+import org.openzen.zenscript.codemodel.expression.CallArguments;
14
+import org.openzen.zenscript.codemodel.expression.Expression;
15
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
16
+import org.openzen.zenscript.codemodel.scope.BaseScope;
17
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
18
+import org.openzen.zenscript.codemodel.scope.StatementScope;
19
+import org.openzen.zenscript.codemodel.statement.Statement;
20
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
21
+import org.openzen.zenscript.shared.CodePosition;
22
+
23
+/**
24
+ *
25
+ * @author Hoofdgebruiker
26
+ */
27
+public class NativeAnnotationDefinition implements AnnotationDefinition {
28
+	public static final NativeAnnotationDefinition INSTANCE = new NativeAnnotationDefinition();
29
+	
30
+	private static final List<FunctionHeader> INITIALIZERS = Collections.singletonList(
31
+			new FunctionHeader(BasicTypeID.VOID, BasicTypeID.STRING));
32
+	
33
+	private NativeAnnotationDefinition() {}
34
+
35
+	@Override
36
+	public String getAnnotationName() {
37
+		return "Native";
38
+	}
39
+
40
+	@Override
41
+	public List<FunctionHeader> getInitializers(BaseScope scope) {
42
+		return INITIALIZERS;
43
+	}
44
+
45
+	@Override
46
+	public ExpressionScope getScopeForMember(IDefinitionMember member, BaseScope scope) {
47
+		return new ExpressionScope(scope);
48
+	}
49
+
50
+	@Override
51
+	public ExpressionScope getScopeForType(HighLevelDefinition definition, BaseScope scope) {
52
+		return new ExpressionScope(scope);
53
+	}
54
+
55
+	@Override
56
+	public ExpressionScope getScopeForStatement(Statement statement, StatementScope scope) {
57
+		return new ExpressionScope(scope);
58
+	}
59
+
60
+	@Override
61
+	public ExpressionScope getScopeForParameter(FunctionHeader header, FunctionParameter parameter, BaseScope scope) {
62
+		return new ExpressionScope(scope);
63
+	}
64
+
65
+	@Override
66
+	public MemberAnnotation createForMember(CodePosition position, CallArguments arguments) {
67
+		Expression value = arguments.arguments[0];
68
+		String constant = value.evaluateStringConstant();
69
+		return new NativeMemberAnnotation(constant);
70
+	}
71
+
72
+	@Override
73
+	public DefinitionAnnotation createForDefinition(CodePosition position, CallArguments arguments) {
74
+		Expression value = arguments.arguments[0];
75
+		String constant = value.evaluateStringConstant();
76
+		return new NativeDefinitionAnnotation(constant);
77
+	}
78
+
79
+	@Override
80
+	public StatementAnnotation createForStatement(CodePosition position, CallArguments arguments) {
81
+		throw new UnsupportedOperationException("Not supported");
82
+	}
83
+
84
+	@Override
85
+	public Annotation createForParameter(CodePosition position, CallArguments arguments) {
86
+		throw new UnsupportedOperationException("Not supported");
87
+	}
88
+}

+ 31
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeDefinitionAnnotation.java Näytä tiedosto

@@ -0,0 +1,31 @@
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.annotations;
7
+
8
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
9
+import org.openzen.zenscript.codemodel.scope.BaseScope;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public class NativeDefinitionAnnotation implements DefinitionAnnotation {
16
+	private final String identifier;
17
+	
18
+	public NativeDefinitionAnnotation(String identifier) {
19
+		this.identifier = identifier;
20
+	}
21
+	
22
+	@Override
23
+	public void apply(HighLevelDefinition definition, BaseScope scope) {
24
+		definition.setTag(NativeTag.class, new NativeTag(identifier));
25
+	}
26
+
27
+	@Override
28
+	public void applyOnSubtype(HighLevelDefinition definition, BaseScope scope) {
29
+		// this annotation is not inherited
30
+	}
31
+}

+ 32
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeMemberAnnotation.java Näytä tiedosto

@@ -0,0 +1,32 @@
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.annotations;
7
+
8
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
9
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
10
+import org.openzen.zenscript.codemodel.scope.BaseScope;
11
+
12
+/**
13
+ *
14
+ * @author Hoofdgebruiker
15
+ */
16
+public class NativeMemberAnnotation implements MemberAnnotation {
17
+	private final String identifier;
18
+	
19
+	public NativeMemberAnnotation(String identifier) {
20
+		this.identifier = identifier;
21
+	}
22
+	
23
+	@Override
24
+	public void apply(IDefinitionMember member, BaseScope scope) {
25
+		member.setTag(NativeTag.class, new NativeTag(identifier));
26
+	}
27
+
28
+	@Override
29
+	public void applyOnOverridingMethod(FunctionalMember member, BaseScope scope) {
30
+		// not inherited
31
+	}
32
+}

+ 18
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/NativeTag.java Näytä tiedosto

@@ -0,0 +1,18 @@
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.annotations;
7
+
8
+/**
9
+ *
10
+ * @author Hoofdgebruiker
11
+ */
12
+public class NativeTag {
13
+	public final String value;
14
+	
15
+	public NativeTag(String value) {
16
+		this.value = value;
17
+	}
18
+}

+ 107
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionAnnotationDefinition.java Näytä tiedosto

@@ -0,0 +1,107 @@
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.annotations;
7
+
8
+import java.util.Arrays;
9
+import java.util.Collections;
10
+import java.util.List;
11
+import org.openzen.zenscript.codemodel.FunctionHeader;
12
+import org.openzen.zenscript.codemodel.FunctionParameter;
13
+import org.openzen.zenscript.codemodel.HighLevelDefinition;
14
+import org.openzen.zenscript.codemodel.definition.FunctionDefinition;
15
+import org.openzen.zenscript.codemodel.expression.CallArguments;
16
+import org.openzen.zenscript.codemodel.expression.Expression;
17
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
18
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
19
+import org.openzen.zenscript.codemodel.scope.BaseScope;
20
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
21
+import org.openzen.zenscript.codemodel.scope.FunctionScope;
22
+import org.openzen.zenscript.codemodel.scope.StatementScope;
23
+import org.openzen.zenscript.codemodel.statement.Statement;
24
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
25
+import org.openzen.zenscript.codemodel.type.GenericName;
26
+import org.openzen.zenscript.shared.CodePosition;
27
+
28
+/**
29
+ *
30
+ * @author Hoofdgebruiker
31
+ */
32
+public class PreconditionAnnotationDefinition implements AnnotationDefinition {
33
+	public static final PreconditionAnnotationDefinition INSTANCE = new PreconditionAnnotationDefinition();
34
+	
35
+	private final List<GenericName> enforcementLevelName = Arrays.asList(
36
+			new GenericName("stdlib"),
37
+			new GenericName("EnforcementLevel"));
38
+	
39
+	private PreconditionAnnotationDefinition() {}
40
+
41
+	@Override
42
+	public String getAnnotationName() {
43
+		return "Precondition";
44
+	}
45
+
46
+	@Override
47
+	public List<FunctionHeader> getInitializers(BaseScope scope) {
48
+		return Collections.singletonList(new FunctionHeader(
49
+				BasicTypeID.VOID,
50
+				scope.getType(CodePosition.BUILTIN, enforcementLevelName),
51
+				BasicTypeID.BOOL,
52
+				BasicTypeID.STRING));
53
+	}
54
+
55
+	@Override
56
+	public ExpressionScope getScopeForMember(IDefinitionMember member, BaseScope scope) {
57
+		if (member instanceof FunctionalMember) {
58
+			FunctionHeader header = ((FunctionalMember)member).header;
59
+			return new ExpressionScope(new FunctionScope(scope, header));
60
+		} else {
61
+			throw new UnsupportedOperationException("Can only assign preconditions to methods");
62
+		}
63
+	}
64
+
65
+	@Override
66
+	public ExpressionScope getScopeForType(HighLevelDefinition definition, BaseScope scope) {
67
+		if (definition instanceof FunctionDefinition) {
68
+			FunctionHeader header = ((FunctionDefinition)definition).header;
69
+			return new ExpressionScope(new FunctionScope(scope, header));
70
+		} else {
71
+			throw new UnsupportedOperationException("Can only assign preconditions to functions");
72
+		}
73
+	}
74
+
75
+	@Override
76
+	public ExpressionScope getScopeForStatement(Statement statement, StatementScope scope) {
77
+		throw new UnsupportedOperationException("Not supported");
78
+	}
79
+
80
+	@Override
81
+	public ExpressionScope getScopeForParameter(FunctionHeader header, FunctionParameter parameter, BaseScope scope) {
82
+		throw new UnsupportedOperationException("Not supported");
83
+	}
84
+
85
+	@Override
86
+	public MemberAnnotation createForMember(CodePosition position, CallArguments arguments) {
87
+		String enforcement = arguments.arguments[0].evaluateEnumConstant().name;
88
+		Expression condition = arguments.arguments[1];
89
+		Expression message = arguments.arguments[2];
90
+		return new PreconditionForMethod(position, enforcement, condition, message);
91
+	}
92
+
93
+	@Override
94
+	public DefinitionAnnotation createForDefinition(CodePosition position, CallArguments arguments) {
95
+		throw new UnsupportedOperationException("Not supported");
96
+	}
97
+
98
+	@Override
99
+	public StatementAnnotation createForStatement(CodePosition position, CallArguments arguments) {
100
+		throw new UnsupportedOperationException("Not supported");
101
+	}
102
+
103
+	@Override
104
+	public Annotation createForParameter(CodePosition position, CallArguments arguments) {
105
+		throw new UnsupportedOperationException("Not supported");
106
+	}
107
+}

+ 67
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/PreconditionForMethod.java Näytä tiedosto

@@ -0,0 +1,67 @@
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.annotations;
7
+
8
+import java.util.ArrayList;
9
+import java.util.List;
10
+import org.openzen.zenscript.codemodel.expression.Expression;
11
+import org.openzen.zenscript.codemodel.expression.ExpressionBuilder;
12
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
13
+import org.openzen.zenscript.codemodel.member.IDefinitionMember;
14
+import org.openzen.zenscript.codemodel.scope.BaseScope;
15
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
16
+import org.openzen.zenscript.codemodel.statement.BlockStatement;
17
+import org.openzen.zenscript.codemodel.statement.IfStatement;
18
+import org.openzen.zenscript.codemodel.statement.Statement;
19
+import org.openzen.zenscript.codemodel.statement.ThrowStatement;
20
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
21
+import org.openzen.zenscript.shared.CodePosition;
22
+
23
+/**
24
+ *
25
+ * @author Hoofdgebruiker
26
+ */
27
+public class PreconditionForMethod implements MemberAnnotation {
28
+	private final CodePosition position;
29
+	private final String enforcement;
30
+	private final Expression condition;
31
+	private final Expression message;
32
+	
33
+	public PreconditionForMethod(
34
+			CodePosition position,
35
+			String enforcement,
36
+			Expression condition,
37
+			Expression message) {
38
+		this.position = position;
39
+		this.enforcement = enforcement;
40
+		this.condition = condition;
41
+		this.message = message;
42
+	}
43
+
44
+	@Override
45
+	public void apply(IDefinitionMember member, BaseScope scope) {
46
+		applyOnOverridingMethod((FunctionalMember)member, scope);
47
+	}
48
+
49
+	@Override
50
+	public void applyOnOverridingMethod(FunctionalMember member, BaseScope scope) {
51
+		if (member.body == null)
52
+			return;
53
+		
54
+		ExpressionScope expressionScope = new ExpressionScope(scope, BasicTypeID.BOOL);
55
+		List<Statement> statements = new ArrayList<>();
56
+		ExpressionBuilder expressionBuilder = new ExpressionBuilder(position, expressionScope);
57
+		Expression inverseCondition = expressionBuilder.not(condition);
58
+		Statement throwStatement = new ThrowStatement(CodePosition.BUILTIN, expressionBuilder.constructNew("stdlib.IllegalArgumentException", message));
59
+		statements.add(new IfStatement(CodePosition.BUILTIN, inverseCondition, throwStatement, null));
60
+		if (member.body instanceof BlockStatement) {
61
+			statements.addAll(((BlockStatement)member.body).statements);
62
+		} else {
63
+			statements.add(member.body);
64
+		}
65
+		member.body = new BlockStatement(position, statements);
66
+	}
67
+}

+ 19
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/annotations/StatementAnnotation.java Näytä tiedosto

@@ -0,0 +1,19 @@
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.annotations;
7
+
8
+import org.openzen.zenscript.codemodel.scope.StatementScope;
9
+import org.openzen.zenscript.codemodel.statement.Statement;
10
+
11
+/**
12
+ *
13
+ * @author Hoofdgebruiker
14
+ */
15
+public interface StatementAnnotation {
16
+	public static final StatementAnnotation[] NONE = new StatementAnnotation[0];
17
+	
18
+	public Statement apply(Statement statement, StatementScope scope);
19
+}

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/definition/FunctionDefinition.java Näytä tiedosto

@@ -36,7 +36,7 @@ public class FunctionDefinition extends HighLevelDefinition {
36 36
 	
37 37
 	public void setHeader(FunctionHeader header) {
38 38
 		this.header = header;
39
-		addMember(caller = new CallerMember(position, this, modifiers | Modifiers.STATIC, header));
39
+		addMember(caller = new CallerMember(position, this, modifiers | Modifiers.STATIC, header, null));
40 40
 		callerGroup.addMethod(caller, TypeMemberPriority.SPECIFIED);
41 41
 	}
42 42
 	

+ 9
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/AndAndExpression.java Näytä tiedosto

@@ -27,4 +27,13 @@ public class AndAndExpression extends Expression {
27 27
 	public <T> T accept(ExpressionVisitor<T> visitor) {
28 28
 		return visitor.visitAndAnd(this);
29 29
 	}
30
+
31
+	@Override
32
+	public Expression transform(ExpressionTransformer transformer) {
33
+		Expression tLeft = left.transform(transformer);
34
+		Expression tRight = right.transform(transformer);
35
+		return tLeft == left && tRight == right
36
+				? this
37
+				: new AndAndExpression(position, tLeft, tRight);
38
+	}
30 39
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ArrayExpression.java Näytä tiedosto

@@ -27,4 +27,10 @@ public class ArrayExpression extends Expression {
27 27
 	public <T> T accept(ExpressionVisitor<T> visitor) {
28 28
 		return visitor.visitArray(this);
29 29
 	}
30
+
31
+	@Override
32
+	public Expression transform(ExpressionTransformer transformer) {
33
+		Expression[] tExpressions = Expression.transform(expressions, transformer);
34
+		return tExpressions == expressions ? this : new ArrayExpression(position, tExpressions, (ArrayTypeID)type);
35
+	}
30 36
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallArguments.java Näytä tiedosto

@@ -43,6 +43,11 @@ public class CallArguments {
43 43
 		return typeArguments.length;
44 44
 	}
45 45
 	
46
+	public CallArguments transform(ExpressionTransformer transformer) {
47
+		Expression[] tArguments = Expression.transform(arguments, transformer);
48
+		return tArguments == arguments ? this : new CallArguments(typeArguments, tArguments);
49
+	}
50
+	
46 51
 	public CallArguments normalize(CodePosition position, TypeScope scope, FunctionHeader header) {
47 52
 		CallArguments result = this;
48 53
 		

+ 23
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallExpression.java Näytä tiedosto

@@ -25,7 +25,7 @@ public class CallExpression extends Expression {
25 25
 		
26 26
 		this.target = target;
27 27
 		this.member = member;
28
-		this.arguments = arguments.normalize(position, scope, instancedHeader);
28
+		this.arguments = scope == null ? arguments : arguments.normalize(position, scope, instancedHeader);
29 29
 		this.instancedHeader = instancedHeader;
30 30
 	}
31 31
 	
@@ -37,4 +37,26 @@ public class CallExpression extends Expression {
37 37
 	public <T> T accept(ExpressionVisitor<T> visitor) {
38 38
 		return visitor.visitCall(this);
39 39
 	}
40
+
41
+	@Override
42
+	public Expression transform(ExpressionTransformer transformer) {
43
+		Expression tTarget = target.transform(transformer);
44
+		CallArguments tArguments = arguments.transform(transformer);
45
+		return tTarget == target && tArguments == arguments
46
+				? this
47
+				: new CallExpression(position, tTarget, member, instancedHeader, tArguments, null);
48
+	}
49
+	
50
+	@Override
51
+	public String evaluateStringConstant() {
52
+		if (member.builtin == null)
53
+			throw new UnsupportedOperationException("Cannot evaluate to a string constant!");
54
+		
55
+		switch (member.builtin) {
56
+			case STRING_ADD_STRING:
57
+				return target.evaluateStringConstant() + arguments.arguments[0].evaluateStringConstant();
58
+			default:
59
+				throw new UnsupportedOperationException("Cannot evaluate to a string constant!");
60
+		}
61
+	}
40 62
 }

+ 7
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CallStaticExpression.java Näytä tiedosto

@@ -26,7 +26,7 @@ public class CallStaticExpression extends Expression {
26 26
 		
27 27
 		this.member = member;
28 28
 		this.target = target;
29
-		this.arguments = arguments.normalize(position, scope, instancedHeader);
29
+		this.arguments = scope == null ? arguments : arguments.normalize(position, scope, instancedHeader);
30 30
 		this.instancedHeader = instancedHeader;
31 31
 	}
32 32
 
@@ -34,4 +34,10 @@ public class CallStaticExpression extends Expression {
34 34
 	public <T> T accept(ExpressionVisitor<T> visitor) {
35 35
 		return visitor.visitCallStatic(this);
36 36
 	}
37
+
38
+	@Override
39
+	public Expression transform(ExpressionTransformer transformer) {
40
+		CallArguments tArguments = arguments.transform(transformer);
41
+		return arguments == tArguments ? this : new CallStaticExpression(position, target, member, tArguments, instancedHeader, null);
42
+	}
37 43
 }

+ 10
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedClosureExpression.java Näytä tiedosto

@@ -29,4 +29,14 @@ public class CapturedClosureExpression extends CapturedExpression {
29 29
 	public <T> T accept(CapturedExpressionVisitor<T> visitor) {
30 30
 		return visitor.visitRecaptured(this);
31 31
 	}
32
+
33
+	@Override
34
+	public Expression transform(ExpressionTransformer transformer) {
35
+		Expression tValue = transformer.transform(value);
36
+		if (!(tValue instanceof CapturedExpression)) {
37
+			throw new IllegalStateException("Transformed CapturedExpression must also be a CapturedExpression!");
38
+		} else {
39
+			return tValue == value ? this : new CapturedClosureExpression(position, (CapturedExpression)tValue, closure);
40
+		}
41
+	}
32 42
 }

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedDirectExpression.java Näytä tiedosto

@@ -5,6 +5,8 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8
+import java.util.Map;
9
+import org.openzen.zenscript.codemodel.FunctionParameter;
8 10
 import org.openzen.zenscript.shared.CodePosition;
9 11
 
10 12
 /**
@@ -28,4 +30,10 @@ public class CapturedDirectExpression extends CapturedExpression {
28 30
 	public <T> T accept(CapturedExpressionVisitor<T> visitor) {
29 31
 		return visitor.visitCapturedDirect(this);
30 32
 	}
33
+
34
+	@Override
35
+	public Expression transform(ExpressionTransformer transformer) {
36
+		Expression tValue = transformer.transform(value);
37
+		return tValue == value ? this : new CapturedDirectExpression(position, closure, tValue);
38
+	}
31 39
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedLocalVariableExpression.java Näytä tiedosto

@@ -30,4 +30,9 @@ public class CapturedLocalVariableExpression extends CapturedExpression {
30 30
 	public <T> T accept(CapturedExpressionVisitor<T> visitor) {
31 31
 		return visitor.visitCapturedLocal(this);
32 32
 	}
33
+
34
+	@Override
35
+	public Expression transform(ExpressionTransformer transformer) {
36
+		return this;
37
+	}
33 38
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedParameterExpression.java Näytä tiedosto

@@ -30,4 +30,9 @@ public class CapturedParameterExpression extends CapturedExpression {
30 30
 	public <T> T accept(CapturedExpressionVisitor<T> visitor) {
31 31
 		return visitor.visitCapturedParameter(this);
32 32
 	}
33
+
34
+	@Override
35
+	public Expression transform(ExpressionTransformer transformer) {
36
+		return this;
37
+	}
33 38
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CapturedThisExpression.java Näytä tiedosto

@@ -26,4 +26,9 @@ public class CapturedThisExpression extends CapturedExpression {
26 26
 	public <T> T accept(CapturedExpressionVisitor<T> visitor) {
27 27
 		return visitor.visitCapturedThis(this);
28 28
 	}
29
+
30
+	@Override
31
+	public Expression transform(ExpressionTransformer transformer) {
32
+		return this;
33
+	}
29 34
 }

+ 8
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CastExpression.java Näytä tiedosto

@@ -5,6 +5,8 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8
+import java.util.Map;
9
+import org.openzen.zenscript.codemodel.FunctionParameter;
8 10
 import org.openzen.zenscript.codemodel.member.CasterMember;
9 11
 import org.openzen.zenscript.shared.CodePosition;
10 12
 
@@ -29,4 +31,10 @@ public class CastExpression extends Expression {
29 31
 	public <T> T accept(ExpressionVisitor<T> visitor) {
30 32
 		return visitor.visitCast(this);
31 33
 	}
34
+
35
+	@Override
36
+	public Expression transform(ExpressionTransformer transformer) {
37
+		Expression tTarget = target.transform(transformer);
38
+		return target == tTarget ? this : new CastExpression(position, tTarget, member, isImplicit);
39
+	}
32 40
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CheckNullExpression.java Näytä tiedosto

@@ -25,4 +25,10 @@ public class CheckNullExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitCheckNull(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		Expression tValue = transformer.transform(value);
32
+		return value == tValue ? this : new CheckNullExpression(position, tValue);
33
+	}
28 34
 }

+ 7
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CoalesceExpression.java Näytä tiedosto

@@ -26,4 +26,11 @@ public class CoalesceExpression extends Expression {
26 26
 	public <T> T accept(ExpressionVisitor<T> visitor) {
27 27
 		return visitor.visitCoalesce(this);
28 28
 	}
29
+
30
+	@Override
31
+	public Expression transform(ExpressionTransformer transformer) {
32
+		Expression tLeft = left.transform(transformer);
33
+		Expression tRight = right.transform(transformer);
34
+		return tLeft == left && tRight == right ? this : new CoalesceExpression(position, tLeft, tRight);
35
+	}
29 36
 }

CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/BasicCompareExpression.java → CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/CompareExpression.java Näytä tiedosto

@@ -6,6 +6,8 @@
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.CompareType;
9
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
10
+import org.openzen.zenscript.codemodel.scope.TypeScope;
9 11
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
10 12
 import org.openzen.zenscript.shared.CodePosition;
11 13
 
@@ -15,21 +17,30 @@ import org.openzen.zenscript.shared.CodePosition;
15 17
  * 
16 18
  * @author Hoofdgebruiker
17 19
  */
18
-public class BasicCompareExpression extends Expression {
20
+public class CompareExpression extends Expression {
19 21
 	public final Expression left;
20 22
 	public final Expression right;
21
-	public final CompareType operator;
23
+	public final FunctionalMember operator;
24
+	public final CompareType comparison;
22 25
 	
23
-	public BasicCompareExpression(CodePosition position, Expression left, Expression right, CompareType operator) {
26
+	public CompareExpression(CodePosition position, Expression left, Expression right, FunctionalMember operator, CompareType comparison, TypeScope scope) {
24 27
 		super(position, BasicTypeID.BOOL, binaryThrow(position, left.thrownType, right.thrownType));
25 28
 		
26 29
 		this.left = left;
27
-		this.right = right;
30
+		this.right = scope == null ? right : right.castImplicit(position, scope, operator.header.parameters[0].type);
28 31
 		this.operator = operator;
32
+		this.comparison = comparison;
29 33
 	}
30 34
 
31 35
 	@Override
32 36
 	public <T> T accept(ExpressionVisitor<T> visitor) {
33 37
 		return visitor.visitCompare(this);
34 38
 	}
39
+
40
+	@Override
41
+	public Expression transform(ExpressionTransformer transformer) {
42
+		Expression tLeft = left.transform(transformer);
43
+		Expression tRight = right.transform(transformer);
44
+		return left == tLeft && right == tRight ? this : new CompareExpression(position, tLeft, tRight, operator, comparison, null);
45
+	}
35 46
 }

+ 10
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConditionalExpression.java Näytä tiedosto

@@ -37,4 +37,14 @@ public class ConditionalExpression extends Expression {
37 37
 	public <T> T accept(ExpressionVisitor<T> visitor) {
38 38
 		return visitor.visitConditional(this);
39 39
 	}
40
+
41
+	@Override
42
+	public Expression transform(ExpressionTransformer transformer) {
43
+		Expression tCondition = transformer.transform(condition);
44
+		Expression tIfThen = transformer.transform(ifThen);
45
+		Expression tIfElse = transformer.transform(ifElse);
46
+		return tCondition == condition && tIfThen == ifThen && tIfElse == ifElse
47
+				? this
48
+				: new ConditionalExpression(position, tCondition, tIfThen, tIfElse, type);
49
+	}
40 50
 }

+ 44
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstExpression.java Näytä tiedosto

@@ -0,0 +1,44 @@
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.member.ConstMember;
9
+import org.openzen.zenscript.codemodel.member.EnumConstantMember;
10
+import org.openzen.zenscript.shared.CodePosition;
11
+
12
+/**
13
+ *
14
+ * @author Hoofdgebruiker
15
+ */
16
+public class ConstExpression extends Expression {
17
+	public final ConstMember constant;
18
+	
19
+	public ConstExpression(CodePosition position, ConstMember constant) {
20
+		super(position, constant.type, null);
21
+		
22
+		this.constant = constant;
23
+	}
24
+
25
+	@Override
26
+	public <T> T accept(ExpressionVisitor<T> visitor) {
27
+		return visitor.visitConst(this);
28
+	}
29
+
30
+	@Override
31
+	public Expression transform(ExpressionTransformer transformer) {
32
+		return this;
33
+	}
34
+	
35
+	@Override
36
+	public String evaluateStringConstant() {
37
+		return constant.value.evaluateStringConstant();
38
+	}
39
+	
40
+	@Override
41
+	public EnumConstantMember evaluateEnumConstant() {
42
+		return constant.value.evaluateEnumConstant();
43
+	}
44
+}

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantBoolExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantBoolExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantBool(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 7
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantByteExpression.java Näytä tiedosto

@@ -13,9 +13,9 @@ import org.openzen.zenscript.shared.CodePosition;
13 13
  * @author Hoofdgebruiker
14 14
  */
15 15
 public class ConstantByteExpression extends Expression {
16
-	public final byte value;
16
+	public final int value;
17 17
 	
18
-	public ConstantByteExpression(CodePosition position, byte value) {
18
+	public ConstantByteExpression(CodePosition position, int value) {
19 19
 		super(position, BasicTypeID.BYTE, null);
20 20
 		
21 21
 		this.value = value;
@@ -25,4 +25,9 @@ public class ConstantByteExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantByte(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantCharExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantCharExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantChar(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantDoubleExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantDoubleExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantDouble(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantFloatExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantFloatExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantFloat(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantIntExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantIntExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantInt(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantLongExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantLongExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantLong(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantSByteExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantSByteExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantSByte(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantShortExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantShortExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantShort(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 10
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantStringExpression.java Näytä tiedosto

@@ -25,4 +25,14 @@ public class ConstantStringExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantString(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
33
+	
34
+	@Override
35
+	public String evaluateStringConstant() {
36
+		return value;
37
+	}
28 38
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantUIntExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantUIntExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantUInt(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantULongExpression.java Näytä tiedosto

@@ -25,4 +25,9 @@ public class ConstantULongExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantULong(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 7
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstantUShortExpression.java Näytä tiedosto

@@ -13,9 +13,9 @@ import org.openzen.zenscript.shared.CodePosition;
13 13
  * @author Hoofdgebruiker
14 14
  */
15 15
 public class ConstantUShortExpression extends Expression {
16
-	public final short value;
16
+	public final int value;
17 17
 	
18
-	public ConstantUShortExpression(CodePosition position, short value) {
18
+	public ConstantUShortExpression(CodePosition position, int value) {
19 19
 		super(position, BasicTypeID.USHORT, null);
20 20
 		
21 21
 		this.value = value;
@@ -25,4 +25,9 @@ public class ConstantUShortExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitConstantUShort(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		return this;
32
+	}
28 33
 }

+ 7
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorSuperCallExpression.java Näytä tiedosto

@@ -25,11 +25,17 @@ public class ConstructorSuperCallExpression extends Expression {
25 25
 		
26 26
 		this.objectType = type;
27 27
 		this.constructor = constructor;
28
-		this.arguments = arguments.normalize(position, scope, constructor.header);
28
+		this.arguments = scope == null ? arguments : arguments.normalize(position, scope, constructor.header);
29 29
 	}
30 30
 
31 31
 	@Override
32 32
 	public <T> T accept(ExpressionVisitor<T> visitor) {
33 33
 		return visitor.visitConstructorSuperCall(this);
34 34
 	}
35
+
36
+	@Override
37
+	public Expression transform(ExpressionTransformer transformer) {
38
+		CallArguments tArguments = arguments.transform(transformer);
39
+		return tArguments == arguments ? this : new ConstructorSuperCallExpression(position, type, constructor, tArguments, null);
40
+	}
35 41
 }

+ 7
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ConstructorThisCallExpression.java Näytä tiedosto

@@ -23,15 +23,19 @@ public class ConstructorThisCallExpression extends Expression {
23 23
 	public ConstructorThisCallExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments, TypeScope scope) {
24 24
 		super(position, BasicTypeID.VOID, binaryThrow(position, constructor.header.thrownType, multiThrow(position, arguments.arguments)));
25 25
 		
26
-		arguments = arguments.normalize(position, scope, constructor.header);
27
-		
28 26
 		this.objectType = type;
29 27
 		this.constructor = constructor;
30
-		this.arguments = arguments;
28
+		this.arguments = scope == null ? arguments : arguments.normalize(position, scope, constructor.header);
31 29
 	}
32 30
 
33 31
 	@Override
34 32
 	public <T> T accept(ExpressionVisitor<T> visitor) {
35 33
 		return visitor.visitConstructorThisCall(this);
36 34
 	}
35
+
36
+	@Override
37
+	public Expression transform(ExpressionTransformer transformer) {
38
+		CallArguments tArguments = arguments.transform(transformer);
39
+		return tArguments == arguments ? this : new ConstructorThisCallExpression(position, type, constructor, tArguments, null);
40
+	}
37 41
 }

+ 10
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/EnumConstantExpression.java Näytä tiedosto

@@ -34,4 +34,14 @@ public class EnumConstantExpression extends Expression {
34 34
 	public <T> T accept(ExpressionVisitor<T> visitor) {
35 35
 		return visitor.visitEnumConstant(this);
36 36
 	}
37
+
38
+	@Override
39
+	public Expression transform(ExpressionTransformer transformer) {
40
+		return this;
41
+	}
42
+	
43
+	@Override
44
+	public EnumConstantMember evaluateEnumConstant() {
45
+		return value;
46
+	}
37 47
 }

+ 53
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/Expression.java Näytä tiedosto

@@ -7,15 +7,20 @@ package org.openzen.zenscript.codemodel.expression;
7 7
 
8 8
 import java.util.Collections;
9 9
 import java.util.List;
10
+import java.util.function.Consumer;
10 11
 import java.util.stream.Collectors;
11 12
 import org.openzen.zenscript.codemodel.FunctionHeader;
12 13
 import org.openzen.zenscript.codemodel.OperatorType;
14
+import org.openzen.zenscript.codemodel.member.EnumConstantMember;
13 15
 import org.openzen.zenscript.codemodel.partial.IPartialExpression;
14 16
 import org.openzen.zenscript.codemodel.type.GenericName;
15 17
 import org.openzen.zenscript.codemodel.type.ITypeID;
16 18
 import org.openzen.zenscript.codemodel.type.member.TypeMembers;
17 19
 import org.openzen.zenscript.shared.CodePosition;
18 20
 import org.openzen.zenscript.codemodel.scope.TypeScope;
21
+import org.openzen.zenscript.codemodel.statement.Statement;
22
+import org.openzen.zenscript.codemodel.statement.StatementTransformer;
23
+import org.openzen.zenscript.codemodel.type.FunctionTypeID;
19 24
 import org.openzen.zenscript.shared.CompileException;
20 25
 import org.openzen.zenscript.shared.CompileExceptionCode;
21 26
 
@@ -24,6 +29,8 @@ import org.openzen.zenscript.shared.CompileExceptionCode;
24 29
  * @author Hoofdgebruiker
25 30
  */
26 31
 public abstract class Expression implements IPartialExpression {
32
+	public static final Expression[] NONE = new Expression[0];
33
+	
27 34
 	public final CodePosition position;
28 35
 	public final ITypeID type;
29 36
 	public final ITypeID thrownType;
@@ -36,6 +43,23 @@ public abstract class Expression implements IPartialExpression {
36 43
 	
37 44
 	public abstract <T> T accept(ExpressionVisitor<T> visitor);
38 45
 	
46
+	public abstract Expression transform(ExpressionTransformer transformer);
47
+	
48
+	public final Expression transform(StatementTransformer transformer) {
49
+		return transform((ExpressionTransformer)expression -> {
50
+			if (expression instanceof FunctionExpression) {
51
+				FunctionExpression function = (FunctionExpression)expression;
52
+				Statement body = function.body.transform(transformer);
53
+				if (body == function.body)
54
+					return function;
55
+				
56
+				return new FunctionExpression(function.position, (FunctionTypeID)function.type, function.closure, body);
57
+			} else {
58
+				return expression;
59
+			}
60
+		});
61
+	}
62
+	
39 63
 	@Override
40 64
 	public List<ITypeID> getAssignHints() {
41 65
 		return Collections.singletonList(type);
@@ -64,8 +88,8 @@ public abstract class Expression implements IPartialExpression {
64 88
 		return scope.getTypeMembers(type)
65 89
 				.getOrCreateGroup(OperatorType.CALL)
66 90
 				.getMethodMembers().stream()
67
-				.filter(method -> method.member.getHeader().parameters.length == arguments && !method.member.isStatic())
68
-				.map(method -> method.member.getHeader())
91
+				.filter(method -> method.member.header.parameters.length == arguments && !method.member.isStatic())
92
+				.map(method -> method.member.header)
69 93
 				.collect(Collectors.toList());
70 94
 	}
71 95
 	
@@ -77,7 +101,10 @@ public abstract class Expression implements IPartialExpression {
77 101
 	@Override
78 102
 	public IPartialExpression getMember(CodePosition position, TypeScope scope, List<ITypeID> hints, GenericName name) {
79 103
 		TypeMembers members = scope.getTypeMembers(type);
80
-		return members.getMemberExpression(position, this, name, false);
104
+		IPartialExpression result = members.getMemberExpression(position, this, name, false);
105
+		if (result == null)
106
+			System.out.println("No such member: " + name.name);
107
+		return result;
81 108
 	}
82 109
 	
83 110
 	@Override
@@ -85,6 +112,18 @@ public abstract class Expression implements IPartialExpression {
85 112
 		return null;
86 113
 	}
87 114
 	
115
+	public void forEachStatement(Consumer<Statement> consumer) {
116
+		
117
+	}
118
+	
119
+	public String evaluateStringConstant() {
120
+		throw new UnsupportedOperationException("Cannot evaluate this value to a string constant!");
121
+	}
122
+	
123
+	public EnumConstantMember evaluateEnumConstant() {
124
+		throw new UnsupportedOperationException("Cannot evaluate this value to an enum constant!");
125
+	}
126
+	
88 127
 	public static ITypeID binaryThrow(CodePosition position, ITypeID left, ITypeID right) {
89 128
 		if (left == right)
90 129
 			return left;
@@ -102,4 +141,15 @@ public abstract class Expression implements IPartialExpression {
102 141
 			result = binaryThrow(position, result, expression.thrownType);
103 142
 		return result;
104 143
 	}
144
+	
145
+	public static Expression[] transform(Expression[] expressions, ExpressionTransformer transformer) {
146
+		Expression[] tExpressions = new Expression[expressions.length];
147
+		boolean changed = false;
148
+		for (int i = 0; i < tExpressions.length; i++) {
149
+			Expression tExpression = expressions[i].transform(transformer);
150
+			changed |= tExpression != expressions[i];
151
+			tExpressions[i] = tExpression;
152
+		}
153
+		return changed ? tExpressions : expressions;
154
+	}
105 155
 }

+ 72
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionBuilder.java Näytä tiedosto

@@ -0,0 +1,72 @@
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 java.util.ArrayList;
9
+import java.util.List;
10
+import org.openzen.zenscript.codemodel.OperatorType;
11
+import org.openzen.zenscript.codemodel.member.ConstructorMember;
12
+import org.openzen.zenscript.codemodel.member.FunctionalMember;
13
+import org.openzen.zenscript.codemodel.scope.ExpressionScope;
14
+import org.openzen.zenscript.codemodel.type.GenericName;
15
+import org.openzen.zenscript.codemodel.type.ITypeID;
16
+import org.openzen.zenscript.codemodel.type.member.DefinitionMemberGroup;
17
+import org.openzen.zenscript.codemodel.type.member.TypeMembers;
18
+import org.openzen.zenscript.shared.CodePosition;
19
+import org.openzen.zenscript.shared.CompileException;
20
+import org.openzen.zenscript.shared.CompileExceptionCode;
21
+import org.openzen.zenscript.shared.StringUtils;
22
+
23
+/**
24
+ *
25
+ * @author Hoofdgebruiker
26
+ */
27
+public class ExpressionBuilder {
28
+	public final CodePosition position;
29
+	public final ExpressionScope scope;
30
+	
31
+	public ExpressionBuilder(CodePosition position, ExpressionScope scope) {
32
+		this.position = position;
33
+		this.scope = scope;
34
+	}
35
+	
36
+	public Expression constructNew(String typename, Expression... arguments) {
37
+		List<String> nameParts = StringUtils.split(typename, '.');
38
+		List<GenericName> name = new ArrayList<>();
39
+		for (String namePart : nameParts)
40
+			name.add(new GenericName(namePart));
41
+		ITypeID type = scope.getType(position, name);
42
+		if (type == null)
43
+			throw new CompileException(position, CompileExceptionCode.NO_SUCH_TYPE, "No such type: " + typename);
44
+		
45
+		return constructNew(type, arguments);
46
+	}
47
+	
48
+	public Expression constructNew(ITypeID type, Expression... arguments) {
49
+		DefinitionMemberGroup constructors = scope.getTypeMembers(type).getOrCreateGroup(OperatorType.CONSTRUCTOR);
50
+		List<ITypeID>[] predictedTypes = constructors.predictCallTypes(scope, scope.hints, arguments.length);
51
+		CallArguments compiledArguments = new CallArguments(arguments);
52
+		FunctionalMember member = constructors.selectMethod(position, scope, compiledArguments, true, true);
53
+		if (member == null)
54
+			throw new CompileException(position, CompileExceptionCode.CALL_NO_VALID_METHOD, "No matching constructor found");
55
+		if (!(member instanceof ConstructorMember))
56
+			throw new CompileException(position, CompileExceptionCode.INTERNAL_ERROR, "COMPILER BUG: constructor is not a constructor");
57
+		
58
+		return new NewExpression(
59
+				position,
60
+				type,
61
+				(ConstructorMember) member,
62
+				compiledArguments,
63
+				compiledArguments.getNumberOfTypeArguments() == 0 ? member.header : member.header.withGenericArguments(scope.getTypeRegistry(), compiledArguments.typeArguments),
64
+				scope);
65
+	}
66
+	
67
+	public Expression not(Expression value) {
68
+		TypeMembers members = scope.getTypeMembers(value.type);
69
+		return members.getOrCreateGroup(OperatorType.NOT)
70
+				.call(position, scope, value, CallArguments.EMPTY, false);
71
+	}
72
+}

+ 14
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionTransformer.java Näytä tiedosto

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

+ 5
- 3
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/ExpressionVisitor.java Näytä tiedosto

@@ -14,7 +14,7 @@ public interface ExpressionVisitor<T> {
14 14
 	
15 15
 	public T visitArray(ArrayExpression expression);
16 16
 	
17
-	public T visitCompare(BasicCompareExpression expression);
17
+	public T visitCompare(CompareExpression expression);
18 18
 	
19 19
 	public T visitCall(CallExpression expression);
20 20
 	
@@ -38,6 +38,8 @@ public interface ExpressionVisitor<T> {
38 38
 	
39 39
 	public T visitConditional(ConditionalExpression expression);
40 40
 	
41
+	public T visitConst(ConstExpression expression);
42
+	
41 43
 	public T visitConstantBool(ConstantBoolExpression expression);
42 44
 	
43 45
 	public T visitConstantByte(ConstantByteExpression expression);
@@ -72,8 +74,6 @@ public interface ExpressionVisitor<T> {
72 74
 	
73 75
 	public T visitFunction(FunctionExpression expression);
74 76
 	
75
-	public T visitGenericCompare(GenericCompareExpression expression);
76
-	
77 77
 	public T visitGetField(GetFieldExpression expression);
78 78
 	
79 79
 	public T visitGetFunctionParameter(GetFunctionParameterExpression expression);
@@ -128,6 +128,8 @@ public interface ExpressionVisitor<T> {
128 128
 	
129 129
 	public T visitThis(ThisExpression expression);
130 130
 	
131
+	public T visitThrow(ThrowExpression expression);
132
+	
131 133
 	public T visitTryConvert(TryConvertExpression expression);
132 134
 	
133 135
 	public T visitTryRethrowAsException(TryRethrowAsExceptionExpression expression);

+ 59
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/FunctionExpression.java Näytä tiedosto

@@ -5,10 +5,17 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+import java.util.function.Consumer;
8 11
 import org.openzen.zenscript.codemodel.FunctionHeader;
12
+import org.openzen.zenscript.codemodel.FunctionParameter;
13
+import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
14
+import org.openzen.zenscript.codemodel.statement.ReturnStatement;
9 15
 import org.openzen.zenscript.codemodel.statement.Statement;
10 16
 import org.openzen.zenscript.codemodel.type.FunctionTypeID;
11 17
 import org.openzen.zenscript.shared.CodePosition;
18
+import org.openzen.zenscript.shared.ConcatMap;
12 19
 
13 20
 /**
14 21
  *
@@ -35,4 +42,56 @@ public class FunctionExpression extends Expression {
35 42
 	public <T> T accept(ExpressionVisitor<T> visitor) {
36 43
 		return visitor.visitFunction(this);
37 44
 	}
45
+
46
+	@Override
47
+	public FunctionExpression transform(ExpressionTransformer transformer) {
48
+		Statement tBody = body.transform(transformer, ConcatMap.empty());
49
+		return tBody == body ? this : new FunctionExpression(position, (FunctionTypeID)type, closure, tBody);
50
+	}
51
+	
52
+	@Override
53
+	public void forEachStatement(Consumer<Statement> consumer) {
54
+		body.forEachStatement(consumer);
55
+	}
56
+	
57
+	/**
58
+	 * Checks if this is a simple function expression. A simple function
59
+	 * expression consists of a body with just a expression or return statement.
60
+	 * 
61
+	 * @return 
62
+	 */
63
+	public boolean isSimple() {
64
+		return body instanceof ReturnStatement || body instanceof ExpressionStatement;
65
+	}
66
+	
67
+	public Expression asReturnExpression(Expression... arguments) {
68
+		Map<FunctionParameter, Expression> filledArguments = new HashMap<>();
69
+		if (body instanceof ReturnStatement) {
70
+			return ((ReturnStatement)body).value.transform(new ReturnExpressionTransformer(closure, filledArguments));
71
+		} else {
72
+			return null;
73
+		}
74
+	}
75
+	
76
+	private static class ReturnExpressionTransformer implements ExpressionTransformer {
77
+		private final LambdaClosure closure;
78
+		private final Map<FunctionParameter, Expression> filledArguments;
79
+		
80
+		public ReturnExpressionTransformer(LambdaClosure closure, Map<FunctionParameter, Expression> filledArguments) {
81
+			this.closure = closure;
82
+			this.filledArguments = filledArguments;
83
+		}
84
+		
85
+		@Override
86
+		public Expression transform(Expression original) {
87
+			if (original instanceof GetFunctionParameterExpression) {
88
+				GetFunctionParameterExpression getParameter = (GetFunctionParameterExpression)original;
89
+				if (filledArguments.containsKey(getParameter.parameter)) {
90
+					return filledArguments.get(getParameter.parameter);
91
+				}
92
+			}
93
+			
94
+			return original;
95
+		}
96
+	}
38 97
 }

+ 0
- 31
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GenericCompareExpression.java Näytä tiedosto

@@ -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.CompareType;
9
-import org.openzen.zenscript.codemodel.type.BasicTypeID;
10
-import org.openzen.zenscript.shared.CodePosition;
11
-
12
-/**
13
- *
14
- * @author Hoofdgebruiker
15
- */
16
-public class GenericCompareExpression extends Expression {
17
-	public final Expression value;
18
-	public final CompareType operator;
19
-	
20
-	public GenericCompareExpression(CodePosition position, Expression value, CompareType operator) {
21
-		super(position, BasicTypeID.BOOL, value.thrownType);
22
-		
23
-		this.value = value;
24
-		this.operator = operator;
25
-	}
26
-
27
-	@Override
28
-	public <T> T accept(ExpressionVisitor<T> visitor) {
29
-		return visitor.visitGenericCompare(this);
30
-	}
31
-}

+ 6
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetFieldExpression.java Näytä tiedosto

@@ -11,8 +11,6 @@ import org.openzen.zenscript.codemodel.member.FieldMember;
11 11
 import org.openzen.zenscript.codemodel.scope.TypeScope;
12 12
 import org.openzen.zenscript.codemodel.type.ITypeID;
13 13
 import org.openzen.zenscript.shared.CodePosition;
14
-import org.openzen.zenscript.shared.CompileException;
15
-import org.openzen.zenscript.shared.CompileExceptionCode;
16 14
 
17 15
 /**
18 16
  *
@@ -48,4 +46,10 @@ public class GetFieldExpression extends Expression {
48 46
 	public <T> T accept(ExpressionVisitor<T> visitor) {
49 47
 		return visitor.visitGetField(this);
50 48
 	}
49
+
50
+	@Override
51
+	public Expression transform(ExpressionTransformer transformer) {
52
+		Expression tTarget = target.transform(transformer);
53
+		return tTarget == target ? this : new GetFieldExpression(position, tTarget, field);
54
+	}
51 55
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetFunctionParameterExpression.java Näytä tiedosto

@@ -38,4 +38,9 @@ public class GetFunctionParameterExpression extends Expression {
38 38
 	public <T> T accept(ExpressionVisitor<T> visitor) {
39 39
 		return visitor.visitGetFunctionParameter(this);
40 40
 	}
41
+
42
+	@Override
43
+	public Expression transform(ExpressionTransformer transformer) {
44
+		return this;
45
+	}
41 46
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetLocalVariableExpression.java Näytä tiedosto

@@ -38,4 +38,9 @@ public class GetLocalVariableExpression extends Expression {
38 38
 	public <T> T accept(ExpressionVisitor<T> visitor) {
39 39
 		return visitor.visitGetLocalVariable(this);
40 40
 	}
41
+
42
+	@Override
43
+	public Expression transform(ExpressionTransformer transformer) {
44
+		return this;
45
+	}
41 46
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetStaticFieldExpression.java Näytä tiedosto

@@ -38,4 +38,9 @@ public class GetStaticFieldExpression extends Expression {
38 38
 	public <T> T accept(ExpressionVisitor<T> visitor) {
39 39
 		return visitor.visitGetStaticField(this);
40 40
 	}
41
+
42
+	@Override
43
+	public Expression transform(ExpressionTransformer transformer) {
44
+		return this;
45
+	}
41 46
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GetterExpression.java Näytä tiedosto

@@ -27,4 +27,10 @@ public class GetterExpression extends Expression {
27 27
 	public <T> T accept(ExpressionVisitor<T> visitor) {
28 28
 		return visitor.visitGetter(this);
29 29
 	}
30
+
31
+	@Override
32
+	public Expression transform(ExpressionTransformer transformer) {
33
+		Expression tTarget = target.transform(transformer);
34
+		return target == tTarget ? this : new GetterExpression(position, tTarget, getter);
35
+	}
30 36
 }

+ 7
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalCallExpression.java Näytä tiedosto

@@ -28,4 +28,11 @@ public class GlobalCallExpression extends Expression {
28 28
 	public <T> T accept(ExpressionVisitor<T> visitor) {
29 29
 		return visitor.visitGlobalCall(this);
30 30
 	}
31
+
32
+	@Override
33
+	public Expression transform(ExpressionTransformer transformer) {
34
+		CallArguments tArguments = arguments.transform(transformer);
35
+		Expression tResolution = resolution.transform(transformer);
36
+		return tArguments == arguments && tResolution == resolution ? this : new GlobalCallExpression(position, name, tArguments, tResolution);
37
+	}
31 38
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/GlobalExpression.java Näytä tiedosto

@@ -26,4 +26,10 @@ public class GlobalExpression extends Expression {
26 26
 	public <T> T accept(ExpressionVisitor<T> visitor) {
27 27
 		return visitor.visitGlobal(this);
28 28
 	}
29
+
30
+	@Override
31
+	public Expression transform(ExpressionTransformer transformer) {
32
+		Expression tResolution = resolution.transform(transformer);
33
+		return resolution == tResolution ? this : new GlobalExpression(position, name, resolution);
34
+	}
29 35
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/InterfaceCastExpression.java Näytä tiedosto

@@ -25,4 +25,10 @@ public class InterfaceCastExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitInterfaceCast(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		Expression tValue = value.transform(transformer);
32
+		return value == tValue ? this : new InterfaceCastExpression(position, tValue, type);
33
+	}
28 34
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/IsExpression.java Näytä tiedosto

@@ -28,4 +28,10 @@ public class IsExpression extends Expression {
28 28
 	public <T> T accept(ExpressionVisitor<T> visitor) {
29 29
 		return visitor.visitIs(this);
30 30
 	}
31
+
32
+	@Override
33
+	public Expression transform(ExpressionTransformer transformer) {
34
+		Expression tValue = value.transform(transformer);
35
+		return tValue == value ? this : new IsExpression(position, tValue, type);
36
+	}
31 37
 }

+ 6
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MakeConstExpression.java Näytä tiedosto

@@ -25,4 +25,10 @@ public class MakeConstExpression extends Expression {
25 25
 	public <T> T accept(ExpressionVisitor<T> visitor) {
26 26
 		return visitor.visitMakeConst(this);
27 27
 	}
28
+
29
+	@Override
30
+	public Expression transform(ExpressionTransformer transformer) {
31
+		Expression tValue = value.transform(transformer);
32
+		return tValue == value ? this : new MakeConstExpression(position, tValue, type);
33
+	}
28 34
 }

+ 7
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MapExpression.java Näytä tiedosto

@@ -31,4 +31,11 @@ public class MapExpression extends Expression {
31 31
 	public <T> T accept(ExpressionVisitor<T> visitor) {
32 32
 		return visitor.visitMap(this);
33 33
 	}
34
+
35
+	@Override
36
+	public Expression transform(ExpressionTransformer transformer) {
37
+		Expression[] tKeys = Expression.transform(keys, transformer);
38
+		Expression[] tValues = Expression.transform(values, transformer);
39
+		return tKeys == keys && tValues == values ? this : new MapExpression(position, tKeys, tValues, type);
40
+	}
34 41
 }

+ 45
- 2
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/MatchExpression.java Näytä tiedosto

@@ -5,7 +5,12 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8
+import java.util.Collections;
8 9
 import org.openzen.zenscript.codemodel.expression.switchvalue.SwitchValue;
10
+import org.openzen.zenscript.codemodel.statement.ExpressionStatement;
11
+import org.openzen.zenscript.codemodel.statement.SwitchCase;
12
+import org.openzen.zenscript.codemodel.statement.SwitchStatement;
13
+import org.openzen.zenscript.codemodel.statement.VarStatement;
9 14
 import org.openzen.zenscript.codemodel.type.ITypeID;
10 15
 import org.openzen.zenscript.shared.CodePosition;
11 16
 
@@ -28,15 +33,53 @@ public class MatchExpression extends Expression {
28 33
 	public <T> T accept(ExpressionVisitor<T> visitor) {
29 34
 		return visitor.visitMatch(this);
30 35
 	}
36
+
37
+	@Override
38
+	public Expression transform(ExpressionTransformer transformer) {
39
+		Expression tValue = value.transform(transformer);
40
+		Case[] tCases = new Case[cases.length];
41
+		boolean unmodified = true;
42
+		for (int i = 0; i < tCases.length; i++) {
43
+			tCases[i] = cases[i].transform(transformer);
44
+			unmodified &= tCases[i] == cases[i];
45
+		}
46
+		return unmodified && tValue == value ? this : new MatchExpression(position, tValue, type, tCases);
47
+	}
48
+	
49
+	public SwitchedMatch convertToSwitch(String tempVariable) {
50
+		VarStatement result = new VarStatement(position, tempVariable, type, null, false);
51
+		SwitchStatement switchStatement = new SwitchStatement(position, null, value);
52
+		for (MatchExpression.Case matchCase : cases) {
53
+			Expression caseExpression = new SetLocalVariableExpression(matchCase.value.position, result, matchCase.value);
54
+			SwitchCase switchCase = new SwitchCase(matchCase.key, Collections.singletonList(new ExpressionStatement(matchCase.value.position, caseExpression)));
55
+			switchStatement.cases.add(switchCase);
56
+		}
57
+		return new SwitchedMatch(result, switchStatement);
58
+	}
59
+	
60
+	public static class SwitchedMatch {
61
+		public final VarStatement result;
62
+		public final SwitchStatement switchStatement;
63
+		
64
+		public SwitchedMatch(VarStatement temp, SwitchStatement switchStatement) {
65
+			this.result = temp;
66
+			this.switchStatement = switchStatement;
67
+		}
68
+	}
31 69
 	
32 70
 	public static class Case {
33 71
 		public final SwitchValue key;
34
-		public final FunctionExpression value;
72
+		public final Expression value;
35 73
 		
36
-		public Case(SwitchValue key, FunctionExpression value) {
74
+		public Case(SwitchValue key, Expression value) {
37 75
 			this.key = key;
38 76
 			this.value = value;
39 77
 		}
78
+		
79
+		public Case transform(ExpressionTransformer transformer) {
80
+			Expression tValue = value.transform(transformer);
81
+			return tValue == value ? this : new Case(key, tValue);
82
+		}
40 83
 	}
41 84
 	
42 85
 	private static ITypeID getThrownType(CodePosition position, Case[] cases) {

+ 31
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/NewExpression.java Näytä tiedosto

@@ -5,7 +5,9 @@
5 5
  */
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8
+import org.openzen.zenscript.codemodel.FunctionHeader;
8 9
 import org.openzen.zenscript.codemodel.member.ConstructorMember;
10
+import org.openzen.zenscript.codemodel.scope.TypeScope;
9 11
 import org.openzen.zenscript.codemodel.type.ITypeID;
10 12
 import org.openzen.zenscript.shared.CodePosition;
11 13
 
@@ -16,16 +18,44 @@ import org.openzen.zenscript.shared.CodePosition;
16 18
 public class NewExpression extends Expression {
17 19
 	public final ConstructorMember constructor;
18 20
 	public final CallArguments arguments;
21
+	public final FunctionHeader instancedHeader;
19 22
 	
20
-	public NewExpression(CodePosition position, ITypeID type, ConstructorMember constructor, CallArguments arguments) {
23
+	public NewExpression(
24
+			CodePosition position,
25
+			ITypeID type,
26
+			ConstructorMember constructor,
27
+			CallArguments arguments)
28
+	{
21 29
 		super(position, type, binaryThrow(position, constructor.header.thrownType, multiThrow(position, arguments.arguments)));
22 30
 		
23 31
 		this.constructor = constructor;
24 32
 		this.arguments = arguments;
33
+		this.instancedHeader = constructor.header;
34
+	}
35
+	
36
+	public NewExpression(
37
+			CodePosition position,
38
+			ITypeID type,
39
+			ConstructorMember constructor,
40
+			CallArguments arguments,
41
+			FunctionHeader instancedHeader,
42
+			TypeScope scope)
43
+	{
44
+		super(position, type, binaryThrow(position, constructor.header.thrownType, multiThrow(position, arguments.arguments)));
45
+		
46
+		this.constructor = constructor;
47
+		this.arguments = scope == null ? arguments : arguments.normalize(position, scope, instancedHeader);
48
+		this.instancedHeader = instancedHeader;
25 49
 	}
26 50
 
27 51
 	@Override
28 52
 	public <T> T accept(ExpressionVisitor<T> visitor) {
29 53
 		return visitor.visitNew(this);
30 54
 	}
55
+
56
+	@Override
57
+	public Expression transform(ExpressionTransformer transformer) {
58
+		CallArguments tArguments = arguments.transform(transformer);
59
+		return tArguments == arguments ? this : new NewExpression(position, type, constructor, tArguments, instancedHeader, null);
60
+	}
31 61
 }

+ 5
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/NullExpression.java Näytä tiedosto

@@ -26,4 +26,9 @@ public class NullExpression extends Expression {
26 26
 	public <T> T accept(ExpressionVisitor<T> visitor) {
27 27
 		return visitor.visitNull(this);
28 28
 	}
29
+
30
+	@Override
31
+	public Expression transform(ExpressionTransformer transformer) {
32
+		return this;
33
+	}
29 34
 }

+ 7
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/OrOrExpression.java Näytä tiedosto

@@ -27,4 +27,11 @@ public class OrOrExpression extends Expression {
27 27
 	public <T> T accept(ExpressionVisitor<T> visitor) {
28 28
 		return visitor.visitOrOr(this);
29 29
 	}
30
+
31
+	@Override
32
+	public Expression transform(ExpressionTransformer transformer) {
33
+		Expression tLeft = left.transform(transformer);
34
+		Expression tRight = right.transform(transformer);
35
+		return tLeft == left && tRight == right ? this : new OrOrExpression(position, tLeft, tRight);
36
+	}
30 37
 }

+ 9
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/PostCallExpression.java Näytä tiedosto

@@ -18,19 +18,27 @@ import org.openzen.zenscript.shared.CodePosition;
18 18
 public class PostCallExpression extends Expression {
19 19
 	public final Expression target;
20 20
 	public final OperatorMember member;
21
+	public final FunctionHeader instancedHeader;
21 22
 	
22 23
 	public PostCallExpression(CodePosition position, Expression target, OperatorMember member, FunctionHeader instancedHeader) {
23
-		super(position, instancedHeader.returnType, binaryThrow(position, member.header.thrownType, target.thrownType));
24
+		super(position, instancedHeader.returnType, binaryThrow(position, instancedHeader.thrownType, target.thrownType));
24 25
 		
25 26
 		if (member.operator != OperatorType.DECREMENT && member.operator != OperatorType.INCREMENT)
26 27
 			throw new IllegalArgumentException("Operator must be increment or decrement");
27 28
 		
28 29
 		this.target = target;
29 30
 		this.member = member;
31
+		this.instancedHeader = instancedHeader;
30 32
 	}
31 33
 
32 34
 	@Override
33 35
 	public <T> T accept(ExpressionVisitor<T> visitor) {
34 36
 		return visitor.visitPostCall(this);
35 37
 	}
38
+
39
+	@Override
40
+	public Expression transform(ExpressionTransformer transformer) {
41
+		Expression tTarget = target.transform(transformer);
42
+		return target == tTarget ? this : new PostCallExpression(position, tTarget, member, instancedHeader);
43
+	}
36 44
 }

+ 15
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/RangeExpression.java Näytä tiedosto

@@ -6,6 +6,7 @@
6 6
 package org.openzen.zenscript.codemodel.expression;
7 7
 
8 8
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
9
+import org.openzen.zenscript.codemodel.type.ITypeID;
9 10
 import org.openzen.zenscript.shared.CodePosition;
10 11
 
11 12
 /**
@@ -22,9 +23,23 @@ public class RangeExpression extends Expression {
22 23
 		this.from = from;
23 24
 		this.to = to;
24 25
 	}
26
+	
27
+	private RangeExpression(CodePosition position, ITypeID type, Expression from, Expression to, ITypeID thrownType) {
28
+		super(position, type, thrownType);
29
+		
30
+		this.from = from;
31
+		this.to = to;
32
+	}
25 33
 
26 34
 	@Override
27 35
 	public <T> T accept(ExpressionVisitor<T> visitor) {
28 36
 		return visitor.visitRange(this);
29 37
 	}
38
+
39
+	@Override
40
+	public Expression transform(ExpressionTransformer transformer) {
41
+		Expression tFrom = from.transform(transformer);
42
+		Expression tTo = to.transform(transformer);
43
+		return tFrom == from && tTo == to ? this : new RangeExpression(position, type, tFrom, tTo, thrownType);
44
+	}
30 45
 }

+ 7
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SameObjectExpression.java Näytä tiedosto

@@ -29,4 +29,11 @@ public class SameObjectExpression extends Expression {
29 29
 	public <T> T accept(ExpressionVisitor<T> visitor) {
30 30
 		return visitor.visitSameObject(this);
31 31
 	}
32
+
33
+	@Override
34
+	public Expression transform(ExpressionTransformer transformer) {
35
+		Expression tLeft = left.transform(transformer);
36
+		Expression tRight = right.transform(transformer);
37
+		return tLeft == left && tRight == right ? this : new SameObjectExpression(position, tLeft, tRight, inverted);
38
+	}
32 39
 }

+ 9
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SetFieldExpression.java Näytä tiedosto

@@ -29,4 +29,13 @@ public class SetFieldExpression extends Expression {
29 29
 	public <T> T accept(ExpressionVisitor<T> visitor) {
30 30
 		return visitor.visitSetField(this);
31 31
 	}
32
+
33
+	@Override
34
+	public Expression transform(ExpressionTransformer transformer) {
35
+		Expression tTarget = target.transform(transformer);
36
+		Expression tValue = value.transform(transformer);
37
+		return tTarget == target && tValue == value
38
+				? this
39
+				: new SetFieldExpression(position, tTarget, field, tValue);
40
+	}
32 41
 }

+ 0
- 0
CodeModel/src/main/java/org/openzen/zenscript/codemodel/expression/SetFunctionParameterExpression.java Näytä tiedosto


Some files were not shown because too many files changed in this diff

Loading…
Peruuta
Tallenna