Browse Source

Removed Membervisitor

kindlich 6 years ago
parent
commit
6756905957
No known key found for this signature in database

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

1
 package org.openzen.zenscript.javabytecode.compiler;
1
 package org.openzen.zenscript.javabytecode.compiler;
2
 
2
 
3
 import org.openzen.zenscript.codemodel.expression.*;
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
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
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
         this.statementVisitor = statementVisitor;
14
         this.statementVisitor = statementVisitor;
14
     }
15
     }
15
 
16
 
34
         for (Expression argument : expression.arguments.arguments) {
35
         for (Expression argument : expression.arguments.arguments) {
35
             argument.accept(this);
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
         return null;
41
         return null;
40
     }
42
     }
86
 
88
 
87
     @Override
89
     @Override
88
     public Void visitConstantBool(ConstantBoolExpression expression) {
90
     public Void visitConstantBool(ConstantBoolExpression expression) {
89
-        javaWriter.constant(expression.value);
91
+        getJavaWriter().constant(expression.value);
90
         return null;
92
         return null;
91
     }
93
     }
92
 
94
 
93
     @Override
95
     @Override
94
     public Void visitConstantByte(ConstantByteExpression expression) {
96
     public Void visitConstantByte(ConstantByteExpression expression) {
95
-        javaWriter.biPush(expression.value);
97
+        getJavaWriter().biPush(expression.value);
96
         return null;
98
         return null;
97
     }
99
     }
98
 
100
 
99
     @Override
101
     @Override
100
     public Void visitConstantChar(ConstantCharExpression expression) {
102
     public Void visitConstantChar(ConstantCharExpression expression) {
101
-        javaWriter.constant(expression.value);
103
+        getJavaWriter().constant(expression.value);
102
         return null;
104
         return null;
103
     }
105
     }
104
 
106
 
105
     @Override
107
     @Override
106
     public Void visitConstantDouble(ConstantDoubleExpression expression) {
108
     public Void visitConstantDouble(ConstantDoubleExpression expression) {
107
-        javaWriter.constant(expression.value);
109
+        getJavaWriter().constant(expression.value);
108
         return null;
110
         return null;
109
     }
111
     }
110
 
112
 
111
     @Override
113
     @Override
112
     public Void visitConstantFloat(ConstantFloatExpression expression) {
114
     public Void visitConstantFloat(ConstantFloatExpression expression) {
113
-        javaWriter.constant(expression.value);
115
+        getJavaWriter().constant(expression.value);
114
         return null;
116
         return null;
115
     }
117
     }
116
 
118
 
117
     @Override
119
     @Override
118
     public Void visitConstantInt(ConstantIntExpression expression) {
120
     public Void visitConstantInt(ConstantIntExpression expression) {
119
-        javaWriter.constant(expression.value);
121
+        getJavaWriter().constant(expression.value);
120
         return null;
122
         return null;
121
     }
123
     }
122
 
124
 
123
     @Override
125
     @Override
124
     public Void visitConstantLong(ConstantLongExpression expression) {
126
     public Void visitConstantLong(ConstantLongExpression expression) {
125
-        javaWriter.constant(expression.value);
127
+        getJavaWriter().constant(expression.value);
126
         return null;
128
         return null;
127
     }
129
     }
128
 
130
 
129
     @Override
131
     @Override
130
     public Void visitConstantSByte(ConstantSByteExpression expression) {
132
     public Void visitConstantSByte(ConstantSByteExpression expression) {
131
-        javaWriter.constant(expression.value);
133
+        getJavaWriter().constant(expression.value);
132
         return null;
134
         return null;
133
     }
135
     }
134
 
136
 
135
     @Override
137
     @Override
136
     public Void visitConstantShort(ConstantShortExpression expression) {
138
     public Void visitConstantShort(ConstantShortExpression expression) {
137
-        javaWriter.siPush(expression.value);
139
+        getJavaWriter().siPush(expression.value);
138
         return null;
140
         return null;
139
     }
141
     }
140
 
142
 
141
     @Override
143
     @Override
142
     public Void visitConstantString(ConstantStringExpression expression) {
144
     public Void visitConstantString(ConstantStringExpression expression) {
143
-        javaWriter.constant(expression.value);
145
+        getJavaWriter().constant(expression.value);
144
         return null;
146
         return null;
145
     }
147
     }
146
 
148
 
147
     @Override
149
     @Override
148
     public Void visitConstantUInt(ConstantUIntExpression expression) {
150
     public Void visitConstantUInt(ConstantUIntExpression expression) {
149
-        javaWriter.constant(expression.value);
151
+        getJavaWriter().constant(expression.value);
150
         return null;
152
         return null;
151
     }
153
     }
152
 
154
 
153
     @Override
155
     @Override
154
     public Void visitConstantULong(ConstantULongExpression expression) {
156
     public Void visitConstantULong(ConstantULongExpression expression) {
155
-        javaWriter.constant(expression.value);
157
+        getJavaWriter().constant(expression.value);
156
         return null;
158
         return null;
157
     }
159
     }
158
 
160
 
159
     @Override
161
     @Override
160
     public Void visitConstantUShort(ConstantUShortExpression expression) {
162
     public Void visitConstantUShort(ConstantUShortExpression expression) {
161
-        javaWriter.constant(expression.value);
163
+        getJavaWriter().constant(expression.value);
162
         return null;
164
         return null;
163
     }
165
     }
164
 
166
 
189
 
191
 
190
     @Override
192
     @Override
191
     public Void visitGetField(GetFieldExpression expression) {
193
     public Void visitGetField(GetFieldExpression expression) {
194
+        if (!checkAndExecuteFieldInfo(expression.field, false))
195
+            throw new IllegalStateException("Missing field info on a field member!");
192
         return null;
196
         return null;
193
     }
197
     }
194
 
198
 
204
 
208
 
205
     @Override
209
     @Override
206
     public Void visitGetStaticField(GetStaticFieldExpression expression) {
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
         return null;
213
         return null;
209
     }
214
     }
210
 
215
 
312
     public Void visitWrapOptional(WrapOptionalExpression expression) {
317
     public Void visitWrapOptional(WrapOptionalExpression expression) {
313
         return null;
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 View File

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 View File

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

Loading…
Cancel
Save