ソースを参照

Made Helloworld possible

kindlich 6年前
コミット
d1b566dc5a
この署名に対応する既知のキーがデータベースに存在しません

+ 1
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/member/GetterMember.java ファイルの表示

@@ -33,7 +33,7 @@ public class GetterMember extends FunctionalMember implements IGettableMember {
33 33
 		this.name = name;
34 34
 		this.type = type;
35 35
 	}
36
-	
36
+
37 37
 	@Override
38 38
 	public String getName() {
39 39
 		return name;

バイナリ
ScriptingExample/Scripts.class ファイルの表示


+ 1
- 0
ScriptingExample/build.gradle ファイルの表示

@@ -14,5 +14,6 @@ if (!hasProperty('mainClass')) {
14 14
 }
15 15
 
16 16
 dependencies {
17
+	compile 'org.ow2.asm:asm-debug-all:6.0_BETA'
17 18
 	compile project(':Parser')
18 19
 }

+ 5
- 0
ScriptingExample/scripts/moreHellos.zs ファイルの表示

@@ -0,0 +1,5 @@
1
+println("Hello world!");
2
+println("Hello world!");
3
+println("Hello world!");
4
+println("Hello world!");
5
+println("Hello world!");

+ 1
- 1
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/GlobalRegistry.java ファイルの表示

@@ -66,7 +66,7 @@ public class GlobalRegistry {
66 66
 		return globals;
67 67
 	}
68 68
 	
69
-	private static final ClassDefinition printStream = new ClassDefinition("PrintStream", Modifiers.MODIFIER_EXPORT, null);
69
+	private static final ClassDefinition printStream = new ClassDefinition("Ljava/io/PrintStream;", Modifiers.MODIFIER_EXPORT, null);
70 70
 	private static final NativeMethodMember printStreamPrintln = new NativeMethodMember(
71 71
 			Modifiers.MODIFIER_EXPORT,
72 72
 			"println",

+ 19
- 1
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/JavaModule.java ファイルの表示

@@ -5,6 +5,7 @@
5 5
  */
6 6
 package org.openzen.zenscript.scriptingexample;
7 7
 
8
+import java.io.*;
8 9
 import java.lang.reflect.InvocationTargetException;
9 10
 import java.util.HashMap;
10 11
 import java.util.Map;
@@ -24,6 +25,11 @@ public class JavaModule {
24 25
 	
25 26
 	public void register(String classname, byte[] bytecode) {
26 27
 		classes.put(classname, bytecode);
28
+		try(FileOutputStream writer = new FileOutputStream(new File(classname + ".class"))) {
29
+			writer.write(bytecode);
30
+		} catch (IOException e) {
31
+			e.printStackTrace();
32
+		}
27 33
 	}
28 34
 	
29 35
 	public void execute() {
@@ -47,6 +53,18 @@ public class JavaModule {
47 53
 	}
48 54
 	
49 55
 	private class ScriptClassLoader extends ClassLoader {
50
-		
56
+		final Map<String, Class> customClasses = new HashMap<>();
57
+
58
+		@Override
59
+		public Class<?> loadClass(String name) throws ClassNotFoundException {
60
+			if(customClasses.containsKey(name))
61
+				return customClasses.get(name);
62
+			if(classes.containsKey(name)) {
63
+				final byte[] bytes = classes.get(name);
64
+				customClasses.put(name, defineClass(name, bytes, 0, bytes.length, null));
65
+				return customClasses.get(name);
66
+			}
67
+			return super.loadClass(name);
68
+		}
51 69
 	}
52 70
 }

+ 35
- 4
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/Main.java ファイルの表示

@@ -5,22 +5,31 @@ import java.io.IOException;
5 5
 import java.util.ArrayList;
6 6
 import java.util.List;
7 7
 import java.util.Map;
8
+import java.util.Optional;
9
+
10
+import org.objectweb.asm.ClassWriter;
11
+import org.objectweb.asm.Opcodes;
8 12
 import org.openzen.zenscript.codemodel.HighLevelDefinition;
9 13
 import org.openzen.zenscript.codemodel.PackageDefinitions;
10 14
 import org.openzen.zenscript.codemodel.ScriptBlock;
11 15
 import org.openzen.zenscript.codemodel.definition.ExpansionDefinition;
12 16
 import org.openzen.zenscript.codemodel.definition.ZSPackage;
17
+import org.openzen.zenscript.codemodel.statement.Statement;
18
+import org.openzen.zenscript.codemodel.statement.StatementVisitor;
13 19
 import org.openzen.zenscript.codemodel.type.GlobalTypeRegistry;
14 20
 import org.openzen.zenscript.linker.symbol.ISymbol;
15 21
 import org.openzen.zenscript.parser.ParsedFile;
22
+import org.openzen.zenscript.scriptingexample.writer.JavaStatementVisitor;
23
+import org.openzen.zenscript.scriptingexample.writer.JavaWriter;
16 24
 
17 25
 public class Main {
18 26
     /**
19 27
      * @param args the command line arguments
20 28
      */
21 29
     public static void main(String[] args) throws IOException {
30
+		System.out.println();
22 31
 		File inputDirectory = new File("scripts");
23
-		File[] inputFiles = inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"));
32
+		File[] inputFiles = Optional.ofNullable(inputDirectory.listFiles((dir, name) -> name.endsWith(".zs"))).orElseGet(() -> new File[0]);
24 33
 		
25 34
 		ParsedFile[] parsedFiles = parse(inputFiles);
26 35
 		
@@ -77,19 +86,41 @@ public class Main {
77 86
 	
78 87
 	private static JavaModule compileSemanticToJava(SemanticModule module) {
79 88
 		JavaModule result = new JavaModule();
80
-		
89
+		final ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
90
+		classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Scripts", null, "java/lang/Object", null);
91
+
81 92
 		// TODO: java bytecode compilation
82 93
 		for (HighLevelDefinition definition : module.definitions.getAll()) {
83 94
 			// convert definitions into java classes
95
+
84 96
 		}
85
-		
97
+		int statementNo = 0;
86 98
 		for (ScriptBlock script : module.scripts) {
87 99
 			// convert scripts into methods (add them to a Scripts class?)
88 100
 			// (TODO: can we break very long scripts into smaller methods? for the extreme scripts)
101
+			final JavaStatementVisitor statementVisitor = new JavaStatementVisitor(new JavaWriter(classWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, makeName(statementNo++), "()V", null, null));
102
+			statementVisitor.start();
103
+			for (Statement statement : script.statements) {
104
+				statement.accept(statementVisitor);
105
+			}
106
+			statementVisitor.end();
89 107
 		}
90
-		
108
+
91 109
 		// create a Scripts.run() method to run all scripts compiled above
110
+		final JavaWriter runWriter = new JavaWriter(classWriter, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "run", "()V", null, null);
111
+		runWriter.start();
112
+		for (int i = 0; i < statementNo; i++) {
113
+			runWriter.invokeStatic("Scripts", makeName(i), "()V");
114
+		}
115
+		runWriter.ret();
116
+		runWriter.end();
117
+		result.register("Scripts", classWriter.toByteArray());
118
+
92 119
 		
93 120
 		return result;
94 121
 	}
122
+
123
+	private static String makeName(int i) {
124
+    	return "scriptBlock" + i;
125
+	}
95 126
 }

+ 320
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaExpressionVisitor.java ファイルの表示

@@ -0,0 +1,320 @@
1
+package org.openzen.zenscript.scriptingexample.writer;
2
+
3
+import org.objectweb.asm.Type;
4
+import org.openzen.zenscript.codemodel.expression.*;
5
+import org.openzen.zenscript.scriptingexample.NativeFieldMember;
6
+import org.openzen.zenscript.scriptingexample.NativeMethodMember;
7
+
8
+public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
9
+
10
+    private final JavaWriter javaWriter;
11
+
12
+    public JavaExpressionVisitor(final JavaWriter javaWriter) {
13
+        this.javaWriter = javaWriter;
14
+    }
15
+
16
+    @Override
17
+    public Void visitAndAnd(AndAndExpression expression) {
18
+        return null;
19
+    }
20
+
21
+    @Override
22
+    public Void visitArray(ArrayExpression expression) {
23
+        return null;
24
+    }
25
+
26
+    @Override
27
+    public Void visitCompare(BasicCompareExpression expression) {
28
+        return null;
29
+    }
30
+
31
+    @Override
32
+    public Void visitCall(CallExpression expression) {
33
+
34
+        expression.target.accept(this);
35
+        for (Expression argument : expression.arguments.arguments) {
36
+            argument.accept(this);
37
+        }
38
+        if (expression.member instanceof NativeMethodMember) {
39
+            javaWriter.invokeVirtual(((NativeMethodMember) expression.member).className.replaceFirst("L", "").replace(";", ""), ((NativeMethodMember) expression.member).name, ((NativeMethodMember) expression.member).signature);
40
+        }
41
+        return null;
42
+    }
43
+
44
+    @Override
45
+    public Void visitCallStatic(CallStaticExpression expression) {
46
+        return null;
47
+    }
48
+
49
+    @Override
50
+    public Void visitCapturedClosure(CapturedClosureExpression expression) {
51
+        return null;
52
+    }
53
+
54
+    @Override
55
+    public Void visitCapturedDirect(CapturedDirectExpression expression) {
56
+        return null;
57
+    }
58
+
59
+    @Override
60
+    public Void visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
61
+        return null;
62
+    }
63
+
64
+    @Override
65
+    public Void visitCapturedParameter(CapturedParameterExpression expression) {
66
+        return null;
67
+    }
68
+
69
+    @Override
70
+    public Void visitCapturedThis(CapturedThisExpression expression) {
71
+        return null;
72
+    }
73
+
74
+    @Override
75
+    public Void visitCheckNull(CheckNullExpression expression) {
76
+        return null;
77
+    }
78
+
79
+    @Override
80
+    public Void visitCoalesce(CoalesceExpression expression) {
81
+        return null;
82
+    }
83
+
84
+    @Override
85
+    public Void visitConditional(ConditionalExpression expression) {
86
+        return null;
87
+    }
88
+
89
+    @Override
90
+    public Void visitConstantBool(ConstantBoolExpression expression) {
91
+        javaWriter.constant(expression.value);
92
+        return null;
93
+    }
94
+
95
+    @Override
96
+    public Void visitConstantByte(ConstantByteExpression expression) {
97
+        javaWriter.biPush(expression.value);
98
+        return null;
99
+    }
100
+
101
+    @Override
102
+    public Void visitConstantChar(ConstantCharExpression expression) {
103
+        javaWriter.constant(expression.value);
104
+        return null;
105
+    }
106
+
107
+    @Override
108
+    public Void visitConstantDouble(ConstantDoubleExpression expression) {
109
+        javaWriter.constant(expression.value);
110
+        return null;
111
+    }
112
+
113
+    @Override
114
+    public Void visitConstantFloat(ConstantFloatExpression expression) {
115
+        javaWriter.constant(expression.value);
116
+        return null;
117
+    }
118
+
119
+    @Override
120
+    public Void visitConstantInt(ConstantIntExpression expression) {
121
+        javaWriter.constant(expression.value);
122
+        return null;
123
+    }
124
+
125
+    @Override
126
+    public Void visitConstantLong(ConstantLongExpression expression) {
127
+        javaWriter.constant(expression.value);
128
+        return null;
129
+    }
130
+
131
+    @Override
132
+    public Void visitConstantSByte(ConstantSByteExpression expression) {
133
+        javaWriter.constant(expression.value);
134
+        return null;
135
+    }
136
+
137
+    @Override
138
+    public Void visitConstantShort(ConstantShortExpression expression) {
139
+        javaWriter.siPush(expression.value);
140
+        return null;
141
+    }
142
+
143
+    @Override
144
+    public Void visitConstantString(ConstantStringExpression expression) {
145
+        javaWriter.constant(expression.value);
146
+        return null;
147
+    }
148
+
149
+    @Override
150
+    public Void visitConstantUInt(ConstantUIntExpression expression) {
151
+        javaWriter.constant(expression.value);
152
+        return null;
153
+    }
154
+
155
+    @Override
156
+    public Void visitConstantULong(ConstantULongExpression expression) {
157
+        javaWriter.constant(expression.value);
158
+        return null;
159
+    }
160
+
161
+    @Override
162
+    public Void visitConstantUShort(ConstantUShortExpression expression) {
163
+        javaWriter.constant(expression.value);
164
+        return null;
165
+    }
166
+
167
+    @Override
168
+    public Void visitConstructorCall(ConstructorCallExpression expression) {
169
+        return null;
170
+    }
171
+
172
+    @Override
173
+    public Void visitEnumConstant(EnumConstantExpression expression) {
174
+        return null;
175
+    }
176
+
177
+    @Override
178
+    public Void visitEquals(EqualsExpression expression) {
179
+        return null;
180
+    }
181
+
182
+    @Override
183
+    public Void visitFunction(FunctionExpression expression) {
184
+        return null;
185
+    }
186
+
187
+    @Override
188
+    public Void visitGenericCompare(GenericCompareExpression expression) {
189
+        return null;
190
+    }
191
+
192
+    @Override
193
+    public Void visitGetField(GetFieldExpression expression) {
194
+        return null;
195
+    }
196
+
197
+    @Override
198
+    public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
199
+        return null;
200
+    }
201
+
202
+    @Override
203
+    public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
204
+        return null;
205
+    }
206
+
207
+    @Override
208
+    public Void visitGetStaticField(GetStaticFieldExpression expression) {
209
+
210
+        if (expression.field instanceof NativeFieldMember)
211
+
212
+            javaWriter.getStaticField(((NativeFieldMember) expression.field).className, expression.field.name, Type.getType(expression.type.accept(new JavaTypeVisitor())).getDescriptor());
213
+        return null;
214
+    }
215
+
216
+    @Override
217
+    public Void visitGetter(GetterExpression expression) {
218
+        return null;
219
+    }
220
+
221
+    @Override
222
+    public Void visitInterfaceCast(InterfaceCastExpression expression) {
223
+        return null;
224
+    }
225
+
226
+    @Override
227
+    public Void visitIs(IsExpression expression) {
228
+        return null;
229
+    }
230
+
231
+    @Override
232
+    public Void visitMakeConst(MakeConstExpression expression) {
233
+        return null;
234
+    }
235
+
236
+    @Override
237
+    public Void visitMap(MapExpression expression) {
238
+        return null;
239
+    }
240
+
241
+    @Override
242
+    public Void visitNew(NewExpression expression) {
243
+        return null;
244
+    }
245
+
246
+    @Override
247
+    public Void visitNot(NotExpression expression) {
248
+        return null;
249
+    }
250
+
251
+    @Override
252
+    public Void visitNull(NullExpression expression) {
253
+        return null;
254
+    }
255
+
256
+    @Override
257
+    public Void visitOrOr(OrOrExpression expression) {
258
+        return null;
259
+    }
260
+
261
+    @Override
262
+    public Void visitRange(RangeExpression expression) {
263
+        return null;
264
+    }
265
+
266
+    @Override
267
+    public Void visitSetField(SetFieldExpression expression) {
268
+        return null;
269
+    }
270
+
271
+    @Override
272
+    public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
273
+        return null;
274
+    }
275
+
276
+    @Override
277
+    public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
278
+        return null;
279
+    }
280
+
281
+    @Override
282
+    public Void visitSetStaticField(SetStaticFieldExpression expression) {
283
+        return null;
284
+    }
285
+
286
+    @Override
287
+    public Void visitSetter(SetterExpression expression) {
288
+        return null;
289
+    }
290
+
291
+    @Override
292
+    public Void visitStaticGetter(StaticGetterExpression expression) {
293
+        return null;
294
+    }
295
+
296
+    @Override
297
+    public Void visitStaticSetter(StaticSetterExpression expression) {
298
+        return null;
299
+    }
300
+
301
+    @Override
302
+    public Void visitStringConcat(StringConcatExpression expression) {
303
+        return null;
304
+    }
305
+
306
+    @Override
307
+    public Void visitSubstring(SubstringExpression expression) {
308
+        return null;
309
+    }
310
+
311
+    @Override
312
+    public Void visitThis(ThisExpression expression) {
313
+        return null;
314
+    }
315
+
316
+    @Override
317
+    public Void visitWrapOptional(WrapOptionalExpression expression) {
318
+        return null;
319
+    }
320
+}

+ 92
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaStatementVisitor.java ファイルの表示

@@ -0,0 +1,92 @@
1
+package org.openzen.zenscript.scriptingexample.writer;
2
+
3
+import org.openzen.zenscript.codemodel.statement.*;
4
+
5
+public class JavaStatementVisitor implements StatementVisitor<Void> {
6
+    private final JavaWriter javaWriter;
7
+
8
+    public JavaStatementVisitor(final JavaWriter javaWriter) {
9
+
10
+        this.javaWriter = javaWriter;
11
+    }
12
+
13
+    @Override
14
+    public Void visitBlock(BlockStatement statement) {
15
+        return null;
16
+    }
17
+
18
+    @Override
19
+    public Void visitBreak(BreakStatement statement) {
20
+        return null;
21
+    }
22
+
23
+    @Override
24
+    public Void visitContinue(ContinueStatement statement) {
25
+        return null;
26
+    }
27
+
28
+    @Override
29
+    public Void visitDoWhile(DoWhileStatement statement) {
30
+        return null;
31
+    }
32
+
33
+    @Override
34
+    public Void visitEmpty(EmptyStatement statement) {
35
+        return null;
36
+    }
37
+
38
+    @Override
39
+    public Void visitExpression(ExpressionStatement statement) {
40
+        statement.expression.accept(new JavaExpressionVisitor(javaWriter));
41
+        return null;
42
+    }
43
+
44
+    @Override
45
+    public Void visitForeach(ForeachStatement statement) {
46
+        return null;
47
+    }
48
+
49
+    @Override
50
+    public Void visitIf(IfStatement statement) {
51
+        return null;
52
+    }
53
+
54
+    @Override
55
+    public Void visitLock(LockStatement statement) {
56
+        return null;
57
+    }
58
+
59
+    @Override
60
+    public Void visitReturn(ReturnStatement statement) {
61
+        return null;
62
+    }
63
+
64
+    @Override
65
+    public Void visitThrow(ThrowStatement statement) {
66
+        return null;
67
+    }
68
+
69
+    @Override
70
+    public Void visitTryCatch(TryCatchStatement statement) {
71
+        return null;
72
+    }
73
+
74
+    @Override
75
+    public Void visitVar(VarStatement statement) {
76
+        return null;
77
+    }
78
+
79
+    @Override
80
+    public Void visitWhile(WhileStatement statement) {
81
+        return null;
82
+    }
83
+
84
+    public void start() {
85
+        javaWriter.start();
86
+    }
87
+
88
+    public void end() {
89
+        javaWriter.ret();
90
+        javaWriter.end();
91
+    }
92
+}

+ 86
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaTypeVisitor.java ファイルの表示

@@ -0,0 +1,86 @@
1
+package org.openzen.zenscript.scriptingexample.writer;
2
+
3
+import org.openzen.zenscript.codemodel.type.*;
4
+
5
+public class JavaTypeVisitor implements ITypeVisitor<String> {
6
+    @Override
7
+    public String visitBasic(BasicTypeID basic) {
8
+        switch (basic) {
9
+            case VOID:
10
+                return "V";
11
+            case NULL:
12
+            case ANY:
13
+            case UNDETERMINED:
14
+                return "Ljava/lang/Object;";
15
+            case BOOL:
16
+                return "Z";
17
+            case BYTE:
18
+            case SBYTE:
19
+                return "B";
20
+            case SHORT:
21
+            case USHORT:
22
+                return "S";
23
+            case INT:
24
+            case UINT:
25
+                return "I";
26
+            case LONG:
27
+            case ULONG:
28
+                return "J";
29
+            case FLOAT:
30
+                return "F";
31
+            case DOUBLE:
32
+                return "D";
33
+            case CHAR:
34
+                return "C";
35
+            case STRING:
36
+                return "Ljava/lang/String;";
37
+        }
38
+        return "";
39
+    }
40
+
41
+    @Override
42
+    public String visitArray(ArrayTypeID array) {
43
+        return "[" + array.elementType.accept(this);
44
+    }
45
+
46
+    @Override
47
+    public String visitAssoc(AssocTypeID assoc) {
48
+        return "Ljava/util/Map;";
49
+    }
50
+
51
+    @Override
52
+    public String visitIterator(IteratorTypeID iterator) {
53
+        return "java/lang/Iterable";
54
+    }
55
+
56
+    @Override
57
+    public String visitFunction(FunctionTypeID function) {
58
+        return null;
59
+    }
60
+
61
+    @Override
62
+    public String visitDefinition(DefinitionTypeID definition) {
63
+
64
+        return definition.definition.name.replaceAll("\"", "");
65
+    }
66
+
67
+    @Override
68
+    public String visitGeneric(GenericTypeID generic) {
69
+        return null;
70
+    }
71
+
72
+    @Override
73
+    public String visitRange(RangeTypeID range) {
74
+        return null;
75
+    }
76
+
77
+    @Override
78
+    public String visitConst(ConstTypeID type) {
79
+        return null;
80
+    }
81
+
82
+    @Override
83
+    public String visitOptional(OptionalTypeID optional) {
84
+        return null;
85
+    }
86
+}

+ 1068
- 0
ScriptingExample/src/main/java/org/openzen/zenscript/scriptingexample/writer/JavaWriter.java
ファイル差分が大きすぎるため省略します
ファイルの表示


読み込み中…
キャンセル
保存