feat:ast
This commit is contained in:
parent
98e9f6dd31
commit
cc92ff577f
@ -30,53 +30,59 @@ public class ASTPrinter {
|
||||
|
||||
private static void printStatement(ASTStatement stmt, int level) {
|
||||
String indent = INDENT.repeat(level);
|
||||
if (stmt instanceof ASTReturn) {
|
||||
System.out.print(indent + "Return: ");
|
||||
printExpression(((ASTReturn) stmt).getExpression());
|
||||
System.out.println();
|
||||
} else if (stmt instanceof ASTDeclare) {
|
||||
ASTDeclare d = (ASTDeclare) stmt;
|
||||
System.out.print(indent + "Declare " + d.getName() + ": " + d.getType());
|
||||
if (d.getInitExpression() != null) {
|
||||
System.out.print(" = "); printExpression(d.getInitExpression());
|
||||
switch (stmt) {
|
||||
case ASTReturn astReturn -> {
|
||||
System.out.print(indent + "Return: ");
|
||||
printExpression(astReturn.getExpression());
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println();
|
||||
} else if (stmt instanceof ASTLoop) {
|
||||
ASTLoop loop = (ASTLoop) stmt;
|
||||
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.println(indent + " Body:");
|
||||
for (ASTStatement s : loop.getBody()) {
|
||||
printStatement(s, level + 2);
|
||||
case ASTDeclare d -> {
|
||||
System.out.print(indent + "Declare " + d.getName() + ": " + d.getType());
|
||||
if (d.getInitExpression() != null) {
|
||||
System.out.print(" = ");
|
||||
printExpression(d.getInitExpression());
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
} else if (stmt instanceof ASTIf) {
|
||||
ASTIf ifs = (ASTIf) stmt;
|
||||
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);
|
||||
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.println(indent + " Body:");
|
||||
for (ASTStatement s : loop.getBody()) {
|
||||
printStatement(s, level + 2);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println(indent + stmt.getClass().getSimpleName());
|
||||
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);
|
||||
}
|
||||
}
|
||||
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;
|
||||
System.out.print("(");
|
||||
printExpression(bin.getLeft());
|
||||
System.out.print(" " + bin.getOperator() + " ");
|
||||
printExpression(bin.getRight());
|
||||
System.out.print(")");
|
||||
} else {
|
||||
System.out.print(expr.getClass().getSimpleName());
|
||||
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(")");
|
||||
}
|
||||
default -> System.out.print(expr.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package org.jcnc.snow.compiler.parser;
|
||||
|
||||
import org.jcnc.snow.compiler.parser.ast.ASTNode;
|
||||
|
||||
public abstract class ASTStatement extends ASTNode {}
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
package org.jcnc.snow.compiler.parser.ast;
|
||||
|
||||
public abstract class ASTStatement extends ASTNode {}
|
||||
Loading…
x
Reference in New Issue
Block a user