Explorar el Código

Statements now Extend Taggable and added local variables

kindlich hace 6 años
padre
commit
8304947fd3
No se encontró ninguna clave conocida en la base de datos para esta firma

+ 2
- 1
CodeModel/src/main/java/org/openzen/zenscript/codemodel/statement/Statement.java Ver fichero

@@ -10,12 +10,13 @@ import java.util.stream.Collectors;
10 10
 import org.openzen.zenscript.codemodel.type.ITypeID;
11 11
 import org.openzen.zenscript.shared.CodePosition;
12 12
 import org.openzen.zenscript.codemodel.scope.TypeScope;
13
+import org.openzen.zenscript.shared.Taggable;
13 14
 
14 15
 /**
15 16
  *
16 17
  * @author Hoofdgebruiker
17 18
  */
18
-public abstract class Statement {
19
+public abstract class Statement extends Taggable {
19 20
 	public final CodePosition position;
20 21
 	
21 22
 	public Statement(CodePosition position) {

+ 13
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/JavaLocalVariableInfo.java Ver fichero

@@ -0,0 +1,13 @@
1
+package org.openzen.zenscript.javabytecode;
2
+
3
+import org.objectweb.asm.Type;
4
+
5
+public class JavaLocalVariableInfo {
6
+    public final Type type;
7
+    public final int local;
8
+
9
+    public JavaLocalVariableInfo(Type type, int local) {
10
+        this.type = type;
11
+        this.local = local;
12
+    }
13
+}

+ 12
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaExpressionVisitor.java Ver fichero

@@ -4,7 +4,10 @@ import org.openzen.zenscript.codemodel.expression.*;
4 4
 import org.openzen.zenscript.codemodel.member.DefinitionMember;
5 5
 import org.openzen.zenscript.javabytecode.JavaBytecodeImplementation;
6 6
 import org.openzen.zenscript.javabytecode.JavaFieldInfo;
7
+import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
7 8
 import org.openzen.zenscript.javabytecode.JavaMethodInfo;
9
+import org.openzen.zenscript.shared.CompileException;
10
+import org.openzen.zenscript.shared.CompileExceptionCode;
8 11
 
9 12
 public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
10 13
 
@@ -203,6 +206,8 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
203 206
 
204 207
     @Override
205 208
     public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
209
+        final JavaLocalVariableInfo tag = expression.variable.getTag(JavaLocalVariableInfo.class);
210
+        javaWriter.load(tag.type, tag.local);
206 211
         return null;
207 212
     }
208 213
 
@@ -275,6 +280,13 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
275 280
 
276 281
     @Override
277 282
     public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
283
+        if(expression.variable.isFinal)
284
+            throw new CompileException(expression.position, CompileExceptionCode.CANNOT_SET_FINAL_VARIABLE, "Cannot set a final variable!");
285
+        expression.value.accept(this);
286
+        final JavaLocalVariableInfo tag = expression.variable.getTag(JavaLocalVariableInfo.class);
287
+
288
+        javaWriter.store(tag.type, tag.local);
289
+
278 290
         return null;
279 291
     }
280 292
 

+ 8
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaStatementVisitor.java Ver fichero

@@ -1,6 +1,8 @@
1 1
 package org.openzen.zenscript.javabytecode.compiler;
2 2
 
3
+import org.objectweb.asm.Type;
3 4
 import org.openzen.zenscript.codemodel.statement.*;
5
+import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
4 6
 
5 7
 public class JavaStatementVisitor implements StatementVisitor<Void> {
6 8
     private final JavaWriter javaWriter;
@@ -78,6 +80,12 @@ public class JavaStatementVisitor implements StatementVisitor<Void> {
78 80
 
79 81
     @Override
80 82
     public Void visitVar(VarStatement statement) {
83
+        if(statement.initializer != null)
84
+            statement.initializer.accept(expressionVisitor);
85
+        Type type = Type.getType(statement.type.accept(JavaTypeClassVisitor.INSTANCE));
86
+        int local = javaWriter.local(type);
87
+        javaWriter.store(type, local);
88
+        statement.setTag(JavaLocalVariableInfo.class, new JavaLocalVariableInfo(type, local));
81 89
         return null;
82 90
     }
83 91
 

+ 97
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaTypeClassVisitor.java Ver fichero

@@ -0,0 +1,97 @@
1
+package org.openzen.zenscript.javabytecode.compiler;
2
+
3
+import org.objectweb.asm.Type;
4
+import org.openzen.zenscript.codemodel.type.*;
5
+
6
+import java.util.Iterator;
7
+import java.util.Map;
8
+
9
+public class JavaTypeClassVisitor implements ITypeVisitor<Class> {
10
+
11
+    public static final JavaTypeClassVisitor INSTANCE = new JavaTypeClassVisitor();
12
+
13
+    @Override
14
+    public Class visitBasic(BasicTypeID basic) {
15
+        switch (basic) {
16
+            case VOID:
17
+                return void.class;
18
+            case ANY:
19
+            case NULL:
20
+            case UNDETERMINED:
21
+                return Object.class;
22
+            case BOOL:
23
+                return boolean.class;
24
+            case BYTE:
25
+            case SBYTE:
26
+                return byte.class;
27
+            case SHORT:
28
+            case USHORT:
29
+                return short.class;
30
+            case INT:
31
+            case UINT:
32
+                return int.class;
33
+            case LONG:
34
+            case ULONG:
35
+                return long.class;
36
+            case FLOAT:
37
+                return float.class;
38
+            case DOUBLE:
39
+                return double.class;
40
+            case CHAR:
41
+                return char.class;
42
+            case STRING:
43
+                return String.class;
44
+        }
45
+        return Object.class;
46
+    }
47
+
48
+    @Override
49
+    public Class visitArray(ArrayTypeID array) {
50
+        try {
51
+            return Class.forName("[" + Type.getInternalName(array.elementType.accept(this)));
52
+        } catch (ClassNotFoundException e) {
53
+            e.printStackTrace();
54
+            return Object[].class;
55
+        }
56
+    }
57
+
58
+    @Override
59
+    public Class visitAssoc(AssocTypeID assoc) {
60
+        return Map.class;
61
+    }
62
+
63
+    @Override
64
+    public Class visitIterator(IteratorTypeID iterator) {
65
+        return Iterator.class;
66
+    }
67
+
68
+    @Override
69
+    public Class visitFunction(FunctionTypeID function) {
70
+        return null;
71
+    }
72
+
73
+    @Override
74
+    public Class visitDefinition(DefinitionTypeID definition) {
75
+        return null;
76
+    }
77
+
78
+    @Override
79
+    public Class visitGeneric(GenericTypeID generic) {
80
+        return null;
81
+    }
82
+
83
+    @Override
84
+    public Class visitRange(RangeTypeID range) {
85
+        return null;
86
+    }
87
+
88
+    @Override
89
+    public Class visitConst(ConstTypeID type) {
90
+        return type.baseType.accept(this);
91
+    }
92
+
93
+    @Override
94
+    public Class visitOptional(OptionalTypeID optional) {
95
+        return null;
96
+    }
97
+}

+ 0
- 92
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaTypeSignatureVisitor.java Ver fichero

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

+ 14
- 4
ScriptingExample/scripts/moreHellos.zs Ver fichero

@@ -1,6 +1,16 @@
1 1
 println("Hello world!");
2
-println("Hello world!");
3
-println("Hello world!");
4
-println("Hello world!");
5
-println("Hello world!");
6 2
 println(1 as string);
3
+
4
+
5
+var test = "test";
6
+println(test);
7
+
8
+
9
+test = "testMore";
10
+println(test);
11
+
12
+test = 13;
13
+println(test);
14
+
15
+val test2 = 14;
16
+println(test2);

+ 2
- 1
Shared/src/main/java/org/openzen/zenscript/shared/CompileExceptionCode.java Ver fichero

@@ -55,5 +55,6 @@ public enum CompileExceptionCode {
55 55
 	RETURN_VALUE_REQUIRED,
56 56
 	RETURN_VALUE_VOID,
57 57
 	INVALID_CONDITION,
58
-	INTERNAL_ERROR
58
+	INTERNAL_ERROR,
59
+	CANNOT_SET_FINAL_VARIABLE
59 60
 }

Loading…
Cancelar
Guardar