diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-08-21 11:54:42 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-03-02 14:24:49 +0000 |
| commit | f9a0ea48bf5be4fb6963e174317bd117914f10ef (patch) | |
| tree | aab5341beb75ae42bbd4df3bb5cc054a11d23735 /alv | |
| parent | move array ops into library, implement concat and size (diff) | |
| download | alive-f9a0ea48bf5be4fb6963e174317bd117914f10ef.tar.gz alive-f9a0ea48bf5be4fb6963e174317bd117914f10ef.zip | |
move struct ops into library
Diffstat (limited to 'alv')
| -rw-r--r-- | alv/builtins.moon | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/alv/builtins.moon b/alv/builtins.moon index 1436962..a0a319a 100644 --- a/alv/builtins.moon +++ b/alv/builtins.moon @@ -507,180 +507,6 @@ This is a pure op, so at most one !-stream input is allowed." pairs = @unwrap_all! @out\set {key, val for {:key, :val} in *pairs} -get = Constant.meta - meta: - name: 'get' - summary: "Index into Arrays and Structs." - examples: { '(get array [key])', '(get struct key [key2…])' } - description: "Get the value for `key`. - -For arrays, `key` can be omitted to peek at the last element in the array. -If multiple keys are specified, they are used to recursively index, i.e. - - (get struct 'a' 0 'note') - -is equivalent to - - (get (get (get struct 'a') 0) 'note') -" - - value: class extends Op - pattern = (sig! / evt!) + (const.str / const.sym / const.num)^0 - setup: (inputs) => - { val, keys } = pattern\match inputs - super val: Input.hot val - - val_type = @inputs.val\type! - - if #keys > 0 - @state = [key.result! for key in *keys] - elseif val_type.__class == Array - assert val_type.size > 0, Error 'argument', "cannot (get) from empty Array" - @state = { val_type.size - 1 } - else - error Error 'argument', "missing key" - - type = val\type! - for key in *@state - type = type\get key - - if val\metatype == '!' - @out = type\mk_evt! - else - @out = type\mk_sig! - - tick: => - val = @inputs.val! - for key in *@state - if type(key) == 'number' - key = key + 1 - val = val[key] - @out\set val - -set = Constant.meta - meta: - name: 'set' - summary: "Update values in Arrays and Structs." - examples: { '(set array key val)', '(set struct key val)' } - description: "Set the value for `key` to `val`. - -`key` has to be a constant expression. This is a pure op, so at most one of -`array`/`struct` and `val` may be a !-stream." - - value: class extends PureOp - pattern: (sig! / evt!) + (const.str / const.sym / const.num) + (sig! / evt!) - type: (inputs) => inputs[1]\type! - - setup: (...) => - super ... - - { comp, key, val } = @inputs - - expected_val_typ = comp\type!\get key! - got_val_typ = 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: => - { comp, key, val } = @unwrap_all! - - if type(key) == 'number' - key = key + 1 - - comp = {k,v for k,v in pairs comp} - comp[key] = val - - @out\set comp - -insert = Constant.meta - meta: - name: 'insert' - summary: "Insert new values into Arrays and Structs." - examples: { '(insert array key val)', '(insert struct key val)' } - description: "Insert `val` into `array`/`struct` at `key`. - -`key` has to be a constant expression. This is a pure op, so at most one of -`array`/`struct` and `val` may be a !-stream." - - value: class extends PureOp - pattern: (sig! / evt!) + (const.str / const.sym / const.num) + (sig! / evt!) - type: (inputs) => - { comp, key, val } = inputs - before = comp\type! - - if before.__class == Array - Array before.size + 1, before.type - else - types = {k,v for k,v in pairs before.types} - types[key\const!!] = val\type! - Struct types - - setup: (...) => - super ... - - { comp, key, val } = @inputs - before = comp\type! - key = key! - if before.__class == Array and (key > before.size or key < 0) - error Error 'argument', "index '#{key}' out of range!" - else if before.__class == Struct and before.types[key] - msg = string.format "key '%s' already exists in value of type %s", - key, before - error Error 'argument', msg - - tick: => - { comp, key, val } = @unwrap_all! - comp = {k,v for k,v in pairs comp} - - if type(key) == 'number' - table.insert comp, key + 1, val - else - comp[key] = val - - @out\set comp - -remove = Constant.meta - meta: - name: 'remove' - summary: "Remove values from Arrays and Structs." - examples: { '(remove array key)', '(remove struct key)' } - description: "Remvoes the value for `key` from `array`/`struct`. - -`key` has to be a constant expression." - - value: class extends PureOp - pattern: (sig! / evt!) + (const.str / const.sym / const.num) - type: (inputs) => - { comp, key } = inputs - before = comp\type! - - if before.__class == Array - Array before.size - 1, before.type - else - types = {k,v for k,v in pairs before.types} - types[key\const!!] = nil - Struct types - - setup: (...) => - super ... - - { comp, key } = @inputs - comp\type!\get key! - - tick: => - { comp, key, val } = @unwrap_all! - comp = {k,v for k,v in pairs comp} - - if type(key) == 'number' - table.remove comp, key + 1, val - else - comp[key] = nil - - @out\set comp - - loop = Constant.meta meta: name: 'loop' @@ -779,7 +605,6 @@ Scope.from_table { '!': to_evt :array, :struct - :get, :set, :insert, :remove :loop, :recur |
