diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-04-13 18:40:35 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-04-14 08:46:54 +0000 |
| commit | 1a8debe87762072b3a63b769aa515ebf63b4c70d (patch) | |
| tree | 33b42866307d11d2c0878b23e0ec0bb8925d3fcc /core/invoke.moon | |
| parent | spec base.match __tostring (diff) | |
| download | alive-1a8debe87762072b3a63b769aa515ebf63b4c70d.tar.gz alive-1a8debe87762072b3a63b769aa515ebf63b4c70d.zip | |
move into proper Lua module (`alv`)
Diffstat (limited to 'core/invoke.moon')
| -rw-r--r-- | core/invoke.moon | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/core/invoke.moon b/core/invoke.moon deleted file mode 100644 index 8fcb525..0000000 --- a/core/invoke.moon +++ /dev/null @@ -1,124 +0,0 @@ ----- --- Builtins for invoking `Op`s and `FnDef`s. --- --- @module invoke -import Result from require 'core.result' -import Builtin from require 'core.base' -import Scope from require 'core.scope' -import Error from require 'core.error' - -get_name = (value, raw) -> - meta = if value.meta then value.meta.name - locl = if raw and raw.type == 'sym' then raw! - - if locl - if meta and meta != locl - "'#{meta}' (local '#{locl}')" - else - "'#{locl}'" - else if meta - "'#{meta}'" - else - "(unnamed)" - ---- `Builtin` implementation that invokes an `Op`. --- --- @type op_invoke -class op_invoke extends Builtin - --- `Builtin:setup` implementation. - -- - -- `Op:fork`s the `prev`'s `Op` instance if given. Creates a new instance - -- otherwise. - setup: (prev) => - if prev - @op = prev.op\fork! - else - def = @head\unwrap 'opdef', "cant op-invoke #{@head}" - @op = def! - - --- `Builtin:destroy` implementation. - -- - -- calls `op`:@{Op:destroy|destroy}. - destroy: => @op\destroy! - - --- evaluate an `Op` invocation. - -- - -- `AST:eval`s the tail, and passes the result to `op`:@{Op:setup|setup}. Then - -- checks if any of `op`:@{Op:all_inputs|all_inputs} are @{Input:dirty|dirty}, - -- and if so, calls `op`:@{Op:tick|tick}. - -- - -- The `Result` contains `op`, `Op.value` and all the `Result`s from the tail. - -- - -- @tparam Scope scope the active scope - -- @tparam {AST,...} tail the arguments to this expression - -- @treturn Result - eval: (scope, tail) => - children = [L\push expr\eval, scope for expr in *tail] - - frame = "invoking op #{get_name @head, @cell\head!} at [#{@tag}]" - Error.wrap frame, @op\setup, [result for result in *children], scope - - any_dirty = false - for input in @op\all_inputs! - if input\dirty! - any_dirty = true - break - - if any_dirty - @op\tick true - - for input in @op\all_inputs! - input\finish_setup! - - Result :children, value: @op.out, op: @op - - --- The `Op` instance. - -- - -- @tfield Op op - ---- `Builtin` implementation that invokes a `FnDef`. --- --- @type fn_invoke -class fn_invoke extends Builtin - --- evaluate a user-function invocation. - -- - -- Creates a new `Scope` that inherits from `FnDef.scope` and has - -- `outer_scope` as an additional parent for dynamic symbol resolution. - -- Then `AST:eval`s the tail in `outer_scope`, and defines the results to the - -- names in `FnDef.params` in the newly created scope. Lastly, `AST:clone`s - -- `FnDef.body` with the prefix `Builtin.tag`, and `AST:eval`s it in the newly - -- created `Scope`. - -- - -- The `Result` contains the `Stream` from the cloned AST, and its children - -- are all the `Result`s from evaluating the tail as well as the cloned - -- `AST`s. - -- - -- @tparam Scope outer_scope the active scope - -- @tparam {AST,...} tail the arguments to this expression - -- @treturn Result the result of this evaluation - eval: (outer_scope, tail) => - name = get_name @head, @cell\head! - frame = "invoking function #{name} at [#{@tag}]" - - { :params, :body, :scope } = @head\unwrap 'fndef', "cant fn-invoke #{@head}" - if #params != #tail - err = Error 'argument', "expected #{#params} arguments, found #{#tail}" - err\add_frame frame - error err - - fn_scope = Scope scope, outer_scope - - children = for i=1,#params - name = params[i]\unwrap 'sym' - with L\push tail[i]\eval, outer_scope - fn_scope\set name, \make_ref! - - clone = body\clone @tag - result = Error.wrap frame, clone\eval, fn_scope - - table.insert children, result - Result :children, value: result.value - -{ - :op_invoke, :fn_invoke -} |
