Browse Source

Implement increment & decrement compilation in the bytecode compiler.

Stan Hebben 6 years ago
parent
commit
e038d2e192

+ 14
- 14
CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java View File

@@ -773,8 +773,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
773 773
 		
774 774
 		invert(builtin, INT_NOT, INT);
775 775
 		neg(builtin, INT_NEG, INT);
776
-		inc(builtin, INT_DEC, INT);
777
-		dec(builtin, INT_INC, INT);
776
+		inc(builtin, INT_INC, INT);
777
+		dec(builtin, INT_DEC, INT);
778 778
 
779 779
 		add(builtin, INT_ADD_INT, INT, INT);
780 780
 		add(builtin, LONG_ADD_LONG, LONG, LONG, INT_TO_LONG);
@@ -853,8 +853,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
853 853
 		ClassDefinition builtin = new ClassDefinition(BUILTIN, Module.BUILTIN, null, "uint", Modifiers.PUBLIC, null);
854 854
 		
855 855
 		invert(builtin, UINT_NOT, INT);
856
-		inc(builtin, UINT_DEC, INT);
857
-		dec(builtin, UINT_INC, INT);
856
+		inc(builtin, UINT_INC, INT);
857
+		dec(builtin, UINT_DEC, INT);
858 858
 
859 859
 		add(builtin, UINT_ADD_UINT, UINT, UINT);
860 860
 		add(builtin, ULONG_ADD_ULONG, USIZE, ULONG, UINT_TO_ULONG);
@@ -934,8 +934,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
934 934
 		
935 935
 		invert(builtin, LONG_NOT, LONG);
936 936
 		neg(builtin, LONG_NEG, LONG);
937
-		inc(builtin, LONG_DEC, LONG);
938
-		dec(builtin, LONG_INC, LONG);
937
+		inc(builtin, LONG_INC, LONG);
938
+		dec(builtin, LONG_DEC, LONG);
939 939
 
940 940
 		add(builtin, LONG_ADD_LONG, LONG, LONG);
941 941
 		add(builtin, FLOAT_ADD_FLOAT, FLOAT, FLOAT, LONG_TO_FLOAT);
@@ -1006,8 +1006,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1006 1006
 		ClassDefinition builtin = new ClassDefinition(BUILTIN, Module.BUILTIN, null, "ulong", Modifiers.PUBLIC, null);
1007 1007
 		
1008 1008
 		invert(builtin, ULONG_NOT, ULONG);
1009
-		inc(builtin, ULONG_DEC, ULONG);
1010
-		dec(builtin, ULONG_INC, ULONG);
1009
+		inc(builtin, ULONG_INC, ULONG);
1010
+		dec(builtin, ULONG_DEC, ULONG);
1011 1011
 
1012 1012
 		add(builtin, ULONG_ADD_ULONG, ULONG, ULONG);
1013 1013
 		add(builtin, FLOAT_ADD_FLOAT, FLOAT, FLOAT, ULONG_TO_FLOAT);
@@ -1078,8 +1078,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1078 1078
 		ClassDefinition builtin = new ClassDefinition(BUILTIN, Module.BUILTIN, null, "usize", Modifiers.PUBLIC, null);
1079 1079
 		
1080 1080
 		invert(builtin, USIZE_NOT, USIZE);
1081
-		inc(builtin, USIZE_DEC, USIZE);
1082
-		dec(builtin, USIZE_INC, USIZE);
1081
+		inc(builtin, USIZE_INC, USIZE);
1082
+		dec(builtin, USIZE_DEC, USIZE);
1083 1083
 
1084 1084
 		add(builtin, USIZE_ADD_USIZE, USIZE, USIZE);
1085 1085
 		add(builtin, ULONG_ADD_ULONG, ULONG, ULONG, USIZE_TO_ULONG);
@@ -1155,8 +1155,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1155 1155
 		ClassDefinition builtin = new ClassDefinition(BUILTIN, Module.BUILTIN, null, "float", Modifiers.PUBLIC, null);
1156 1156
 		
1157 1157
 		neg(builtin, FLOAT_NEG, FLOAT);
1158
-		inc(builtin, FLOAT_DEC, FLOAT);
1159
-		dec(builtin, FLOAT_INC, FLOAT);
1158
+		inc(builtin, FLOAT_INC, FLOAT);
1159
+		dec(builtin, FLOAT_DEC, FLOAT);
1160 1160
 
1161 1161
 		add(builtin, FLOAT_ADD_FLOAT, FLOAT, FLOAT);
1162 1162
 		add(builtin, DOUBLE_ADD_DOUBLE, DOUBLE, DOUBLE, FLOAT_TO_DOUBLE);
@@ -1200,8 +1200,8 @@ public class TypeMemberBuilder implements TypeVisitorWithContext<Void, Void, Run
1200 1200
 		ClassDefinition builtin = new ClassDefinition(BUILTIN, Module.BUILTIN, null, "double", Modifiers.PUBLIC, null);
1201 1201
 		
1202 1202
 		neg(builtin, DOUBLE_NEG, DOUBLE);
1203
-		inc(builtin, DOUBLE_DEC, DOUBLE);
1204
-		dec(builtin, DOUBLE_INC, DOUBLE);
1203
+		inc(builtin, DOUBLE_INC, DOUBLE);
1204
+		dec(builtin, DOUBLE_DEC, DOUBLE);
1205 1205
 
1206 1206
 		add(builtin, DOUBLE_ADD_DOUBLE, DOUBLE, DOUBLE);
1207 1207
 		sub(builtin, DOUBLE_SUB_DOUBLE, DOUBLE, DOUBLE);

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

@@ -9,6 +9,7 @@ import org.openzen.zenscript.codemodel.expression.switchvalue.*;
9 9
 import org.openzen.zenscript.codemodel.member.FieldMember;
10 10
 import org.openzen.zenscript.codemodel.member.IDefinitionMember;
11 11
 import org.openzen.zenscript.codemodel.type.BasicTypeID;
12
+import org.openzen.zenscript.codemodel.type.StoredType;
12 13
 import org.openzen.zenscript.codemodel.type.TypeID;
13 14
 import org.openzen.zenscript.javashared.JavaParameterInfo;
14 15
 
@@ -16,6 +17,7 @@ import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
16 17
 import org.openzen.zenscript.javashared.JavaCompiledModule;
17 18
 
18 19
 public class CompilerUtils {
20
+
19 21
 	private CompilerUtils() {}
20 22
 
21 23
 	public static boolean isPrimitive(TypeID id) {
@@ -23,6 +25,10 @@ public class CompilerUtils {
23 25
 				|| (id.isOptional() && id.withoutOptional() == BasicTypeID.USIZE);
24 26
 	}
25 27
 
28
+	public static boolean isLarge(StoredType type) {
29
+		return type.type == BasicTypeID.DOUBLE || type.type == BasicTypeID.DOUBLE;
30
+	}
31
+	
26 32
 	public static int calcAccess(int modifiers) {
27 33
 		int out = 0;
28 34
 		if (Modifiers.isStatic(modifiers))

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

@@ -26,6 +26,7 @@ import java.util.Arrays;
26 26
 import java.util.Comparator;
27 27
 import java.util.Objects;
28 28
 import java.util.StringJoiner;
29
+import org.openzen.zenscript.javabytecode.compiler.JavaModificationExpressionVisitor.PushOption;
29 30
 
30 31
 public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativeTranslator<Void> {
31 32
 	private static final JavaMethod BOOLEAN_PARSE = JavaMethod.getNativeStatic(JavaClass.BOOLEAN, "parseBoolean", "(Ljava/lang/String;)Z");
@@ -392,6 +393,132 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
392 393
 			case ARRAY_INDEXGETRANGE:
393 394
 			case ARRAY_INDEXGET:
394 395
 				break;
396
+			case BYTE_INC:
397
+				modify(expression.target, () -> {
398
+					javaWriter.iConst1();
399
+					javaWriter.iAdd();
400
+					javaWriter.constant(0xFF);
401
+					javaWriter.iAnd();
402
+				}, PushOption.AFTER);
403
+				return null;
404
+			case BYTE_DEC:
405
+				modify(expression.target, () -> {
406
+					javaWriter.iConst1();
407
+					javaWriter.iSub();
408
+					javaWriter.constant(0xFF);
409
+					javaWriter.iAnd();
410
+				}, PushOption.AFTER);
411
+				return null;
412
+			case SBYTE_INC:
413
+				modify(expression.target, () -> {
414
+					javaWriter.iConst1();
415
+					javaWriter.iAdd();
416
+					javaWriter.i2b();
417
+				}, PushOption.AFTER);
418
+				return null;
419
+			case SBYTE_DEC:
420
+				modify(expression.target, () -> {
421
+					javaWriter.iConst1();
422
+					javaWriter.iSub();
423
+					javaWriter.i2b();
424
+				}, PushOption.AFTER);
425
+				return null;
426
+			case SHORT_INC:
427
+				modify(expression.target, () -> {
428
+					javaWriter.iConst1();
429
+					javaWriter.iAdd();
430
+					javaWriter.i2s();
431
+				}, PushOption.AFTER);
432
+				return null;
433
+			case SHORT_DEC:
434
+				modify(expression.target, () -> {
435
+					javaWriter.iConst1();
436
+					javaWriter.iSub();
437
+					javaWriter.i2s();
438
+				}, PushOption.AFTER);
439
+				return null;
440
+			case USHORT_INC:
441
+				modify(expression.target, () -> {
442
+					javaWriter.iConst1();
443
+					javaWriter.iAdd();
444
+					javaWriter.constant(0xFFFF);
445
+					javaWriter.iAnd();
446
+				}, PushOption.AFTER);
447
+				return null;
448
+			case USHORT_DEC:
449
+				modify(expression.target, () -> {
450
+					javaWriter.iConst1();
451
+					javaWriter.iSub();
452
+					javaWriter.constant(0xFFFF);
453
+					javaWriter.iAnd();
454
+				}, PushOption.AFTER);
455
+				return null;
456
+			case INT_INC:
457
+			case UINT_INC:
458
+			case USIZE_INC:
459
+				if (expression.target instanceof GetLocalVariableExpression) {
460
+					JavaLocalVariableInfo local = javaWriter.getLocalVariable(((GetLocalVariableExpression)expression.target).variable.variable);
461
+					javaWriter.iinc(local.local);
462
+					javaWriter.load(local);
463
+				} else {
464
+					modify(expression.target, () -> {
465
+						javaWriter.iConst1();
466
+						javaWriter.iAdd();
467
+					}, PushOption.AFTER);
468
+				}
469
+				return null;
470
+			case INT_DEC:
471
+			case UINT_DEC:
472
+			case USIZE_DEC:
473
+				if (expression.target instanceof GetLocalVariableExpression) {
474
+					JavaLocalVariableInfo local = javaWriter.getLocalVariable(((GetLocalVariableExpression)expression.target).variable.variable);
475
+					javaWriter.iinc(local.local, -1);
476
+					javaWriter.load(local);
477
+				} else {
478
+					modify(expression.target, () -> {
479
+						javaWriter.iConst1();
480
+						javaWriter.iSub();
481
+					}, PushOption.AFTER);
482
+				}
483
+				return null;
484
+			case LONG_INC:
485
+			case ULONG_INC:
486
+				modify(expression.target, () -> {
487
+					javaWriter.constant(1l);
488
+					javaWriter.iAdd();
489
+				}, PushOption.AFTER);
490
+				return null;
491
+			case LONG_DEC:
492
+			case ULONG_DEC:
493
+				modify(expression.target, () -> {
494
+					javaWriter.constant(1l);
495
+					javaWriter.iSub();
496
+				}, PushOption.AFTER);
497
+				return null;
498
+			case FLOAT_INC:
499
+				modify(expression.target, () -> {
500
+					javaWriter.constant(1f);
501
+					javaWriter.iAdd();
502
+				}, PushOption.AFTER);
503
+				return null;
504
+			case FLOAT_DEC:
505
+				modify(expression.target, () -> {
506
+					javaWriter.constant(1f);
507
+					javaWriter.iSub();
508
+				}, PushOption.AFTER);
509
+				return null;
510
+			case DOUBLE_INC:
511
+				modify(expression.target, () -> {
512
+					javaWriter.constant(1d);
513
+					javaWriter.iAdd();
514
+				}, PushOption.AFTER);
515
+				return null;
516
+			case DOUBLE_DEC:
517
+				modify(expression.target, () -> {
518
+					javaWriter.constant(1d);
519
+					javaWriter.iSub();
520
+				}, PushOption.AFTER);
521
+				return null;
395 522
 			default:
396 523
 				expression.target.accept(this);
397 524
 				for (Expression argument : expression.arguments.arguments) {
@@ -3337,14 +3464,150 @@ public class JavaExpressionVisitor implements ExpressionVisitor<Void>, JavaNativ
3337 3464
 		javaWriter.aThrow();
3338 3465
 		return null;
3339 3466
 	}
3467
+	
3468
+	private void modify(Expression source, Runnable modification, PushOption push) {
3469
+		source.accept(new JavaModificationExpressionVisitor(context, module, javaWriter, this, modification, push));
3470
+	}
3340 3471
 
3341 3472
 	@Override
3342 3473
 	public Void visitPostCall(PostCallExpression expression) {
3343
-		expression.target.accept(this);
3344
-		javaWriter.dup(context.getType(expression.type));
3345
-		if (!checkAndExecuteMethodInfo(expression.member, expression.type, expression))
3346
-			throw new IllegalStateException("Call target has no method info!");
3347
-
3474
+		if (expression.member.getBuiltin() != null) {
3475
+			switch (expression.member.getBuiltin()) {
3476
+				case BYTE_INC:
3477
+					modify(expression.target, () -> {
3478
+						javaWriter.iConst1();
3479
+						javaWriter.iAdd();
3480
+						javaWriter.constant(255);
3481
+						javaWriter.iAnd();
3482
+					}, PushOption.BEFORE);
3483
+					return null;
3484
+				case BYTE_DEC:
3485
+					modify(expression.target, () -> {
3486
+						javaWriter.iConst1();
3487
+						javaWriter.iSub();
3488
+						javaWriter.constant(255);
3489
+						javaWriter.iAnd();
3490
+					}, PushOption.BEFORE);
3491
+				case SBYTE_INC:
3492
+					modify(expression.target, () -> {
3493
+						javaWriter.iConst1();
3494
+						javaWriter.iAdd();
3495
+						javaWriter.i2b();
3496
+					}, PushOption.BEFORE);
3497
+					return null;
3498
+				case SBYTE_DEC:
3499
+					modify(expression.target, () -> {
3500
+						javaWriter.iConst1();
3501
+						javaWriter.iSub();
3502
+						javaWriter.i2b();
3503
+					}, PushOption.BEFORE);
3504
+					return null;
3505
+				case SHORT_INC:
3506
+					modify(expression.target, () -> {
3507
+						javaWriter.iConst1();
3508
+						javaWriter.iAdd();
3509
+						javaWriter.i2s();
3510
+					}, PushOption.BEFORE);
3511
+					return null;
3512
+				case SHORT_DEC:
3513
+					modify(expression.target, () -> {
3514
+						javaWriter.iConst1();
3515
+						javaWriter.iSub();
3516
+						javaWriter.i2s();
3517
+					}, PushOption.BEFORE);
3518
+					return null;
3519
+				case USHORT_INC:
3520
+					modify(expression.target, () -> {
3521
+						javaWriter.iConst1();
3522
+						javaWriter.iAdd();
3523
+						javaWriter.constant(0xFFFF);
3524
+						javaWriter.iAnd();
3525
+					}, PushOption.BEFORE);
3526
+					return null;
3527
+				case USHORT_DEC:
3528
+					modify(expression.target, () -> {
3529
+						javaWriter.iConst1();
3530
+						javaWriter.iSub();
3531
+						javaWriter.constant(0xFFFF);
3532
+						javaWriter.iAnd();
3533
+					}, PushOption.BEFORE);
3534
+					return null;
3535
+				case INT_INC:
3536
+				case UINT_INC:
3537
+				case USIZE_INC:
3538
+					if (expression.target instanceof GetLocalVariableExpression) {
3539
+						JavaLocalVariableInfo local = javaWriter.getLocalVariable(((GetLocalVariableExpression)expression.target).variable.variable);
3540
+						javaWriter.load(local);
3541
+						javaWriter.iinc(local.local);
3542
+					} else {
3543
+						modify(expression.target, () -> {
3544
+							javaWriter.iConst1();
3545
+							javaWriter.iAdd();
3546
+						}, PushOption.BEFORE);
3547
+					}
3548
+					return null;
3549
+				case INT_DEC:
3550
+				case UINT_DEC:
3551
+				case USIZE_DEC:
3552
+					if (expression.target instanceof GetLocalVariableExpression) {
3553
+						JavaLocalVariableInfo local = javaWriter.getLocalVariable(((GetLocalVariableExpression)expression.target).variable.variable);
3554
+						javaWriter.load(local);
3555
+						javaWriter.iinc(local.local, -1);
3556
+					} else {
3557
+						modify(expression.target, () -> {
3558
+							javaWriter.iConst1();
3559
+							javaWriter.iSub();
3560
+						}, PushOption.BEFORE);
3561
+					}
3562
+					return null;
3563
+				case LONG_INC:
3564
+				case ULONG_INC:
3565
+					modify(expression.target, () -> {
3566
+						javaWriter.constant(1l);
3567
+						javaWriter.lAdd();
3568
+					}, PushOption.BEFORE);
3569
+					return null;
3570
+				case LONG_DEC:
3571
+				case ULONG_DEC:
3572
+					modify(expression.target, () -> {
3573
+						javaWriter.constant(1l);
3574
+						javaWriter.lSub();
3575
+					}, PushOption.BEFORE);
3576
+					return null;
3577
+				case FLOAT_INC:
3578
+					modify(expression.target, () -> {
3579
+						javaWriter.constant(1f);
3580
+						javaWriter.fAdd();
3581
+					}, PushOption.BEFORE);
3582
+					return null;
3583
+				case FLOAT_DEC:
3584
+					modify(expression.target, () -> {
3585
+						javaWriter.constant(1f);
3586
+						javaWriter.fSub();
3587
+					}, PushOption.BEFORE);
3588
+					return null;
3589
+				case DOUBLE_INC:
3590
+					modify(expression.target, () -> {
3591
+						javaWriter.constant(1d);
3592
+						javaWriter.dAdd();
3593
+					}, PushOption.BEFORE);
3594
+					return null;
3595
+				case DOUBLE_DEC:
3596
+					modify(expression.target, () -> {
3597
+						javaWriter.constant(1d);
3598
+						javaWriter.dSub();
3599
+					}, PushOption.BEFORE);
3600
+					return null;
3601
+				default:
3602
+					throw new IllegalArgumentException("Unknown postcall builtin: " + expression.member.getBuiltin());
3603
+			}
3604
+		}
3605
+		
3606
+		modify(expression.target, () -> {
3607
+			if (!checkAndExecuteMethodInfo(expression.member, expression.type, expression))
3608
+				throw new IllegalStateException("Call target has no method info!");
3609
+		}, PushOption.BEFORE);
3610
+		
3348 3611
 		return null;
3349 3612
 	}
3350 3613
 

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

@@ -0,0 +1,495 @@
1
+/*
2
+ * To change this license header, choose License Headers in Project Properties.
3
+ * To change this template file, choose Tools | Templates
4
+ * and open the template in the editor.
5
+ */
6
+package org.openzen.zenscript.javabytecode.compiler;
7
+
8
+import org.openzen.zenscript.codemodel.expression.AndAndExpression;
9
+import org.openzen.zenscript.codemodel.expression.ArrayExpression;
10
+import org.openzen.zenscript.codemodel.expression.CallExpression;
11
+import org.openzen.zenscript.codemodel.expression.CallStaticExpression;
12
+import org.openzen.zenscript.codemodel.expression.CapturedClosureExpression;
13
+import org.openzen.zenscript.codemodel.expression.CapturedDirectExpression;
14
+import org.openzen.zenscript.codemodel.expression.CapturedLocalVariableExpression;
15
+import org.openzen.zenscript.codemodel.expression.CapturedParameterExpression;
16
+import org.openzen.zenscript.codemodel.expression.CapturedThisExpression;
17
+import org.openzen.zenscript.codemodel.expression.CastExpression;
18
+import org.openzen.zenscript.codemodel.expression.CheckNullExpression;
19
+import org.openzen.zenscript.codemodel.expression.CoalesceExpression;
20
+import org.openzen.zenscript.codemodel.expression.CompareExpression;
21
+import org.openzen.zenscript.codemodel.expression.ConditionalExpression;
22
+import org.openzen.zenscript.codemodel.expression.ConstExpression;
23
+import org.openzen.zenscript.codemodel.expression.ConstantBoolExpression;
24
+import org.openzen.zenscript.codemodel.expression.ConstantByteExpression;
25
+import org.openzen.zenscript.codemodel.expression.ConstantCharExpression;
26
+import org.openzen.zenscript.codemodel.expression.ConstantDoubleExpression;
27
+import org.openzen.zenscript.codemodel.expression.ConstantFloatExpression;
28
+import org.openzen.zenscript.codemodel.expression.ConstantIntExpression;
29
+import org.openzen.zenscript.codemodel.expression.ConstantLongExpression;
30
+import org.openzen.zenscript.codemodel.expression.ConstantSByteExpression;
31
+import org.openzen.zenscript.codemodel.expression.ConstantShortExpression;
32
+import org.openzen.zenscript.codemodel.expression.ConstantStringExpression;
33
+import org.openzen.zenscript.codemodel.expression.ConstantUIntExpression;
34
+import org.openzen.zenscript.codemodel.expression.ConstantULongExpression;
35
+import org.openzen.zenscript.codemodel.expression.ConstantUShortExpression;
36
+import org.openzen.zenscript.codemodel.expression.ConstantUSizeExpression;
37
+import org.openzen.zenscript.codemodel.expression.ConstructorSuperCallExpression;
38
+import org.openzen.zenscript.codemodel.expression.ConstructorThisCallExpression;
39
+import org.openzen.zenscript.codemodel.expression.EnumConstantExpression;
40
+import org.openzen.zenscript.codemodel.expression.ExpressionVisitor;
41
+import org.openzen.zenscript.codemodel.expression.FunctionExpression;
42
+import org.openzen.zenscript.codemodel.expression.GetFieldExpression;
43
+import org.openzen.zenscript.codemodel.expression.GetFunctionParameterExpression;
44
+import org.openzen.zenscript.codemodel.expression.GetLocalVariableExpression;
45
+import org.openzen.zenscript.codemodel.expression.GetMatchingVariantField;
46
+import org.openzen.zenscript.codemodel.expression.GetStaticFieldExpression;
47
+import org.openzen.zenscript.codemodel.expression.GetterExpression;
48
+import org.openzen.zenscript.codemodel.expression.GlobalCallExpression;
49
+import org.openzen.zenscript.codemodel.expression.GlobalExpression;
50
+import org.openzen.zenscript.codemodel.expression.InterfaceCastExpression;
51
+import org.openzen.zenscript.codemodel.expression.IsExpression;
52
+import org.openzen.zenscript.codemodel.expression.MakeConstExpression;
53
+import org.openzen.zenscript.codemodel.expression.MapExpression;
54
+import org.openzen.zenscript.codemodel.expression.MatchExpression;
55
+import org.openzen.zenscript.codemodel.expression.NewExpression;
56
+import org.openzen.zenscript.codemodel.expression.NullExpression;
57
+import org.openzen.zenscript.codemodel.expression.OrOrExpression;
58
+import org.openzen.zenscript.codemodel.expression.PanicExpression;
59
+import org.openzen.zenscript.codemodel.expression.PostCallExpression;
60
+import org.openzen.zenscript.codemodel.expression.RangeExpression;
61
+import org.openzen.zenscript.codemodel.expression.SameObjectExpression;
62
+import org.openzen.zenscript.codemodel.expression.SetFieldExpression;
63
+import org.openzen.zenscript.codemodel.expression.SetFunctionParameterExpression;
64
+import org.openzen.zenscript.codemodel.expression.SetLocalVariableExpression;
65
+import org.openzen.zenscript.codemodel.expression.SetStaticFieldExpression;
66
+import org.openzen.zenscript.codemodel.expression.SetterExpression;
67
+import org.openzen.zenscript.codemodel.expression.StaticGetterExpression;
68
+import org.openzen.zenscript.codemodel.expression.StaticSetterExpression;
69
+import org.openzen.zenscript.codemodel.expression.StorageCastExpression;
70
+import org.openzen.zenscript.codemodel.expression.SupertypeCastExpression;
71
+import org.openzen.zenscript.codemodel.expression.ThisExpression;
72
+import org.openzen.zenscript.codemodel.expression.ThrowExpression;
73
+import org.openzen.zenscript.codemodel.expression.TryConvertExpression;
74
+import org.openzen.zenscript.codemodel.expression.TryRethrowAsExceptionExpression;
75
+import org.openzen.zenscript.codemodel.expression.TryRethrowAsResultExpression;
76
+import org.openzen.zenscript.codemodel.expression.VariantValueExpression;
77
+import org.openzen.zenscript.codemodel.expression.WrapOptionalExpression;
78
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
79
+import org.openzen.zenscript.codemodel.type.StoredType;
80
+import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
81
+import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
82
+import org.openzen.zenscript.javashared.JavaCompiledModule;
83
+import org.openzen.zenscript.javashared.JavaField;
84
+import org.openzen.zenscript.javashared.JavaParameterInfo;
85
+
86
+/**
87
+ *
88
+ * @author Hoofdgebruiker
89
+ */
90
+public class JavaModificationExpressionVisitor implements ExpressionVisitor<Void> {
91
+	public static enum PushOption {
92
+		NONE, // don't push result
93
+		BEFORE, // push result before modification (eg. i++)
94
+		AFTER // push result after modification (eg. ++i)
95
+	}
96
+	
97
+	private final JavaBytecodeContext context;
98
+	private final JavaCompiledModule module;
99
+	private final JavaWriter javaWriter;
100
+	private final JavaExpressionVisitor expressionVisitor;
101
+	private final Runnable modification;
102
+	private final PushOption push;
103
+	
104
+	public JavaModificationExpressionVisitor(
105
+			JavaBytecodeContext context,
106
+			JavaCompiledModule module,
107
+			JavaWriter javaWriter,
108
+			JavaExpressionVisitor expressionVisitor,
109
+			Runnable modification,
110
+			PushOption push) {
111
+		this.context = context;
112
+		this.module = module;
113
+		this.javaWriter = javaWriter;
114
+		this.expressionVisitor = expressionVisitor;
115
+		this.modification = modification;
116
+		this.push = push;
117
+	}
118
+	
119
+	private void modify(StoredType type) {
120
+		boolean large = type.type == BasicTypeID.DOUBLE || type.type == BasicTypeID.LONG;
121
+		modify(large);
122
+	}
123
+	
124
+	private void modify(boolean large) {
125
+		if (push == PushOption.BEFORE)
126
+			javaWriter.dup(large);
127
+		modification.run();
128
+		if (push == PushOption.AFTER)
129
+			javaWriter.dup(large);
130
+	}
131
+
132
+	@Override
133
+	public Void visitAndAnd(AndAndExpression expression) {
134
+		throw new UnsupportedOperationException("Invalid lvalue: &&");
135
+	}
136
+
137
+	@Override
138
+	public Void visitArray(ArrayExpression expression) {
139
+		throw new UnsupportedOperationException("Invalid lvalue: array");
140
+	}
141
+
142
+	@Override
143
+	public Void visitCompare(CompareExpression expression) {
144
+		throw new UnsupportedOperationException("Invalid lvalue: comparison");
145
+	}
146
+
147
+	@Override
148
+	public Void visitCall(CallExpression expression) {
149
+		throw new UnsupportedOperationException("Invalid lvalue: call");
150
+	}
151
+
152
+	@Override
153
+	public Void visitCallStatic(CallStaticExpression expression) {
154
+		throw new UnsupportedOperationException("Invalid lvalue: static call");
155
+	}
156
+
157
+	@Override
158
+	public Void visitCapturedClosure(CapturedClosureExpression expression) {
159
+		throw new UnsupportedOperationException("Invalid lvalue: captured closure");
160
+	}
161
+
162
+	@Override
163
+	public Void visitCapturedDirect(CapturedDirectExpression expression) {
164
+		throw new UnsupportedOperationException("Invalid lvalue: captured direct");
165
+	}
166
+
167
+	@Override
168
+	public Void visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
169
+		throw new UnsupportedOperationException("Invalid lvalue: captured local variable cannot be modified");
170
+	}
171
+
172
+	@Override
173
+	public Void visitCapturedParameter(CapturedParameterExpression expression) {
174
+		throw new UnsupportedOperationException("Invalid lvalue: captured parameter cannot be modified");
175
+	}
176
+
177
+	@Override
178
+	public Void visitCapturedThis(CapturedThisExpression expression) {
179
+		throw new UnsupportedOperationException("Invalid lvalue: this");
180
+	}
181
+
182
+	@Override
183
+	public Void visitCast(CastExpression expression) {
184
+		throw new UnsupportedOperationException("Invalid lvalue: cast");
185
+	}
186
+
187
+	@Override
188
+	public Void visitCheckNull(CheckNullExpression expression) {
189
+		throw new UnsupportedOperationException("Invalid lvalue: null check");
190
+	}
191
+
192
+	@Override
193
+	public Void visitCoalesce(CoalesceExpression expression) {
194
+		throw new UnsupportedOperationException("Invalid lvalue: coalesce operator");
195
+	}
196
+
197
+	@Override
198
+	public Void visitConditional(ConditionalExpression expression) {
199
+		throw new UnsupportedOperationException("Invalid lvalue: conditional expression");
200
+	}
201
+
202
+	@Override
203
+	public Void visitConst(ConstExpression expression) {
204
+		throw new UnsupportedOperationException("Invalid lvalue: constant cannot be modified");
205
+	}
206
+
207
+	@Override
208
+	public Void visitConstantBool(ConstantBoolExpression expression) {
209
+		throw new UnsupportedOperationException("Invalid lvalue: constant bool");
210
+	}
211
+
212
+	@Override
213
+	public Void visitConstantByte(ConstantByteExpression expression) {
214
+		throw new UnsupportedOperationException("Invalid lvalue: constant byte");
215
+	}
216
+
217
+	@Override
218
+	public Void visitConstantChar(ConstantCharExpression expression) {
219
+		throw new UnsupportedOperationException("Invalid lvalue: constant char");
220
+	}
221
+
222
+	@Override
223
+	public Void visitConstantDouble(ConstantDoubleExpression expression) {
224
+		throw new UnsupportedOperationException("Invalid lvalue: constant double");
225
+	}
226
+
227
+	@Override
228
+	public Void visitConstantFloat(ConstantFloatExpression expression) {
229
+		throw new UnsupportedOperationException("Invalid lvalue: constant float");
230
+	}
231
+
232
+	@Override
233
+	public Void visitConstantInt(ConstantIntExpression expression) {
234
+		throw new UnsupportedOperationException("Invalid lvalue: constant int");
235
+	}
236
+
237
+	@Override
238
+	public Void visitConstantLong(ConstantLongExpression expression) {
239
+		throw new UnsupportedOperationException("Invalid lvalue: constant long");
240
+	}
241
+
242
+	@Override
243
+	public Void visitConstantSByte(ConstantSByteExpression expression) {
244
+		throw new UnsupportedOperationException("Invalid lvalue: constant sbyte");
245
+	}
246
+
247
+	@Override
248
+	public Void visitConstantShort(ConstantShortExpression expression) {
249
+		throw new UnsupportedOperationException("Invalid lvalue: constant short");
250
+	}
251
+
252
+	@Override
253
+	public Void visitConstantString(ConstantStringExpression expression) {
254
+		throw new UnsupportedOperationException("Invalid lvalue: constant string");
255
+	}
256
+
257
+	@Override
258
+	public Void visitConstantUInt(ConstantUIntExpression expression) {
259
+		throw new UnsupportedOperationException("Invalid lvalue: constant uint");
260
+	}
261
+
262
+	@Override
263
+	public Void visitConstantULong(ConstantULongExpression expression) {
264
+		throw new UnsupportedOperationException("Invalid lvalue: constant ulong");
265
+	}
266
+
267
+	@Override
268
+	public Void visitConstantUShort(ConstantUShortExpression expression) {
269
+		throw new UnsupportedOperationException("Invalid lvalue: constant ushort");
270
+	}
271
+
272
+	@Override
273
+	public Void visitConstantUSize(ConstantUSizeExpression expression) {
274
+		throw new UnsupportedOperationException("Invalid lvalue: constant usize");
275
+	}
276
+
277
+	@Override
278
+	public Void visitConstructorThisCall(ConstructorThisCallExpression expression) {
279
+		throw new UnsupportedOperationException("Invalid lvalue: constructor forwarding call");
280
+	}
281
+
282
+	@Override
283
+	public Void visitConstructorSuperCall(ConstructorSuperCallExpression expression) {
284
+		throw new UnsupportedOperationException("Invalid lvalue: super forwarding call");
285
+	}
286
+
287
+	@Override
288
+	public Void visitEnumConstant(EnumConstantExpression expression) {
289
+		throw new UnsupportedOperationException("Invalid lvalue: enum constant");
290
+	}
291
+
292
+	@Override
293
+	public Void visitFunction(FunctionExpression expression) {
294
+		throw new UnsupportedOperationException("Invalid lvalue: function");
295
+	}
296
+
297
+	@Override
298
+	public Void visitGetField(GetFieldExpression expression) {
299
+		JavaField field = context.getJavaField(expression.field);
300
+		expression.target.accept(expressionVisitor);
301
+		javaWriter.getField(field);
302
+		modify(expression.field.getType());
303
+		javaWriter.putField(field);
304
+		return null;
305
+	}
306
+
307
+	@Override
308
+	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
309
+		JavaParameterInfo parameter = module.getParameterInfo(expression.parameter);
310
+		javaWriter.load(parameter);
311
+		modify(expression.type);
312
+		javaWriter.store(parameter);
313
+		return null;
314
+	}
315
+
316
+	@Override
317
+	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
318
+		JavaLocalVariableInfo variable = javaWriter.getLocalVariable(expression.variable.variable);
319
+		javaWriter.load(variable);
320
+		modify(expression.type);
321
+		javaWriter.store(variable);
322
+		return null;
323
+	}
324
+
325
+	@Override
326
+	public Void visitGetMatchingVariantField(GetMatchingVariantField expression) {
327
+		throw new UnsupportedOperationException("Invalid lvalue: matching variant field");
328
+	}
329
+
330
+	@Override
331
+	public Void visitGetStaticField(GetStaticFieldExpression expression) {
332
+		JavaField field = context.getJavaField(expression.field);
333
+		javaWriter.getStaticField(field);
334
+		modify(expression.type);
335
+		javaWriter.putStaticField(field);
336
+		return null;
337
+	}
338
+
339
+	@Override
340
+	public Void visitGetter(GetterExpression expression) {
341
+		// TODO: find corresponding setter
342
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
343
+	}
344
+
345
+	@Override
346
+	public Void visitGlobal(GlobalExpression expression) {
347
+		throw new UnsupportedOperationException("Invalid lvalue: global");
348
+	}
349
+
350
+	@Override
351
+	public Void visitGlobalCall(GlobalCallExpression expression) {
352
+		throw new UnsupportedOperationException("Invalid lvalue: global call");
353
+	}
354
+
355
+	@Override
356
+	public Void visitInterfaceCast(InterfaceCastExpression expression) {
357
+		throw new UnsupportedOperationException("Invalid lvalue: cast");
358
+	}
359
+
360
+	@Override
361
+	public Void visitIs(IsExpression expression) {
362
+		throw new UnsupportedOperationException("Invalid lvalue: is");
363
+	}
364
+
365
+	@Override
366
+	public Void visitMakeConst(MakeConstExpression expression) {
367
+		throw new UnsupportedOperationException("Invalid lvalue: const");
368
+	}
369
+
370
+	@Override
371
+	public Void visitMap(MapExpression expression) {
372
+		throw new UnsupportedOperationException("Invalid lvalue: map");
373
+	}
374
+
375
+	@Override
376
+	public Void visitMatch(MatchExpression expression) {
377
+		throw new UnsupportedOperationException("Invalid lvalue: match");
378
+	}
379
+
380
+	@Override
381
+	public Void visitNew(NewExpression expression) {
382
+		throw new UnsupportedOperationException("Invalid lvalue: new");
383
+	}
384
+
385
+	@Override
386
+	public Void visitNull(NullExpression expression) {
387
+		throw new UnsupportedOperationException("Invalid lvalue: null");
388
+	}
389
+
390
+	@Override
391
+	public Void visitOrOr(OrOrExpression expression) {
392
+		throw new UnsupportedOperationException("Invalid lvalue: ||");
393
+	}
394
+
395
+	@Override
396
+	public Void visitPanic(PanicExpression expression) {
397
+		throw new UnsupportedOperationException("Invalid lvalue: panic");
398
+	}
399
+
400
+	@Override
401
+	public Void visitPostCall(PostCallExpression expression) {
402
+		throw new UnsupportedOperationException("Invalid lvalue: post call");
403
+	}
404
+
405
+	@Override
406
+	public Void visitRange(RangeExpression expression) {
407
+		throw new UnsupportedOperationException("Invalid lvalue: range");
408
+	}
409
+
410
+	@Override
411
+	public Void visitSameObject(SameObjectExpression expression) {
412
+		throw new UnsupportedOperationException("Invalid lvalue: ===");
413
+	}
414
+
415
+	@Override
416
+	public Void visitSetField(SetFieldExpression expression) {
417
+		throw new UnsupportedOperationException("Invalid lvalue: set field");
418
+	}
419
+
420
+	@Override
421
+	public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
422
+		throw new UnsupportedOperationException("Invalid lvalue: set function parameter");
423
+	}
424
+
425
+	@Override
426
+	public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
427
+		throw new UnsupportedOperationException("Invalid lvalue: set local variable");
428
+	}
429
+
430
+	@Override
431
+	public Void visitSetStaticField(SetStaticFieldExpression expression) {
432
+		throw new UnsupportedOperationException("Invalid lvalue: set static field");
433
+	}
434
+
435
+	@Override
436
+	public Void visitSetter(SetterExpression expression) {
437
+		throw new UnsupportedOperationException("Invalid lvalue: setter");
438
+	}
439
+
440
+	@Override
441
+	public Void visitStaticGetter(StaticGetterExpression expression) {
442
+		// TODO: find corresponding setter
443
+		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
444
+	}
445
+
446
+	@Override
447
+	public Void visitStaticSetter(StaticSetterExpression expression) {
448
+		throw new UnsupportedOperationException("Invalid lvalue: static setter");
449
+	}
450
+
451
+	@Override
452
+	public Void visitStorageCast(StorageCastExpression expression) {
453
+		throw new UnsupportedOperationException("Invalid lvalue: cast");
454
+	}
455
+
456
+	@Override
457
+	public Void visitSupertypeCast(SupertypeCastExpression expression) {
458
+		throw new UnsupportedOperationException("Invalid lvalue: cast");
459
+	}
460
+
461
+	@Override
462
+	public Void visitThis(ThisExpression expression) {
463
+		throw new UnsupportedOperationException("Invalid lvalue: this");
464
+	}
465
+
466
+	@Override
467
+	public Void visitThrow(ThrowExpression expression) {
468
+		throw new UnsupportedOperationException("Invalid lvalue: throw");
469
+	}
470
+
471
+	@Override
472
+	public Void visitTryConvert(TryConvertExpression expression) {
473
+		throw new UnsupportedOperationException("Invalid lvalue: try conversion");
474
+	}
475
+
476
+	@Override
477
+	public Void visitTryRethrowAsException(TryRethrowAsExceptionExpression expression) {
478
+		throw new UnsupportedOperationException("Invalid lvalue: try rethrow");
479
+	}
480
+
481
+	@Override
482
+	public Void visitTryRethrowAsResult(TryRethrowAsResultExpression expression) {
483
+		throw new UnsupportedOperationException("Invalid lvalue: try rethrow");
484
+	}
485
+
486
+	@Override
487
+	public Void visitVariantValue(VariantValueExpression expression) {
488
+		throw new UnsupportedOperationException("Invalid lvalue: variant value");
489
+	}
490
+
491
+	@Override
492
+	public Void visitWrapOptional(WrapOptionalExpression expression) {
493
+		throw new UnsupportedOperationException("Invalid lvalue: wrap optional");
494
+	}
495
+}

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

@@ -1,394 +0,0 @@
1
-/*
2
- * To change this license header, choose License Headers in Project Properties.
3
- * To change this template file, choose Tools | Templates
4
- * and open the template in the editor.
5
- */
6
-package org.openzen.zenscript.javabytecode.compiler;
7
-
8
-import org.objectweb.asm.Type;
9
-import org.openzen.zenscript.codemodel.expression.*;
10
-import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
-import org.openzen.zenscript.javashared.JavaCompiledModule;
12
-
13
-/**
14
- *
15
- * @author Hoofdgebruiker
16
- */
17
-public class JavaPreDecrementVisitor implements ExpressionVisitor<Void> {
18
-	private final JavaExpressionVisitor expressionCompiler;
19
-	private final JavaWriter javaWriter;
20
-	private final JavaCompiledModule module;
21
-	private final JavaBytecodeContext context;
22
-	
23
-	public JavaPreDecrementVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaExpressionVisitor expressionCompiler) {
24
-		this.expressionCompiler = expressionCompiler;
25
-		this.context = context;
26
-		this.module = module;
27
-		javaWriter = expressionCompiler.getJavaWriter();
28
-	}
29
-
30
-	@Override
31
-	public Void visitAndAnd(AndAndExpression expression) {
32
-		throw new UnsupportedOperationException("Invalid increment target");
33
-	}
34
-
35
-	@Override
36
-	public Void visitArray(ArrayExpression expression) {
37
-		throw new UnsupportedOperationException("Invalid increment target");
38
-	}
39
-
40
-	@Override
41
-	public Void visitCompare(CompareExpression expression) {
42
-		throw new UnsupportedOperationException("Invalid increment target");
43
-	}
44
-
45
-	@Override
46
-	public Void visitCall(CallExpression expression) {
47
-		throw new UnsupportedOperationException("Invalid increment target");
48
-	}
49
-
50
-	@Override
51
-	public Void visitCallStatic(CallStaticExpression expression) {
52
-		throw new UnsupportedOperationException("Invalid increment target");
53
-	}
54
-
55
-	@Override
56
-	public Void visitCapturedClosure(CapturedClosureExpression expression) {
57
-		throw new UnsupportedOperationException("Invalid increment target");
58
-	}
59
-
60
-	@Override
61
-	public Void visitCapturedDirect(CapturedDirectExpression expression) {
62
-		throw new UnsupportedOperationException("Invalid increment target");
63
-	}
64
-
65
-	@Override
66
-	public Void visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
67
-		throw new UnsupportedOperationException("Invalid increment target");
68
-	}
69
-
70
-	@Override
71
-	public Void visitCapturedParameter(CapturedParameterExpression expression) {
72
-		throw new UnsupportedOperationException("Invalid increment target");
73
-	}
74
-
75
-	@Override
76
-	public Void visitCapturedThis(CapturedThisExpression expression) {
77
-		throw new UnsupportedOperationException("Invalid increment target");
78
-	}
79
-
80
-	@Override
81
-	public Void visitCast(CastExpression expression) {
82
-		throw new UnsupportedOperationException("Invalid increment target");
83
-	}
84
-
85
-	@Override
86
-	public Void visitCheckNull(CheckNullExpression expression) {
87
-		throw new UnsupportedOperationException("Invalid increment target");
88
-	}
89
-
90
-	@Override
91
-	public Void visitCoalesce(CoalesceExpression expression) {
92
-		throw new UnsupportedOperationException("Invalid increment target");
93
-	}
94
-
95
-	@Override
96
-	public Void visitConditional(ConditionalExpression expression) {
97
-		throw new UnsupportedOperationException("Invalid increment target");
98
-	}
99
-	
100
-	@Override
101
-	public Void visitConst(ConstExpression expression) {
102
-		throw new UnsupportedOperationException("Invalid increment target");
103
-	}
104
-
105
-	@Override
106
-	public Void visitConstantBool(ConstantBoolExpression expression) {
107
-		throw new UnsupportedOperationException("Invalid increment target");
108
-	}
109
-
110
-	@Override
111
-	public Void visitConstantByte(ConstantByteExpression expression) {
112
-		throw new UnsupportedOperationException("Invalid increment target");
113
-	}
114
-
115
-	@Override
116
-	public Void visitConstantChar(ConstantCharExpression expression) {
117
-		throw new UnsupportedOperationException("Invalid increment target");
118
-	}
119
-
120
-	@Override
121
-	public Void visitConstantDouble(ConstantDoubleExpression expression) {
122
-		throw new UnsupportedOperationException("Invalid increment target");
123
-	}
124
-
125
-	@Override
126
-	public Void visitConstantFloat(ConstantFloatExpression expression) {
127
-		throw new UnsupportedOperationException("Invalid increment target");
128
-	}
129
-
130
-	@Override
131
-	public Void visitConstantInt(ConstantIntExpression expression) {
132
-		throw new UnsupportedOperationException("Invalid increment target");
133
-	}
134
-
135
-	@Override
136
-	public Void visitConstantLong(ConstantLongExpression expression) {
137
-		throw new UnsupportedOperationException("Invalid increment target");
138
-	}
139
-
140
-	@Override
141
-	public Void visitConstantSByte(ConstantSByteExpression expression) {
142
-		throw new UnsupportedOperationException("Invalid increment target");
143
-	}
144
-
145
-	@Override
146
-	public Void visitConstantShort(ConstantShortExpression expression) {
147
-		throw new UnsupportedOperationException("Invalid increment target");
148
-	}
149
-
150
-	@Override
151
-	public Void visitConstantString(ConstantStringExpression expression) {
152
-		throw new UnsupportedOperationException("Invalid increment target");
153
-	}
154
-
155
-	@Override
156
-	public Void visitConstantUInt(ConstantUIntExpression expression) {
157
-		throw new UnsupportedOperationException("Invalid increment target");
158
-	}
159
-
160
-	@Override
161
-	public Void visitConstantULong(ConstantULongExpression expression) {
162
-		throw new UnsupportedOperationException("Invalid increment target");
163
-	}
164
-
165
-	@Override
166
-	public Void visitConstantUShort(ConstantUShortExpression expression) {
167
-		throw new UnsupportedOperationException("Invalid increment target");
168
-	}
169
-
170
-	@Override
171
-	public Void visitConstantUSize(ConstantUSizeExpression expression) {
172
-		throw new UnsupportedOperationException("Invalid increment target");
173
-	}
174
-
175
-	@Override
176
-	public Void visitConstructorThisCall(ConstructorThisCallExpression expression) {
177
-		throw new UnsupportedOperationException("Invalid increment target");
178
-	}
179
-
180
-	@Override
181
-	public Void visitConstructorSuperCall(ConstructorSuperCallExpression expression) {
182
-		throw new UnsupportedOperationException("Invalid increment target");
183
-	}
184
-
185
-	@Override
186
-	public Void visitEnumConstant(EnumConstantExpression expression) {
187
-		throw new UnsupportedOperationException("Invalid increment target");
188
-	}
189
-
190
-	@Override
191
-	public Void visitFunction(FunctionExpression expression) {
192
-		throw new UnsupportedOperationException("Invalid increment target");
193
-	}
194
-
195
-	@Override
196
-	public Void visitGetField(GetFieldExpression expression) {
197
-		Type objectType = context.getType(expression.target.type);
198
-		
199
-		int local = javaWriter.local(objectType);
200
-		expression.target.accept(expressionCompiler);
201
-		javaWriter.dup();
202
-		javaWriter.store(objectType, local);
203
-		
204
-        expressionCompiler.getField(expression.field);
205
-		
206
-		javaWriter.iConst1();
207
-		javaWriter.iSub();
208
-		
209
-		javaWriter.load(objectType, local);
210
-        expressionCompiler.putField(expression.field);
211
-		return null;
212
-	}
213
-
214
-	@Override
215
-	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
216
-		javaWriter.idec(module.getParameterInfo(expression.parameter).index);
217
-		return null;
218
-	}
219
-	
220
-	@Override
221
-	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
222
-		javaWriter.idec(javaWriter.getLocalVariable(expression.variable.variable).local);
223
-		return null;
224
-	}
225
-
226
-	@Override
227
-	public Void visitGetMatchingVariantField(GetMatchingVariantField expression) {
228
-		throw new UnsupportedOperationException("Invalid increment target");
229
-	}
230
-
231
-	@Override
232
-	public Void visitGetStaticField(GetStaticFieldExpression expression) {
233
-		expressionCompiler.getField(expression.field);
234
-		javaWriter.iConst1();
235
-		javaWriter.iAdd();
236
-		expressionCompiler.putField(expression.field);
237
-		return null;
238
-	}
239
-
240
-	@Override
241
-	public Void visitGetter(GetterExpression expression) {
242
-		throw new UnsupportedOperationException("Not yet supported");
243
-	}
244
-
245
-	@Override
246
-	public Void visitGlobal(GlobalExpression expression) {
247
-		throw new UnsupportedOperationException("Invalid increment target");
248
-	}
249
-
250
-	@Override
251
-	public Void visitGlobalCall(GlobalCallExpression expression) {
252
-		throw new UnsupportedOperationException("Invalid increment target");
253
-	}
254
-
255
-	@Override
256
-	public Void visitInterfaceCast(InterfaceCastExpression expression) {
257
-		throw new UnsupportedOperationException("Invalid increment target");
258
-	}
259
-
260
-	@Override
261
-	public Void visitIs(IsExpression expression) {
262
-		throw new UnsupportedOperationException("Invalid increment target");
263
-	}
264
-
265
-	@Override
266
-	public Void visitMakeConst(MakeConstExpression expression) {
267
-		throw new UnsupportedOperationException("Invalid increment target");
268
-	}
269
-
270
-	@Override
271
-	public Void visitMap(MapExpression expression) {
272
-		throw new UnsupportedOperationException("Invalid increment target");
273
-	}
274
-
275
-	@Override
276
-	public Void visitMatch(MatchExpression expression) {
277
-		throw new UnsupportedOperationException("Invalid increment target");
278
-	}
279
-
280
-	@Override
281
-	public Void visitNew(NewExpression expression) {
282
-		throw new UnsupportedOperationException("Invalid increment target");
283
-	}
284
-
285
-	@Override
286
-	public Void visitNull(NullExpression expression) {
287
-		throw new UnsupportedOperationException("Invalid increment target");
288
-	}
289
-
290
-	@Override
291
-	public Void visitOrOr(OrOrExpression expression) {
292
-		throw new UnsupportedOperationException("Invalid increment target");
293
-	}
294
-	
295
-	@Override
296
-	public Void visitPanic(PanicExpression expression) {
297
-		throw new UnsupportedOperationException("Invalid increment target");
298
-	}
299
-
300
-	@Override
301
-	public Void visitPostCall(PostCallExpression expression) {
302
-		throw new UnsupportedOperationException("Invalid increment target");
303
-	}
304
-
305
-	@Override
306
-	public Void visitRange(RangeExpression expression) {
307
-		throw new UnsupportedOperationException("Invalid increment target");
308
-	}
309
-	
310
-	@Override
311
-	public Void visitSameObject(SameObjectExpression expression) {
312
-		throw new UnsupportedOperationException("Invalid increment target");
313
-	}
314
-
315
-	@Override
316
-	public Void visitSetField(SetFieldExpression expression) {
317
-		throw new UnsupportedOperationException("Invalid increment target");
318
-	}
319
-
320
-	@Override
321
-	public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
322
-		throw new UnsupportedOperationException("Invalid increment target");
323
-	}
324
-
325
-	@Override
326
-	public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
327
-		throw new UnsupportedOperationException("Invalid increment target");
328
-	}
329
-
330
-	@Override
331
-	public Void visitSetStaticField(SetStaticFieldExpression expression) {
332
-		throw new UnsupportedOperationException("Invalid increment target");
333
-	}
334
-
335
-	@Override
336
-	public Void visitSetter(SetterExpression expression) {
337
-		throw new UnsupportedOperationException("Invalid increment target");
338
-	}
339
-
340
-	@Override
341
-	public Void visitStaticGetter(StaticGetterExpression expression) {
342
-		throw new UnsupportedOperationException("Invalid increment target");
343
-	}
344
-
345
-	@Override
346
-	public Void visitStaticSetter(StaticSetterExpression expression) {
347
-		throw new UnsupportedOperationException("Invalid increment target");
348
-	}
349
-	
350
-	@Override
351
-	public Void visitStorageCast(StorageCastExpression expression) {
352
-		throw new UnsupportedOperationException("Invalid increment target");
353
-	}
354
-	
355
-	@Override
356
-	public Void visitSupertypeCast(SupertypeCastExpression expression) {
357
-		throw new UnsupportedOperationException("Invalid increment target");
358
-	}
359
-
360
-	@Override
361
-	public Void visitThis(ThisExpression expression) {
362
-		throw new UnsupportedOperationException("Invalid increment target");
363
-	}
364
-
365
-	@Override
366
-	public Void visitThrow(ThrowExpression expression) {
367
-		throw new UnsupportedOperationException("Invalid increment target");
368
-	}
369
-
370
-	@Override
371
-	public Void visitTryConvert(TryConvertExpression expression) {
372
-		throw new UnsupportedOperationException("Invalid increment target");
373
-	}
374
-
375
-	@Override
376
-	public Void visitTryRethrowAsException(TryRethrowAsExceptionExpression expression) {
377
-		throw new UnsupportedOperationException("Invalid increment target");
378
-	}
379
-
380
-	@Override
381
-	public Void visitTryRethrowAsResult(TryRethrowAsResultExpression expression) {
382
-		throw new UnsupportedOperationException("Invalid increment target");
383
-	}
384
-
385
-	@Override
386
-	public Void visitVariantValue(VariantValueExpression expression) {
387
-		throw new UnsupportedOperationException("Invalid increment target");
388
-	}
389
-
390
-	@Override
391
-	public Void visitWrapOptional(WrapOptionalExpression expression) {
392
-		throw new UnsupportedOperationException("Invalid increment target");
393
-	}
394
-}

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

@@ -1,402 +0,0 @@
1
-/*
2
- * To change this license header, choose License Headers in Project Properties.
3
- * To change this template file, choose Tools | Templates
4
- * and open the template in the editor.
5
- */
6
-package org.openzen.zenscript.javabytecode.compiler;
7
-
8
-import org.objectweb.asm.Type;
9
-import org.openzen.zenscript.codemodel.expression.*;
10
-import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
11
-import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
12
-import org.openzen.zenscript.javashared.JavaCompiledModule;
13
-import org.openzen.zenscript.javashared.JavaParameterInfo;
14
-
15
-/**
16
- *
17
- * @author Hoofdgebruiker
18
- */
19
-public class JavaPreIncrementVisitor implements ExpressionVisitor<Void> {
20
-	private final JavaExpressionVisitor expressionCompiler;
21
-	private final JavaBytecodeContext context;
22
-	private final JavaCompiledModule module;
23
-	private final JavaWriter javaWriter;
24
-	
25
-	public JavaPreIncrementVisitor(JavaBytecodeContext context, JavaCompiledModule module, JavaExpressionVisitor expressionCompiler) {
26
-		this.expressionCompiler = expressionCompiler;
27
-		this.context = context;
28
-		this.module = module;
29
-		javaWriter = expressionCompiler.getJavaWriter();
30
-	}
31
-
32
-	@Override
33
-	public Void visitAndAnd(AndAndExpression expression) {
34
-		throw new UnsupportedOperationException("Invalid increment target");
35
-	}
36
-
37
-	@Override
38
-	public Void visitArray(ArrayExpression expression) {
39
-		throw new UnsupportedOperationException("Invalid increment target");
40
-	}
41
-
42
-	@Override
43
-	public Void visitCompare(CompareExpression expression) {
44
-		throw new UnsupportedOperationException("Invalid increment target");
45
-	}
46
-
47
-	@Override
48
-	public Void visitCall(CallExpression expression) {
49
-		throw new UnsupportedOperationException("Invalid increment target");
50
-	}
51
-
52
-	@Override
53
-	public Void visitCallStatic(CallStaticExpression expression) {
54
-		throw new UnsupportedOperationException("Invalid increment target");
55
-	}
56
-
57
-	@Override
58
-	public Void visitCapturedClosure(CapturedClosureExpression expression) {
59
-		throw new UnsupportedOperationException("Invalid increment target");
60
-	}
61
-
62
-	@Override
63
-	public Void visitCapturedDirect(CapturedDirectExpression expression) {
64
-		throw new UnsupportedOperationException("Invalid increment target");
65
-	}
66
-
67
-	@Override
68
-	public Void visitCapturedLocalVariable(CapturedLocalVariableExpression expression) {
69
-		throw new UnsupportedOperationException("Invalid increment target");
70
-	}
71
-
72
-	@Override
73
-	public Void visitCapturedParameter(CapturedParameterExpression expression) {
74
-		throw new UnsupportedOperationException("Invalid increment target");
75
-	}
76
-
77
-	@Override
78
-	public Void visitCapturedThis(CapturedThisExpression expression) {
79
-		throw new UnsupportedOperationException("Invalid increment target");
80
-	}
81
-
82
-	@Override
83
-	public Void visitCast(CastExpression expression) {
84
-		throw new UnsupportedOperationException("Invalid increment target");
85
-	}
86
-
87
-	@Override
88
-	public Void visitCheckNull(CheckNullExpression expression) {
89
-		throw new UnsupportedOperationException("Invalid increment target");
90
-	}
91
-
92
-	@Override
93
-	public Void visitCoalesce(CoalesceExpression expression) {
94
-		throw new UnsupportedOperationException("Invalid increment target");
95
-	}
96
-
97
-	@Override
98
-	public Void visitConditional(ConditionalExpression expression) {
99
-		throw new UnsupportedOperationException("Invalid increment target");
100
-	}
101
-	
102
-	@Override
103
-	public Void visitConst(ConstExpression expression) {
104
-		throw new UnsupportedOperationException("Invalid increment target");
105
-	}
106
-
107
-	@Override
108
-	public Void visitConstantBool(ConstantBoolExpression expression) {
109
-		throw new UnsupportedOperationException("Invalid increment target");
110
-	}
111
-
112
-	@Override
113
-	public Void visitConstantByte(ConstantByteExpression expression) {
114
-		throw new UnsupportedOperationException("Invalid increment target");
115
-	}
116
-
117
-	@Override
118
-	public Void visitConstantChar(ConstantCharExpression expression) {
119
-		throw new UnsupportedOperationException("Invalid increment target");
120
-	}
121
-
122
-	@Override
123
-	public Void visitConstantDouble(ConstantDoubleExpression expression) {
124
-		throw new UnsupportedOperationException("Invalid increment target");
125
-	}
126
-
127
-	@Override
128
-	public Void visitConstantFloat(ConstantFloatExpression expression) {
129
-		throw new UnsupportedOperationException("Invalid increment target");
130
-	}
131
-
132
-	@Override
133
-	public Void visitConstantInt(ConstantIntExpression expression) {
134
-		throw new UnsupportedOperationException("Invalid increment target");
135
-	}
136
-
137
-	@Override
138
-	public Void visitConstantLong(ConstantLongExpression expression) {
139
-		throw new UnsupportedOperationException("Invalid increment target");
140
-	}
141
-
142
-	@Override
143
-	public Void visitConstantSByte(ConstantSByteExpression expression) {
144
-		throw new UnsupportedOperationException("Invalid increment target");
145
-	}
146
-
147
-	@Override
148
-	public Void visitConstantShort(ConstantShortExpression expression) {
149
-		throw new UnsupportedOperationException("Invalid increment target");
150
-	}
151
-
152
-	@Override
153
-	public Void visitConstantString(ConstantStringExpression expression) {
154
-		throw new UnsupportedOperationException("Invalid increment target");
155
-	}
156
-
157
-	@Override
158
-	public Void visitConstantUInt(ConstantUIntExpression expression) {
159
-		throw new UnsupportedOperationException("Invalid increment target");
160
-	}
161
-
162
-	@Override
163
-	public Void visitConstantULong(ConstantULongExpression expression) {
164
-		throw new UnsupportedOperationException("Invalid increment target");
165
-	}
166
-
167
-	@Override
168
-	public Void visitConstantUShort(ConstantUShortExpression expression) {
169
-		throw new UnsupportedOperationException("Invalid increment target");
170
-	}
171
-
172
-	@Override
173
-	public Void visitConstantUSize(ConstantUSizeExpression expression) {
174
-		throw new UnsupportedOperationException("Invalid increment target");
175
-	}
176
-
177
-	@Override
178
-	public Void visitConstructorThisCall(ConstructorThisCallExpression expression) {
179
-		throw new UnsupportedOperationException("Invalid increment target");
180
-	}
181
-
182
-	@Override
183
-	public Void visitConstructorSuperCall(ConstructorSuperCallExpression expression) {
184
-		throw new UnsupportedOperationException("Invalid increment target");
185
-	}
186
-
187
-	@Override
188
-	public Void visitEnumConstant(EnumConstantExpression expression) {
189
-		throw new UnsupportedOperationException("Invalid increment target");
190
-	}
191
-
192
-	@Override
193
-	public Void visitFunction(FunctionExpression expression) {
194
-		throw new UnsupportedOperationException("Invalid increment target");
195
-	}
196
-
197
-	@Override
198
-	public Void visitGetField(GetFieldExpression expression) {
199
-		Type objectType = context.getType(expression.target.type);
200
-		
201
-		int local = javaWriter.local(objectType);
202
-		expression.target.accept(expressionCompiler);
203
-		javaWriter.dup();
204
-		javaWriter.store(objectType, local);
205
-		
206
-        expressionCompiler.getField(expression.field);
207
-		
208
-		javaWriter.iConst1();
209
-		javaWriter.iAdd();
210
-		
211
-		javaWriter.load(objectType, local);
212
-		javaWriter.dupX1();
213
-        expressionCompiler.putField(expression.field);
214
-		return null;
215
-	}
216
-
217
-	@Override
218
-	public Void visitGetFunctionParameter(GetFunctionParameterExpression expression) {
219
-		JavaParameterInfo parameter = module.getParameterInfo(expression.parameter);
220
-		javaWriter.iinc(parameter.index);
221
-		javaWriter.load(parameter);
222
-		return null;
223
-	}
224
-
225
-	@Override
226
-	public Void visitGetLocalVariable(GetLocalVariableExpression expression) {
227
-		JavaLocalVariableInfo localVariable = javaWriter.getLocalVariable(expression.variable.variable);
228
-		javaWriter.iinc(localVariable.local);
229
-		javaWriter.load(localVariable);
230
-		return null;
231
-	}
232
-
233
-	@Override
234
-	public Void visitGetMatchingVariantField(GetMatchingVariantField expression) {
235
-		throw new UnsupportedOperationException("Invalid increment target");
236
-	}
237
-
238
-	@Override
239
-	public Void visitGetStaticField(GetStaticFieldExpression expression) {
240
-		expressionCompiler.getField(expression.field);
241
-		javaWriter.iConst1();
242
-		javaWriter.iAdd();
243
-		javaWriter.dup();
244
-		expressionCompiler.putField(expression.field);
245
-		return null;
246
-	}
247
-
248
-	@Override
249
-	public Void visitGetter(GetterExpression expression) {
250
-		throw new UnsupportedOperationException("Not yet supported");
251
-	}
252
-
253
-	@Override
254
-	public Void visitGlobal(GlobalExpression expression) {
255
-		throw new UnsupportedOperationException("Invalid increment target");
256
-	}
257
-
258
-	@Override
259
-	public Void visitGlobalCall(GlobalCallExpression expression) {
260
-		throw new UnsupportedOperationException("Invalid increment target");
261
-	}
262
-
263
-	@Override
264
-	public Void visitInterfaceCast(InterfaceCastExpression expression) {
265
-		throw new UnsupportedOperationException("Invalid increment target");
266
-	}
267
-
268
-	@Override
269
-	public Void visitIs(IsExpression expression) {
270
-		throw new UnsupportedOperationException("Invalid increment target");
271
-	}
272
-
273
-	@Override
274
-	public Void visitMakeConst(MakeConstExpression expression) {
275
-		throw new UnsupportedOperationException("Invalid increment target");
276
-	}
277
-
278
-	@Override
279
-	public Void visitMap(MapExpression expression) {
280
-		throw new UnsupportedOperationException("Invalid increment target");
281
-	}
282
-
283
-	@Override
284
-	public Void visitMatch(MatchExpression expression) {
285
-		throw new UnsupportedOperationException("Invalid increment target");
286
-	}
287
-
288
-	@Override
289
-	public Void visitNew(NewExpression expression) {
290
-		throw new UnsupportedOperationException("Invalid increment target");
291
-	}
292
-
293
-	@Override
294
-	public Void visitNull(NullExpression expression) {
295
-		throw new UnsupportedOperationException("Invalid increment target");
296
-	}
297
-
298
-	@Override
299
-	public Void visitOrOr(OrOrExpression expression) {
300
-		throw new UnsupportedOperationException("Invalid increment target");
301
-	}
302
-	
303
-	@Override
304
-	public Void visitPanic(PanicExpression expression) {
305
-		throw new UnsupportedOperationException("Invalid increment target");
306
-	}
307
-
308
-	@Override
309
-	public Void visitPostCall(PostCallExpression expression) {
310
-		throw new UnsupportedOperationException("Invalid increment target");
311
-	}
312
-
313
-	@Override
314
-	public Void visitRange(RangeExpression expression) {
315
-		throw new UnsupportedOperationException("Invalid increment target");
316
-	}
317
-	
318
-	@Override
319
-	public Void visitSameObject(SameObjectExpression expression) {
320
-		throw new UnsupportedOperationException("Invalid increment target");
321
-	}
322
-
323
-	@Override
324
-	public Void visitSetField(SetFieldExpression expression) {
325
-		throw new UnsupportedOperationException("Invalid increment target");
326
-	}
327
-
328
-	@Override
329
-	public Void visitSetFunctionParameter(SetFunctionParameterExpression expression) {
330
-		throw new UnsupportedOperationException("Invalid increment target");
331
-	}
332
-
333
-	@Override
334
-	public Void visitSetLocalVariable(SetLocalVariableExpression expression) {
335
-		throw new UnsupportedOperationException("Invalid increment target");
336
-	}
337
-
338
-	@Override
339
-	public Void visitSetStaticField(SetStaticFieldExpression expression) {
340
-		throw new UnsupportedOperationException("Invalid increment target");
341
-	}
342
-
343
-	@Override
344
-	public Void visitSetter(SetterExpression expression) {
345
-		throw new UnsupportedOperationException("Invalid increment target");
346
-	}
347
-
348
-	@Override
349
-	public Void visitStaticGetter(StaticGetterExpression expression) {
350
-		throw new UnsupportedOperationException("Invalid increment target");
351
-	}
352
-
353
-	@Override
354
-	public Void visitStaticSetter(StaticSetterExpression expression) {
355
-		throw new UnsupportedOperationException("Invalid increment target");
356
-	}
357
-	
358
-	@Override
359
-	public Void visitStorageCast(StorageCastExpression expression) {
360
-		throw new UnsupportedOperationException("Invalid increment target");
361
-	}
362
-	
363
-	@Override
364
-	public Void visitSupertypeCast(SupertypeCastExpression expression) {
365
-		throw new UnsupportedOperationException("Invalid increment target");
366
-	}
367
-
368
-	@Override
369
-	public Void visitThis(ThisExpression expression) {
370
-		throw new UnsupportedOperationException("Invalid increment target");
371
-	}
372
-
373
-	@Override
374
-	public Void visitThrow(ThrowExpression expression) {
375
-		throw new UnsupportedOperationException("Invalid increment target");
376
-	}
377
-
378
-	@Override
379
-	public Void visitTryConvert(TryConvertExpression expression) {
380
-		throw new UnsupportedOperationException("Invalid increment target");
381
-	}
382
-
383
-	@Override
384
-	public Void visitTryRethrowAsException(TryRethrowAsExceptionExpression expression) {
385
-		throw new UnsupportedOperationException("Invalid increment target");
386
-	}
387
-
388
-	@Override
389
-	public Void visitTryRethrowAsResult(TryRethrowAsResultExpression expression) {
390
-		throw new UnsupportedOperationException("Invalid increment target");
391
-	}
392
-
393
-	@Override
394
-	public Void visitVariantValue(VariantValueExpression expression) {
395
-		throw new UnsupportedOperationException("Invalid increment target");
396
-	}
397
-
398
-	@Override
399
-	public Void visitWrapOptional(WrapOptionalExpression expression) {
400
-		throw new UnsupportedOperationException("Invalid increment target");
401
-	}
402
-}

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

@@ -7,6 +7,7 @@ import org.openzen.zenscript.javabytecode.JavaLocalVariableInfo;
7 7
 
8 8
 import java.util.Arrays;
9 9
 import java.util.List;
10
+import org.openzen.zenscript.codemodel.type.BasicTypeID;
10 11
 import org.openzen.zenscript.codemodel.type.StringTypeID;
11 12
 import org.openzen.zenscript.javabytecode.JavaBytecodeContext;
12 13
 import org.openzen.zenscript.javashared.JavaCompiledModule;
@@ -80,6 +81,11 @@ public class JavaStatementVisitor implements StatementVisitor<Boolean> {
80 81
 	@Override
81 82
 	public Boolean visitExpression(ExpressionStatement statement) {
82 83
 		statement.expression.accept(expressionVisitor);
84
+		
85
+		// TODO: make the expression not push anything
86
+		if (statement.expression.type.type != BasicTypeID.VOID)
87
+			javaWriter.pop(CompilerUtils.isLarge(statement.expression.type));
88
+		
83 89
 		return false;
84 90
 	}
85 91
 

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

@@ -112,7 +112,8 @@ public class JavaWriter {
112 112
 
113 113
 		try {
114 114
 	        visitor.visitMaxs(0, 0);
115
-		} catch (ArrayIndexOutOfBoundsException ex) {
115
+		} catch (ArrayIndexOutOfBoundsException | NegativeArraySizeException ex) {
116
+			//ex.printStackTrace();
116 117
 			throw ex;
117 118
 		}
118 119
 		

+ 4
- 0
ScriptingExample/scripts/arithmetic.zs View File

@@ -0,0 +1,4 @@
1
+var j = 0;
2
+println(j);
3
+j++;
4
+println(j);

Loading…
Cancel
Save