@@ -38,6 +38,10 @@ public EmitterVisitor with(ContextType contextType) {
38
38
39
39
@ Override
40
40
public void visit (NumberNode node ) {
41
+ System .out .println ("visit(NumberNode) in context " + ctx .contextType );
42
+ if (ctx .contextType == ContextType .VOID ) {
43
+ ctx .mv .visitInsn (Opcodes .POP );
44
+ }
41
45
boolean isInteger = !node .value .contains ("." );
42
46
if (ctx .isBoxed ) { // expect a Runtime object
43
47
if (isInteger ) {
@@ -62,10 +66,6 @@ public void visit(NumberNode node) {
62
66
ctx .mv .visitLdcInsn (Double .parseDouble (node .value )); // emit native double
63
67
}
64
68
}
65
- System .out .println ("Emit context " + (ctx .contextType == ContextType .VOID ? "void" : "scalar" ));
66
- if (ctx .contextType == ContextType .VOID ) {
67
- ctx .mv .visitInsn (Opcodes .POP );
68
- }
69
69
}
70
70
71
71
@ Override
@@ -80,8 +80,8 @@ public void visit(IdentifierNode node) throws Exception {
80
80
*
81
81
* @param operator The name of the built-in method to call.
82
82
*/
83
- private void emitCallBuiltin (String operator ) {
84
- ctx .mv .visitMethodInsn (
83
+ private void handleBinaryBuiltin (String operator ) {
84
+ ctx .mv .visitMethodInsn (
85
85
Opcodes .INVOKEVIRTUAL ,
86
86
"Runtime" ,
87
87
operator ,
@@ -94,34 +94,36 @@ private void emitCallBuiltin(String operator) {
94
94
95
95
@ Override
96
96
public void visit (BinaryOperatorNode node ) throws Exception {
97
+ String operator = node .operator ;
98
+ System .out .println ("visit(BinaryOperatorNode) " + operator + " in context " + ctx .contextType );
97
99
EmitterVisitor scalarVisitor = this .with (ContextType .SCALAR ); // execute operands in scalar context
98
100
node .left .accept (scalarVisitor ); // target
99
101
node .right .accept (scalarVisitor ); // parameter
100
102
101
- switch (node . operator ) {
103
+ switch (operator ) {
102
104
case "+" :
103
- emitCallBuiltin ("add" ); // TODO optimize use: ctx.mv.visitInsn(Opcodes.IADD)
105
+ handleBinaryBuiltin ("add" ); // TODO optimize use: ctx.mv.visitInsn(Opcodes.IADD)
104
106
break ;
105
107
case "-" :
106
- emitCallBuiltin ("subtract" );
108
+ handleBinaryBuiltin ("subtract" );
107
109
break ;
108
110
case "*" :
109
- emitCallBuiltin ("multiply" );
111
+ handleBinaryBuiltin ("multiply" );
110
112
break ;
111
113
case "/" :
112
- emitCallBuiltin ("divide" );
114
+ handleBinaryBuiltin ("divide" );
113
115
break ;
114
116
case "." :
115
- emitCallBuiltin ("stringConcat" );
117
+ handleBinaryBuiltin ("stringConcat" );
116
118
break ;
117
119
case "=" :
118
- emitCallBuiltin ("set" );
120
+ handleBinaryBuiltin ("set" );
119
121
break ;
120
122
case "->" :
121
123
handleArrowOperator (node );
122
124
break ;
123
125
default :
124
- throw new RuntimeException ("Unexpected infix operator: " + node . operator );
126
+ throw new RuntimeException ("Unexpected infix operator: " + operator );
125
127
}
126
128
}
127
129
@@ -152,9 +154,8 @@ private void handleArrowOperator(BinaryOperatorNode node) throws Exception {
152
154
153
155
@ Override
154
156
public void visit (UnaryOperatorNode node ) throws Exception {
155
- // Emit code for unary operator
156
157
String operator = node .operator ;
157
- System .out .println ("visit(UnaryOperatorNode) " + operator );
158
+ System .out .println ("visit(UnaryOperatorNode) " + operator + " in context " + ctx . contextType );
158
159
159
160
switch (operator ) {
160
161
case "$" :
0 commit comments