Skip to content

Commit c3e4c80

Browse files
committed
Merge pull request #10 from jbeda/fix-lint
Fix up lint errors.
2 parents bf2c1df + 0eedf43 commit c3e4c80

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

interpreter.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ type vmExt struct {
3131

3232
type vmExtMap map[string]vmExt
3333

34+
// RuntimeError is an error discovered during evaluation of the program
3435
type RuntimeError struct {
35-
StackTrace []traceFrame
36+
StackTrace []TraceFrame
3637
Msg string
3738
}
3839

@@ -127,7 +128,8 @@ func makeValueArray(elements []thunk) *valueArray {
127128

128129
// The stack
129130

130-
type traceFrame struct {
131+
// TraceFrame is a single frame of the call stack.
132+
type TraceFrame struct {
131133
Loc LocationRange
132134
Name string
133135
}
@@ -158,17 +160,17 @@ type interpreter struct {
158160
ExternalVars vmExtMap
159161
}
160162

161-
func (this interpreter) execute(ast_ astNode) (value, error) {
163+
func (i *interpreter) execute(a astNode) (value, error) {
162164
// TODO(dcunnin): All the other cases...
163-
switch ast := ast_.(type) {
165+
switch ast := a.(type) {
164166
case *astBinary:
165167
// TODO(dcunnin): Assume it's + on numbers for now
166-
leftVal, err := this.execute(ast.left)
168+
leftVal, err := i.execute(ast.left)
167169
if err != nil {
168170
return nil, err
169171
}
170172
leftNum := leftVal.(*valueNumber).value
171-
rightVal, err := this.execute(ast.right)
173+
rightVal, err := i.execute(ast.right)
172174
if err != nil {
173175
return nil, err
174176
}
@@ -188,18 +190,17 @@ func (this interpreter) execute(ast_ astNode) (value, error) {
188190
func unparseNumber(v float64) string {
189191
if v == math.Floor(v) {
190192
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)
196193
}
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)
197199
}
198200

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 {
201202
// TODO(dcunnin): All the other types...
202-
switch v := v_.(type) {
203+
switch v := v.(type) {
203204
case *valueBoolean:
204205
if v.value {
205206
buf.WriteString("true")
@@ -217,16 +218,16 @@ func (this interpreter) manifestJson(
217218
}
218219

219220
func execute(ast astNode, ext vmExtMap, maxStack int) (string, error) {
220-
theInterpreter := interpreter{
221+
i := interpreter{
221222
Stack: makeCallStack(maxStack),
222223
ExternalVars: ext,
223224
}
224-
result, err := theInterpreter.execute(ast)
225+
result, err := i.execute(ast)
225226
if err != nil {
226227
return "", err
227228
}
228229
var buffer bytes.Buffer
229-
err = theInterpreter.manifestJson(result, true, "", &buffer)
230+
err = i.manifestJSON(result, true, "", &buffer)
230231
if err != nil {
231232
return "", err
232233
}

main.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,40 @@ limitations under the License.
1616

1717
package jsonnet
1818

19-
// Note: There are no garbage collection params because we're using the native Go garbage collector.
20-
type Vm struct {
21-
maxStack int
22-
maxTrace int
19+
// Note: There are no garbage collection params because we're using the native
20+
// Go garbage collector.
21+
22+
// VM is the core interpreter and is the touchpoint used to parse and execute
23+
// Jsonnet.
24+
type VM struct {
25+
MaxStack int
26+
MaxTrace int // The number of lines of stack trace to display (0 for all of them).
2327
ext vmExtMap
2428
}
2529

26-
func MakeVm() *Vm {
27-
return &Vm{
28-
maxStack: 500,
29-
maxTrace: 20,
30+
// MakeVM creates a new VM with default parameters.
31+
func MakeVM() *VM {
32+
return &VM{
33+
MaxStack: 500,
34+
MaxTrace: 20,
3035
}
3136
}
3237

33-
func (vm *Vm) ExtVar(key string, val string) {
38+
// ExtVar binds a Jsonnet external var to the given value.
39+
func (vm *VM) ExtVar(key string, val string) {
3440
vm.ext[key] = vmExt{value: val, isCode: false}
3541
}
3642

37-
func (vm *Vm) ExtCode(key string, val string) {
43+
// ExtCode binds a Jsonnet external code var to the given value.
44+
func (vm *VM) ExtCode(key string, val string) {
3845
vm.ext[key] = vmExt{value: val, isCode: true}
3946
}
4047

41-
func (vm *Vm) EvaluateSnippet(filename string, snippet string) (string, error) {
48+
// EvaluateSnippet evaluates a string containing Jsonnet code, return a JSON
49+
// string.
50+
//
51+
// The filename parameter is only used for error messages.
52+
func (vm *VM) EvaluateSnippet(filename string, snippet string) (string, error) {
4253
tokens, err := lex(filename, snippet)
4354
if err != nil {
4455
return "", err
@@ -48,7 +59,7 @@ func (vm *Vm) EvaluateSnippet(filename string, snippet string) (string, error) {
4859
return "", err
4960
}
5061
ast, err = desugarFile(ast)
51-
output, err := execute(ast, vm.ext, vm.maxStack)
62+
output, err := execute(ast, vm.ext, vm.MaxStack)
5263
if err != nil {
5364
return "", err
5465
}

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var mainTests = []mainTest{
4040

4141
func TestMain(t *testing.T) {
4242
for _, test := range mainTests {
43-
vm := MakeVm()
43+
vm := MakeVM()
4444
output, err := vm.EvaluateSnippet(test.name, test.input)
4545
var errString string
4646
if err != nil {

0 commit comments

Comments
 (0)