kindlich 6 роки тому
джерело
коміт
6756905957
Не вдалося знайти GPG ключ що відповідає даному підпису

+ 80
- 20
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Переглянути файл

@@ -1,15 +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.codemodel.statement.StatementVisitor;
4
+import org.openzen.zenscript.codemodel.member.DefinitionMember;
5
+import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
6
+import org.openzen.zenscript.javabytecode.JavaFieldInfo;
7
+import org.openzen.zenscript.javabytecode.JavaMethodInfo;
5 8
 
6 9
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
7 10
 
8
-    final StatementVisitor<?> statementVisitor;
9
-    private final JavaWriter javaWriter;
11
+    private final JavaStatementVisitor statementVisitor;
10 12
 
11
-    public JavaExpressionVisitor(final JavaWriter javaWriter, StatementVisitor<?> statementVisitor) {
12
-        this.javaWriter = javaWriter;
13
+    public JavaExpressionVisitor(JavaStatementVisitor statementVisitor) {
13 14
         this.statementVisitor = statementVisitor;
14 15
     }
15 16
 
@@ -34,7 +35,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
34 35
         for (Expression argument : expression.arguments.arguments) {
35 36
             argument.accept(this);
36 37
         }
37
-        expression.member.accept(new JavaMemberVisitor(javaWriter, this));
38
+        if (!checkAndExecuteByteCodeImplementation(expression.member) && !checkAndExecuteMethodInfo(expression.member))
39
+            throw new IllegalStateException("Call target has no method info!");
38 40
 
39 41
         return null;
40 42
     }
@@ -86,79 +88,79 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
86 88
 
87 89
     @Override
88 90
     public Void visitConstantBool(ConstantBoolExpression expression) {
89
-        javaWriter.constant(expression.value);
91
+        getJavaWriter().constant(expression.value);
90 92
         return null;
91 93
     }
92 94
 
93 95
     @Override
94 96
     public Void visitConstantByte(ConstantByteExpression expression) {
95
-        javaWriter.biPush(expression.value);
97
+        getJavaWriter().biPush(expression.value);
96 98
         return null;
97 99
     }
98 100
 
99 101
     @Override
100 102
     public Void visitConstantChar(ConstantCharExpression expression) {
101
-        javaWriter.constant(expression.value);
103
+        getJavaWriter().constant(expression.value);
102 104
         return null;
103 105
     }
104 106
 
105 107
     @Override
106 108
     public Void visitConstantDouble(ConstantDoubleExpression expression) {
107
-        javaWriter.constant(expression.value);
109
+        getJavaWriter().constant(expression.value);
108 110
         return null;
109 111
     }
110 112
 
111 113
     @Override
112 114
     public Void visitConstantFloat(ConstantFloatExpression expression) {
113
-        javaWriter.constant(expression.value);
115
+        getJavaWriter().constant(expression.value);
114 116
         return null;
115 117
     }
116 118
 
117 119
     @Override
118 120
     public Void visitConstantInt(ConstantIntExpression expression) {
119
-        javaWriter.constant(expression.value);
121
+        getJavaWriter().constant(expression.value);
120 122
         return null;
121 123
     }
122 124
 
123 125
     @Override
124 126
     public Void visitConstantLong(ConstantLongExpression expression) {
125
-        javaWriter.constant(expression.value);
127
+        getJavaWriter().constant(expression.value);
126 128
         return null;
127 129
     }
128 130
 
129 131
     @Override
130 132
     public Void visitConstantSByte(ConstantSByteExpression expression) {
131
-        javaWriter.constant(expression.value);
133
+        getJavaWriter().constant(expression.value);
132 134
         return null;
133 135
     }
134 136
 
135 137
     @Override
136 138
     public Void visitConstantShort(ConstantShortExpression expression) {
137
-        javaWriter.siPush(expression.value);
139
+        getJavaWriter().siPush(expression.value);
138 140
         return null;
139 141
     }
140 142
 
141 143
     @Override
142 144
     public Void visitConstantString(ConstantStringExpression expression) {
143
-        javaWriter.constant(expression.value);
145
+        getJavaWriter().constant(expression.value);
144 146
         return null;
145 147
     }
146 148
 
147 149
     @Override
148 150
     public Void visitConstantUInt(ConstantUIntExpression expression) {
149
-        javaWriter.constant(expression.value);
151
+        getJavaWriter().constant(expression.value);
150 152
         return null;
151 153
     }
152 154
 
153 155
     @Override
154 156
     public Void visitConstantULong(ConstantULongExpression expression) {
155
-        javaWriter.constant(expression.value);
157
+        getJavaWriter().constant(expression.value);
156 158
         return null;
157 159
     }
158 160
 
159 161
     @Override
160 162
     public Void visitConstantUShort(ConstantUShortExpression expression) {
161
-        javaWriter.constant(expression.value);
163
+        getJavaWriter().constant(expression.value);
162 164
         return null;
163 165
     }
164 166
 
@@ -189,6 +191,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
189 191
 
190 192
     @Override
191 193
     public Void visitGetField(GetFieldExpression expression) {
194
+        if (!checkAndExecuteFieldInfo(expression.field, false))
195
+            throw new IllegalStateException("Missing field info on a field member!");
192 196
         return null;
193 197
     }
194 198
 
@@ -204,7 +208,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
204 208
 
205 209
     @Override
206 210
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
207
-        expression.field.accept(new JavaMemberVisitor(javaWriter, this));
211
+        if (!checkAndExecuteFieldInfo(expression.field, true))
212
+            throw new IllegalStateException("Missing field info on a field member!");
208 213
         return null;
209 214
     }
210 215
 
@@ -312,4 +317,59 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
312 317
     public Void visitWrapOptional(WrapOptionalExpression expression) {
313 318
         return null;
314 319
     }
320
+
321
+    public JavaWriter getJavaWriter() {
322
+        return statementVisitor.getJavaWriter();
323
+    }
324
+
325
+
326
+    //Will return true if a JavaBytecodeImplementation.class tag exists, and will compile that tag
327
+    private boolean checkAndExecuteByteCodeImplementation(DefinitionMember member) {
328
+        JavaBytecodeImplementation implementation = member.getTag(JavaBytecodeImplementation.class);
329
+        if (implementation != null) {
330
+            implementation.compile(getJavaWriter());
331
+            return true;
332
+        }
333
+        return false;
334
+    }
335
+
336
+    //Will return true if a JavaMethodInfo.class tag exists, and will compile that tag
337
+    private boolean checkAndExecuteMethodInfo(DefinitionMember member) {
338
+        JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
339
+        if (methodInfo == null)
340
+            return false;
341
+        if (methodInfo.isStatic) {
342
+            getJavaWriter().invokeStatic(
343
+                    methodInfo.javaClass.internalClassName,
344
+                    methodInfo.name,
345
+                    methodInfo.signature);
346
+        } else {
347
+            getJavaWriter().invokeVirtual(
348
+                    methodInfo.javaClass.internalClassName,
349
+                    methodInfo.name,
350
+                    methodInfo.signature);
351
+        }
352
+        return true;
353
+    }
354
+
355
+
356
+    //TODO: Should isStatic go to the fieldInfo or stay here?
357
+    //Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
358
+    private boolean checkAndExecuteFieldInfo(DefinitionMember field, boolean isStatic) {
359
+        JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
360
+        if (fieldInfo == null)
361
+            return false;
362
+        if (isStatic) {
363
+            getJavaWriter().getStaticField(
364
+                    fieldInfo.javaClass.internalClassName,
365
+                    fieldInfo.name,
366
+                    fieldInfo.signature);
367
+        } else {
368
+            getJavaWriter().getField(
369
+                    fieldInfo.javaClass.internalClassName,
370
+                    fieldInfo.name,
371
+                    fieldInfo.signature);
372
+        }
373
+        return true;
374
+    }
315 375
 }

+ 0
- 123
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaMemberVisitor.java Переглянути файл

@@ -1,123 +0,0 @@
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
-}

+ 7
- 1
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java Переглянути файл

@@ -4,10 +4,12 @@ import org.openzen.zenscript.codemodel.statement.*;
4 4
 
5 5
 public class JavaStatementVisitor implements StatementVisitor<Void> {
6 6
     private final JavaWriter javaWriter;
7
+    private final JavaExpressionVisitor expressionVisitor;
7 8
 
8 9
     public JavaStatementVisitor(final JavaWriter javaWriter) {
9 10
 
10 11
         this.javaWriter = javaWriter;
12
+        this.expressionVisitor = new JavaExpressionVisitor( this);
11 13
     }
12 14
 
13 15
     @Override
@@ -40,7 +42,7 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
40 42
 
41 43
     @Override
42 44
     public Void visitExpression(ExpressionStatement statement) {
43
-        statement.expression.accept(new JavaExpressionVisitor(javaWriter, this));
45
+        statement.expression.accept(expressionVisitor);
44 46
         return null;
45 47
     }
46 48
 
@@ -92,4 +94,8 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
92 94
         javaWriter.ret();
93 95
         javaWriter.end();
94 96
     }
97
+
98
+    public JavaWriter getJavaWriter() {
99
+        return javaWriter;
100
+    }
95 101
 }

Завантаження…
Відмінити
Зберегти