diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/builtin.moon | 67 | ||||
| -rw-r--r-- | lib/debug.moon | 3 | ||||
| -rw-r--r-- | lib/gui.moon | 3 | ||||
| -rw-r--r-- | lib/math.moon | 28 | ||||
| -rw-r--r-- | lib/osc.moon | 6 | ||||
| -rw-r--r-- | lib/time.moon | 5 | ||||
| -rw-r--r-- | lib/util.moon | 6 |
7 files changed, 78 insertions, 40 deletions
diff --git a/lib/builtin.moon b/lib/builtin.moon index 96eb0f0..5c3d7d4 100644 --- a/lib/builtin.moon +++ b/lib/builtin.moon @@ -1,6 +1,19 @@ import Action, Const, Cell from require 'base' import Scope from require 'scope' +class UpdateChildren + new: (@children) => + + update: (dt) => + for child in *@children + L\trace "updating #{child}" + L\push child\update, dt + + get: => @children[#@children]\get! + getc: => @children[#@children]\getc! + + __tostring: => '<forwarder>' + -- (def sym1 val-expr1 -- [sym2 val-expr2]...) -- @@ -9,45 +22,35 @@ import Scope from require 'scope' -- if val-expr is a expand-time constant, defines a `Const`, -- otherwise places a `Forward` for the expr class def extends Action - expand: (scope, tail) => + eval: (scope, tail) => L\trace "expanding #{@}" assert #tail > 1, "'def' requires at least 2 arguments" assert #tail % 2 == 0, "'def' requires an even number of arguments" - L\push -> - for i=1,#tail,2 + values = L\push -> + return for i=1,#tail,2 name, val_expr = tail[i], tail[i+1] - name = (name\quote scope, @registry)!\getc! - assert name.type == 'sym', "'def's argument ##{i} has to be a symbol" - - scope\set name, val_expr\eval scope, @registry + name = (name\quote scope, @registry)\getc 'sym' - nil - - patch: (registry) => - L\trace "patching #{@}" - for child in *@node[3,,2] - L\push child\patch, registry + val = val_expr\eval scope, @registry + scope\set name, val + val - update: (dt) => - L\trace "updating #{@}" - for child in *@node[3,,2] - L\push child\update, dt + UpdateChildren values -- (require name-str) -- -- require a lua module and return its `Scope` -- name-str has to be an expand-time constant class require_mod extends Action - expand: (scope, tail) => + eval: (scope, tail) => L\trace "expanding #{@}" assert #tail == 1, "'require' takes only one parameter" name = L\push tail[1]\eval, scope, @registry - assert name.type == 'str', "'require' only works on strings" L\trace @, "loading module #{name}" - scope = Scope.from_table require "lib.#{name\getc!}" + scope = Scope.from_table require "lib.#{name\getc 'str'}" Const 'scope', scope -- (use scope1 [scope2]...) @@ -61,26 +64,28 @@ class use extends Action value = L\push child\eval, scope, @registry L\trace @, "merging #{value} into #{scope}" assert value.type == 'scope', "'use' only works on scopes" - scope\use value\getc! + scope\use value\getc 'scope' nil -- (fn (p1 [p2]...) body-expr) -- -- declare a function --- --- pX are symbols that will resolve to a 'Forward' in the body class fn extends Action class FnDef new: (@params, @body) => + __tostring: => + table.concat [p\stringify! for p in *@params], ' ' + eval: (scope, tail) => L\trace "expanding #{@}" assert #tail == 2, "'fn' takes exactly two arguments" { params, body } = tail + assert params.__class == Cell, "'fn's first argument has to be an expression" - param_symbols = for param in *params + param_symbols = for param in *params.children assert param.type == 'sym', "function parameter declaration has to be a symbol" param\quote scope, @registry @@ -94,15 +99,16 @@ class op_invoke extends Action @op\destroy! if @op @head = head - assert @head.type == 'fndef', "cant op-invoke #{@head}" + assert @head.type == 'opdef', "cant op-invoke #{@head}" @op = @head\getc!! true eval: (scope, tail) => args = [expr\eval scope, @registry for expr in *tail] + -- Const 'op', with @op with @op - \patch unpack args + \setup unpack args class fn_invoke extends Action -- @TODO: @@ -127,10 +133,9 @@ class fn_invoke extends Action for i=1,#params name = params[i]\getc! argm = tail[i] - L\trace "EXPANDING ARG", argument fn_scope\set name, L\push argm\eval, scope, @registry - body\expand fn_scope, @registry + body\eval fn_scope, @registry class do_expr extends Action class DoWrapper @@ -138,13 +143,15 @@ class do_expr extends Action update: (dt) => for child in *@children - child\update dt + L\push child\update, dt get: => @children[#@children]\get! getc: => @children[#@children]\getc! + __tostring: => '<dowrapper>' + eval: (scope, tail) => - DoWrapper [expr\eval scope, @registry for expr in *tail] + UpdateChildren [(expr\eval scope, @registry) or Const.empty! for expr in *tail] { 'op-invoke': op_invoke diff --git a/lib/debug.moon b/lib/debug.moon index 3b5df47..f87b06a 100644 --- a/lib/debug.moon +++ b/lib/debug.moon @@ -4,7 +4,8 @@ class out extends Op setup: (name, @chld) => @name = name\getc! - update: => + update: (dt) => + @chld\update dt L\print "@name", @chld\get! { diff --git a/lib/gui.moon b/lib/gui.moon index 20d3bcc..37df233 100644 --- a/lib/gui.moon +++ b/lib/gui.moon @@ -9,7 +9,8 @@ class out extends Op @name = name\getc! @@instances[@name] = @ - update: => + update: (dt) => + @chld\update dt @value = @chld\get! destroy: => diff --git a/lib/math.moon b/lib/math.moon index 8e54bbd..0a514a6 100644 --- a/lib/math.moon +++ b/lib/math.moon @@ -6,26 +6,38 @@ class BinOp extends Op @children = { ... } assert #@children >= 2, "#{@} needs at least two parameters" + update: (dt) => + for child in *@children + child\update dt + class add extends BinOp - update: => + update: (dt) => + super\update dt + @value = 0 for child in *@children @value += child\get! class sub extends BinOp - update: => + update: (dt) => + super\update dt + @value = @children[1]\get! for child in *@children[2,] @value -= child\get! class mul extends BinOp - update: => + update: (dt) => + super\update dt + @value = 1 for child in *@children @value *= child\get! class div extends BinOp - update: => + update: (dt) => + super\update dt + @value = @children[1]\get! for child in *@children[2,] @value /= child\get! @@ -37,8 +49,12 @@ func_op = (name, arity, func) -> if arity != '*' assert #@params == arity, "#{@} needs exactly #{arity} parameters" - update: => - @value = func unpack [param\get! for param in *@params] + update: (dt) => + params = for param in *@params + param\update dt + param\get! + + @value = func unpack params k.__name = name k diff --git a/lib/osc.moon b/lib/osc.moon index a99aab0..02e3ade 100644 --- a/lib/osc.moon +++ b/lib/osc.moon @@ -10,7 +10,11 @@ class out extends Op setup: (@host, @port, @path, @value) => - update: => + update: (dt) => + L\trace "updating #{@}" + for p in *{@host, @port, @path, @value} + L\push p\update, dt + ip = dns.toip @host\get! port = @port\get! msg = pack @path\get!, @value\get! diff --git a/lib/time.moon b/lib/time.moon index 3252b9f..22457bb 100644 --- a/lib/time.moon +++ b/lib/time.moon @@ -11,6 +11,9 @@ class lfo extends Op L\trace "setup #{@}, freq=#{@freq}, wave=#{@wave}" update: (dt) => + @freq\update dt + @wave\update dt + @phase += dt * @freq\get! @value = switch @wave\get! when 'sin' then .5 + .5 * math.cos @phase * tau @@ -26,6 +29,8 @@ class tick extends Op setup: (@freq) => update: (dt) => + @freq\update dt + @phase += dt / @freq\get! @value = math.floor @phase diff --git a/lib/util.moon b/lib/util.moon index 7306fbc..5079b1b 100644 --- a/lib/util.moon +++ b/lib/util.moon @@ -4,7 +4,11 @@ class pick extends Op setup: (@i, ...) => @choices = { ... } - update: => + update: (dt) => + @i\update dt + for choice in *@choices + choice\update dt + i = 1 + (math.floor @i\get!) % #@choices @value = @choices[i]\get! |
