From 966eef2d6557eeffc5b5c7b379efc1f2e7d0de0b Mon Sep 17 00:00:00 2001 From: s-ol Date: Sun, 10 May 2020 16:25:09 +0200 Subject: make == --- alv-lib/logic.moon | 5 +++-- alv/base/match.moon | 4 +++- alv/result/base.moon | 11 +++++------ alv/result/const.moon | 9 ++------- alv/result/evt.moon | 4 ++-- alv/result/sig.moon | 11 ++++------- alv/type.moon | 1 + alv/util.moon | 8 ++++++++ spec/result/sig_spec.moon | 11 ++++++++++- spec/type_spec.moon | 13 ++++++++++++- 10 files changed, 50 insertions(+), 27 deletions(-) diff --git a/alv-lib/logic.moon b/alv-lib/logic.moon index c3cc66f..aaf255e 100644 --- a/alv-lib/logic.moon +++ b/alv-lib/logic.moon @@ -59,10 +59,11 @@ eq = Constant.meta return { :first, :rest } = @unwrap_all! + type = @inputs.first\type! equal = true for other in *rest - if first != other + if not type\eq first, other equal = false break @@ -89,7 +90,7 @@ not_eq = Constant.meta diff = true for a=1, #@inputs-1 for b=a+1, #@inputs - if @inputs[a].stream == @inputs[b].stream + if @inputs[a].result == @inputs[b].result diff = false break diff --git a/alv/base/match.moon b/alv/base/match.moon index 926c970..f19c4d8 100644 --- a/alv/base/match.moon +++ b/alv/base/match.moon @@ -50,11 +50,13 @@ import Primitive from require 'alv.type' local Repeat, Sequence, Choice, Optional class Pattern + fulltype = (res) -> (tostring res.type) .. res.metatype + match: (seq) => @reset! num, cap = @capture seq, 1 if num != #seq - args = table.concat [arg.result\fulltype! for arg in *seq], ' ' + args = table.concat [fulltype arg.result for arg in *seq], ' ' msg = "couldn't match arguments (#{args}) against pattern #{@}" error Error 'argument', msg cap diff --git a/alv/result/base.moon b/alv/result/base.moon index 7775349..734d614 100644 --- a/alv/result/base.moon +++ b/alv/result/base.moon @@ -4,6 +4,8 @@ -- implemented by `Constant`, `SigStream`, `EvtStream`, and `IOStream`. -- -- @classmod Result +import Type from require 'alv.type' +import ancestor from require 'alv.util' class Result --- Result interface. @@ -41,10 +43,6 @@ class Result -- -- @tfield string metatype - --- get the full typestring. - -- @treturn string `type .. metatype` - fulltype: => (tostring @type) .. @metatype - --- documentation metadata. -- -- an optional table containing metadata for error messages and @@ -60,15 +58,16 @@ class Result --- static functions -- @section static - _type = type --- construct a new Result. -- -- @classmethod -- @tparam type.Type type the type -- @tparam ?table meta the `meta` table new: (@type, @meta={}) => - assert @type and (_type @type) == 'table', "not a type: #{@type}" + assert @type and (ancestor @type.__class) == Type, "not a type: #{@type}" { :Result + __eq: (a, b) -> + a.type == b.type and a.type\eq a.value, b.value } diff --git a/alv/result/const.moon b/alv/result/const.moon index 077a2d8..72d5b96 100644 --- a/alv/result/const.moon +++ b/alv/result/const.moon @@ -4,17 +4,12 @@ -- Implements the `Result` and `AST` inteface. -- -- @classmod Constant -import Result from require 'alv.result.base' +import Result, __eq from require 'alv.result.base' import T from require 'alv.type' import RTNode from require 'alv.rtnode' import Error from require 'alv.error' import scope, base from require 'alv.cycle' - -ancestor = (klass) -> - assert klass, "cant find the ancestor of nil" - while klass.__parent - klass = klass.__parent - klass +import ancestor from require 'alv.util' class Constant extends Result --- Result interface diff --git a/alv/result/evt.moon b/alv/result/evt.moon index 7e6a249..ad4c102 100644 --- a/alv/result/evt.moon +++ b/alv/result/evt.moon @@ -65,8 +65,6 @@ class EvtStream extends Result --- push an event value into the stream. -- -- Marks this stream as dirty for the remainder of the current tick. - -- - -- @tparam any event add: (event) => if not @dirty! @events = {} @@ -74,6 +72,8 @@ class EvtStream extends Result @updated = COPILOT.T table.insert @events, event + --- the wrapped Lua value. + -- @tfield {any,...} events --- static functions -- @section static diff --git a/alv/result/sig.moon b/alv/result/sig.moon index b23afbe..7e9582f 100644 --- a/alv/result/sig.moon +++ b/alv/result/sig.moon @@ -2,7 +2,7 @@ -- Continuous stream of values. -- -- @classmod SigStream -import Result from require 'alv.result.base' +import Result, __eq from require 'alv.result.base' class SigStream extends Result --- Result interface @@ -37,11 +37,6 @@ class SigStream extends Result --- alias for `unwrap`. __call: (...) => @unwrap ... - --- compare two values. - -- - -- Compares two `SigStream`s by comparing their types and their Lua values. - __eq: (other) => other.type == @type and other.value == @value - --- the type of this Result's value. -- @tfield type.Type type @@ -68,10 +63,12 @@ class SigStream extends Result -- -- Marks this stream as dirty for the remainder of the current tick. set: (value) => - if value != @value + if not @type\eq value, @value @value = value @updated = COPILOT.T + :__eq + --- the wrapped Lua value. -- @tfield any value diff --git a/alv/type.moon b/alv/type.moon index c8808d4..686f6c0 100644 --- a/alv/type.moon +++ b/alv/type.moon @@ -144,6 +144,7 @@ T = setmetatable {}, __index: (key) => rawset @, key, type { + :Type :T :Primitive :Array diff --git a/alv/util.moon b/alv/util.moon index a93fe3f..316233b 100644 --- a/alv/util.moon +++ b/alv/util.moon @@ -24,6 +24,14 @@ opairs = (t, order_fn, only_strings=false) -> state = { :t, i: 0, index: sort t, order_fn, only_strings } onext, state, nil +--- find the ancestor of a MoonScript class +ancestor = (klass) -> + assert klass, "cant find the ancestor of nil" + while klass.__parent + klass = klass.__parent + klass + { :opairs + :ancestor } diff --git a/spec/result/sig_spec.moon b/spec/result/sig_spec.moon index 4d04b9c..6e9a11c 100644 --- a/spec/result/sig_spec.moon +++ b/spec/result/sig_spec.moon @@ -1,5 +1,5 @@ import do_setup from require 'spec.test_setup' -import SigStream, RTNode, Scope, SimpleRegistry, T from require 'alv' +import SigStream, Constant, RTNode, Scope, SimpleRegistry, T from require 'alv' import Op, Builtin from require 'alv.base' class TestOp extends Op @@ -51,6 +51,15 @@ describe 'SigStream', -> assert.is.equal (SigStream T.num, 3), val assert.not.equal (SigStream T.num, 4), val + it 'can be compared to a Constant', -> + val = SigStream T.num, 3 + assert.is.equal (Constant.num 3), val + assert.not.equal (Constant.num 4), val + + val = SigStream T.str, 'hello' + assert.is.equal (Constant.str 'hello'), val + assert.not.equal (Constant.sym 'hello'), val + describe ':set', -> it 'sets the value', -> val = SigStream T.num, 3 diff --git a/spec/type_spec.moon b/spec/type_spec.moon index a435067..b6315f1 100644 --- a/spec/type_spec.moon +++ b/spec/type_spec.moon @@ -1,11 +1,15 @@ require 'spec.test_setup' -import T, Primitive, Array, Struct from require 'alv' +import T, Type, Primitive, Array, Struct from require 'alv.type' +import ancestor from require 'alv.util' bool = Primitive 'bool' num = Primitive 'num' str = Primitive 'str' describe 'Primitive', -> + it 'inherits from Type', -> + assert.is.equal Type, ancestor Primitive.__class + it 'stringifies well', -> assert.is.equal 'bool', tostring bool assert.is.equal 'num', tostring num @@ -35,6 +39,10 @@ describe 'Primitive', -> describe 'Array', -> vec3 = Array 3, num str32 = Array 2, Array 3, str + + it 'inherits from Type', -> + assert.is.equal Type, ancestor Array.__class + it 'stringifies well', -> assert.is.equal 'num[3]', tostring vec3 assert.is.equal 'my-type[3][24]', tostring Array 24, Array 3, 'my-type' @@ -65,6 +73,9 @@ describe 'Struct', -> play = Struct { note: str, dur: num } abc = Struct { c: num, b: num, a: num } + it 'inherits from Type', -> + assert.is.equal Type, ancestor Struct.__class + it 'stringifies well', -> assert.is.equal '{dur: num note: str}', tostring play assert.is.equal '{a: num b: num c: num}', tostring abc -- cgit v1.2.3