-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
91 lines (76 loc) · 1.49 KB
/
script.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
package system
import (
"strings"
"bufio"
"github.com/troykinsella/crash/system/ast"
"github.com/troykinsella/crash/system/parser"
"errors"
)
type ScriptType uint8
const (
STMT ScriptType = iota
EXPR
)
type Script struct {
str string
stmt *ast.Statement
expr *ast.Expression
}
type ScriptList []*Script
type OpAdapter struct {
interp *Interpreter
script *Script
}
func NewOpAdapter(interp *Interpreter, script *Script) *OpAdapter {
return &OpAdapter{
interp: interp,
script: script,
}
}
// implements Op
func (o *OpAdapter) Exec(subjects []interface{}, args []interface{}) (bool, interface{}, error) {
return false, nil, nil
}
func NewScriptList(expressions []string, t ScriptType) (*ScriptList, error) {
if expressions == nil {
return nil, nil
}
scripts := make(ScriptList, len(expressions))
for i, e := range expressions {
ch, err := NewScript(e, t)
if err != nil {
return nil, err
}
if ch != nil {
scripts[i] = ch
}
}
return &scripts, nil
}
func NewScript(script string, t ScriptType) (*Script, error) {
if script == "" {
return nil, nil
}
buf := bufio.NewReader(strings.NewReader(script))
parser := parser.New(buf)
if t == STMT {
n, err := parser.Statement()
if err != nil {
return nil, err
}
return &Script{
str: script,
stmt: n,
}, nil
} else if t == EXPR {
n, err := parser.Expression()
if err != nil {
return nil, err
}
return &Script{
str: script,
expr: n,
}, nil
}
return nil, errors.New("internal error")
}