-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheval.go
More file actions
147 lines (133 loc) · 2.69 KB
/
Copy patheval.go
File metadata and controls
147 lines (133 loc) · 2.69 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
package apl
import (
"bufio"
"fmt"
"io"
"runtime/debug"
"strings"
)
// Program contains a slice of parsed expressions.
type Program []expr
// Eval executes an apl program.
// It can be called in a loop for every line of input.
func (a *Apl) Eval(p Program) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %s\n%s", r, string(debug.Stack()))
}
}()
write := func(val Value) {
switch v := val.(type) {
case Image:
if a.stdimg != nil {
a.stdimg.WriteImage(v)
} else {
fmt.Fprintln(a.stdout, val.String(a.Format))
}
default:
fmt.Fprintln(a.stdout, val.String(a.Format))
}
}
var val Value
for _, expr := range p {
val, err = expr.Eval(a)
if err != nil {
return err
}
if isAssignment(expr) == false {
switch v := val.(type) {
case Channel:
i := 0
for e := range v[0] {
if i == 0 {
i++
if _, ok := e.(Image); ok && a.stdimg != nil {
a.stdimg.StartLoop()
defer a.stdimg.StopLoop()
}
}
write(e)
}
default:
write(val)
}
}
}
return nil
}
// EvalProgram evaluates all expressions in the program and returns the values.
func (a *Apl) EvalProgram(p Program) ([]Value, error) {
res := make([]Value, len(p))
for i, e := range p {
if v, err := e.Eval(a); err != nil {
return nil, err
} else {
res[i] = v
}
}
return res, nil
}
func (p Program) String(f Format) string {
v := make([]string, len(p))
for i := range p {
v[i] = p[i].String(f)
}
return strings.Join(v, "⋄")
}
// EvalFile parses and evalutes from a reader.
// It handles multiline statements.
// The file argument is used only in the error message.
func (a *Apl) EvalFile(r io.Reader, file string) (err error) {
line := 0
defer func() {
if err != nil {
err = fileError{file: file, line: line, err: err}
}
}()
ok := true
var p Program
b := NewLineBuffer(a)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line++
ok, err = b.Add(scanner.Text())
if err != nil {
return
}
if ok {
p, err = b.Parse()
if err != nil {
return
}
err = a.Eval(p)
if err != nil {
return
}
}
}
if ok == false && b.Len() > 0 {
return fmt.Errorf("multiline statement is not terminated")
}
return nil
}
type fileError struct {
file string
line int
err error
}
func (f fileError) Error() string {
return fmt.Sprintf("%s:%d: %s", f.file, f.line, f.err.Error())
}
type expr interface {
Eval(*Apl) (Value, error)
String(f Format) string
}
func isAssignment(e expr) bool {
// Assignment is implemented as an operator.
if fn, ok := e.(*function); ok && fn != nil {
if d, ok := fn.Function.(*derived); ok && d.op == "←" {
return true
}
}
return false
}