Skip to content
Snippets Groups Projects
Commit 98738c94 authored by Leo's avatar Leo
Browse files

feat: add performance exec time tracking for the parser

parent 2720b349
No related branches found
No related tags found
No related merge requests found
import frontend.Performance;
import frontend.ast.ASTPrettyPrinter; import frontend.ast.ASTPrettyPrinter;
import frontend.ast.ProgramStmt; import frontend.ast.ProgramStmt;
import frontend.lexer.CodeReconstructor; import frontend.lexer.CodeReconstructor;
...@@ -108,7 +109,10 @@ public class Main { ...@@ -108,7 +109,10 @@ public class Main {
} }
System.out.println(Colors.BLUE + "Output:"); System.out.println(Colors.BLUE + "Output:");
System.out.println("=".repeat(25) + "\n" + Colors.RESET); System.out.println("=".repeat(25) + "\n" + Colors.RESET);
Interpreter.evaluate(program, rootEnv, debug); boolean finalDebug = debug;
Performance.timeFunction(() -> {
Interpreter.evaluate(program, rootEnv, finalDebug);
}, "Executing program");
} catch (Parser.ParserException e) { } catch (Parser.ParserException e) {
System.out.println("Parser error: " + e.getMessage()); System.out.println("Parser error: " + e.getMessage());
} catch (Exception e) { } catch (Exception e) {
......
package frontend;
public class Performance {
public static void timeFunction(Runnable function) {
long startTime = System.nanoTime();
function.run();
long endTime = System.nanoTime();
long duration = (endTime - startTime) / 1000000;
System.out.println(Constants.Colors.YELLOW + "Execution time: " + duration + "ms" + Constants.Colors.RESET);
}
public static void timeFunction(Runnable function, String message) {
System.out.println(Constants.Colors.YELLOW + message + Constants.Colors.RESET);
timeFunction(function);
}
}
package frontend.parser; package frontend.parser;
import frontend.Constants; import frontend.Constants;
import frontend.Performance;
import frontend.ast.*; import frontend.ast.*;
import frontend.ast.object.ObjectLiteral; import frontend.ast.object.ObjectLiteral;
import frontend.ast.object.PropertyLiteral; import frontend.ast.object.PropertyLiteral;
...@@ -67,7 +68,10 @@ public class Parser { ...@@ -67,7 +68,10 @@ public class Parser {
*/ */
public ProgramStmt parse(String sourceCode) throws ParserException { public ProgramStmt parse(String sourceCode) throws ParserException {
Lexer lexer = new Lexer(); Lexer lexer = new Lexer();
Performance.timeFunction(() -> {
this.tokens = lexer.tokenize(sourceCode); this.tokens = lexer.tokenize(sourceCode);
System.out.println(Constants.Colors.YELLOW + "Source code tokens: " + this.tokens.size() + Constants.Colors.RESET);
}, "Lexing source code...");
this.reset(); this.reset();
if (this.debug) { if (this.debug) {
for (Token token : this.tokens) { for (Token token : this.tokens) {
...@@ -75,6 +79,7 @@ public class Parser { ...@@ -75,6 +79,7 @@ public class Parser {
} }
} }
List<Stmt> programBody = new ArrayList<>(); List<Stmt> programBody = new ArrayList<>();
Performance.timeFunction(() -> {
this.print("Parsing program body, is not EOF: " + this.notEOF()); this.print("Parsing program body, is not EOF: " + this.notEOF());
while (this.notEOF()) { while (this.notEOF()) {
this.print("Pre-parse statement: " + this.peek()); this.print("Pre-parse statement: " + this.peek());
...@@ -89,9 +94,15 @@ public class Parser { ...@@ -89,9 +94,15 @@ public class Parser {
ProgramStmt program = new ProgramStmt(1, programBody); ProgramStmt program = new ProgramStmt(1, programBody);
System.out.println(ASTPrettyPrinter.toRepr(program, 0, 2)); System.out.println(ASTPrettyPrinter.toRepr(program, 0, 2));
} }
try {
throw e; throw e;
} catch (ParserException ex) {
throw new RuntimeException(ex);
}
} }
} }
System.out.println(Constants.Colors.YELLOW + "Program body statement count: " + programBody.size() + Constants.Colors.RESET);
}, "Parsing program body...");
ProgramStmt program = new ProgramStmt(1, programBody); ProgramStmt program = new ProgramStmt(1, programBody);
this.print("Parsed program: " + program); this.print("Parsed program: " + program);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment