functions with bindings
This commit is contained in:
@@ -14,6 +14,10 @@ func TestDefine(t *testing.T) {
|
||||
Scope: GlobalScope,
|
||||
Index: 1,
|
||||
},
|
||||
"c": {Name: "c", Scope: LocalScope, Index: 0},
|
||||
"d": {Name: "d", Scope: LocalScope, Index: 1},
|
||||
"e": {Name: "e", Scope: LocalScope, Index: 0},
|
||||
"f": {Name: "f", Scope: LocalScope, Index: 1},
|
||||
}
|
||||
|
||||
global := NewSymbolTable()
|
||||
@@ -27,6 +31,30 @@ func TestDefine(t *testing.T) {
|
||||
if b != expected["b"] {
|
||||
t.Errorf("expected b=%+v, got=%+v", expected["b"], b)
|
||||
}
|
||||
|
||||
firstLocal := NewEnclosedSymbolTable(global)
|
||||
|
||||
c := firstLocal.Define("c")
|
||||
if c != expected["c"] {
|
||||
t.Errorf("expected c=%+v, got=%+v", expected["c"], c)
|
||||
}
|
||||
|
||||
d := firstLocal.Define("d")
|
||||
if d != expected["d"] {
|
||||
t.Errorf("expected d=%+v, got=%+v", expected["d"], d)
|
||||
}
|
||||
|
||||
secondLocal := NewEnclosedSymbolTable(firstLocal)
|
||||
|
||||
e := secondLocal.Define("e")
|
||||
if e != expected["e"] {
|
||||
t.Errorf("expected e=%+v, got=%+v", expected["e"], e)
|
||||
}
|
||||
|
||||
f := secondLocal.Define("f")
|
||||
if f != expected["f"] {
|
||||
t.Errorf("expected f=%+v, got=%+v", expected["f"], f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveGlobal(t *testing.T) {
|
||||
@@ -57,3 +85,84 @@ func TestResolveGlobal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLocal(t *testing.T) {
|
||||
global := NewSymbolTable()
|
||||
global.Define("a")
|
||||
global.Define("b")
|
||||
|
||||
local := NewEnclosedSymbolTable(global)
|
||||
local.Define("c")
|
||||
local.Define("d")
|
||||
|
||||
expected := []Symbol{
|
||||
Symbol{Name: "a", Scope: GlobalScope, Index: 0},
|
||||
Symbol{Name: "b", Scope: GlobalScope, Index: 1},
|
||||
Symbol{Name: "c", Scope: LocalScope, Index: 0},
|
||||
Symbol{Name: "d", Scope: LocalScope, Index: 1},
|
||||
}
|
||||
|
||||
for _, sym := range expected {
|
||||
result, ok := local.Resolve(sym.Name)
|
||||
if !ok {
|
||||
t.Errorf("name %s not resolvable", sym.Name)
|
||||
continue
|
||||
}
|
||||
if result != sym {
|
||||
t.Errorf("expected %s to resolve to %+v, got=%+v",
|
||||
sym.Name, sym, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveNestedLocal(t *testing.T) {
|
||||
global := NewSymbolTable()
|
||||
global.Define("a")
|
||||
global.Define("b")
|
||||
|
||||
firstLocal := NewEnclosedSymbolTable(global)
|
||||
firstLocal.Define("c")
|
||||
firstLocal.Define("d")
|
||||
|
||||
secondLocal := NewEnclosedSymbolTable(global)
|
||||
secondLocal.Define("e")
|
||||
secondLocal.Define("f")
|
||||
|
||||
tests := []struct {
|
||||
table *SymbolTable
|
||||
expectedSymbols []Symbol
|
||||
}{
|
||||
{
|
||||
firstLocal,
|
||||
[]Symbol{
|
||||
Symbol{Name: "a", Scope: GlobalScope, Index: 0},
|
||||
Symbol{Name: "b", Scope: GlobalScope, Index: 1},
|
||||
Symbol{Name: "c", Scope: LocalScope, Index: 0},
|
||||
Symbol{Name: "d", Scope: LocalScope, Index: 1},
|
||||
},
|
||||
},
|
||||
{
|
||||
secondLocal,
|
||||
[]Symbol{
|
||||
Symbol{Name: "a", Scope: GlobalScope, Index: 0},
|
||||
Symbol{Name: "b", Scope: GlobalScope, Index: 1},
|
||||
Symbol{Name: "e", Scope: LocalScope, Index: 0},
|
||||
Symbol{Name: "f", Scope: LocalScope, Index: 1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
for _, sym := range tt.expectedSymbols {
|
||||
result, ok := tt.table.Resolve(sym.Name)
|
||||
if !ok {
|
||||
t.Errorf("name %s not resolvable", sym.Name)
|
||||
continue
|
||||
}
|
||||
if result != sym {
|
||||
t.Errorf("expected %s to resolve to %+v, got=%+v",
|
||||
sym.Name, sym, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user