This commit is contained in:
Luke 2025-04-22 17:37:44 +08:00
parent 98e9f6dd31
commit cc92ff577f
8 changed files with 57 additions and 43 deletions

View File

@ -30,53 +30,59 @@ public class ASTPrinter {
private static void printStatement(ASTStatement stmt, int level) {
String indent = INDENT.repeat(level);
if (stmt instanceof ASTReturn) {
switch (stmt) {
case ASTReturn astReturn -> {
System.out.print(indent + "Return: ");
printExpression(((ASTReturn) stmt).getExpression());
printExpression(astReturn.getExpression());
System.out.println();
} else if (stmt instanceof ASTDeclare) {
ASTDeclare d = (ASTDeclare) stmt;
}
case ASTDeclare d -> {
System.out.print(indent + "Declare " + d.getName() + ": " + d.getType());
if (d.getInitExpression() != null) {
System.out.print(" = "); printExpression(d.getInitExpression());
System.out.print(" = ");
printExpression(d.getInitExpression());
}
System.out.println();
} else if (stmt instanceof ASTLoop) {
ASTLoop loop = (ASTLoop) stmt;
}
case ASTLoop loop -> {
System.out.println(indent + "Loop:");
System.out.print(indent + " Init: "); printStatement(loop.getInit(), 0);
System.out.print(indent + " Condition: "); printExpression(loop.getCondition()); System.out.println();
System.out.print(indent + " Update: "); printStatement(loop.getUpdate(), 0);
System.out.print(indent + " Init: ");
printStatement(loop.getInit(), 0);
System.out.print(indent + " Condition: ");
printExpression(loop.getCondition());
System.out.println();
System.out.print(indent + " Update: ");
printStatement(loop.getUpdate(), 0);
System.out.println(indent + " Body:");
for (ASTStatement s : loop.getBody()) {
printStatement(s, level + 2);
}
} else if (stmt instanceof ASTIf) {
ASTIf ifs = (ASTIf) stmt;
System.out.print(indent + "If condition: "); printExpression(ifs.getCondition()); System.out.println();
}
case ASTIf ifs -> {
System.out.print(indent + "If condition: ");
printExpression(ifs.getCondition());
System.out.println();
System.out.println(indent + " Then:");
for (ASTStatement s : ifs.getThenStatements()) {
printStatement(s, level + 2);
}
} else {
System.out.println(indent + stmt.getClass().getSimpleName());
}
default -> System.out.println(indent + stmt.getClass().getSimpleName());
}
}
private static void printExpression(ASTExpression expr) {
if (expr instanceof ASTLiteral) {
System.out.print(((ASTLiteral) expr).getValue());
} else if (expr instanceof ASTIdentifier) {
System.out.print(((ASTIdentifier) expr).getName());
} else if (expr instanceof ASTBinaryOp) {
ASTBinaryOp bin = (ASTBinaryOp) expr;
switch (expr) {
case ASTLiteral astLiteral -> System.out.print(astLiteral.getValue());
case ASTIdentifier astIdentifier -> System.out.print(astIdentifier.getName());
case ASTBinaryOp bin -> {
System.out.print("(");
printExpression(bin.getLeft());
System.out.print(" " + bin.getOperator() + " ");
printExpression(bin.getRight());
System.out.print(")");
} else {
System.out.print(expr.getClass().getSimpleName());
}
default -> System.out.print(expr.getClass().getSimpleName());
}
}
}

View File

@ -0,0 +1,5 @@
package org.jcnc.snow.compiler.parser;
import org.jcnc.snow.compiler.parser.ast.ASTNode;
public abstract class ASTStatement extends ASTNode {}

View File

@ -1,6 +1,7 @@
package org.jcnc.snow.compiler.parser.ast;
import org.jcnc.snow.compiler.parser.ASTExpression;
import org.jcnc.snow.compiler.parser.ASTStatement;
public class ASTDeclare extends ASTStatement {
final String name, type;

View File

@ -1,5 +1,7 @@
package org.jcnc.snow.compiler.parser.ast;
import org.jcnc.snow.compiler.parser.ASTStatement;
import java.util.ArrayList;
import java.util.List;

View File

@ -1,6 +1,7 @@
package org.jcnc.snow.compiler.parser.ast;
import org.jcnc.snow.compiler.parser.ASTExpression;
import org.jcnc.snow.compiler.parser.ASTStatement;
import java.util.List;

View File

@ -1,6 +1,7 @@
package org.jcnc.snow.compiler.parser.ast;
import org.jcnc.snow.compiler.parser.ASTExpression;
import org.jcnc.snow.compiler.parser.ASTStatement;
import java.util.List;

View File

@ -1,6 +1,7 @@
package org.jcnc.snow.compiler.parser.ast;
import org.jcnc.snow.compiler.parser.ASTExpression;
import org.jcnc.snow.compiler.parser.ASTStatement;
public class ASTReturn extends ASTStatement {
final ASTExpression expr;

View File

@ -1,3 +0,0 @@
package org.jcnc.snow.compiler.parser.ast;
public abstract class ASTStatement extends ASTNode {}