Browse Source

PanicExpression and primitive Boxing Expression, probably

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

+ 121
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaBoxingTypeVisitor.java View File

@@ -0,0 +1,121 @@
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.javabytecode.JavaMethodInfo;
6
+
7
+public class JavaBoxingTypeVisitor implements ITypeVisitor<JavaMethodInfo> {
8
+
9
+	private final JavaWriter writer;
10
+
11
+	public JavaBoxingTypeVisitor(JavaWriter writer) {
12
+		this.writer = writer;
13
+	}
14
+
15
+	@Override
16
+	public JavaMethodInfo visitBasic(BasicTypeID basic) {
17
+		final JavaMethodInfo info;
18
+		switch (basic) {
19
+			case BOOL:
20
+				writer.newObject(Boolean.class);
21
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Boolean"), "<init>", "(Z)V", -1);
22
+				break;
23
+			case BYTE:
24
+			case SBYTE:
25
+				writer.newObject(Byte.class);
26
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Byte"), "<init>", "(B)V", -1);
27
+				break;
28
+			case SHORT:
29
+			case USHORT:
30
+				writer.newObject(Short.class);
31
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Short"), "<init>", "(S)V", -1);
32
+				break;
33
+			case INT:
34
+			case UINT:
35
+				writer.newObject(Byte.class);
36
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Byte"), "<init>", "(B)V", -1);
37
+				break;
38
+			case LONG:
39
+			case ULONG:
40
+				writer.newObject(Long.class);
41
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Long"), "<init>", "(S)V", -1);
42
+				break;
43
+			case FLOAT:
44
+				writer.newObject(Float.class);
45
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Float"), "<init>", "(F)V", -1);
46
+				break;
47
+			case DOUBLE:
48
+				writer.newObject(Double.class);
49
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Double"), "<init>", "(D)V", -1);
50
+				break;
51
+			case CHAR:
52
+				writer.newObject(Character.class);
53
+				info = new JavaMethodInfo(new JavaClassInfo("java/lang/Character"), "<init>", "(C)V", -1);
54
+				break;
55
+			default:
56
+				return null;
57
+		}
58
+		writer.dup();
59
+		return info;
60
+	}
61
+
62
+	@Override
63
+	public JavaMethodInfo visitArray(ArrayTypeID array) {
64
+		//NO-OP
65
+		return null;
66
+	}
67
+
68
+	@Override
69
+	public JavaMethodInfo visitAssoc(AssocTypeID assoc) {
70
+		//NO-OP
71
+		return null;
72
+	}
73
+
74
+	@Override
75
+	public JavaMethodInfo visitGenericMap(GenericMapTypeID map) {
76
+		//NO-OP
77
+		return null;
78
+	}
79
+
80
+	@Override
81
+	public JavaMethodInfo visitIterator(IteratorTypeID iterator) {
82
+		//NO-OP
83
+		return null;
84
+	}
85
+
86
+	@Override
87
+	public JavaMethodInfo visitFunction(FunctionTypeID function) {
88
+		//NO-OP
89
+		return null;
90
+	}
91
+
92
+	@Override
93
+	public JavaMethodInfo visitDefinition(DefinitionTypeID definition) {
94
+		//NO-OP
95
+		return null;
96
+	}
97
+
98
+	@Override
99
+	public JavaMethodInfo visitGeneric(GenericTypeID generic) {
100
+		//NO-OP
101
+		return null;
102
+	}
103
+
104
+	@Override
105
+	public JavaMethodInfo visitRange(RangeTypeID range) {
106
+		//NO-OP
107
+		return null;
108
+	}
109
+
110
+	@Override
111
+	public JavaMethodInfo visitConst(ConstTypeID type) {
112
+		//NO-OP
113
+		return null;
114
+	}
115
+
116
+	@Override
117
+	public JavaMethodInfo visitOptional(OptionalTypeID optional) {
118
+		//NO-OP
119
+		return null;
120
+	}
121
+}

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

@@ -162,12 +162,6 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
162 162
 		this.javaWriter = javaWriter;
163 163
 	}
164 164
 
165
-	private static Class<?> getForEquals(ITypeID id) {
166
-		if (CompilerUtils.isPrimitive(id))
167
-			return id.accept(JavaTypeClassVisitor.INSTANCE);
168
-		return Object.class;
169
-	}
170
-
171 165
 	@Override
172 166
 	public Void visitAndAnd(AndAndExpression expression) {
173 167
 		Label end = new Label();
@@ -1338,7 +1332,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
1338 1332
 		javaWriter.invokeSpecial(NullPointerException.class, "<init>", "(Ljava/lang/String;)V");
1339 1333
 		javaWriter.aThrow();
1340 1334
 		javaWriter.label(end);
1341
-		
1335
+
1342 1336
 		return null;
1343 1337
 	}
1344 1338
 
@@ -2086,8 +2080,12 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2086 2080
 
2087 2081
 	@Override
2088 2082
 	public Void visitPanic(PanicExpression expression) {
2089
-		// TODO: compile to: throw new AssertionError(expression.value)
2090
-		throw new UnsupportedOperationException("Not yet supported");
2083
+		javaWriter.newObject(AssertionError.class);
2084
+		javaWriter.dup();
2085
+		expression.value.accept(this);
2086
+		javaWriter.invokeSpecial(AssertionError.class, "<init>", "(Ljava/lang/String;)V");
2087
+		javaWriter.aThrow();
2088
+		return null;
2091 2089
 	}
2092 2090
 
2093 2091
 	@Override
@@ -2110,7 +2108,6 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2110 2108
 		javaWriter.dup();
2111 2109
 		expression.from.accept(this);
2112 2110
 		expression.to.accept(this);
2113
-		System.out.println(IntRange.class.getName());
2114 2111
 		javaWriter.invokeSpecial("org/openzen/zenscript/implementations/IntRange", "<init>", "(II)V");
2115 2112
 
2116 2113
 		return null;
@@ -2319,9 +2316,13 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2319 2316
 
2320 2317
 	@Override
2321 2318
 	public Void visitWrapOptional(WrapOptionalExpression expression) {
2322
-		// TODO: convert basic types (char, int, float, ...) to their boxed (Character, Integer, Float, ...) counterparts
2323
-		// -- any object type values can just be passed as-is
2319
+		//Does nothing if not required to be wrapped
2320
+		final JavaMethodInfo info = expression.value.type.accept(new JavaBoxingTypeVisitor(javaWriter));
2324 2321
 		expression.value.accept(this);
2322
+
2323
+		//i.e. if it was a primitive
2324
+		if(info != null)
2325
+			javaWriter.invokeSpecial(info);
2325 2326
 		return null;
2326 2327
 	}
2327 2328
 
@@ -2332,7 +2333,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2332 2333
 
2333 2334
 	//Will return true if a JavaBytecodeImplementation.class tag exists, and will compile that tag
2334 2335
 	private boolean checkAndExecuteByteCodeImplementation(DefinitionMemberRef member) {
2335
-		JavaBytecodeImplementation implementation = member.getTag(JavaBytecodeImplementation.class);
2336
+		final JavaBytecodeImplementation implementation = member.getTag(JavaBytecodeImplementation.class);
2336 2337
 		if (implementation != null) {
2337 2338
 			implementation.compile(getJavaWriter());
2338 2339
 			return true;
@@ -2342,7 +2343,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2342 2343
 
2343 2344
 	//Will return true if a JavaMethodInfo.class tag exists, and will compile that tag
2344 2345
 	private boolean checkAndExecuteMethodInfo(DefinitionMemberRef member) {
2345
-		JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
2346
+		final JavaMethodInfo methodInfo = member.getTag(JavaMethodInfo.class);
2346 2347
 		if (methodInfo == null)
2347 2348
 			return false;
2348 2349
 
@@ -2357,7 +2358,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2357 2358
 
2358 2359
 	//Will return true if a JavaFieldInfo.class tag exists, and will compile that tag
2359 2360
 	public boolean checkAndPutFieldInfo(FieldMemberRef field, boolean isStatic) {
2360
-		JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2361
+		final JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2361 2362
 		if (fieldInfo == null)
2362 2363
 			return false;
2363 2364
 		//TODO Remove isStatic
@@ -2370,7 +2371,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2370 2371
 	}
2371 2372
 
2372 2373
 	public boolean checkAndGetFieldInfo(ConstMemberRef field, boolean isStatic) {
2373
-		JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2374
+		final JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2374 2375
 		if (fieldInfo == null)
2375 2376
 			return false;
2376 2377
 
@@ -2379,7 +2380,7 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void> {
2379 2380
 	}
2380 2381
 
2381 2382
 	public boolean checkAndGetFieldInfo(FieldMemberRef field, boolean isStatic) {
2382
-		JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2383
+		final JavaFieldInfo fieldInfo = field.getTag(JavaFieldInfo.class);
2383 2384
 		if (fieldInfo == null)
2384 2385
 			return false;
2385 2386
 

+ 4
- 0
JavaBytecodeCompiler/src/main/java/org/openzen/zenscript/javabytecode/compiler/JavaWriter.java View File

@@ -828,6 +828,10 @@ public class JavaWriter {
828 828
 		invokeSpecial(Type.getInternalName(owner), name, descriptor);
829 829
 	}
830 830
 
831
+	public void invokeSpecial (JavaMethodInfo method) {
832
+		invokeSpecial(method.javaClass.internalClassName, method.name, method.descriptor);
833
+	}
834
+
831 835
 	public void invokeVirtual(JavaMethodInfo method) {
832 836
 		if (debug)
833 837
 			System.out.println("invokeVirtual " + method.javaClass.internalClassName + '.' + method.name + method.descriptor);

Loading…
Cancel
Save