2019-12-24 18:42:30 +03:00
|
|
|
package tengo_test
|
2019-12-20 22:40:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
"github.com/d5/tengo"
|
|
|
|
"github.com/d5/tengo/require"
|
2019-12-20 22:40:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2019-12-24 18:42:30 +03:00
|
|
|
scope tengo.SymbolScope,
|
2019-12-20 22:40:38 +03:00
|
|
|
index int,
|
2019-12-24 18:42:30 +03:00
|
|
|
) *tengo.Symbol {
|
|
|
|
return &tengo.Symbol{
|
2019-12-20 22:40:38 +03:00
|
|
|
Name: name,
|
|
|
|
Scope: scope,
|
|
|
|
Index: index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func globalSymbol(name string, index int) *tengo.Symbol {
|
|
|
|
return symbol(name, tengo.ScopeGlobal, index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func localSymbol(name string, index int) *tengo.Symbol {
|
|
|
|
return symbol(name, tengo.ScopeLocal, index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func freeSymbol(name string, index int) *tengo.Symbol {
|
|
|
|
return symbol(name, tengo.ScopeFree, index)
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 18:42:30 +03:00
|
|
|
func symbolTable() *tengo.SymbolTable {
|
|
|
|
return tengo.NewSymbolTable()
|
2019-12-20 22:40:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func resolveExpect(
|
|
|
|
t *testing.T,
|
2019-12-24 18:42:30 +03:00
|
|
|
symbolTable *tengo.SymbolTable,
|
2019-12-20 22:40:38 +03:00
|
|
|
name string,
|
2019-12-24 18:42:30 +03:00
|
|
|
expectedSymbol *tengo.Symbol,
|
2019-12-20 22:40:38 +03:00
|
|
|
expectedDepth int,
|
|
|
|
) {
|
|
|
|
actualSymbol, actualDepth, ok := symbolTable.Resolve(name)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, expectedSymbol, actualSymbol)
|
|
|
|
require.Equal(t, expectedDepth, actualDepth)
|
|
|
|
}
|