-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLox.fs
71 lines (52 loc) · 1.71 KB
/
Lox.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace LoxFs
open System
open System.IO
open Scanner
open Error
open Parser
open Interpreter
open System.Diagnostics
module LoxFs =
[<EntryPoint>]
let main argv =
let errorHandler = ErrorHandler(false)
let usage code =
printfn "Usage: dotnet run [script]"
Environment.Exit code
let run source =
let sw = Stopwatch()
sw.Start()
let scanner = Scanner(source, errorHandler)
let tokens = scanner.ScanTokens
sw.Stop()
printfn $"DEBUG Scanned {tokens.Length} Token(s) In {sw.ElapsedMilliseconds}ms"
sw.Reset()
sw.Start()
let parser = Parser(tokens)
let statements = parser.Start()
sw.Stop()
printfn $"DEBUG Parsed {statements.Length} Statements(s) In {sw.ElapsedMilliseconds}ms"
sw.Reset()
sw.Start()
let interpreter = Interpreter()
interpreter.Interpret statements |> ignore
sw.Stop()
printfn $"DEBUG Interpreted In {sw.ElapsedMilliseconds}ms"
()
let runFile path =
let file = File.ReadAllText path
match errorHandler.HadError with
| true -> Environment.Exit 65
| false -> run file
let rec runPrompt code =
printf "> "
let line = Console.ReadLine()
run line
errorHandler.SetError false
runPrompt code
let exitCode = 64
match argv.Length with
| 2 -> usage exitCode
| 1 -> runPrompt ""
| _ -> runFile "test.lox"
0