-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (47 loc) · 1007 Bytes
/
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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"github.com/Revolyssup/monkey/eval"
"github.com/Revolyssup/monkey/lexer"
"github.com/Revolyssup/monkey/obj"
"github.com/Revolyssup/monkey/parser"
"github.com/Revolyssup/monkey/repl"
)
func main() {
user, err := user.Current()
if err != nil {
panic(err)
}
if len(os.Args) > 1 {
file, err := ioutil.ReadFile(os.Args[1])
fileact := string(file)
if err != nil {
panic(err)
}
run(fileact, os.Stdout)
return
}
fmt.Printf("Welcome to monkey %s\n", user.Username)
fmt.Printf("STARTING REPL SESSION...\n")
repl.StartRepl(os.Stdin, os.Stdout)
}
func run(input string, out io.Writer) {
env := obj.NewEnvironment()
l := lexer.New(input)
p := parser.New(l)
program := p.ParseProgram()
if len(p.Errors()) != 0 {
repl.PrintParserErrors(out, p.Errors())
}
evalObj := eval.Eval(program, env)
if evalObj != nil {
io.WriteString(out, evalObj.Inspect())
io.WriteString(out, "\n")
}
}
func deleteFile() {
}