-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathivy.go
More file actions
187 lines (164 loc) · 4.68 KB
/
ivy.go
File metadata and controls
187 lines (164 loc) · 4.68 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main // import "robpike.io/ivy"
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"runtime/debug"
"runtime/pprof"
"strings"
"robpike.io/ivy/config"
"robpike.io/ivy/exec"
"robpike.io/ivy/lib"
"robpike.io/ivy/parse"
"robpike.io/ivy/run"
"robpike.io/ivy/scan"
"robpike.io/ivy/state"
"robpike.io/ivy/value"
)
var (
execute = flag.String("e", "", "execute `argument` and quit")
executeContinue = flag.String("i", "", "execute `argument` and continue")
file = flag.String("f", "", "execute `file` before input")
library = flag.String("lib", "", "comma-separated list of `names` of libraries to load")
format = flag.String("format", "", "use `fmt` as format for printing numbers; empty sets default format")
gformat = flag.Bool("g", false, `shorthand for -format="%.12g"`)
maxbits = flag.Uint("maxbits", 1e9, "maximum size of an integer, in bits; 0 means no limit")
maxdigits = flag.Uint("maxdigits", 1e4, "above this many `digits`, integers print as floating point; 0 disables")
maxstack = flag.Uint("stack", 100000, "maximum call stack `depth` allowed")
origin = flag.Int("origin", 1, "set index origin to `n` (must be >=0)")
prompt = flag.String("prompt", "", "command `prompt`")
profile = flag.String("profile", "", "write profile to `file`")
debugFlag = flag.String("debug", "", "comma-separated `names` of debug settings to enable")
versionFlag = flag.Bool("version", false, "print version and exit")
)
var (
conf config.Config
context value.Context
)
func main() {
flag.Usage = usage
flag.Parse()
if *versionFlag {
if build, ok := debug.ReadBuildInfo(); ok {
fmt.Println(build.Main.Version)
} else {
fmt.Println("unknown version")
}
return
}
if *profile != "" {
f, err := os.Create(*profile)
if err != nil {
fmt.Fprintf(os.Stderr, "ivy: cannot create profile file: %v\n", err)
os.Exit(2)
}
defer f.Close()
err = pprof.StartCPUProfile(f)
if err != nil {
fmt.Fprintf(os.Stderr, "ivy: cannot start CPU profile: %v\n", err)
os.Exit(2)
}
defer pprof.StopCPUProfile()
}
if *origin < 0 {
fmt.Fprintf(os.Stderr, "ivy: illegal origin value %d\n", *origin)
os.Exit(2)
}
if *gformat {
*format = "%.12g"
}
conf.SetFormat(*format)
conf.SetMaxBits(*maxbits)
conf.SetMaxDigits(*maxdigits)
conf.SetMaxStack(*maxstack)
conf.SetOrigin(*origin)
conf.SetPrompt(*prompt)
var debugConf config.Config
value.SetDebugContext(exec.NewContext(&debugConf))
if *debugFlag != "" {
for _, debug := range strings.Split(*debugFlag, ",") {
if !conf.SetDebug(debug, 1) {
fmt.Fprintf(os.Stderr, "ivy: unknown debug flag %q\n", debug)
os.Exit(2)
}
}
}
context = exec.NewContext(&conf)
if *file != "" {
if !runFile(context, *file) {
os.Exit(1)
}
}
if *library != "" {
for _, name := range strings.Split(*library, ",") {
entry := lib.Lookup(name)
if entry == nil {
fmt.Fprintf(os.Stderr, "ivy: unknown library %q\n", name)
os.Exit(1)
}
if !runString(context, entry.Source) {
os.Exit(1)
}
}
}
if *executeContinue != "" {
if !runString(context, *executeContinue) {
os.Exit(1)
}
}
if *execute != "" {
if !runString(context, *execute) {
os.Exit(1)
}
return
}
if flag.NArg() > 0 {
for i := 0; i < flag.NArg(); i++ {
if !runFile(context, flag.Arg(i)) {
os.Exit(1)
}
}
return
}
scanner := scan.New(state.New(context), "<stdin>", bufio.NewReader(os.Stdin))
parser := parse.NewParser("<stdin>", scanner, context)
for !run.Run(parser, context, true) {
}
}
// runFile executes the contents of the file as an ivy program.
func runFile(context value.Context, file string) bool {
var fd io.Reader
var err error
interactive := false
if file == "-" {
interactive = true
fd = os.Stdin
} else {
interactive = false
fd, err = os.Open(file)
}
if err != nil {
fmt.Fprintf(os.Stderr, "ivy: %s\n", err)
os.Exit(1)
}
scanner := scan.New(state.New(context), file, bufio.NewReader(fd))
parser := parse.NewParser(file, scanner, context)
return run.Run(parser, context, interactive)
}
// runString executes the string, typically a command-line argument, as an ivy program.
func runString(context value.Context, str string) bool {
scanner := scan.New(state.New(context), "<args>", strings.NewReader(str))
parser := parse.NewParser("<args>", scanner, context)
return run.Run(parser, context, false)
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: ivy [options] [file ...]\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
os.Exit(2)
}