From 02a469d0eadafb403a44fe78e2033a990af631b8 Mon Sep 17 00:00:00 2001 From: Bernardo Ramos Date: Wed, 10 Jul 2024 05:20:00 +0000 Subject: [PATCH] add 'in' on 'for each' command --- contract/vm_dummy/vm_dummy_test.go | 34 +++++++++++++------------- contract/vm_multicall.go | 39 ++++++++++++++++-------------- contract/vm_multicall.lua | 39 ++++++++++++++++-------------- tests/test-multicall.sh | 34 +++++++++++++------------- 4 files changed, 76 insertions(+), 70 deletions(-) diff --git a/contract/vm_dummy/vm_dummy_test.go b/contract/vm_dummy/vm_dummy_test.go index 53b5659b3..5ba3c32ea 100644 --- a/contract/vm_dummy/vm_dummy_test.go +++ b/contract/vm_dummy/vm_dummy_test.go @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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%"], @@ -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%"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"], @@ -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"] ]`) @@ -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], diff --git a/contract/vm_multicall.go b/contract/vm_multicall.go index 71ed07a82..bbf5b9397 100644 --- a/contract/vm_multicall.go +++ b/contract/vm_multicall.go @@ -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" @@ -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 diff --git a/contract/vm_multicall.lua b/contract/vm_multicall.lua index 0a82a91a8..de6e33437 100644 --- a/contract/vm_multicall.lua +++ b/contract/vm_multicall.lua @@ -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" @@ -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 diff --git a/tests/test-multicall.sh b/tests/test-multicall.sh index d42e2152e..9f14655f7 100755 --- a/tests/test-multicall.sh +++ b/tests/test-multicall.sh @@ -702,7 +702,7 @@ multicall "ac1" '[ multicall "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"], @@ -712,7 +712,7 @@ multicall "ac1" '[ multicall "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"], @@ -722,7 +722,7 @@ multicall "ac1" '[ multicall "ac1" '[ ["let","list",[]], ["let","counter",0], - ["for each","item","%list%"], + ["for each","item","in","%list%"], ["add","%counter%",1], ["store result as","counter"], ["loop"], @@ -732,7 +732,7 @@ multicall "ac1" '[ multicall "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"], @@ -742,7 +742,7 @@ multicall "ac1" '[ multicall "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"], @@ -756,7 +756,7 @@ multicall "ac1" '[ ["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"], @@ -771,7 +771,7 @@ multicall "ac1" '[ ["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%"], @@ -784,7 +784,7 @@ multicall "ac1" '[ ["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%"], @@ -797,7 +797,7 @@ multicall "ac1" '[ ["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"], @@ -808,7 +808,7 @@ multicall "ac1" '[ ["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"], @@ -820,7 +820,7 @@ multicall "ac1" '[ multicall "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"], @@ -912,7 +912,7 @@ multicall "ac1" '[ multicall "ac1" '[ ["let","names",[]], ["let","list",["one","two","three","four"]], - ["for each","item","%list%"], + ["for each","item","in","%list%"], ["if","%item%","=","three"], ["break"], ["end"], @@ -924,7 +924,7 @@ multicall "ac1" '[ multicall "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"], @@ -934,7 +934,7 @@ multicall "ac1" '[ multicall "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"], @@ -946,7 +946,7 @@ multicall "ac1" '[ multicall "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"], @@ -1038,7 +1038,7 @@ multicall "ac3" '[ multicall "ac3" '[ ["let","list",["first","second","third"]], - ["for each","item","%list%"], + ["for each","item","in","%list%"], ["call","'$address'","add","%item%"], ["loop"] ]' @@ -1055,7 +1055,7 @@ multicall "ac1" '[ multicall "ac3" '[ ["let","list",["1st","2nd","3rd"]], ["let","n",1], - ["for each","item","%list%"], + ["for each","item","in","%list%"], ["to string","%n%"], ["call","'$address'","set","%last_result%","%item%"], ["add","%n%",1],