Skip to content

Commit 8ff10a5

Browse files
committed
Perlito5 - misc/Java-Asm-Interpreter/MethodExecutorAsm refactor
1 parent 493bde2 commit 8ff10a5

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

misc/Java-Asm-Interpreter/MethodExecutorAsm/EmitterVisitor.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public EmitterVisitor with(ContextType contextType) {
3838

3939
@Override
4040
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+
}
4145
boolean isInteger = !node.value.contains(".");
4246
if (ctx.isBoxed) { // expect a Runtime object
4347
if (isInteger) {
@@ -62,10 +66,6 @@ public void visit(NumberNode node) {
6266
ctx.mv.visitLdcInsn(Double.parseDouble(node.value)); // emit native double
6367
}
6468
}
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-
}
6969
}
7070

7171
@Override
@@ -80,8 +80,8 @@ public void visit(IdentifierNode node) throws Exception {
8080
*
8181
* @param operator The name of the built-in method to call.
8282
*/
83-
private void emitCallBuiltin(String operator) {
84-
ctx.mv.visitMethodInsn(
83+
private void handleBinaryBuiltin(String operator) {
84+
ctx.mv.visitMethodInsn(
8585
Opcodes.INVOKEVIRTUAL,
8686
"Runtime",
8787
operator,
@@ -94,34 +94,36 @@ private void emitCallBuiltin(String operator) {
9494

9595
@Override
9696
public void visit(BinaryOperatorNode node) throws Exception {
97+
String operator = node.operator;
98+
System.out.println("visit(BinaryOperatorNode) " + operator + " in context " + ctx.contextType);
9799
EmitterVisitor scalarVisitor = this.with(ContextType.SCALAR); // execute operands in scalar context
98100
node.left.accept(scalarVisitor); // target
99101
node.right.accept(scalarVisitor); // parameter
100102

101-
switch (node.operator) {
103+
switch (operator) {
102104
case "+":
103-
emitCallBuiltin("add"); // TODO optimize use: ctx.mv.visitInsn(Opcodes.IADD)
105+
handleBinaryBuiltin("add"); // TODO optimize use: ctx.mv.visitInsn(Opcodes.IADD)
104106
break;
105107
case "-":
106-
emitCallBuiltin("subtract");
108+
handleBinaryBuiltin("subtract");
107109
break;
108110
case "*":
109-
emitCallBuiltin("multiply");
111+
handleBinaryBuiltin("multiply");
110112
break;
111113
case "/":
112-
emitCallBuiltin("divide");
114+
handleBinaryBuiltin("divide");
113115
break;
114116
case ".":
115-
emitCallBuiltin("stringConcat");
117+
handleBinaryBuiltin("stringConcat");
116118
break;
117119
case "=":
118-
emitCallBuiltin("set");
120+
handleBinaryBuiltin("set");
119121
break;
120122
case "->":
121123
handleArrowOperator(node);
122124
break;
123125
default:
124-
throw new RuntimeException("Unexpected infix operator: " + node.operator);
126+
throw new RuntimeException("Unexpected infix operator: " + operator);
125127
}
126128
}
127129

@@ -152,9 +154,8 @@ private void handleArrowOperator(BinaryOperatorNode node) throws Exception {
152154

153155
@Override
154156
public void visit(UnaryOperatorNode node) throws Exception {
155-
// Emit code for unary operator
156157
String operator = node.operator;
157-
System.out.println("visit(UnaryOperatorNode) " + operator);
158+
System.out.println("visit(UnaryOperatorNode) " + operator + " in context " + ctx.contextType);
158159

159160
switch (operator) {
160161
case "$":

0 commit comments

Comments
 (0)