8
8
*
9
9
* @author aNNiMON
10
10
*/
11
- public final class UnaryExpression implements Expression {
12
-
11
+ public final class UnaryExpression implements Expression , Statement {
12
+
13
13
public static enum Operator {
14
+ INCREMENT_PREFIX ("++" ),
15
+ DECREMENT_PREFIX ("--" ),
16
+ INCREMENT_POSTFIX ("++" ),
17
+ DECREMENT_POSTFIX ("--" ),
14
18
NEGATE ("-" ),
15
19
// Boolean
16
20
NOT ("!" ),
@@ -37,10 +41,41 @@ public UnaryExpression(Operator operation, Expression expr1) {
37
41
this .expr1 = expr1 ;
38
42
}
39
43
44
+ @ Override
45
+ public void execute () {
46
+ eval ();
47
+ }
48
+
40
49
@ Override
41
50
public Value eval () {
42
51
final Value value = expr1 .eval ();
43
52
switch (operation ) {
53
+ case INCREMENT_PREFIX : {
54
+ if (expr1 instanceof Accessible ) {
55
+ return ((Accessible ) expr1 ).set (new NumberValue (value .asNumber () + 1 ));
56
+ }
57
+ return new NumberValue (value .asNumber () + 1 );
58
+ }
59
+ case DECREMENT_PREFIX : {
60
+ if (expr1 instanceof Accessible ) {
61
+ return ((Accessible ) expr1 ).set (new NumberValue (value .asNumber () - 1 ));
62
+ }
63
+ return new NumberValue (value .asNumber () - 1 );
64
+ }
65
+ case INCREMENT_POSTFIX : {
66
+ if (expr1 instanceof Accessible ) {
67
+ ((Accessible ) expr1 ).set (new NumberValue (value .asNumber () + 1 ));
68
+ return value ;
69
+ }
70
+ return new NumberValue (value .asNumber () + 1 );
71
+ }
72
+ case DECREMENT_POSTFIX : {
73
+ if (expr1 instanceof Accessible ) {
74
+ ((Accessible ) expr1 ).set (new NumberValue (value .asNumber () - 1 ));
75
+ return value ;
76
+ }
77
+ return new NumberValue (value .asNumber () - 1 );
78
+ }
44
79
case NEGATE : return new NumberValue (-value .asNumber ());
45
80
case COMPLEMENT : return new NumberValue (~(int )value .asNumber ());
46
81
case NOT : return new NumberValue (value .asNumber () != 0 ? 0 : 1 );
0 commit comments