forked from Fysh-Fyve/fysh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
62 lines (54 loc) · 1.15 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
package main
import (
"fmt"
"io"
"log"
"os"
"os/user"
"golang.org/x/term"
"github.com/Fysh-Fyve/go-fysh/evaluator"
"github.com/Fysh-Fyve/go-fysh/object"
"github.com/Fysh-Fyve/go-fysh/parser"
"github.com/Fysh-Fyve/go-fysh/repl"
"github.com/Fysh-Fyve/go-fysh/scanner"
)
func main() {
atty := term.IsTerminal(int(os.Stdin.Fd()))
if atty && len(os.Args) <= 1 {
user, err := user.Current()
if err != nil {
panic(err)
}
fmt.Printf("Hello %s! This is the Fysh programming language!\n",
user.Username)
fmt.Printf("Feel free to type in commands.\n")
repl.Start(os.Stdin, os.Stdout, atty)
} else {
env := object.NewEnvironment()
l := scanner.New(string(getInput()))
p := parser.New(l)
program, err := p.Parse()
if err != nil {
log.Fatal(err)
}
evaluated := evaluator.Eval(program, env)
if object.IsError(evaluated) {
io.WriteString(os.Stdout, evaluated.Inspect())
io.WriteString(os.Stdout, "\n")
os.Exit(1)
}
}
}
func getInput() []byte {
var b []byte
var err error
if len(os.Args) > 1 {
b, err = os.ReadFile(os.Args[1])
} else {
b, err = io.ReadAll(os.Stdin)
}
if err != nil {
panic(err)
}
return b
}