/
special.go
485 lines (465 loc) · 11.4 KB
/
special.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// 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.
//go:generate go run helpgen.go
package parse // import "robpike.io/ivy/parse"
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strings"
"time"
"robpike.io/ivy/config"
"robpike.io/ivy/demo"
"robpike.io/ivy/exec"
"robpike.io/ivy/scan"
"robpike.io/ivy/value"
)
const defaultFile = "save.ivy"
func (p *Parser) need(want ...scan.Type) scan.Token {
tok := p.next()
for _, w := range want {
if tok.Type == w {
return tok
}
}
// Make the output look nice; usually there is only one item.
if len(want) == 1 {
p.errorf("expected %s, got %s", want[0], tok)
}
str := want[0].String()
for _, s := range want[1:] {
str += " or " + s.String()
}
p.errorf("expected %s; got %s", str, tok)
panic("not reached")
}
// nextDecimalNumber returns the next number, which
// must be a non-negative int32 (even though the result is of type int).
func (p *Parser) nextDecimalNumber() int {
n64 := p.nextDecimalNumber64()
if n64 != int64(int32(n64)) {
p.errorf("value too large: %v", n64)
}
return int(n64)
}
// nextDecimalNumber64 returns the next number, which
// must fit in a non-negative int64.
func (p *Parser) nextDecimalNumber64() int64 {
conf := p.context.Config()
ibase, obase := conf.Base()
defer conf.SetBase(ibase, obase)
conf.SetBase(10, obase)
v, err := value.Parse(conf, p.need(scan.Number).Text)
if err != nil {
p.errorf("%s", err)
}
var n int64 = -1
switch num := v.(type) {
case value.Int:
n = int64(num)
case value.BigInt:
// The Int64 method produces undefined results if
// the value is too large, so we must check.
if num.Int.Sign() < 0 || num.Int.Cmp(value.MaxBigInt63) > 0 {
p.errorf("value out of range: %v", v)
}
n = num.Int64()
}
if n < 0 {
p.errorf("value must be a positive integer: %v", v)
}
return n
}
func truth(x bool) int {
if x {
return 1
}
return 0
}
func (p *Parser) special() {
p.need(scan.RightParen)
conf := p.context.Config()
// Save the base and do everything here base 0, which is decimal but
// allows hex and octal in C syntax: 0xFF, 072.
// The base command will set the values of the variables ibase and obase.
ibase, obase := conf.Base()
defer func() {
conf.SetBase(ibase, obase)
}()
conf.SetBase(0, 0)
Switch:
// Permit scan.Number in case we are in a high base (say 52) in which
// case text looks numeric.
switch text := p.need(scan.Identifier, scan.Number, scan.Op).Text; text {
case "help":
p.Println("")
tok := p.peek()
if tok.Type == scan.EOF {
p.helpOverview()
break
}
str := strings.ToLower(strings.TrimSpace(tok.Text))
// Section headers are used to separate the help output.
const (
unary = "Unary operators"
binary = "Binary operators"
axis = "Operators and axis indicator"
convert = "Type-converting operations"
constant = "# Pre-defined constants"
char = "# Character data"
operators = "# User-defined operators"
special = "# Special commands"
end = "$$EOF$$"
)
switch str {
case "help":
p.helpOverview()
case "intro", "introduction":
p.printHelpBlock("", unary)
case "unary", "monadic":
p.printHelpBlock(unary, binary)
case "binary", "dyadic":
p.printHelpBlock(binary, axis)
case "axis":
p.printHelpBlock(axis, convert)
case "type", "types", "conversion", "conversions", "convert":
p.printHelpBlock(convert, constant)
case "constant", "constants":
p.printHelpBlock(constant, char)
case "char", "character":
p.printHelpBlock(char, operators)
case "op", "ops", "operator", "operators":
p.printHelpBlock(operators, special)
case "special":
p.printHelpBlock(special, end)
case "about":
p.next()
tok = p.next()
if tok.Type == scan.EOF {
p.helpOverview()
break
}
p.helpAbout(tok.Text)
default:
p.help(str)
}
p.next()
case "base", "ibase", "obase":
if p.peek().Type == scan.EOF {
p.Printf("ibase\t%d\n", ibase)
p.Printf("obase\t%d\n", obase)
break Switch
}
base := p.nextDecimalNumber()
if base != 0 && (base < 2 || 16 < base) {
p.errorf("illegal base %d", base)
}
switch text {
case "base":
ibase, obase = base, base
case "ibase":
ibase = base
case "obase":
obase = base
}
case "cpu":
p.Printf("%s\n", conf.PrintCPUTime())
case "debug":
if p.peek().Type == scan.EOF {
for _, f := range config.DebugFlags {
p.Printf("%s\t%d\n", f, truth(conf.Debug(f)))
}
break Switch
}
name := p.need(scan.Identifier).Text
if p.peek().Type == scan.EOF {
// Toggle the value
if !conf.SetDebug(name, !conf.Debug(name)) {
p.Println("no such debug flag:", name)
}
if conf.Debug(name) {
p.Println("1")
} else {
p.Println("0")
}
break
}
number := p.nextDecimalNumber()
if !conf.SetDebug(name, number != 0) {
p.Println("no such debug flag:", name)
}
case "demo":
p.need(scan.EOF)
if conf.Mobile() {
p.Printf("For a demo on mobile platforms, use the Demo button in the UI.\n")
break
}
// Use a default configuration.
var conf config.Config
err := demo.Run(os.Stdin, DemoRunner(os.Stdin, conf.Output()), conf.Output())
if err != nil {
p.errorf("%v", err)
}
p.Println("Demo finished")
case "format":
if p.peek().Type == scan.EOF {
p.Printf("%q\n", conf.Format())
break Switch
}
conf.SetFormat(p.getString())
case "get":
if p.peek().Type == scan.EOF {
p.runFromFile(p.context, defaultFile)
} else {
p.runFromFile(p.context, p.getString())
}
case "maxbits":
if p.peek().Type == scan.EOF {
p.Printf("%d\n", conf.MaxBits())
break Switch
}
max := p.nextDecimalNumber()
conf.SetMaxBits(uint(max))
case "maxdigits":
if p.peek().Type == scan.EOF {
p.Printf("%d\n", conf.MaxDigits())
break Switch
}
max := p.nextDecimalNumber()
conf.SetMaxDigits(uint(max))
case "maxstack":
if p.peek().Type == scan.EOF {
p.Printf("%d\n", conf.MaxStack())
break Switch
}
max := p.nextDecimalNumber()
conf.SetMaxStack(uint(max))
case "op", "ops": // We keep forgetting whether it's a plural or not.
if p.peek().Type == scan.EOF {
var unary, binary []string
for _, def := range p.context.Defs {
if def.IsBinary {
binary = append(binary, def.Name)
} else {
unary = append(unary, def.Name)
}
}
sort.Strings(unary)
sort.Strings(binary)
if unary != nil {
p.Println("\nUnary: \t")
for _, s := range unary {
p.Println("\t" + s)
}
}
if binary != nil {
p.Println("\nBinary: \t")
for _, s := range binary {
p.Println("\t" + s)
}
}
break Switch
}
name := p.need(scan.Operator, scan.Identifier).Text
fn := p.context.UnaryFn[name]
found := false
if fn != nil {
p.Println(fn)
found = true
}
fn = p.context.BinaryFn[name]
if fn != nil {
p.Println(fn)
found = true
}
if !found {
p.errorf("%q not defined", name)
}
case "origin":
if p.peek().Type == scan.EOF {
p.Println(conf.Origin())
break Switch
}
origin := p.nextDecimalNumber()
if origin < 0 {
p.errorf("illegal origin %d", origin)
}
conf.SetOrigin(origin)
case "prec":
if p.peek().Type == scan.EOF {
p.Printf("%d\n", conf.FloatPrec())
break Switch
}
prec := p.nextDecimalNumber()
if prec == 0 || prec > 1e6 {
p.errorf("illegal prec %d", prec) // TODO: make 0 be disable?
}
conf.SetFloatPrec(uint(prec))
case "prompt":
if p.peek().Type == scan.EOF {
p.Printf("%q\n", conf.Format())
break Switch
}
conf.SetPrompt(p.getString())
case "save":
// Must restore ibase, obase for save.
conf.SetBase(ibase, obase)
if p.peek().Type == scan.EOF {
save(p.context, defaultFile)
} else {
save(p.context, p.getString())
}
case "seed":
if p.peek().Type == scan.EOF {
p.Println(conf.RandomSeed())
break Switch
}
conf.SetRandomSeed(p.nextDecimalNumber64())
case "timezone":
if p.peek().Type == scan.EOF {
_, offset := time.Now().In(conf.Location()).Zone()
p.Println(conf.TimeZone(), offset)
break Switch
}
err := conf.SetLocation(p.getString())
if err != nil {
p.errorf("no such location: %s", err)
}
case "var", "vars":
if p.peek().Type == scan.EOF {
var vars []string
for def := range p.context.Globals {
vars = append(vars, def)
}
sort.Strings(vars)
if vars != nil {
for _, s := range vars {
p.Println("\t" + s)
}
}
break Switch
}
name := p.need(scan.Identifier).Text
value := p.context.Global(name)
if value == nil {
p.errorf("%q not defined", name)
}
fmt.Printf("%s = ", name)
put(conf, conf.Output(), value, false)
fmt.Print("\n")
default:
p.errorf(")%s: not recognized", text)
}
// We set the configuration in the scanner here, before it retrieves
// the following newline. That means that any number it scans
// at the beginning of the next line will happen after the config
// has been updated.
conf.SetBase(ibase, obase)
p.need(scan.EOF)
}
// getString returns the value of the string that must be next in the input.
func (p *Parser) getString() string {
return value.ParseString(p.need(scan.String).Text)
}
var runDepth = 0
// runFromFile executes the contents of the named file.
func (p *Parser) runFromFile(context value.Context, name string) {
fd, err := os.Open(name)
if err != nil {
p.errorf("%s", err)
}
p.runFromReader(context, name, fd, true)
}
// runFromReader executes the contents of the io.Reader, identified by name.
func (p *Parser) runFromReader(context value.Context, name string, reader io.Reader, stopOnError bool) {
runDepth++
if runDepth > 10 {
p.errorf("invocations of %q nested too deep", name)
}
defer func() {
runDepth--
err := recover()
if err == nil {
return
}
if err, ok := err.(value.Error); ok {
fmt.Fprintf(p.context.Config().ErrOutput(), "%s%s\n", p.Loc(), err)
return
}
panic(err)
}()
scanner := scan.New(context, name, bufio.NewReader(reader))
parser := NewParser(name, scanner, p.context)
for parser.runUntilError(name) != io.EOF {
if stopOnError {
break
}
}
}
func (p *Parser) runUntilError(name string) error {
runDepth++
if runDepth > 10 {
p.errorf("get %q nested too deep", name)
}
defer func() {
runDepth--
err := recover()
if err == nil {
return
}
if err, ok := err.(value.Error); ok {
fmt.Fprintf(p.context.Config().ErrOutput(), "%s%s\n", p.Loc(), err)
return
}
panic(err)
}()
for {
exprs, ok := p.Line()
for _, expr := range exprs {
val := expr.Eval(p.context)
if val == nil {
continue
}
if _, ok := val.(Assignment); ok {
continue
}
p.context.AssignGlobal("_", val)
fmt.Fprintf(p.context.Config().Output(), "%v\n", val.Sprint(p.context.Config()))
}
if !ok {
return io.EOF
}
}
}
// A simple way to connect the user's input to the interpreter.
// Sending one byte at a time is slow but very easy, and
// it's just for a demo.
type demoIO chan byte
func (dio demoIO) Write(b []byte) (int, error) {
for _, c := range b {
dio <- c
}
return len(b), nil
}
func (dio demoIO) Read(b []byte) (int, error) {
for i := range b {
b[i] = <-dio
if b[i] == '\n' {
return i + 1, nil
}
}
return len(b), nil
}
func DemoRunner(userInput io.Reader, userOutput io.Writer) io.Writer {
conf := &config.Config{}
conf.SetOutput(userOutput)
conf.SetRandomSeed(0)
context := exec.NewContext(conf)
dio := demoIO(make(chan byte, 1000))
parser := NewParser("demo", nil, context) // Only needed for error prints in runFromReader.
go parser.runFromReader(context, "demo", dio, false)
return dio
}