1
1
package com .annimon .ownlang ;
2
2
3
3
import com .annimon .ownlang .lib .CallStack ;
4
- import com .annimon .ownlang .lib .Function ;
5
- import com .annimon .ownlang .lib .Functions ;
6
4
import com .annimon .ownlang .parser .Lexer ;
7
5
import com .annimon .ownlang .parser .Parser ;
8
6
import com .annimon .ownlang .parser .Token ;
14
12
import java .nio .file .Files ;
15
13
import java .nio .file .Paths ;
16
14
import java .util .List ;
15
+ import java .util .concurrent .TimeUnit ;
17
16
18
17
/**
19
18
* @author aNNiMON
@@ -22,11 +21,11 @@ public final class Main {
22
21
23
22
public static void main (String [] args ) throws IOException {
24
23
if (args .length == 0 ) {
25
- run (readFile ("program.own" ), true , true );
24
+ run (readFile ("program.own" ), true , true , true );
26
25
return ;
27
26
}
28
27
29
- boolean showTokens = false , showAst = false ;
28
+ boolean showTokens = false , showAst = false , showMeasurements = false ;
30
29
String input = null ;
31
30
for (int i = 0 ; i < args .length ; i ++) {
32
31
switch (args [i ]) {
@@ -40,6 +39,11 @@ public static void main(String[] args) throws IOException {
40
39
showTokens = true ;
41
40
break ;
42
41
42
+ case "-m" :
43
+ case "--showtime" :
44
+ showMeasurements = true ;
45
+ break ;
46
+
43
47
case "-f" :
44
48
case "--file" :
45
49
if (i + 1 < args .length ) {
@@ -55,23 +59,28 @@ public static void main(String[] args) throws IOException {
55
59
if (input == null ) {
56
60
throw new IllegalArgumentException ("Empty input" );
57
61
}
58
- run (input , showTokens , showAst );
62
+ run (input , showTokens , showAst , showMeasurements );
59
63
}
60
64
61
65
private static String readFile (String file ) throws IOException {
62
66
return new String ( Files .readAllBytes (Paths .get (file )), "UTF-8" );
63
67
}
64
68
65
- private static void run (String input , boolean showTokens , boolean showAst ) {
69
+ private static void run (String input , boolean showTokens , boolean showAst , boolean showMeasurements ) {
70
+ final TimeMeasurement measurement = new TimeMeasurement ();
71
+ measurement .start ("Tokenize time" );
66
72
final List <Token > tokens = new Lexer (input ).tokenize ();
73
+ measurement .stop ("Tokenize time" );
67
74
if (showTokens ) {
68
75
for (int i = 0 ; i < tokens .size (); i ++) {
69
76
System .out .println (i + " " + tokens .get (i ));
70
77
}
71
78
}
72
79
80
+ measurement .start ("Parse time" );
73
81
final Parser parser = new Parser (tokens );
74
82
final Statement program = parser .parse ();
83
+ measurement .stop ("Parse time" );
75
84
if (showAst ) {
76
85
System .out .println (program .toString ());
77
86
}
@@ -83,9 +92,15 @@ private static void run(String input, boolean showTokens, boolean showAst) {
83
92
// program.accept(new VariablePrinter());
84
93
program .accept (new AssignValidator ());
85
94
try {
95
+ measurement .start ("Execution time" );
86
96
program .execute ();
87
97
} catch (Exception ex ) {
88
98
handleException (Thread .currentThread (), ex );
99
+ } finally {
100
+ if (showMeasurements ) {
101
+ measurement .stop ("Execution time" );
102
+ System .out .println (measurement .summary (TimeUnit .MILLISECONDS , true ));
103
+ }
89
104
}
90
105
}
91
106
0 commit comments