package org.jcnc.snow.compiler; import org.jcnc.snow.compiler.lexer.LexerEngine; import org.jcnc.snow.compiler.lexer.token.Token; import org.jcnc.snow.compiler.lexer.utils.TokenPrinter; import org.jcnc.snow.compiler.parser.ParserEngine; import org.jcnc.snow.compiler.parser.context.ParserContext; import org.jcnc.snow.compiler.parser.ast.Node; import org.jcnc.snow.compiler.parser.ast.ASTPrinter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public class Main { public static void main(String[] args) throws IOException { // 读取源文件 String filePath = args.length > 0 ? args[0] : "test"; String source = Files.readString(Path.of(filePath), StandardCharsets.UTF_8); // 1. 词法分析 LexerEngine lexerEngine = new LexerEngine(source); List tokens = lexerEngine.getAllTokens(); TokenPrinter.printTokens(tokens); // 可选:打印 Token 列表 // 2. 语法分析 ParserContext ctx = new ParserContext(tokens); List ast = new ParserEngine(ctx).parse(); // 3. 可读地打印 AST ASTPrinter.print(ast); // // 1) 输出紧凑 JSON // String compact = ASTJsonSerializer.toJsonString(ast); // System.out.println("=== Compact JSON ==="); // System.out.println(compact); // // // 2) 输出美化后的 JSON // System.out.println("=== Pretty JSON ==="); // System.out.println(JsonFormatter.prettyPrint(compact)); } }