@@ -31,8 +31,9 @@ type vmExt struct {
31
31
32
32
type vmExtMap map [string ]vmExt
33
33
34
+ // RuntimeError is an error discovered during evaluation of the program
34
35
type RuntimeError struct {
35
- StackTrace []traceFrame
36
+ StackTrace []TraceFrame
36
37
Msg string
37
38
}
38
39
@@ -127,7 +128,8 @@ func makeValueArray(elements []thunk) *valueArray {
127
128
128
129
// The stack
129
130
130
- type traceFrame struct {
131
+ // TraceFrame is a single frame of the call stack.
132
+ type TraceFrame struct {
131
133
Loc LocationRange
132
134
Name string
133
135
}
@@ -158,17 +160,17 @@ type interpreter struct {
158
160
ExternalVars vmExtMap
159
161
}
160
162
161
- func (this interpreter ) execute (ast_ astNode ) (value , error ) {
163
+ func (i * interpreter ) execute (a astNode ) (value , error ) {
162
164
// TODO(dcunnin): All the other cases...
163
- switch ast := ast_ .(type ) {
165
+ switch ast := a .(type ) {
164
166
case * astBinary :
165
167
// TODO(dcunnin): Assume it's + on numbers for now
166
- leftVal , err := this .execute (ast .left )
168
+ leftVal , err := i .execute (ast .left )
167
169
if err != nil {
168
170
return nil , err
169
171
}
170
172
leftNum := leftVal .(* valueNumber ).value
171
- rightVal , err := this .execute (ast .right )
173
+ rightVal , err := i .execute (ast .right )
172
174
if err != nil {
173
175
return nil , err
174
176
}
@@ -188,18 +190,17 @@ func (this interpreter) execute(ast_ astNode) (value, error) {
188
190
func unparseNumber (v float64 ) string {
189
191
if v == math .Floor (v ) {
190
192
return fmt .Sprintf ("%.0f" , v )
191
- } else {
192
- // See "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
193
- // Theorem 15
194
- // http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
195
- return fmt .Sprintf ("%.17g" , v )
196
193
}
194
+
195
+ // See "What Every Computer Scientist Should Know About Floating-Point Arithmetic"
196
+ // Theorem 15
197
+ // http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
198
+ return fmt .Sprintf ("%.17g" , v )
197
199
}
198
200
199
- func (this interpreter ) manifestJson (
200
- v_ value , multiline bool , indent string , buf * bytes.Buffer ) error {
201
+ func (i * interpreter ) manifestJSON (v value , multiline bool , indent string , buf * bytes.Buffer ) error {
201
202
// TODO(dcunnin): All the other types...
202
- switch v := v_ .(type ) {
203
+ switch v := v .(type ) {
203
204
case * valueBoolean :
204
205
if v .value {
205
206
buf .WriteString ("true" )
@@ -217,16 +218,16 @@ func (this interpreter) manifestJson(
217
218
}
218
219
219
220
func execute (ast astNode , ext vmExtMap , maxStack int ) (string , error ) {
220
- theInterpreter := interpreter {
221
+ i := interpreter {
221
222
Stack : makeCallStack (maxStack ),
222
223
ExternalVars : ext ,
223
224
}
224
- result , err := theInterpreter .execute (ast )
225
+ result , err := i .execute (ast )
225
226
if err != nil {
226
227
return "" , err
227
228
}
228
229
var buffer bytes.Buffer
229
- err = theInterpreter . manifestJson (result , true , "" , & buffer )
230
+ err = i . manifestJSON (result , true , "" , & buffer )
230
231
if err != nil {
231
232
return "" , err
232
233
}
0 commit comments