forked from d5/tengo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table_test.go
133 lines (109 loc) · 3.35 KB
/
symbol_table_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package tengo_test
import (
"testing"
"github.com/BigtigerGG/tengo"
"github.com/BigtigerGG/tengo/require"
)
func TestSymbolTable(t *testing.T) {
/*
GLOBAL
[0] a
[1] b
LOCAL 1
[0] d
LOCAL 2
[0] e
[1] f
LOCAL 2 BLOCK 1
[2] g
[3] h
LOCAL 2 BLOCK 2
[2] i
[3] j
[4] k
LOCAL 1 BLOCK 1
[1] l
[2] m
[3] n
[4] o
[5] p
LOCAL 3
[0] q
[1] r
*/
global := symbolTable()
require.Equal(t, globalSymbol("a", 0), global.Define("a"))
require.Equal(t, globalSymbol("b", 1), global.Define("b"))
local1 := global.Fork(false)
require.Equal(t, localSymbol("d", 0), local1.Define("d"))
local1Block1 := local1.Fork(true)
require.Equal(t, localSymbol("l", 1), local1Block1.Define("l"))
require.Equal(t, localSymbol("m", 2), local1Block1.Define("m"))
require.Equal(t, localSymbol("n", 3), local1Block1.Define("n"))
require.Equal(t, localSymbol("o", 4), local1Block1.Define("o"))
require.Equal(t, localSymbol("p", 5), local1Block1.Define("p"))
local2 := local1.Fork(false)
require.Equal(t, localSymbol("e", 0), local2.Define("e"))
require.Equal(t, localSymbol("f", 1), local2.Define("f"))
local2Block1 := local2.Fork(true)
require.Equal(t, localSymbol("g", 2), local2Block1.Define("g"))
require.Equal(t, localSymbol("h", 3), local2Block1.Define("h"))
local2Block2 := local2.Fork(true)
require.Equal(t, localSymbol("i", 2), local2Block2.Define("i"))
require.Equal(t, localSymbol("j", 3), local2Block2.Define("j"))
require.Equal(t, localSymbol("k", 4), local2Block2.Define("k"))
local3 := local1Block1.Fork(false)
require.Equal(t, localSymbol("q", 0), local3.Define("q"))
require.Equal(t, localSymbol("r", 1), local3.Define("r"))
require.Equal(t, 2, global.MaxSymbols())
require.Equal(t, 6, local1.MaxSymbols())
require.Equal(t, 6, local1Block1.MaxSymbols())
require.Equal(t, 5, local2.MaxSymbols())
require.Equal(t, 4, local2Block1.MaxSymbols())
require.Equal(t, 5, local2Block2.MaxSymbols())
require.Equal(t, 2, local3.MaxSymbols())
resolveExpect(t, global, "a", globalSymbol("a", 0), 0)
resolveExpect(t, local1, "d", localSymbol("d", 0), 0)
resolveExpect(t, local1, "a", globalSymbol("a", 0), 1)
resolveExpect(t, local3, "a", globalSymbol("a", 0), 3)
resolveExpect(t, local3, "d", freeSymbol("d", 0), 2)
resolveExpect(t, local3, "r", localSymbol("r", 1), 0)
resolveExpect(t, local2Block2, "k", localSymbol("k", 4), 0)
resolveExpect(t, local2Block2, "e", localSymbol("e", 0), 1)
resolveExpect(t, local2Block2, "b", globalSymbol("b", 1), 3)
}
func symbol(
name string,
scope tengo.SymbolScope,
index int,
) *tengo.Symbol {
return &tengo.Symbol{
Name: name,
Scope: scope,
Index: index,
}
}
func globalSymbol(name string, index int) *tengo.Symbol {
return symbol(name, tengo.ScopeGlobal, index)
}
func localSymbol(name string, index int) *tengo.Symbol {
return symbol(name, tengo.ScopeLocal, index)
}
func freeSymbol(name string, index int) *tengo.Symbol {
return symbol(name, tengo.ScopeFree, index)
}
func symbolTable() *tengo.SymbolTable {
return tengo.NewSymbolTable()
}
func resolveExpect(
t *testing.T,
symbolTable *tengo.SymbolTable,
name string,
expectedSymbol *tengo.Symbol,
expectedDepth int,
) {
actualSymbol, actualDepth, ok := symbolTable.Resolve(name, true)
require.True(t, ok)
require.Equal(t, expectedSymbol, actualSymbol)
require.Equal(t, expectedDepth, actualDepth)
}