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) {
|
private static void printStatement(ASTStatement stmt, int level) {
|
||||||
String indent = INDENT.repeat(level);
|
String indent = INDENT.repeat(level);
|
||||||
if (stmt instanceof ASTReturn) {
|
switch (stmt) {
|
||||||
System.out.print(indent + "Return: ");
|
case ASTReturn astReturn -> {
|
||||||
printExpression(((ASTReturn) stmt).getExpression());
|
System.out.print(indent + "Return: ");
|
||||||
System.out.println();
|
printExpression(astReturn.getExpression());
|
||||||
} else if (stmt instanceof ASTDeclare) {
|
System.out.println();
|
||||||
ASTDeclare d = (ASTDeclare) stmt;
|
|
||||||
System.out.print(indent + "Declare " + d.getName() + ": " + d.getType());
|
|
||||||
if (d.getInitExpression() != null) {
|
|
||||||
System.out.print(" = "); printExpression(d.getInitExpression());
|
|
||||||
}
|
}
|
||||||
System.out.println();
|
case ASTDeclare d -> {
|
||||||
} else if (stmt instanceof ASTLoop) {
|
System.out.print(indent + "Declare " + d.getName() + ": " + d.getType());
|
||||||
ASTLoop loop = (ASTLoop) stmt;
|
if (d.getInitExpression() != null) {
|
||||||
System.out.println(indent + "Loop:");
|
System.out.print(" = ");
|
||||||
System.out.print(indent + " Init: "); printStatement(loop.getInit(), 0);
|
printExpression(d.getInitExpression());
|
||||||
System.out.print(indent + " Condition: "); printExpression(loop.getCondition()); System.out.println();
|
}
|
||||||
System.out.print(indent + " Update: "); printStatement(loop.getUpdate(), 0);
|
System.out.println();
|
||||||
System.out.println(indent + " Body:");
|
|
||||||
for (ASTStatement s : loop.getBody()) {
|
|
||||||
printStatement(s, level + 2);
|
|
||||||
}
|
}
|
||||||
} else if (stmt instanceof ASTIf) {
|
case ASTLoop loop -> {
|
||||||
ASTIf ifs = (ASTIf) stmt;
|
System.out.println(indent + "Loop:");
|
||||||
System.out.print(indent + "If condition: "); printExpression(ifs.getCondition()); System.out.println();
|
System.out.print(indent + " Init: ");
|
||||||
System.out.println(indent + " Then:");
|
printStatement(loop.getInit(), 0);
|
||||||
for (ASTStatement s : ifs.getThenStatements()) {
|
System.out.print(indent + " Condition: ");
|
||||||
printStatement(s, level + 2);
|
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 {
|
case ASTIf ifs -> {
|
||||||
System.out.println(indent + stmt.getClass().getSimpleName());
|
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) {
|
private static void printExpression(ASTExpression expr) {
|
||||||
if (expr instanceof ASTLiteral) {
|
switch (expr) {
|
||||||
System.out.print(((ASTLiteral) expr).getValue());
|
case ASTLiteral astLiteral -> System.out.print(astLiteral.getValue());
|
||||||
} else if (expr instanceof ASTIdentifier) {
|
case ASTIdentifier astIdentifier -> System.out.print(astIdentifier.getName());
|
||||||
System.out.print(((ASTIdentifier) expr).getName());
|
case ASTBinaryOp bin -> {
|
||||||
} else if (expr instanceof ASTBinaryOp) {
|
System.out.print("(");
|
||||||
ASTBinaryOp bin = (ASTBinaryOp) expr;
|
printExpression(bin.getLeft());
|
||||||
System.out.print("(");
|
System.out.print(" " + bin.getOperator() + " ");
|
||||||
printExpression(bin.getLeft());
|
printExpression(bin.getRight());
|
||||||
System.out.print(" " + bin.getOperator() + " ");
|
System.out.print(")");
|
||||||
printExpression(bin.getRight());
|
}
|
||||||
System.out.print(")");
|
default -> System.out.print(expr.getClass().getSimpleName());
|
||||||
} else {
|
|
||||||
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;
|
package org.jcnc.snow.compiler.parser.ast;
|
||||||
|
|
||||||
import org.jcnc.snow.compiler.parser.ASTExpression;
|
import org.jcnc.snow.compiler.parser.ASTExpression;
|
||||||
|
import org.jcnc.snow.compiler.parser.ASTStatement;
|
||||||
|
|
||||||
public class ASTDeclare extends ASTStatement {
|
public class ASTDeclare extends ASTStatement {
|
||||||
final String name, type;
|
final String name, type;
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package org.jcnc.snow.compiler.parser.ast;
|
package org.jcnc.snow.compiler.parser.ast;
|
||||||
|
|
||||||
|
import org.jcnc.snow.compiler.parser.ASTStatement;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.snow.compiler.parser.ast;
|
package org.jcnc.snow.compiler.parser.ast;
|
||||||
|
|
||||||
import org.jcnc.snow.compiler.parser.ASTExpression;
|
import org.jcnc.snow.compiler.parser.ASTExpression;
|
||||||
|
import org.jcnc.snow.compiler.parser.ASTStatement;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.snow.compiler.parser.ast;
|
package org.jcnc.snow.compiler.parser.ast;
|
||||||
|
|
||||||
import org.jcnc.snow.compiler.parser.ASTExpression;
|
import org.jcnc.snow.compiler.parser.ASTExpression;
|
||||||
|
import org.jcnc.snow.compiler.parser.ASTStatement;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package org.jcnc.snow.compiler.parser.ast;
|
package org.jcnc.snow.compiler.parser.ast;
|
||||||
|
|
||||||
import org.jcnc.snow.compiler.parser.ASTExpression;
|
import org.jcnc.snow.compiler.parser.ASTExpression;
|
||||||
|
import org.jcnc.snow.compiler.parser.ASTStatement;
|
||||||
|
|
||||||
public class ASTReturn extends ASTStatement {
|
public class ASTReturn extends ASTStatement {
|
||||||
final ASTExpression expr;
|
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