aboutsummaryrefslogtreecommitdiffstats
path: root/alv-lib
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-08-21 11:54:42 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commitf9a0ea48bf5be4fb6963e174317bd117914f10ef (patch)
treeaab5341beb75ae42bbd4df3bb5cc054a11d23735 /alv-lib
parentmove array ops into library, implement concat and size (diff)
downloadalive-f9a0ea48bf5be4fb6963e174317bd117914f10ef.tar.gz
alive-f9a0ea48bf5be4fb6963e174317bd117914f10ef.zip
move struct ops into library
Diffstat (limited to 'alv-lib')
-rw-r--r--alv-lib/array.moon32
-rw-r--r--alv-lib/struct.moon125
2 files changed, 142 insertions, 15 deletions
diff --git a/alv-lib/array.moon b/alv-lib/array.moon
index 94499bc..cb83334 100644
--- a/alv-lib/array.moon
+++ b/alv-lib/array.moon
@@ -1,16 +1,18 @@
import Array, Op, PureOp, Constant, Error, const, sig, evt from require 'alv.base'
+any = sig! / evt!
+
get = Constant.meta
meta:
name: 'get'
- summary: "Index into Arrays."
+ summary: "Index into an array."
examples: { '(get array i)' }
description: "Get the value at index `i` (starting at 0).
`i` has to be a constant expression."
value: class extends PureOp
- pattern: (sig! / evt!) + const.num
+ pattern: any + const.num
type: (inputs) =>
{ array, i } = inputs
array\type!\get i.result!
@@ -22,7 +24,7 @@ get = Constant.meta
set = Constant.meta
meta:
name: 'set'
- summary: "Update values in Arrays."
+ summary: "Update a value in an array."
examples: { '(set array i val)' }
description: "Set the value for `i` to `val`.
@@ -30,7 +32,7 @@ set = Constant.meta
`array` and `val` may be a !-stream."
value: class extends PureOp
- pattern: (sig! / evt!) + const.num + (sig! / evt!)
+ pattern: any + const.num + any
type: (inputs) =>
{ array, i, val } = inputs
type = array\type!
@@ -58,7 +60,7 @@ head = Constant.meta
examples: { '(head array)' }
value: class extends PureOp
- pattern: (sig! / evt!)*1
+ pattern: any*1
type: (inputs) =>
type = inputs[1]\type!
@@ -78,7 +80,7 @@ tail = Constant.meta
examples: { '(tail array)' }
value: class extends PureOp
- pattern: (sig! / evt!)*1
+ pattern: any*1
type: (inputs) =>
type = inputs[1]\type!
@@ -94,14 +96,14 @@ tail = Constant.meta
prepend = Constant.meta
meta:
name: 'prepend'
- summary: "Prepend a new value at the start of an Array."
+ summary: "Prepend a new value at the start of an array."
examples: { '(prepend array val)' }
description: "Prepend `val` to `array` at index `0`, moving other values back.
This is a pure op, so at most one of `array` and `val` may be a !-stream."
value: class extends PureOp
- pattern: (sig! / evt!) + (sig! / evt!)
+ pattern: any + any
type: (inputs) =>
{ array, val } = inputs
type = array\type!
@@ -124,7 +126,7 @@ This is a pure op, so at most one of `array` and `val` may be a !-stream."
insert = Constant.meta
meta:
name: 'insert'
- summary: "Insert new values into Arrays."
+ summary: "Insert a new value into an array."
examples: { '(insert array i val)' }
description: "Insert `val` into `array` at `i`, moving other values back if
necessary.
@@ -133,7 +135,7 @@ necessary.
`array` and `val` may be a !-stream."
value: class extends PureOp
- pattern: (sig! / evt!) + const.num + (sig! / evt!)
+ pattern: any + const.num + any
type: (inputs) =>
{ array, i, val } = inputs
type = array\type!
@@ -159,14 +161,14 @@ necessary.
remove = Constant.meta
meta:
name: 'remove'
- summary: "Remove values from Arrays."
+ summary: "Remove a value from an Array."
examples: { '(remove array i)' }
description: "Removes the value at index `i` from `array`.
`i` has to be a constant expression."
value: class extends PureOp
- pattern: (sig! / evt!) + const.num
+ pattern: any + const.num
type: (inputs) =>
{ array, i } = inputs
type = array\type!
@@ -187,7 +189,7 @@ remove = Constant.meta
size = Constant.meta
meta:
name: 'size'
- summary: "Get Array size"
+ summary: "Get the size of an array."
examples: { '(size array)' }
value: class extends Op
@@ -203,11 +205,11 @@ size = Constant.meta
concat = Constant.meta
meta:
name: 'concat'
- summary: "Concatenate Arrays"
+ summary: "Concatenate Arrays."
examples: { '(concat arr1 arr2 [arr3…])' }
value: class extends PureOp
- pattern: (sig! / evt!)\rep 2
+ pattern: any\rep 2
type: (inputs) =>
size = 0
type = inputs[1]\type!.type
diff --git a/alv-lib/struct.moon b/alv-lib/struct.moon
new file mode 100644
index 0000000..74e23f4
--- /dev/null
+++ b/alv-lib/struct.moon
@@ -0,0 +1,125 @@
+import Struct, Op, PureOp, Constant, Error, const, sig, evt from require 'alv.base'
+
+any = sig! / evt!
+key_type = const.str / const.sym
+
+get = Constant.meta
+ meta:
+ name: 'get'
+ summary: "Index into a struct."
+ examples: { '(get struct key)' }
+ description: "Get the value at `key`.
+
+`key` has to be a constant expression."
+
+ value: class extends PureOp
+ pattern: any + key_type
+ type: (inputs) =>
+ { struct, key } = inputs
+ struct\type!\get key.result!
+
+ tick: =>
+ { struct, key } = @unwrap_all!
+ @out\set struct[key]
+
+set = Constant.meta
+ meta:
+ name: 'set'
+ summary: "Update values in a struct."
+ examples: { '(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
+`struct` and `val` may be a !-stream."
+
+ value: class extends PureOp
+ pattern: any + key_type + any
+ type: (inputs) =>
+ { struct, key, val } = inputs
+ type = struct\type!
+ expected = type\get key.result!
+
+ if expected ~= val\type!
+ msg = string.format "expected value for key '%s' to be %s, not %s",
+ key.result!, expected, val\type!
+ error Error 'argument', msg
+
+ type
+
+ tick: =>
+ { struct, key, val } = @unwrap_all!
+
+ struct = {k,v for k,v in pairs struct}
+ struct[key] = val
+
+ @out\set struct
+
+insert = Constant.meta
+ meta:
+ name: 'insert'
+ summary: "Insert a new value into a struct."
+ examples: { '(insert struct key val)' }
+ description: "Insert `val` into `struct` at `key`.
+
+`key` has to be a constant expression. This is a pure op, so at most one of
+`struct` and `val` may be a !-stream."
+
+ value: class extends PureOp
+ pattern: any + key_type + any
+ type: (inputs) =>
+ { struct, key, val } = inputs
+ type = struct\type!
+ key = key.result!
+
+ if type.types[key]
+ msg = string.format "key '%s' already exists in value of type %s",
+ key, type
+ error Error 'argument', msg
+
+ types = {k,v for k,v in pairs type.types}
+ types[key] = val\type!
+ Struct types
+
+ tick: =>
+ { struct, key, val } = @unwrap_all!
+
+ struct = {k,v for k,v in pairs struct}
+ struct[key] = val
+
+ @out\set struct
+
+remove = Constant.meta
+ meta:
+ name: 'remove'
+ summary: "Remove values from a struct."
+ examples: { '(remove struct key)' }
+ description: "Removes the value at index `key` from `struct`.
+
+`key` has to be a constant expression."
+
+ value: class extends PureOp
+ pattern: any + key_type
+ type: (inputs) =>
+ { struct, key } = inputs
+ type = struct\type!
+ key = key.result!
+
+ -- check key exists
+ type\get key
+
+ types = {k,v for k,v in pairs type.types}
+ types[key] = nil
+ Struct types
+
+ tick: =>
+ { struct, key, val } = @unwrap_all!
+
+ struct = {k,v for k,v in pairs struct}
+ struct[key] = nil
+
+ @out\set struct
+
+{
+ :get, :set
+ :insert, :remove
+}