-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
85 lines (78 loc) · 1.89 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"flag"
"fmt"
"os"
golang "runtime"
"github.com/islisp-dev/iris/core"
"github.com/islisp-dev/iris/lib"
)
var commit string
func repl(quiet bool) {
if !quiet {
if commit == "" {
commit = "HEAD"
}
fmt.Printf("Iris ISLisp Interpreter Commit %v on %v\n", commit, golang.Version())
fmt.Printf("Copyright 2017 islisp-dev All Rights Reserved.\n")
fmt.Print(">>> ")
}
lib.TopLevel.StandardInput = core.NewStream(os.Stdin, nil, core.CharacterClass)
lib.TopLevel.StandardOutput = core.NewStream(nil, os.Stdout, core.CharacterClass)
lib.TopLevel.ErrorOutput = core.NewStream(nil, os.Stderr, core.CharacterClass)
for exp, err := lib.Read(lib.TopLevel); err == nil; exp, err = lib.Read(lib.TopLevel) {
ret, err := lib.Eval(lib.TopLevel, exp)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(ret)
}
if !quiet {
fmt.Print(">>> ")
}
}
}
func script(path string) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
lib.TopLevel.StandardInput = core.NewStream(file, nil, core.CharacterClass)
lib.TopLevel.StandardOutput = core.NewStream(nil, os.Stdout, core.CharacterClass)
lib.TopLevel.ErrorOutput = core.NewStream(nil, os.Stderr, core.CharacterClass)
for {
exp, err := lib.Read(lib.TopLevel)
if err != nil {
if fmt.Sprint(err) != "#<END-OF-STREAM >" {
fmt.Println(err)
}
return
}
_, err = lib.Eval(lib.TopLevel, exp)
if err != nil {
fmt.Println(err)
return
}
}
}
func main() {
flag.Parse()
if flag.NArg() > 0 {
script(flag.Arg(0))
return
}
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if (info.Mode() & os.ModeNamedPipe) == 0 {
repl(false)
return
}
repl(true)
return
}