diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-08-19 14:45:31 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | 2bc0fe723d0f5cf2be5d84545b823dec0dbfb52b (patch) | |
| tree | ba0ff0449d9f56b53e55b9d4b9ae2b68662d5073 /alv | |
| parent | add (failing) struct spec (diff) | |
| download | alive-2bc0fe723d0f5cf2be5d84545b823dec0dbfb52b.tar.gz alive-2bc0fe723d0f5cf2be5d84545b823dec0dbfb52b.zip | |
implement (set) as per spec
Diffstat (limited to 'alv')
| -rw-r--r-- | alv/builtins.moon | 36 | ||||
| -rw-r--r-- | alv/result/const.moon | 2 |
2 files changed, 36 insertions, 2 deletions
diff --git a/alv/builtins.moon b/alv/builtins.moon index b1c8593..9610b5f 100644 --- a/alv/builtins.moon +++ b/alv/builtins.moon @@ -532,6 +532,39 @@ get = Constant.meta val = val[key] @out\set val +set = Constant.meta + meta: + name: 'set' + summary: "Update Arrays and Structs." + examples: { '(set val key new-val)' } + + value: class extends PureOp + pattern: (sig! / evt!) + (const.str / const.sym / const.num) + (sig! / evt!) + type: (inputs) => inputs[1]\type! + + setup: (...) => + super ... + + { val, key, new_val } = @inputs + + expected_val_typ = val\type!\get key! + got_val_typ = new_val\type! + if expected_val_typ ~= got_val_typ + msg = string.format "expected value for key '%s' to be %s, not %s", + key!, expected_val_typ, got_val_typ + error Error 'argument', msg + + tick: => + { val, key, new_val } = @unwrap_all! + + if type(key) == 'number' + key = key + 1 + + val = {k,v for k,v in pairs val} + val[key] = new_val + + @out\set val + loop = Constant.meta meta: name: 'loop' @@ -629,7 +662,8 @@ Scope.from_table { '~': to_sig '!': to_evt - :array, :struct, :get + :array, :struct + :get, :set :loop, :recur diff --git a/alv/result/const.moon b/alv/result/const.moon index 9e24ab1..ecd386d 100644 --- a/alv/result/const.moon +++ b/alv/result/const.moon @@ -45,7 +45,7 @@ class Constant extends Result --- compare two values. -- -- Compares two `SigStream`s by comparing their types and their Lua values. - __eq: (other) => other.type == @type and other.value == @value + __eq: (other) => other.type == @type and @type\eq other.value, @value --- Result metatype. -- @tfield string metatype (`=`) |
