-
Notifications
You must be signed in to change notification settings - Fork 1
/
std_internal.go
161 lines (144 loc) · 3.88 KB
/
std_internal.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
package pragmash
import (
"errors"
"io/ioutil"
"os"
"strings"
)
// StdInternal implements built-in commands that make the language usable.
type StdInternal struct{}
// Call calls a function by expanding one or more lists of arguments.
func (_ StdInternal) Call(r Runner, name string,
args ...[]*Value) (*Value, error) {
totalCount := 0
for _, x := range args {
totalCount += len(x)
}
allArgs := make([]*Value, 0, totalCount)
for _, x := range args {
allArgs = append(allArgs, x...)
}
return r.RunCommand(name, allArgs)
}
// Eval runs some pragmash code inside the current runner.
func (_ StdInternal) Eval(r Runner, code string) (*Value, error) {
lines, contexts, err := TokenizeString(code)
if err != nil {
return nil, err
}
// Update the contexts to reflect that we're in an eval.
for i, x := range contexts {
contexts[i] = x + " in eval"
}
// Generate the runner and run it
runnable, err := ScanAll(lines, contexts)
if err != nil {
return nil, err
}
if val, bo := runnable.Run(r); bo == nil {
return val, nil
} else if bo.Type() == BreakoutTypeReturn {
return bo.Value(), nil
} else {
return nil, errors.New(bo.Context() + ": " + bo.Error().Error())
}
}
// Exec runs a pragmash script inside the current runner. It will be able to
// affect variables, throw exceptions, print to the console, etc.
func (_ StdInternal) Exec(r Runner, path string) (*Value, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines, contexts, err := TokenizeString(string(contents))
if err != nil {
return nil, err
}
// Update the contexts to include the path
for i, x := range contexts {
contexts[i] = x + " in " + path
}
// Generate the runner and run it
runnable, err := ScanAll(lines, contexts)
if err != nil {
return nil, err
}
if val, bo := runnable.Run(r); bo == nil {
return val, nil
} else if bo.Type() == BreakoutTypeReturn {
return bo.Value(), nil
} else {
return nil, errors.New(bo.Context() + ": " + bo.Error().Error())
}
}
// Exit exits the current program with an optional exit code.
func (_ StdInternal) Exit(args ...*Value) {
if len(args) != 1 {
os.Exit(0)
} else {
num, err := args[0].Number()
if err != nil {
os.Exit(1)
}
os.Exit(int(num.Float()))
}
}
// Pragmash runs a script with a given set of arguments in a new, standard
// runner. This is different from Exec because it isolates the variables of the
// new script and it sets its $DIR and $ARGV variables.
func (_ StdInternal) Pragmash(path string, args ...*Value) (*Value, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
lines, contexts, err := TokenizeString(string(contents))
if err != nil {
return nil, err
}
// Update the contexts to include the path
for i, x := range contexts {
contexts[i] = x + " in " + path
}
// Generate the runnable
runnable, err := ScanAll(lines, contexts)
if err != nil {
return nil, err
}
// Generate the runner
variables := CreateStandardVariables(path, args)
runner := NewStdRunner(variables)
// Run the file.
if val, bo := runnable.Run(runner); bo == nil {
return val, nil
} else if bo.Type() == BreakoutTypeReturn {
return bo.Value(), nil
} else {
return nil, errors.New(bo.Context() + ": " + bo.Error().Error())
}
}
// Swap swaps the values of two variables.
func (_ StdInternal) Swap(r Runner, a, b *Value) error {
v1, err := r.RunCommand("get", []*Value{a})
if err != nil {
return err
}
v2, err := r.RunCommand("get", []*Value{b})
if err != nil {
return err
}
if _, err := r.RunCommand("set", []*Value{a, v2}); err != nil {
return err
}
if _, err := r.RunCommand("set", []*Value{b, v1}); err != nil {
return err
}
return nil
}
// Throw throws an exception.
func (_ StdInternal) Throw(args ...*Value) error {
strArgs := make([]string, len(args))
for i, x := range args {
strArgs[i] = x.String()
}
return errors.New(strings.Join(strArgs, " "))
}