From cd493d4152d7b9a2dc7a4383bb24e4650afb4ea4 Mon Sep 17 00:00:00 2001 From: s-ol Date: Tue, 1 Sep 2020 15:24:37 +0200 Subject: support :vis() on builtins --- alv/base/builtin.moon | 11 +++++++++++ alv/builtins.moon | 7 ++++++- alv/copilot/udp.moon | 2 +- alv/invoke.moon | 7 +++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/alv/base/builtin.moon b/alv/base/builtin.moon index 54f7433..558a65a 100644 --- a/alv/base/builtin.moon +++ b/alv/base/builtin.moon @@ -53,6 +53,17 @@ class Builtin -- @tparam ?Builtin prev the previous Builtin instance setup: (prev) => + --- collect visualisation data (optional). + -- + -- This may return any simple Lua value, including Lua tables, as long as it + -- has no metatables, multiple references/loops, userdata etc. + -- + -- This value is exposed to alv editors in order to render realtime + -- visualisations overlaid onto the program text. + -- + -- @treturn table vis + vis: => {} + --- the `Cell` this Builtin was created for. -- @tfield Cell cell diff --git a/alv/builtins.moon b/alv/builtins.moon index 8a08705..d6a52b9 100644 --- a/alv/builtins.moon +++ b/alv/builtins.moon @@ -279,6 +279,7 @@ to `then-expr`, otherwise it is equivalent to `else-xpr` if given, or nil otherw msg = "'if'-expression needs to be constant, did you mean 'switch'?" error Error 'argument', msg xif = xif.result\unwrap! + @state = xif super if xif xthen\eval scope @@ -287,6 +288,8 @@ to `then-expr`, otherwise it is equivalent to `else-xpr` if given, or nil otherw else RTNode! + vis: => step: if @state then 0 else 1 + when_ = Constant.meta meta: name: 'when' @@ -366,7 +369,9 @@ switch_ = Constant.meta @out\set if v = values[@state + 1] then v! - vis: => step: @state + vis: => + with Op.vis @ + .step = @state trace_ = Constant.meta meta: diff --git a/alv/copilot/udp.moon b/alv/copilot/udp.moon index 0640189..f5deb50 100644 --- a/alv/copilot/udp.moon +++ b/alv/copilot/udp.moon @@ -49,7 +49,7 @@ class UDPServer if builtin and builtin.__class.__name ~= 'DummyReg' res.head_meta = builtin.head.meta res.result = encode_res builtin.node.result - res.vis = if builtin.op then builtin.op\vis! + res.vis = builtin\vis! res.kind = switch builtin.__class when op_invoke then 'op' when fn_invoke then 'fn' diff --git a/alv/invoke.moon b/alv/invoke.moon index 6962f4d..e40cbc4 100644 --- a/alv/invoke.moon +++ b/alv/invoke.moon @@ -77,6 +77,13 @@ class op_invoke extends Builtin super RTNode :children, result: @op.out, op: @op + --- `Builtin:vis` implementation. + -- + -- calls `op`:@{Op:vis|vis}. + -- + -- @treturn table vis + vis: => if @op then @op\vis! + --- The `Op` instance. -- -- @tfield Op op -- cgit v1.2.3