Globals to compiler

This commit is contained in:
Chuck Smith
2024-02-20 16:07:01 -05:00
parent 6ba2d3abe4
commit e8254fc996
7 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
package compiler
import "testing"
func TestDefine(t *testing.T) {
expected := map[string]Symbol{
"a": {
Name: "a",
Scope: GlobalScope,
Index: 0,
},
"b": {
Name: "b",
Scope: GlobalScope,
Index: 1,
},
}
global := NewSymbolTable()
a := global.Define("a")
if a != expected["a"] {
t.Errorf("expected a=%+v, got=%+v", expected["a"], a)
}
b := global.Define("b")
if a != expected["b"] {
t.Errorf("expected b=%+v, got=%+v", expected["b"], b)
}
}
func TestResolveGlobal(t *testing.T) {
global := NewSymbolTable()
global.Define("a")
global.Define("b")
expected := []Symbol{
Symbol{
Name: "a",
Scope: GlobalScope,
Index: 0,
},
Symbol{
Name: "b",
Scope: GlobalScope,
Index: 1,
},
}
for _, sym := range expected {
result, ok := global.Resolve(sym.Name)
if !ok {
t.Errorf("name %s not resolvable", sym.Name)
}
if result != sym {
t.Errorf("expected %s to resolve to %+v, got=%+v", sym.Name, sym, result)
}
}
}