-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (61 loc) · 1.23 KB
/
main.go
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
package main
import (
"bufio"
"dot/eval"
"dot/lexer"
"dot/object"
"dot/parser"
"fmt"
"os"
)
func main() {
if os.Args[1] == "repl" {
startRepl()
return
}
filename := os.Args[1]
if filename == "" {
fmt.Printf("Usage: %s <filename>\n", filename)
}
content, err := os.ReadFile(filename)
if err != nil {
fmt.Println(err)
return
}
contentStr := string(content)
lexer := lexer.NewLexer(contentStr)
parser := parser.NewParser(lexer)
program := parser.ParseProgram()
parser.PrintErrors()
// fmt.Println(program.String())
env := object.NewEnvironment()
evaluated := eval.Eval(program, env, *lexer)
fmt.Print(evaluated.String())
}
func startRepl() {
in := os.Stdin
out := os.Stdout
scanner := bufio.NewScanner(in)
env := object.NewEnvironment()
fmt.Println("Welcome to Dot programming language")
fmt.Println("Type '.exit' to exit")
for {
fmt.Print(">> ")
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()
if line == ".exit" {
return
}
lexer := lexer.NewLexer(line)
parser := parser.NewParser(lexer)
program := parser.ParseProgram()
parser.PrintErrors()
evaluated := eval.Eval(program, env, *lexer)
if evaluated != nil {
fmt.Fprintln(out, evaluated.String())
}
}
}