Skip to content

Commit 431277b

Browse files
committed
refactoring
1 parent 4a63a2c commit 431277b

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (env *Environment) get(name string, exp *expression) interface{} {
3030
if env.parent != nil {
3131
return env.parent.get(name, exp)
3232
}
33-
Error(exp, "Undefined variable '%s'", name)
33+
evalError(exp, "Undefined variable '%s'", name)
3434
return nil
3535
}
3636

evaluator.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ type ContinueValue struct {
3535

3636
func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
3737
if exp == nil {
38-
Error(exp, "Null expression error, this is a bug and should never happen!. Please file a bug!")
38+
evalError(exp, "Null expression error, this is a bug and should never happen!. Please file a bug!")
3939
return nil
4040
}
4141

4242
e.recursionDepth++
4343
defer func() { e.recursionDepth-- }()
4444

4545
if e.recursionDepth > MaxRecursionDepth {
46-
Error(exp, "Maximum recursion depth exceeded")
46+
evalError(exp, "Maximum recursion depth exceeded")
4747
}
4848

4949
switch exp.Type {
@@ -60,18 +60,18 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
6060
case []interface{}:
6161
index := e.evaluate(exp.Index, env).(int)
6262
if index < 0 || index >= len(v) {
63-
Error(exp, "Index '%d' out of bounds for array '%v[%d]'", index, exp.Value, len(v))
63+
evalError(exp, "Index '%d' out of bounds for array '%v[%d]'", index, exp.Value, len(v))
6464
}
6565
return v[index]
6666
case map[string]interface{}:
6767
key := e.evaluate(exp.Index, env).(string)
6868
val, ok := v[key]
6969
if !ok {
70-
Error(exp, "Key '%s' not found in table '%v'", key, exp.Value)
70+
evalError(exp, "Key '%s' not found in table '%v'", key, exp.Value)
7171
}
7272
return val
7373
default:
74-
Error(exp, "Variable %v is not an array or table", exp.Value)
74+
evalError(exp, "Variable %v is not an array or table", exp.Value)
7575
}
7676
}
7777

@@ -81,7 +81,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
8181
if exp.Left.Type == varExpr && exp.Left.Index != nil {
8282
arrayOrTable := e.evaluate(exp.Left.Left, env)
8383
if arrayOrTable == nil {
84-
Error(exp, "Cannot assign to an index on a null expression")
84+
evalError(exp, "Cannot assign to an index on a null expression")
8585
return nil
8686
}
8787
index := e.evaluate(exp.Left.Index, env)
@@ -91,25 +91,25 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
9191
case []interface{}:
9292
idx, ok := index.(int)
9393
if !ok {
94-
Error(exp, "Array index must be an integer")
94+
evalError(exp, "Array index must be an integer")
9595
return nil
9696
}
9797
if idx < 0 || idx >= len(arr) {
98-
Error(exp, "Array index out of bounds")
98+
evalError(exp, "Array index out of bounds")
9999
return nil
100100
}
101101
arr[idx] = value
102102
return value
103103
case map[string]interface{}:
104104
key, ok := index.(string)
105105
if !ok {
106-
Error(exp, "Table key must be a string")
106+
evalError(exp, "Table key must be a string")
107107
return nil
108108
}
109109
arr[key] = value
110110
return value
111111
default:
112-
Error(exp, "Cannot index into type %T", arrayOrTable)
112+
evalError(exp, "Cannot index into type %T", arrayOrTable)
113113
return nil
114114
}
115115
} else if exp.Left.Type == varExpr && exp.Left.Value != nil {
@@ -122,7 +122,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
122122
m[field] = value
123123
return value
124124
} else {
125-
Error(exp, "Cannot assign to field %v on non-table object", field)
125+
evalError(exp, "Cannot assign to field %v on non-table object", field)
126126
return nil
127127
}
128128
}
@@ -131,7 +131,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
131131
}
132132

133133
if exp.Left.Type != varExpr {
134-
Error(exp, "Cannot assign to %v", exp.Left)
134+
evalError(exp, "Cannot assign to %v", exp.Left)
135135
}
136136
return env.set(exp.Left.Value.(string), e.evaluate(exp.Right, env))
137137

@@ -230,7 +230,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
230230
case callExpr:
231231
fn, ok := e.evaluate(exp.Func, env).(func(args ...interface{}) interface{})
232232
if !ok {
233-
Error(exp, "'%s' is not a function", exp.Func.Value)
233+
evalError(exp, "'%s' is not a function", exp.Func.Value)
234234
}
235235

236236
var args []interface{}
@@ -250,7 +250,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
250250

251251
ret := fn(args...)
252252
if err, ok := ret.(error); ok {
253-
Error(exp, "Error in function call: '%v'", err)
253+
evalError(exp, "Error in function call: '%v'", err)
254254
}
255255
return ret
256256

@@ -266,14 +266,14 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
266266
case importExpr:
267267
path := e.evaluate(exp.Left, env).(string) + ".rune"
268268
if _, alreadyImported := e.importedPaths[path]; alreadyImported {
269-
Error(exp, "Duplicate import detected: '%s' was already imported", path)
269+
evalError(exp, "Duplicate import detected: '%s' was already imported", path)
270270
}
271271

272272
e.importedPaths[path] = true
273273

274274
importedSource, err := os.ReadFile(path)
275275
if err != nil {
276-
Error(exp, "Failed to import file '%s': %v", path, err)
276+
evalError(exp, "Failed to import file '%s': %v", path, err)
277277
}
278278

279279
importStream := newInputStream(string(importedSource), path)
@@ -285,7 +285,7 @@ func (e *Evaluator) evaluate(exp *expression, env *Environment) interface{} {
285285
return nil
286286

287287
default:
288-
Error(exp, "I don't know how to evaluate %v", exp.Type)
288+
evalError(exp, "I don't know how to evaluate %v", exp.Type)
289289
return nil
290290
}
291291
}
@@ -294,13 +294,13 @@ func parseNumber(val string, exp *expression) interface{} {
294294
if strings.Contains(val, ".") {
295295
f, err := strconv.ParseFloat(val, 64)
296296
if err != nil {
297-
Error(exp, "Expected number but got %T", val)
297+
evalError(exp, "Expected number but got %T", val)
298298
}
299299
return f
300300
}
301301
i, err := strconv.Atoi(val)
302302
if err != nil {
303-
Error(exp, "Expected number but got %T", val)
303+
evalError(exp, "Expected number but got %T", val)
304304
}
305305
return i
306306
}
@@ -322,7 +322,7 @@ func applyUnaryOp(op string, a interface{}, exp *expression) interface{} {
322322
if x != nil {
323323
return false
324324
} else {
325-
Error(exp, fmt.Sprintf("Unary operator '%s' needs a valid operand", op))
325+
evalError(exp, fmt.Sprintf("Unary operator '%s' needs a valid operand", op))
326326
}
327327
}
328328
return false
@@ -331,7 +331,7 @@ func applyUnaryOp(op string, a interface{}, exp *expression) interface{} {
331331
case "not":
332332
return !boolVal(a)
333333
default:
334-
Error(exp, "Can't apply unary operator %s", op)
334+
evalError(exp, "Can't apply unary operator %s", op)
335335
return nil
336336
}
337337
}
@@ -352,13 +352,13 @@ func applyBinaryOp(op string, a, b interface{}, exp *expression) interface{} {
352352
case float64:
353353
return float64(v)
354354
default:
355-
Error(exp, "Expected number but got %T", x)
355+
evalError(exp, "Expected number but got %T", x)
356356
return 0
357357
}
358358
}
359359
div := func(x interface{}) float64 {
360360
if num(x) == 0 {
361-
Error(exp, "Divide by zero")
361+
evalError(exp, "Divide by zero")
362362
}
363363
return num(x)
364364
}
@@ -375,7 +375,7 @@ func applyBinaryOp(op string, a, b interface{}, exp *expression) interface{} {
375375
case float64:
376376
return v != 0
377377
default:
378-
Error(exp, "Expected bool but got %v", x)
378+
evalError(exp, "Expected bool but got %v", x)
379379
}
380380
return false
381381
}
@@ -414,7 +414,7 @@ func applyBinaryOp(op string, a, b interface{}, exp *expression) interface{} {
414414
case "!=":
415415
return num(a) != num(b)
416416
default:
417-
Error(exp, "Can't apply operator %s", op)
417+
evalError(exp, "Can't apply operator %s", op)
418418
return nil
419419
}
420420
}
@@ -435,7 +435,7 @@ func (e *Evaluator) makeFun(env *Environment, exp *expression) func(args ...inte
435435
}
436436
}
437437

438-
func Error(exp *expression, format string, a ...interface{}) {
438+
func evalError(exp *expression, format string, a ...interface{}) {
439439
msg := fmt.Sprintf(format, a...)
440440
if exp != nil {
441441
fmt.Printf("error (%s:%d:%d): %s\n", exp.File, exp.Line, exp.Col, msg)

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func (p *Parser) parsePairDecl() *expression {
311311
key := p.parseExpression()
312312
_, ok := key.Value.(string)
313313
if !ok {
314-
Error(key, "key must be of type string, but got: '%v'", key.Value)
314+
evalError(key, "key must be of type string, but got: '%v'", key.Value)
315315
}
316316
// remove any occurences of whitespaces including space, tabs and newlines
317317
key.Value = strings.Join(strings.Fields(key.Value.(string)), "")

0 commit comments

Comments
 (0)