Tests and Main

This commit is contained in:
i22011 2024-05-08 11:25:16 +02:00
parent d0448b01cd
commit ed6fce0b5a
3 changed files with 20 additions and 5 deletions

BIN
Tester.class Normal file

Binary file not shown.

View File

@ -1,5 +1,5 @@
public class Tester { public class Tester {
public static void main(String[] args) { public static void main(String[] args) {
new Test(); new Example();
} }
} }

View File

@ -8,10 +8,23 @@ import org.antlr.v4.runtime.tree.ParseTree;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser; import parser.generated.SimpleJavaParser;
import java.io.IOException;
import java.nio.file.Paths;
public class Main { public class Main {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// Assuming args[0] contains the path to the input file CharStream codeCharStream = null;
CharStream codeCharStream = CharStreams.fromString("class Example { }"); try {
codeCharStream = CharStreams.fromPath(Paths.get("path/to/your/file.txt"));
parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
static void parsefile(CharStream codeCharStream){
// CharStream codeCharStream = CharStreams.fromString("class Example { } class Example2 { }");
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
SimpleJavaParser parser = new SimpleJavaParser(tokens); SimpleJavaParser parser = new SimpleJavaParser(tokens);
@ -22,8 +35,10 @@ public class Main {
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
// Optionally print or process the AST // Optionally print or process the AST
System.out.println("Parsed " + ast.classes.size() + " classes."); System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:");
System.out.println(ast.classes.getFirst().identifier); for (ClassDeclarationNode classNode : ast.classes) {
System.out.println(classNode.identifier);
}
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(ast); byteCodeGenerator.generateByteCode(ast);