Skip to content

Commit be5b123

Browse files
committed
go: Refactor scope in parser and evaluator
Refactor scope in parser and evaluator for both to be private. Rename `newEnclosedScope` to `newInnerScope` as this term confuses my little brain a little less. Because `newInnerScope` is now private in evaluator it triggered an "unused" linter warning, so remove it. We will re-add it when we need. It'd be neat to use generics with scope and then re-use the same implementation for evaluator and parser. However, tinygo doesn't support generics yet, so we have to do a little bit of copying here.
1 parent c7b5ca3 commit be5b123

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

pkg/evaluator/evaluator.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func Run(input string, print func(string)) {
99
prog := p.Parse()
1010
e := &Evaluator{print: print}
1111
e.builtins = newBuiltins(e)
12-
val := e.Eval(prog, NewScope())
12+
val := e.Eval(prog, newScope())
1313
if isError(val) {
1414
print(val.String())
1515
}
@@ -20,7 +20,7 @@ type Evaluator struct {
2020
builtins map[string]Builtin
2121
}
2222

23-
func (e *Evaluator) Eval(node parser.Node, scope *Scope) Value {
23+
func (e *Evaluator) Eval(node parser.Node, scope *scope) Value {
2424
switch node := node.(type) {
2525
case *parser.Program:
2626
return e.evalProgram(node, scope)
@@ -43,7 +43,7 @@ func (e *Evaluator) Eval(node parser.Node, scope *Scope) Value {
4343
return nil
4444
}
4545

46-
func (e *Evaluator) evalProgram(program *parser.Program, scope *Scope) Value {
46+
func (e *Evaluator) evalProgram(program *parser.Program, scope *scope) Value {
4747
var result Value
4848
for _, statement := range program.Statements {
4949
result = e.Eval(statement, scope)
@@ -54,16 +54,16 @@ func (e *Evaluator) evalProgram(program *parser.Program, scope *Scope) Value {
5454
return result
5555
}
5656

57-
func (e *Evaluator) evalDeclaration(decl *parser.Declaration, scope *Scope) Value {
57+
func (e *Evaluator) evalDeclaration(decl *parser.Declaration, scope *scope) Value {
5858
val := e.Eval(decl.Value, scope)
5959
if isError(val) {
6060
return val
6161
}
62-
scope.Set(decl.Var.Name, val)
62+
scope.set(decl.Var.Name, val)
6363
return nil
6464
}
6565

66-
func (e *Evaluator) evalFunctionCall(funcCall *parser.FunctionCall, scope *Scope) Value {
66+
func (e *Evaluator) evalFunctionCall(funcCall *parser.FunctionCall, scope *scope) Value {
6767
args := e.evalTerms(funcCall.Arguments, scope)
6868
if len(args) == 1 && isError(args[0]) {
6969
return args[0]
@@ -75,18 +75,18 @@ func (e *Evaluator) evalFunctionCall(funcCall *parser.FunctionCall, scope *Scope
7575
return builtin(args)
7676
}
7777

78-
func (e *Evaluator) evalVar(v *parser.Var, scope *Scope) Value {
79-
if val, ok := scope.Get(v.Name); ok {
78+
func (e *Evaluator) evalVar(v *parser.Var, scope *scope) Value {
79+
if val, ok := scope.get(v.Name); ok {
8080
return val
8181
}
8282
return newError("cannot find variable " + v.Name)
8383
}
8484

85-
func (e *Evaluator) evalTerm(term parser.Node, scope *Scope) Value {
85+
func (e *Evaluator) evalTerm(term parser.Node, scope *scope) Value {
8686
return e.Eval(term, scope)
8787
}
8888

89-
func (e *Evaluator) evalTerms(terms []parser.Node, scope *Scope) []Value {
89+
func (e *Evaluator) evalTerms(terms []parser.Node, scope *scope) []Value {
9090
result := make([]Value, len(terms))
9191

9292
for i, t := range terms {

pkg/evaluator/scope.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
package evaluator
22

3-
type Scope struct {
4-
store map[string]Value
5-
outer *Scope
3+
type scope struct {
4+
values map[string]Value
5+
outer *scope
66
}
77

8-
func NewScope() *Scope {
9-
return &Scope{store: map[string]Value{}}
8+
func newScope() *scope {
9+
return &scope{values: map[string]Value{}}
1010
}
1111

12-
func NewEnclosedScope(outer *Scope) *Scope {
13-
return &Scope{store: map[string]Value{}, outer: outer}
14-
}
15-
16-
func (s *Scope) Get(name string) (Value, bool) {
12+
func (s *scope) get(name string) (Value, bool) {
1713
if s == nil {
1814
return nil, false
1915
}
20-
if val, ok := s.store[name]; ok {
16+
if val, ok := s.values[name]; ok {
2117
return val, ok
2218
}
23-
return s.outer.Get(name)
19+
return s.outer.get(name)
2420
}
2521

26-
func (s *Scope) Set(name string, val Value) Value {
27-
s.store[name] = val
22+
func (s *scope) set(name string, val Value) Value {
23+
s.values[name] = val
2824
return val
2925
}

pkg/parser/parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (p *Parser) parseFunc(scope *scope) Node {
123123

124124
p.advancePastNL() // // advance past signature, already parsed into p.funcs earlier
125125
fd := p.funcs[funcName]
126-
scope = newEnclosedScope(scope)
126+
scope = newInnerScope(scope)
127127
p.addParamsToScope(scope, fd)
128128
block := p.parseBlock(scope) // parse to "end"
129129

@@ -524,23 +524,23 @@ func (p *Parser) parseBreakStatment() Node {
524524

525525
//TODO: implemented
526526
func (p *Parser) parseForStatment(scope *scope) Node {
527-
scope = newEnclosedScope(scope)
527+
scope = newInnerScope(scope)
528528
p.advancePastNL()
529529
p.parseBlock(scope)
530530
return nil
531531
}
532532

533533
//TODO: implemented
534534
func (p *Parser) parseWhileStatment(scope *scope) Node {
535-
scope = newEnclosedScope(scope)
535+
scope = newInnerScope(scope)
536536
p.advancePastNL()
537537
p.parseBlock(scope)
538538
return nil
539539
}
540540

541541
//TODO: implemented
542542
func (p *Parser) parseIfStatment(scope *scope) Node {
543-
scope = newEnclosedScope(scope)
543+
scope = newInnerScope(scope)
544544
p.advancePastNL()
545545
p.parseBlock(scope)
546546
return nil

pkg/parser/scope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func newScope() *scope {
99
return &scope{vars: map[string]*Var{}}
1010
}
1111

12-
func newEnclosedScope(outer *scope) *scope {
12+
func newInnerScope(outer *scope) *scope {
1313
return &scope{vars: map[string]*Var{}, outer: outer}
1414
}
1515

0 commit comments

Comments
 (0)