diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-03-01 17:44:56 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-03-01 17:46:04 +0000 |
| commit | a1312100fad100190e20b1b3443b5cd962dc8999 (patch) | |
| tree | 8fc6194fc4b326ee39a9ca6d798cd208664f5fc3 /core | |
| parent | add type-pattern matching language (diff) | |
| download | alive-a1312100fad100190e20b1b3443b5cd962dc8999.tar.gz alive-a1312100fad100190e20b1b3443b5cd962dc8999.zip | |
new op interface part one
Diffstat (limited to 'core')
| -rw-r--r-- | core/base.moon | 77 | ||||
| -rw-r--r-- | core/cell.moon | 4 | ||||
| -rw-r--r-- | core/invoke.moon | 8 | ||||
| -rw-r--r-- | core/pattern.moon | 4 | ||||
| -rw-r--r-- | core/registry.moon | 6 | ||||
| -rw-r--r-- | core/value.moon | 33 |
6 files changed, 97 insertions, 35 deletions
diff --git a/core/base.moon b/core/base.moon index 422e631..0a88cc1 100644 --- a/core/base.moon +++ b/core/base.moon @@ -1,8 +1,40 @@ -- base definitions for extensions -import Value from require 'core.value' +import Value, Result from require 'core.value' unpack or= table.unpack +class Input + new: (value) => + @stream = switch value.__class + when Result + assert value.value, "Input from result without value!" + when Value + value + else + error "Input from unknown value: #{value}" + + merge: (previous) => + + finish_setup: => + dirty: => @stream\dirty! + unwrap: => @stream\unwrap! + type: => @stream.type + __call: => @stream\unwrap! + __inherited: (cls) => cls.__base.__call = @__call + +-- ValueInput scheduling policy +-- +-- during setup, only marked dirty if old and new stream differ in value +class ValueInput extends Input + merge: (old) => @dirty_setup = not old or @stream\unwrap! != old\unwrap! + finish_setup: => @dirty_setup = false + dirty: => @dirty_setup or @stream\dirty! + +-- EventInput scheduling policy +-- +-- only marked dirty if the input stream itself is dirty +class EventInput extends Input + -- a persistent expression Operator -- -- accepts Const or Stream inputs and produces a Stream output @@ -17,7 +49,37 @@ class Op -- after this method finishes, :tick(true) is called once, after which -- @impulses and @out have to be set and may not change until :setup() -- is called again. - setup: (@inputs) => + setup: do + do_merge = (old, cur) -> + for k, cur_val in pairs cur + old_val = old and old[k] + + -- are these inputs or nested tables? + cur_plain = cur_val and not cur_val.__class + old_plain = old_val and not old_val.__class + + if cur_plain and old_plain + -- both are tables, recurse + do_merge old_val, cur_val + elseif cur_plain == old_plain + -- both are streams (or nil), merge them + cur_val\merge old_val + + (inputs) => + old_inputs = @inputs + @inputs = inputs + do_merge old_inputs, @inputs + + -- iterate over the (potentially nested) inputs table + all_inputs: do + do_yield = (table) -> + for k, v in pairs table + if v.__class + coroutine.yield v + else + do_yield v + + => coroutine.wrap -> do_yield @inputs -- called once per frame if any inputs or impulses are dirty, and once -- immediately after :setup(). 'first' will be true in the latter case. @@ -28,8 +90,14 @@ class Op destroy: => -- utilities - unwrap_inputs: => - unpack [input! for input in *@inputs] + unwrap_all: do + do_unwrap = (value) -> + if value.__class + value\unwrap! + else + {k, do_unwrap v for k,v in pairs value} + + => do_unwrap @inputs assert_types: (...) => num = select '#', ... @@ -123,6 +191,7 @@ class FnDef "(fn (#{table.concat [p\stringify! for p in *@params], ' '}) ...)" { + :ValueInput, :EventInput :Dispatcher :Op :Action diff --git a/core/cell.moon b/core/cell.moon index 4b28a04..c052d46 100644 --- a/core/cell.moon +++ b/core/cell.moon @@ -19,10 +19,6 @@ class Cell when 'builtin' head\unwrap! else - print head - for k,v in pairs head - print k,v - print head.__class.__name error "cannot evaluate expr with head #{head}" Action\eval_cell scope, @tag, head, @tail! diff --git a/core/invoke.moon b/core/invoke.moon index da82d00..7b14e97 100644 --- a/core/invoke.moon +++ b/core/invoke.moon @@ -10,15 +10,15 @@ class op_invoke extends Action def = head\unwrap 'opdef', "cant op-invoke #{@head}" @head, @op = head, def! - @first = true true eval: (scope, tail) => children = L\push -> [L\push expr\eval, scope for expr in *tail] - @op\setup [child.value for child in *children] - @op\tick @first - @first = nil + @op\setup [result for result in *children] + @op\tick true + for input in @op\all_inputs! + input\finish_setup! Result :children, value: @op.out, op: @op diff --git a/core/pattern.moon b/core/pattern.moon index 45701e6..64b53b8 100644 --- a/core/pattern.moon +++ b/core/pattern.moon @@ -40,7 +40,7 @@ class Pattern else matches = @matches results[1] assert @opt or matches, "couldn't match argument #{results[1]} as type #{@type}!" - matches and table.remove results, 1 + if matches then table.remove results, 1 match = (pattern, results) -> patterns = while pattern @@ -50,7 +50,7 @@ match = (pattern, results) -> Pattern pat values = [p\match results for p in *patterns] assert #results == 0, "#{#results} extra arguments given!" - unpack values + values { :Pattern diff --git a/core/registry.moon b/core/registry.moon index 2ac5825..2a83f7e 100644 --- a/core/registry.moon +++ b/core/registry.moon @@ -1,11 +1,11 @@ -import Value from require 'core.value' +import Result, Value from require 'core.value' class Registry new: () => @map = {} @tick = 0 - @kr = Value.bool true + @kr = Result value: Value.bool true -- methods for Tag @@ -48,7 +48,7 @@ class Registry wrap_tick: (fn) => (...) -> @grab! @tick += 1 - @kr\set true + @kr.value\set true with fn ... @release! diff --git a/core/value.moon b/core/value.moon index 5379921..0d44320 100644 --- a/core/value.moon +++ b/core/value.moon @@ -27,36 +27,38 @@ class Result @op = params.op @children = params.children or {} - @all_impulses = {} + @side_inputs, is_child = {}, {} for child in *@children - for d in pairs child.all_impulses - @all_impulses[d] = true + for d in pairs child.side_inputs + @side_inputs[d] = true + if child.value + is_child[child.value] = true if @op - assert @op.impulses, "#{@op} not set up correctly (impulses)" - for d in *@op.impulses - @all_impulses[d] = true + for input in @op\all_inputs! + continue if is_child[input] + @side_inputs[input] = true - is_const: => not next @all_impulses + is_const: => not next @side_inputs -- asserts value-constness and returns the value const: (msg) => - assert not (next @all_impulses), msg or "eval-time const expected" + assert not (next @side_inputs), msg or "eval-time const expected" @value -- create a value-copy of this result that has the same impulses but without -- affecting the original's update logic make_ref: => with Result value: @value - .all_impulses = @all_impulses + .side_inputs = @side_inputs -- in depth-first order, tick all Ops who have dirty Stream inputs or impulses -- -- short-circuits if there are no dirty Streams in the entire subtree tick: => any_dirty = false - for stream in pairs @all_impulses - if stream\dirty! + for input in pairs @side_inputs + if input\dirty! any_dirty = true break @@ -70,16 +72,12 @@ class Result -- we have to check self_dirty here, because streams from child -- expressions might have changed self_dirty = false - for stream in *@op.impulses - if stream\dirty! - self_dirty = true - break - for stream in *@op.inputs + for stream in @op\all_inputs! if stream\dirty! self_dirty = true break - L\trace "#{op} is #{if self_dirty then 'dirty' else 'clean'}" + L\trace "#{@op} is #{if self_dirty then 'dirty' else 'clean'}" return unless self_dirty @op\tick! @@ -100,7 +98,6 @@ class Value -- @value - Lua value - access through :unwrap() new: (@type, @value, @raw) => @updated = 0 - pcall @\set, @value dirty: => @updated == Registry.active!.tick |
