Skip to content

Commit 6e09dd4

Browse files
authored
Make info.globals a map so it's easy to check info.globals.foo if foo is defined - removed all_ids, replaced by globals+stack (#300)
* Switch info.all_ids to an array of map of bool so it's easy to check if an identifier exists info.all_ids[0].foo is true if foo exists * split globals from stack in info * fixed bug that info was cached
1 parent 2380f00 commit 6e09dd4

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

Diff for: README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ $ m=fact(7)
4848
$ m/n
4949
== Parse ==> m / n
5050
== Eval ==> 7
51-
$ func fx(n) {if n>0 {return fx(n-1)}; info.all_ids}; fx(3)
52-
== Parse ==> func fx(n) {
51+
$ func fx(n,s) {if n>0 {return fx(n-1,s)}; info.stack}; fx(3,"abc")
52+
== Parse ==> func fx(n, s) {
5353
if n > 0 {
54-
return fx(n - 1)
54+
return fx(n - 1, s)
5555
}
56-
info.all_ids
56+
info.stack
5757
}
58-
fx(3)
59-
== Eval ==> {0:["E","PI","abs","fact","fx","log2","n","printf"],1:["n","self"],2:["fx","n","self"],3:["fx","n","self"],4:["fx","n","self"]}
58+
fx(3, "abc")
59+
== Eval ==> [{"s":true},{"s":true},{"s":true},{"s":true}] // registers don't show up, so only s does and not n
6060
$ info["gofuncs"] // other way to access map keys, for when they aren't strings for instance
6161
== Parse ==> info["gofuncs"] // other way to access map keys, for when they aren't strings for instance
6262
== Eval ==> ["acos","asin","atan","ceil","cos","eval","exp","floor","json","ln","log10","pow","round","sin","sprintf","sqrt","tan","trunc","unjson"]

Diff for: eval/eval_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func TestEvalBooleanExpression(t *testing.T) {
146146
{"(1 > 2) == false", true},
147147
{`"hello" == "world"`, false},
148148
{`"hello" == "hello"`, true},
149-
{`info["all_ids"] == info.all_ids`, true},
149+
{`info["globals"] == info.globals`, true},
150150
}
151151

152152
for _, tt := range tests {

Diff for: main_test.txtar

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ stdout '^3$'
121121
!stderr .
122122

123123
# eval() runs in the same context as when called
124-
grol -quiet -c 'func foo(x) {eval("info")};foo("A")["all_ids"][1]'
125-
stdout '^\["x"\]$'
124+
grol -quiet -c 'func foo(x) {eval("info")};foo("A")["stack"][0]'
125+
stdout '^{"x":true}$'
126126
!stderr .
127127

128128
# json of (nested) arrays

Diff for: object/state.go

+13-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (e *Environment) BaseInfo() *BigMap {
5656
if baseInfo.kv != nil {
5757
return &baseInfo
5858
}
59-
baseInfo.kv = make([]keyValuePair, 0, 7) // 6 here + all_ids
59+
baseInfo.kv = make([]keyValuePair, 0, 8) // 6 here + globals + stack
6060
tokInfo := token.Info()
6161
keys := make([]Object, 0, len(tokInfo.Keywords))
6262
for _, v := range sets.Sort(tokInfo.Keywords) {
@@ -88,25 +88,30 @@ func (e *Environment) BaseInfo() *BigMap {
8888
}
8989

9090
func (e *Environment) Info() Object {
91-
allKeys := make([]Object, e.depth+1)
91+
allKeys := make([]Object, e.depth)
92+
info := e.BaseInfo()
9293
for {
9394
keys := make([]string, 0, len(e.store))
9495
for k := range e.store {
9596
keys = append(keys, k)
9697
}
9798
slices.Sort(keys)
98-
arr := MakeObjectSlice(len(keys))
99+
set := NewMapSize(len(keys))
99100
for _, k := range keys {
100-
arr = append(arr, String{Value: k})
101+
set = set.Set(String{Value: k}, TRUE)
102+
}
103+
log.LogVf("Info() for %d - %d - keys %v set %v", e.depth, len(e.store), keys, set)
104+
if e.depth == 0 {
105+
info.Set(String{"globals"}, set) // 0 == globals
106+
} else {
107+
allKeys[e.depth-1] = set
101108
}
102-
allKeys[e.depth] = NewArray(arr)
103109
if e.outer == nil {
104110
break
105111
}
106112
e = e.outer
107113
}
108-
info := e.BaseInfo()
109-
info.Set(String{"all_ids"}, BigArray{elements: allKeys}) // 7
114+
info.Set(String{"stack"}, NewArray(allKeys))
110115
return info
111116
}
112117

@@ -228,6 +233,7 @@ func (e *Environment) makeRef(name string) (*Reference, bool) {
228233

229234
func (e *Environment) Get(name string) (Object, bool) {
230235
if name == "info" {
236+
e.TriggerNoCache()
231237
return e.Info(), true
232238
}
233239
if name == "self" {

Diff for: tests/conversions.gr

+9
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ Assert("int() with trim() whitespaces work", int(trim("\n 123\n\t")) == 123)
2929
Assert("int(\"0\") is 0", int("0") == 0)
3030
Assert("int(\"0xa\") is 10", int("0xa") == 10) // hex still working despite leading 0 handling
3131
Assert("int(\"0755\") octal is 493", int("0755") == 493) // octal still working despite leading 0 handling
32+
33+
// Unrelated for small test for existence of stuff
34+
func Exists(x) {
35+
// log(info.globals)
36+
info.globals[x] == true
37+
}
38+
Assert("exists false before we set foo", Exists("foo") == false)
39+
foo=42
40+
Assert("exists true after we set foo", Exists("foo") == true)

0 commit comments

Comments
 (0)