-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (50 loc) · 1.2 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
// Dito is a Toy interpreted programming language for fun.
// : )
package main
import (
"dito/src/eval"
"dito/src/object"
"dito/src/parser"
"dito/src/repl"
"dito/src/scanner"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
)
func main() {
args := os.Args[1:] // args without program.
// https://gobyexample.com/command-line-flags
if len(args) > 0 {
filepath := args[0]
file, err := ioutil.ReadFile(filepath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
}
execFile(string(file), os.Stdout)
return
}
welcomeMsg(repl.QUIT)
repl.Start(os.Stdin, os.Stdout)
}
func execFile(file string, out io.Writer) {
env := object.InitialEnvironment()
l := scanner.Init(file + "\n\nmain()")
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
p.PrintParseErrors(out, p.Errors())
return
}
evaluated := eval.Eval(program, env)
if evaluated != nil && evaluated != object.NONE {
io.WriteString(out, evaluated.Inspect())
io.WriteString(out, "\n")
}
// quick hack for automatic main calls.
}
func welcomeMsg(quit string) {
fmt.Printf("\033[33mDito Interactive Shell V0.01\033[m on %s\n", runtime.GOOS)
fmt.Printf("Enter '%s' to quit. Help is coming soon...\n", quit)
}