git.s-ol.nu alive / e5e77aa
add base.match.any shorthand s-ol 4 months ago
2 changed file(s) with 26 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
2727 import Builtin from require 'alv.base.builtin'
2828 import FnDef from require 'alv.base.fndef'
2929 import Input from require 'alv.base.input'
30 import const, sig, evt from require 'alv.base.match'
30 import const, sig, evt, any from require 'alv.base.match'
3131 import Constant, SigStream, EvtStream from require 'alv.result'
3232 import T, Primitive, Array, Struct from require 'alv.type'
3333 import RTNode from require 'alv.rtnode'
3838 :Builtin
3939 :FnDef
4040 :Input
41 :const, :sig, :evt
41 :const, :sig, :evt, :any
4242
4343 -- redundant exports, to keep anything an extension might need in one import
4444
104104 return unless seq[i]
105105 type, mt = seq[i]\type!, seq[i]\metatype!
106106
107 if not casts[@metatype .. mt]
107 if @metatype and not casts[@metatype .. mt]
108108 return
109109
110110 match = if @type then type == @type else @remember type
112112 1, seq[i]
113113
114114 __call: => @@ @metatype, @type, true
115 __tostring: => "#{@type or 'any'}#{@metatype}"
115 __tostring: => "#{@type or 'any'}#{@metatype or ''}"
116116
117117 --- Repeat a pattern.
118118 --
278278
279279 --- `Type` shorthands for matching `ValueStream`s and `Constant`s.
280280 --
281 -- Call or index with a string to obtain a `Type` instance.
281 -- Call or index with a type or string to obtain a `Type` instance.
282282 -- Call to obtain a wildcard pattern.
283283 --
284284 -- sig.str, sig.num
296296
297297 --- `Type` shorthands for matching `EvtStream`s.
298298 --
299 -- Call or index with a string to obtain an `Type` instance.
299 -- Call or index with a type or string to obtain an `Type` instance.
300300 -- Call to obtain a wildcard pattern.
301301 --
302302 -- evt.bang, evt.str, evt.num
312312 __call: (...) => Type '!', ...
313313 }
314314
315 --- `Type` shorthands for matching any `Result`s.
316 --
317 -- Call or index with a type or string to obtain an `Type` instance.
318 -- Call to obtain a wildcard pattern.
319 --
320 -- any.bang, any.str, any.num
321 -- any['midi/message'], any(Primitive 'midi/message')
322 -- any()
323 --
324 -- @table evt
325 any = setmetatable {}, {
326 __index: (key) =>
327 with v = Type nil, T[key]
328 @[key] = v
329
330 __call: (...) => Type nil, ...
331 }
332
315333 {
316334 :Type, :Repeat, :Sequence, :Choice, :Optional
317 :const, :sig, :evt
318 }
335 :const, :sig, :evt, :any
336 }