Skip to content

Commit

Permalink
add 'in' on 'for each' command
Browse files Browse the repository at this point in the history
  • Loading branch information
kroggen committed Jul 10, 2024
1 parent 8dfd84e commit 02a469d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
34 changes: 17 additions & 17 deletions contract/vm_dummy/vm_dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5357,7 +5357,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","list",[11,22,33]],
["let","r",0],
["for each","item","%list%"],
["for each","item","in","%list%"],
["add","%r%","%item%"],
["store result as","r"],
["loop"],
Expand All @@ -5367,7 +5367,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","list",[11,22,33]],
["let","counter",0],
["for each","item","%list%"],
["for each","item","in","%list%"],
["add","%counter%",1],
["store result as","counter"],
["loop"],
Expand All @@ -5377,7 +5377,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","list",[]],
["let","counter",0],
["for each","item","%list%"],
["for each","item","in","%list%"],
["add","%counter%",1],
["store result as","counter"],
["loop"],
Expand All @@ -5387,7 +5387,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","list",["one",1,"two",2,2.5,true,false]],
["let","counter",0],
["for each","item","%list%"],
["for each","item","in","%list%"],
["add","%counter%",1],
["store result as","counter"],
["loop"],
Expand All @@ -5397,7 +5397,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","list",[10,21,32]],
["let","r",0],
["for each","item","%list%"],
["for each","item","in","%list%"],
["if","%item%","<",30],
["add","%r%","%item%"],
["store result as","r"],
Expand All @@ -5411,7 +5411,7 @@ func TestComposableTransactions(t *testing.T) {
["let","str",""],
["let","obj",{"one":1,"two":2,"three":3}],
["get keys","%obj%"],
["for each","key","%last_result%"],
["for each","key","in","%last_result%"],
["combine","%str%","%key%"],
["store result as","str"],
["loop"],
Expand All @@ -5426,7 +5426,7 @@ func TestComposableTransactions(t *testing.T) {
["let","str",""],
["let","sum",0],
["let","obj",{"one":1,"two":2,"three":3}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["combine","%str%","%key%"],
["store result as","str"],
["add","%sum%","%value%"],
Expand All @@ -5439,7 +5439,7 @@ func TestComposableTransactions(t *testing.T) {
["let","str",""],
["let","sum",0],
["let","obj",{"one":1.5,"two":2.5,"three":3.5,"four":4.5}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["combine","%str%","%key%"],
["store result as","str"],
["add","%sum%","%value%"],
Expand All @@ -5452,7 +5452,7 @@ func TestComposableTransactions(t *testing.T) {
["let","names",[]],
["let","values",[]],
["let","obj",{"one":1.5,"two":2.5,"three":3.5,"four":4.5}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["insert","%names%","%key%"],
["insert","%values%","%value%"],
["loop"],
Expand All @@ -5463,7 +5463,7 @@ func TestComposableTransactions(t *testing.T) {
["let","names",[]],
["let","values",[]],
["let","obj",{"one":1.5,"two":2.5,"three":3.5,"four":4.5}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["insert","%names%","%key%"],
["insert","%values%","%value%"],
["loop"],
Expand All @@ -5475,7 +5475,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","obj",{}],
["let","counter",0],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["add","%counter%",1],
["store result as","counter"],
["loop"],
Expand Down Expand Up @@ -5567,7 +5567,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","names",[]],
["let","list",["one","two","three","four"]],
["for each","item","%list%"],
["for each","item","in","%list%"],
["if","%item%","=","three"],
["break"],
["end"],
Expand All @@ -5579,7 +5579,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","names",[]],
["let","list",["one","two","three","four"]],
["for each","item","%list%"],
["for each","item","in","%list%"],
["break","if","%item%","=","three"],
["insert","%names%","%item%"],
["loop"],
Expand All @@ -5589,7 +5589,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","names",[]],
["let","obj",{"one":true,"two":false,"three":false,"four":true}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["if","%value%","=",false],
["break"],
["end"],
Expand All @@ -5601,7 +5601,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac1", `[
["let","names",[]],
["let","obj",{"one":true,"two":false,"three":false,"four":true}],
["for pair","key","value","%obj%"],
["for each","key","value","in","%obj%"],
["break","if","%value%","=",false],
["insert","%names%","%key%"],
["loop"],
Expand Down Expand Up @@ -5694,7 +5694,7 @@ func TestComposableTransactions(t *testing.T) {

multicall(t, bc, "ac3", `[
["let","list",["first","second","third"]],
["for each","item","%list%"],
["for each","item","in","%list%"],
["call","AmhbUWkqenFtgKLnbDd1NXHce7hn35pcHWYRWBnq5vauLfEQXXRA","add","%item%"],
["loop"]
]`)
Expand All @@ -5711,7 +5711,7 @@ func TestComposableTransactions(t *testing.T) {
multicall(t, bc, "ac3", `[
["let","list",["1st","2nd","3rd"]],
["let","n",1],
["for each","item","%list%"],
["for each","item","in","%list%"],
["to string","%n%"],
["call","AmhbUWkqenFtgKLnbDd1NXHce7hn35pcHWYRWBnq5vauLfEQXXRA","set","%last_result%","%item%"],
["add","%n%",1],
Expand Down
39 changes: 21 additions & 18 deletions contract/vm_multicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,28 @@ function execute(calls)
elseif cmd == "end" then
if_on = true
-- for foreach forpair break loop
-- for foreach break loop
elseif cmd == "for each" and if_on then
for_var2 = "__"
for_obj = {}
for_list = args[2]
elseif cmd == "for pair" and if_on then
for_var2 = args[2]
for_obj = args[3]
for_list = action["get keys"](for_obj)
vars[for_var2] = for_obj[for_list[1]]
-- "for each", "item", "in", "list"
if args[2] == "in" then
for_var2 = "__"
for_obj = {}
for_list = args[3]
-- "for each", "key", "value", "in", "object"
elseif args[3] == "in" then
for_var2 = args[2]
for_obj = args[4]
for_list = action["get keys"](for_obj)
vars[for_var2] = for_obj[for_list[1]]
else
assert(false, "for each: invalid syntax")
end
for_cmdpos = cmdpos
for_type = "each"
for_var = args[1]
for_pos = 1
vars[for_var] = for_list[1]
skip_for = (for_list[1] == nil) -- if the list is empty or it is a dictionary
elseif cmd == "for" and if_on then
for_cmdpos = cmdpos
for_type = "number"
Expand Down Expand Up @@ -184,15 +196,6 @@ function execute(calls)
assert(false, "command not found: " .. cmd)
end
if if_on and (cmd == "for each" or cmd == "for pair") then
for_cmdpos = cmdpos
for_type = "each"
for_var = args[1]
for_pos = 1
vars[for_var] = for_list[1]
skip_for = (for_list[1] == nil) -- if the list is empty or it is a dictionary
end
if skip_for then
repeat
cmdpos = cmdpos + 1
Expand Down
39 changes: 21 additions & 18 deletions contract/vm_multicall.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,28 @@ function execute(calls)
elseif cmd == "end" then
if_on = true

-- for foreach forpair break loop
-- for foreach break loop
elseif cmd == "for each" and if_on then
for_var2 = "__"
for_obj = {}
for_list = args[2]
elseif cmd == "for pair" and if_on then
for_var2 = args[2]
for_obj = args[3]
for_list = action["get keys"](for_obj)
vars[for_var2] = for_obj[for_list[1]]
-- "for each", "item", "in", "list"
if args[2] == "in" then
for_var2 = "__"
for_obj = {}
for_list = args[3]
-- "for each", "key", "value", "in", "object"
elseif args[3] == "in" then
for_var2 = args[2]
for_obj = args[4]
for_list = action["get keys"](for_obj)
vars[for_var2] = for_obj[for_list[1]]
else
assert(false, "for each: invalid syntax")
end
for_cmdpos = cmdpos
for_type = "each"
for_var = args[1]
for_pos = 1
vars[for_var] = for_list[1]
skip_for = (for_list[1] == nil) -- if the list is empty or it is a dictionary
elseif cmd == "for" and if_on then
for_cmdpos = cmdpos
for_type = "number"
Expand Down Expand Up @@ -179,15 +191,6 @@ function execute(calls)
assert(false, "command not found: " .. cmd)
end

if if_on and (cmd == "for each" or cmd == "for pair") then
for_cmdpos = cmdpos
for_type = "each"
for_var = args[1]
for_pos = 1
vars[for_var] = for_list[1]
skip_for = (for_list[1] == nil) -- if the list is empty or it is a dictionary
end

if skip_for then
repeat
cmdpos = cmdpos + 1
Expand Down
Loading

0 comments on commit 02a469d

Please sign in to comment.