Skip to content

Commit

Permalink
Perlito5 - misc/Java-Asm-Interpreter/MethodExecutorAsm debug on/off
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Aug 1, 2024
1 parent 71b4578 commit c57be46
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static Class<?> createClassWithMethod(EmitterContext ctx, String[] env, N

// Add static fields to the class for closure variables
for (String fieldName : env) {
System.out.println("Create static field: " + fieldName);
ctx.logDebug("Create static field: " + fieldName);
cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, fieldName, "LRuntime;", null, null).visitEnd();
}

Expand All @@ -58,7 +58,7 @@ public static Class<?> createClassWithMethod(EmitterContext ctx, String[] env, N
/*
for (int i = 0; i < env.length; i++) { // Initialize the static fields
String fieldName = env[i];
System.out.println("Init static field: " + fieldName);
ctx.logDebug("Init static field: " + fieldName);
ctx.mv.visitTypeInsn(Opcodes.NEW, "Runtime");
ctx.mv.visitInsn(Opcodes.DUP);
ctx.mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "Runtime", "<init>", "()V", false); // Create a new instance of Runtime
Expand All @@ -79,7 +79,7 @@ public static Class<?> createClassWithMethod(EmitterContext ctx, String[] env, N
ctx.mv.visitEnd();

// Create the main method for the generated class
System.out.println("Create the method");
ctx.logDebug("Create the method");
String returnType = "(LRuntime;LContextType;)Ljava/lang/Object;";

// Define the method as public and static
Expand All @@ -92,7 +92,7 @@ public static Class<?> createClassWithMethod(EmitterContext ctx, String[] env, N
// Skip indices 0 and 1 because they are reserved for special arguments ("@_" and call context)
for (int i = 2; i < env.length; i++) {
String fieldName = env[i];
System.out.println("Init closure variable: " + fieldName);
ctx.logDebug("Init closure variable: " + fieldName);
ctx.mv.visitFieldInsn(Opcodes.GETSTATIC, javaClassName, fieldName, "LRuntime;");
ctx.mv.visitVarInsn(Opcodes.ASTORE, i);
}
Expand All @@ -105,7 +105,7 @@ public static Class<?> createClassWithMethod(EmitterContext ctx, String[] env, N
ast.accept(visitor);

// Handle the return value
System.out.println("Return the last value");
ctx.logDebug("Return the last value");
ctx.mv.visitLabel(ctx.returnLabel); // "return" from other places arrive here
ctx.mv.visitInsn(Opcodes.ARETURN); // returns an Object
ctx.mv.visitMaxs(0, 0); // Automatically computed
Expand Down
10 changes: 5 additions & 5 deletions misc/Java-Asm-Interpreter/MethodExecutorAsm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public static void main(String[] args) {
ctx.symbolTable.addVariable("@_"); // Argument list is local variable 0
ctx.symbolTable.addVariable("wantarray"); // Call context is local variable 1

System.out.println("parse code: " + code);
System.out.println(" call context " + ctx.contextType);
ctx.logDebug("parse code: " + code);
ctx.logDebug(" call context " + ctx.contextType);

// Create the Token list
Lexer lexer = new Lexer(code);
Expand All @@ -72,10 +72,10 @@ public static void main(String[] args) {
ErrorMessageUtil errorUtil = new ErrorMessageUtil(ctx.fileName, tokens);
Parser parser = new Parser(errorUtil, tokens); // Parse the tokens
Node ast = parser.parse(); // Generate the abstract syntax tree (AST)
System.out.println("-- AST:\n" + ast + "--\n");
ctx.logDebug("-- AST:\n" + ast + "--\n");

// Create the Java class from the AST
System.out.println("createClassWithMethod");
ctx.logDebug("createClassWithMethod");
// Create a new instance of ErrorMessageUtil, resetting the line counter
ctx.errorUtil = new ErrorMessageUtil(ctx.fileName, tokens);
Class<?> generatedClass = ASMMethodCreator.createClassWithMethod(
Expand All @@ -91,7 +91,7 @@ public static void main(String[] args) {
Runtime result = anonSub.apply(new Runtime(999), ContextType.SCALAR); // Execute the generated method

// Print the result of the execution
System.out.println("Result of generatedMethod: " + result);
ctx.logDebug("Result of generatedMethod: " + result);
} catch (Exception e) {
e.printStackTrace(); // Print any exceptions that occur during the process
}
Expand Down
3 changes: 0 additions & 3 deletions misc/Java-Asm-Interpreter/MethodExecutorAsm/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,11 @@ private Node parseDoubleQuotedString() {
}
} else if (text.equals("$")) {
if (str.length() > 0) {
System.out.println("add string part " + str.toString());
parts.add(new StringNode(str.toString(), tokenIndex)); // string so far
str = new StringBuilder(); // continue
}
Token nextToken = peek();
if (nextToken.type == TokenType.IDENTIFIER) {
System.out.println("add string variable");
parts.add(new UnaryOperatorNode("$", new IdentifierNode(consume().text, tokenIndex), tokenIndex));
} else if (nextToken.type == TokenType.OPERATOR && nextToken.text.equals("{")) {
consume(); // consume '{'
Expand All @@ -254,7 +252,6 @@ private Node parseDoubleQuotedString() {
token = tokens.get(tokenIndex);
}
if (str.length() > 0) {
System.out.println("add string last part " + str.toString());
parts.add(new StringNode(str.toString(), tokenIndex));
}
consume(TokenType.OPERATOR, "\""); // Consume the closing double quote
Expand Down

0 comments on commit c57be46

Please sign in to comment.