Преглед изворни кода

Added MemberVisitor and moved some calls there

kindlich пре 6 година
родитељ
комит
f02581f3f2
No known key found for this signature in database

+ 7
- 36
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Прегледај датотеку

@@ -1,16 +1,16 @@
1 1
 package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3 3
 import org.openzen.zenscript.codemodel.expression.*;
4
-import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
5
-import org.openzen.zenscript.javabytecode.JavaFieldInfo;
6
-import org.openzen.zenscript.javabytecode.JavaMethodInfo;
4
+import org.openzen.zenscript.codemodel.statement.StatementVisitor;
7 5
 
8 6
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
9 7
 
8
+    final StatementVisitor<?> statementVisitor;
10 9
     private final JavaWriter javaWriter;
11 10
 
12
-    public JavaExpressionVisitor(final JavaWriter javaWriter) {
11
+    public JavaExpressionVisitor(final JavaWriter javaWriter, StatementVisitor<?> statementVisitor) {
13 12
         this.javaWriter = javaWriter;
13
+        this.statementVisitor = statementVisitor;
14 14
     }
15 15
 
16 16
     @Override
@@ -34,29 +34,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
34 34
         for (Expression argument : expression.arguments.arguments) {
35 35
             argument.accept(this);
36 36
         }
37
-		
38
-		JavaBytecodeImplementation implementation = expression.member.getTag(JavaBytecodeImplementation.class);
39
-		if (implementation != null) {
40
-			implementation.compile(javaWriter);
41
-			return null;
42
-		}
43
-		
44
-		JavaMethodInfo methodInfo = expression.member.getTag(JavaMethodInfo.class);
45
-		if (methodInfo == null)
46
-			throw new IllegalStateException("Call target has no method info!");
47
-		
48
-		if (methodInfo.isStatic) {
49
-			javaWriter.invokeStatic(
50
-					methodInfo.javaClass.internalClassName,
51
-					methodInfo.name,
52
-					methodInfo.signature);
53
-		} else {
54
-			javaWriter.invokeVirtual(
55
-					methodInfo.javaClass.internalClassName,
56
-					methodInfo.name,
57
-					methodInfo.signature);
58
-		}
59
-		
37
+        expression.member.accept(new JavaMemberVisitor(javaWriter, this));
38
+
60 39
         return null;
61 40
     }
62 41
 
@@ -225,15 +204,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
225 204
 
226 205
     @Override
227 206
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
228
-		JavaFieldInfo fieldInfo = expression.field.getTag(JavaFieldInfo.class);
229
-		if (fieldInfo == null)
230
-			throw new IllegalStateException("Missing field info on a field member!");
231
-		
232
-		javaWriter.getStaticField(
233
-				fieldInfo.javaClass.internalClassName,
234
-				fieldInfo.name,
235
-				fieldInfo.signature);
236
-		
207
+        expression.field.accept(new JavaMemberVisitor(javaWriter, this));
237 208
         return null;
238 209
     }
239 210
 

+ 123
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMemberVisitor.java Прегледај датотеку

@@ -0,0 +1,123 @@
1
+package org.openzen.zenscript.javabytecode.compiler;
2
+
3
+import org.openzen.zenscript.codemodel.member.*;
4
+import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
5
+import org.openzen.zenscript.javabytecode.JavaFieldInfo;
6
+import org.openzen.zenscript.javabytecode.JavaMethodInfo;
7
+
8
+public class JavaMemberVisitor implements MemberVisitor<Void> {
9
+
10
+    private final JavaWriter javaWriter;
11
+    private final JavaExpressionVisitor expressionVisitor;
12
+
13
+    public JavaMemberVisitor(JavaWriter javaWriter, JavaExpressionVisitor expressionVisitor) {
14
+        this.javaWriter = javaWriter;
15
+        this.expressionVisitor = expressionVisitor;
16
+    }
17
+
18
+
19
+    @Override
20
+    public Void visitField(FieldMember member) {
21
+        JavaFieldInfo fieldInfo = member.getTag(JavaFieldInfo.class);
22
+        if (fieldInfo == null)
23
+            throw new IllegalStateException("Missing field info on a field member!");
24
+
25
+        javaWriter.getStaticField(
26
+                fieldInfo.javaClass.internalClassName,
27
+                fieldInfo.name,
28
+                fieldInfo.signature);
29
+        return null;
30
+    }
31
+
32
+    @Override
33
+    public Void visitConstructor(ConstructorMember member) {
34
+        return null;
35
+    }
36
+
37
+    @Override
38
+    public Void visitMethod(MethodMember member) {
39
+        if(!checkAndExecuteByteCodeImplementation(member) && !checkAndExecuteMethodInfo(member))
40
+            throw new IllegalStateException("Call target has no method info!");
41
+
42
+        return null;
43
+    }
44
+
45
+    @Override
46
+    public Void visitGetter(GetterMember member) {
47
+        return null;
48
+    }
49
+
50
+    @Override
51
+    public Void visitSetter(SetterMember member) {
52
+        return null;
53
+    }
54
+
55
+    @Override
56
+    public Void visitEnumConstant(EnumConstantMember member) {
57
+        return null;
58
+    }
59
+
60
+    @Override
61
+    public Void visitOperator(OperatorMember member) {
62
+        if(!checkAndExecuteByteCodeImplementation(member) && !checkAndExecuteMethodInfo(member))
63
+            throw new IllegalStateException("Call target has no method info!");
64
+        return null;
65
+    }
66
+
67
+    @Override
68
+    public Void visitCaster(CasterMember member) {
69
+        if(!checkAndExecuteByteCodeImplementation(member) && !checkAndExecuteMethodInfo(member))
70
+            throw new IllegalStateException("Call target has no method info!");
71
+        return null;
72
+    }
73
+
74
+    @Override
75
+    public Void visitCustomIterator(CustomIteratorMember member) {
76
+        return null;
77
+    }
78
+
79
+    @Override
80
+    public Void visitCaller(CallerMember member) {
81
+        if(!checkAndExecuteByteCodeImplementation(member) && !checkAndExecuteMethodInfo(member))
82
+            throw new IllegalStateException("Call target has no method info!");
83
+        return null;
84
+    }
85
+
86
+    @Override
87
+    public Void visitImplementation(ImplementationMember member) {
88
+        return null;
89
+    }
90
+
91
+    @Override
92
+    public Void visitInnerDefinition(InnerDefinitionMember member) {
93
+        return null;
94
+    }
95
+
96
+
97
+    private boolean checkAndExecuteByteCodeImplementation(DefinitionMember member) {
98
+        JavaBytecodeImplementation implementation = member.getTag(JavaBytecodeImplementation.class);
99
+        if (implementation != null) {
100
+            implementation.compile(javaWriter);
101
+            return true;
102
+        }
103
+        return false;
104
+    }
105
+
106
+    private boolean checkAndExecuteMethodInfo(DefinitionMember member) {
107
+        JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
108
+        if (methodInfo == null)
109
+            return false;
110
+        if (methodInfo.isStatic) {
111
+            javaWriter.invokeStatic(
112
+                    methodInfo.javaClass.internalClassName,
113
+                    methodInfo.name,
114
+                    methodInfo.signature);
115
+        } else {
116
+            javaWriter.invokeVirtual(
117
+                    methodInfo.javaClass.internalClassName,
118
+                    methodInfo.name,
119
+                    methodInfo.signature);
120
+        }
121
+        return true;
122
+    }
123
+}

+ 4
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java Прегледај датотеку

@@ -12,6 +12,9 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
12 12
 
13 13
     @Override
14 14
     public Void visitBlock(BlockStatement statement) {
15
+        for (Statement statement1 : statement.statements) {
16
+            statement1.accept(this);
17
+        }
15 18
         return null;
16 19
     }
17 20
 
@@ -37,7 +40,7 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
37 40
 
38 41
     @Override
39 42
     public Void visitExpression(ExpressionStatement statement) {
40
-        statement.expression.accept(new JavaExpressionVisitor(javaWriter));
43
+        statement.expression.accept(new JavaExpressionVisitor(javaWriter, this));
41 44
         return null;
42 45
     }
43 46
 

+ 1
- 0
ScriptingExample/scripts/moreHellos.zs Прегледај датотеку

@@ -3,3 +3,4 @@ println("Hello world!");
3 3
 println("Hello world!");
4 4
 println("Hello world!");
5 5
 println("Hello world!");
6
+println(1 as string);

+ 266
- 0
Shared/out/production/resources/org/openzen/zenscript/shared/characterEntities.properties Прегледај датотеку

@@ -0,0 +1,266 @@
1
+quot=34
2
+amp=38
3
+apos=39
4
+lt=60
5
+gt=62
6
+
7
+nbsp=160
8
+iexcl=161
9
+cent=162
10
+pound=163
11
+curren=164
12
+yen=165
13
+brvbar=166
14
+sect=167
15
+uml=168
16
+copy=169
17
+ordf=170
18
+laquo=171
19
+not=172
20
+shy=173
21
+reg=174
22
+macr=175
23
+deg=176
24
+plusmn=177
25
+sup2=178
26
+sup3=179
27
+acute=180
28
+micro=181
29
+para=182
30
+middot=183
31
+cedil=184
32
+sup1=185
33
+ordm=186
34
+raquo=187
35
+frac14=188
36
+frac12=189
37
+frac34=190
38
+iquest=191
39
+
40
+Agrave=192
41
+Aacute=193
42
+Acirc=194
43
+Atilde=195
44
+Auml=196
45
+Aring=197
46
+AElig=198
47
+Ccedil=199
48
+Egrave=200
49
+Eacute=201
50
+Ecirc=202
51
+Euml=203
52
+lgrave=204
53
+lacute=205
54
+lcirc=206
55
+luml=207
56
+ETH=208
57
+Ntilde=209
58
+Ograve=210
59
+Oacute=211
60
+Ocirc=212
61
+Otilde=213
62
+Ouml=214
63
+times=215
64
+Oslash=216
65
+Ugrave=217
66
+Uacute=218
67
+Ucirc=219
68
+Uuml=220
69
+Yacute=221
70
+THORN=222
71
+szlig=223
72
+agrave=224
73
+aacute=225
74
+acirc=226
75
+atilde=227
76
+auml=228
77
+aring=229
78
+aelig=230
79
+ccedil=231
80
+egrave=232
81
+eacute=233
82
+ecirc=234
83
+euml=235
84
+igrave=236
85
+iacute=237
86
+icirc=238
87
+iuml=239
88
+eth=240
89
+ntilde=241
90
+ograve=242
91
+oacute=243
92
+ocirc=244
93
+otilde=245
94
+ouml=246
95
+divide=247
96
+oslash=248
97
+ugrave=249
98
+uacute=250
99
+ucirc=251
100
+uuml=252
101
+yacute=253
102
+thorn=254
103
+yuml=255
104
+
105
+OElig=338
106
+oelig=339
107
+Scaron=352
108
+scaron=353
109
+Yuml=376
110
+
111
+fnof=402
112
+
113
+circ=710
114
+tilde=732
115
+
116
+Alpha=913
117
+Beta=914
118
+Gamma=915
119
+Delta=916
120
+Epsilon=917
121
+Zeta=918
122
+Eta=919
123
+Theta=920
124
+Iota=921
125
+Kappa=922
126
+Lambda=923
127
+Mu=924
128
+Nu=925
129
+Xi=926
130
+Omicron=927
131
+Pi=928
132
+Rho=929
133
+Sigma=931
134
+Tau=932
135
+Upsilon=933
136
+Phi=934
137
+Chi=935
138
+Psi=936
139
+Omega=937
140
+
141
+alpha=945
142
+beta=946
143
+gamma=947
144
+delta=948
145
+epsilon=949
146
+zeta=950
147
+eta=951
148
+theta=952
149
+iota=953
150
+kappa=954
151
+lambda=955
152
+mu=956
153
+nu=957
154
+xi=958
155
+omicron=959
156
+pi=960
157
+rho=961
158
+sigmaf=962
159
+sigma=963
160
+tau=964
161
+upsilon=965
162
+phi=966
163
+chi=967
164
+psi=968
165
+omega=969
166
+thetasym=977
167
+upsih=978
168
+piv=982
169
+
170
+ensp=8194
171
+emsp=8195
172
+thinsp=8201
173
+zwnj=8204
174
+zwj=8205
175
+lrm=8206
176
+rlm=8207
177
+ndash=8211
178
+mdash=8212
179
+lsquo=8216
180
+rsquo=8217
181
+sbquo=8218
182
+ldquo=8220
183
+rdquo=8221
184
+bdquo=8222
185
+dagger=8224
186
+Dagger=8225
187
+bull=8226
188
+hellip=8230
189
+permil=8240
190
+prime=8242
191
+Prime=8243
192
+lsaquo=8249
193
+rsaquo=8250
194
+oline=8254
195
+frasl=8260
196
+
197
+euro=8364
198
+
199
+image=8465
200
+weierp=8472
201
+real=8476
202
+trade=8482
203
+alefsym=8501
204
+
205
+larr=8592
206
+uarr=8593
207
+rarr=8594
208
+darr=8595
209
+harr=8596
210
+crarr=8629
211
+lArr=8656
212
+uArr=8657
213
+rArr=8658
214
+dArr=8659
215
+hArr=8660
216
+
217
+forall=8704
218
+part=8706
219
+exist=8707
220
+empty=8709
221
+nabla=8711
222
+isin=8712
223
+notin=8713
224
+ni=8715
225
+prod=8719
226
+sum=8721
227
+minus=8722
228
+lowast=8727
229
+radic=8730
230
+prop=8733
231
+infin=8734
232
+ang=8736
233
+and=8743
234
+or=8744
235
+cap=8745
236
+cup=8746
237
+int=8747
238
+there4=8756
239
+sim=8764
240
+cong=8773
241
+asymp=8776
242
+ne=8800
243
+equiv=8801
244
+le=8804
245
+ge=8805
246
+sub=8834
247
+sup=8835
248
+nsub=8836
249
+sube=8838
250
+supe=8839
251
+oplus=8853
252
+otimes=8855
253
+perp=8869
254
+sdot=8901
255
+
256
+lceil=8968
257
+rceil=8969
258
+lfloor=8970
259
+rfloor=8971
260
+lang=9001
261
+rang=9002
262
+loz=9674
263
+spades=9824
264
+clubs=8927
265
+hearts=9829
266
+diams=9830

+ 19
- 10
Shared/src/main/java/org/openzen/zenscript/shared/TagDictionary.java Прегледај датотеку

@@ -5,21 +5,30 @@
5 5
  */
6 6
 package org.openzen.zenscript.shared;
7 7
 
8
+import java.util.Collections;
8 9
 import java.util.HashMap;
9 10
 import java.util.Map;
10 11
 
11 12
 /**
12
- *
13 13
  * @author Hoofdgebruiker
14 14
  */
15
+@SuppressWarnings("unchecked")
15 16
 public class TagDictionary {
16
-	private final Map<Class<?>, Object> tags = new HashMap<>();
17
-	
18
-	public <T> void put(Class<T> cls, T tag) {
19
-		tags.put(cls, tag);
20
-	}
21
-	
22
-	public <T> T get(Class<T> cls) {
23
-		return (T) tags.get(cls);
24
-	}
17
+    private Map<Class<?>, Object> tags = Collections.EMPTY_MAP;
18
+
19
+    public <T> void put(Class<T> cls, T tag) {
20
+        if (tags == Collections.EMPTY_MAP) {
21
+            tags = Collections.singletonMap(cls, tag);
22
+        } else if (tags.size() == 1) {
23
+            Map<Class<?>, Object> newTags = new HashMap<>(tags);
24
+            newTags.put(cls, tag);
25
+            this.tags = newTags;
26
+        } else {
27
+            tags.put(cls, tag);
28
+        }
29
+    }
30
+
31
+    public <T> T get(Class<T> cls) {
32
+        return (T) tags.get(cls);
33
+    }
25 34
 }

Loading…
Откажи
Сачувај