-
Notifications
You must be signed in to change notification settings - Fork 2
/
interp.go
52 lines (46 loc) · 940 Bytes
/
interp.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
package gotcl
import (
"fmt"
"strings"
)
type Interp struct {
}
func NewInterp() *Interp {
return &Interp{}
}
func (interp *Interp) GetVar(name, index string) (string, error) {
if len(index) == 0 {
return name, nil
}
var (
b strings.Builder
err error
)
_, err = b.WriteString(name)
_, err = b.WriteString("(")
_, err = b.WriteString(index)
_, err = b.WriteString(")")
if err != nil {
return "", err
}
return b.String(), nil
}
func (interp *Interp) EvalTokens(tok token) (string, error) {
return SubstTokens(interp, SubstAll, tok)
}
func (interp *Interp) Eval(script string) (string, error) {
ts, _, err := ParseCommand([]rune(script), false)
if err != nil {
return "", err
}
ws := make([]string, 0, len(ts))
for i := 0; i < len(ts); i++ {
tok := ts[i]
s, err := SubstTokens(interp, SubstAll, tok)
if err != nil {
return "", err
}
ws = append(ws, s)
}
return fmt.Sprintf("%q", ws), nil
}