This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgigue.go
More file actions
70 lines (66 loc) · 1.2 KB
/
gigue.go
File metadata and controls
70 lines (66 loc) · 1.2 KB
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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/suzuken/gigue/eval"
"github.com/suzuken/gigue/types"
"io"
"os"
"strings"
)
func main() {
env := eval.NewEnv()
env.Setup()
flag.Parse()
if flag.NArg() == 0 {
var buf bytes.Buffer
scanner := bufio.NewScanner(os.Stdin)
for {
if buf.Len() == 0 {
fmt.Print("> ")
} else {
fmt.Print(">> ")
}
if !scanner.Scan() {
if scanner.Err() != nil {
fmt.Fprintf(os.Stderr, "input error: %s\n", scanner.Err())
}
break
}
input := scanner.Text()
if input == "exit" {
return
}
s := strings.TrimSpace(input)
if buf.Len() > 0 && s != ")" {
buf.WriteRune(' ')
} else {
input = s
}
buf.WriteString(input)
exp, err := eval.EvalReader(strings.NewReader(buf.String()), env)
if err != nil {
if err != io.EOF {
fmt.Fprintf(os.Stderr, "eval error: %s\n", err)
buf.Reset()
}
} else {
output(exp)
buf.Reset()
}
}
} else {
if _, err := eval.EvalFile(flag.Arg(0), env); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
}
// output simply output results.
func output(exp types.Expression) {
if exp != nil {
fmt.Printf("%v\n", exp)
}
}