From e83df1af2cdad8c2d61ba790a96875cd260eceaf Mon Sep 17 00:00:00 2001 From: s-ol Date: Sat, 21 Mar 2020 20:06:18 +0100 Subject: new meta/doc system --- lib/util.moon | 188 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 91 insertions(+), 97 deletions(-) (limited to 'lib/util.moon') diff --git a/lib/util.moon b/lib/util.moon index f816eee..a2c65ba 100644 --- a/lib/util.moon +++ b/lib/util.moon @@ -7,106 +7,100 @@ all_same = (list) -> list[1] -class switch_ extends Op - @doc: "(switch i v0 [v1 v2...]) - switch between multiple inputs - -when i is true, the first value is reproduced. -when i is false, the second value is reproduced. -when i is a num, it is (floor)ed and the matching argument (starting from 0) is reproduced." - - setup: (inputs) => - { i, values } = match 'any *any', inputs - - i_type = i\type! - assert i_type == 'bool' or i_type == 'num', Error 'argument', "i has to be bool or num" - typ = all_same [v\type! for v in *values] - @out = Value typ if not @out or typ != @out.type - - super - i: Input.value i - values: [Input.value v for v in *values] - - tick: => - { :i, :values } = @inputs - active = switch i! - when true - values[1] - when false - values[2] - else - i = 1 + (math.floor i!) % #values - values[i] - @out\set active and active! - -class route extends Op - @doc: "(route i v0 [v1 v2...]) - route between multiple inputs - -when i is true, the first value is reproduced. -when i is false, the second value is reproduced. -when i is a num, it is (floor)ed and the matching argument (starting from 0) is reproduced." - - setup: (inputs) => - { i, values } = match 'any *any', inputs - - i_type = i\type! - assert i_type == 'bool' or i_type == 'num', Error 'argument', "i has to be bool or num" - typ = all_same [v\type! for v in *values] - @out = Value typ if not @out or typ != @out.type - - super - i: Input.value i - values: [Input.value v for v in *values] - - tick: => - { :i, :values } = @inputs - active = switch i! - when true - values[1] - when false - values[2] - else - i = 1 + (math.floor i!) % #values - values[i] - if active and active\dirty! - @out\set active! - -class edge extends Op - @doc: "(edge bool) - convert rising edges to bangs" - new: => super 'bang' - - setup: (inputs) => - { value } = match 'bool', inputs - super value: Input.value value - - tick: => - now = @inputs.value! - if now and not @state.last - @out\set true - @state.last = now - -class default extends Op - @doc: "(default stream default) - provide a default value for an event stream - -starts out as default but forwards events from stream. -default defaults to zero." - - setup: (params) => - { value, init } = match 'any any', inputs - super - value: Input.event value - init: Input.cold init - - @out = Value value\type! - @out\set @inputs.init\unwrap! - - tick: => - { :value } = @inputs - if value\dirty! - @out\set value! +switch_ = Value.meta + meta: + name: 'switch' + summary: "Switch between multiple inputs." + examples: { '(switch i v0 [v1 v2…])' } + description: " +- when `i` is `true`, the first value is reproduced. +- when `i` is `false`, the second value is reproduced. +- when `i` is a `num`, it is [math/floor][]ed and the matching argument + (indexed starting from 0) is reproduced." + + value: class extends Op + setup: (inputs) => + { i, values } = match 'any *any', inputs + + i_type = i\type! + assert i_type == 'bool' or i_type == 'num', Error 'argument', "i has to be bool or num" + typ = all_same [v\type! for v in *values] + @out = Value typ if not @out or typ != @out.type + + super + i: Input.value i + values: [Input.value v for v in *values] + + tick: => + { :i, :values } = @inputs + active = switch i! + when true + values[1] + when false + values[2] + else + i = 1 + (math.floor i!) % #values + values[i] + @out\set active and active! + +route = Value.meta + meta: + name: 'route' + summary: "Route between multiple inputs." + examples: { '(route i v0 [e1 e2…])' } + description: " +- when `i` is `true`, the first event stream is reproduced. +- when `i` is `false`, the second event stream is reproduced. +- when `i` is a `num`, it is [math/floor][]ed and the matching argument + (indexed starting from 0) is reproduced." + + value: class extends Op + setup: (inputs) => + { i, values } = match 'any *any', inputs + + i_type = i\type! + assert i_type == 'bool' or i_type == 'num', Error 'argument', "i has to be bool or num" + typ = all_same [v\type! for v in *values] + @out = Value typ if not @out or typ != @out.type + + super + i: Input.value i + values: [Input.value v for v in *values] + + tick: => + { :i, :values } = @inputs + active = switch i! + when true + values[1] + when false + values[2] + else + i = 1 + (math.floor i!) % #values + values[i] + if active and active\dirty! + @out\set active! + +route = Value.meta + meta: + name: 'edge' + summary: "Convert rising edges to bangs." + examples: { '(edge bool)' } + + value: class extends Op + new: => super 'bang' + + setup: (inputs) => + { value } = match 'bool', inputs + super value: Input.value value + + tick: => + now = @inputs.value! + if now and not @state.last + @out\set true + @state.last = now { 'switch': switch_ :route :edge - :default } -- cgit v1.2.3