diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-02-13 09:07:05 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-02-13 09:07:05 +0000 |
| commit | 9f80c8b41ef2e42f9bc2cce0c2172e879d308dbc (patch) | |
| tree | 3b075136c22c090e9610eb51dd7e1a70bdb3c64e /core/registry.moon | |
| parent | add logic lib (diff) | |
| download | alive-9f80c8b41ef2e42f9bc2cce0c2172e879d308dbc.tar.gz alive-9f80c8b41ef2e42f9bc2cce0c2172e879d308dbc.zip | |
no more @registry juggling
Diffstat (limited to 'core/registry.moon')
| -rw-r--r-- | core/registry.moon | 102 |
1 files changed, 29 insertions, 73 deletions
diff --git a/core/registry.moon b/core/registry.moon index 7c89fa4..6afd361 100644 --- a/core/registry.moon +++ b/core/registry.moon @@ -1,75 +1,3 @@ -import Const from require 'core.const' -import Scope from require 'core.scope' - -local ClonedTag - -class Tag - new: (@value) => - - clone: (parent) => ClonedTag @, parent - - last: => - if index = @index! - @registry\last index - - keep: (expr) => - index = assert @index! - assert expr == @registry\last index - @registry\replace index, expr - - replace: (expr) => - if index = @index! - @registry\replace index, expr - else - @registry\init @, expr - -index: => @value - - set: (value) => - assert not @value, "setting #{@} again" - @value = value - - @blank: -> Tag! - @parse: (num) => @ tonumber num - - stringify: => if @value then "[#{@value}]" else '' - - __tostring: => if @value then "#{@value}" else '[blank]' - -class ClonedTag extends Tag - class DummyReg - destroy: => - - new: (@original, @parent) => - @registry = @original.registry - @dummy = DummyReg! - - keep: (expr) => - super\keep expr - @original.registry or= @registry - @original\replace @dummy - - replace: (expr) => - super\replace expr - @original.registry or= @registry - @original\replace @dummy - - index: => - orig = @original\index! - parent = @parent\index! - if orig and parent - "#{parent}.#{orig}" - - set: (value) => @original\set value - - stringify: => error "cant stringify ClonedTag" - - __tostring: => - if @parent - "#{@parent}.#{@original}" - else - tostring @original - class Registry new: () => @map = {} @@ -87,9 +15,20 @@ class Registry L\trace "reg: init pending to #{expr}" table.insert @pending, { :tag, :expr } + active: -> + assert Registry.active_registry, "no active Registry!" + -- public methods + wrap: (fn) => + (...) -> + @prepare! + with fn ... + @finalize! + prepare: => + assert not @prev, "already have a previous registry? #{@prev}" + @prev, @@active_registry = @@active_registry, @ @last_map, @map, @pending = @map, {}, {} finalize: => @@ -106,9 +45,26 @@ class Registry tag\set @next_tag! @map[tag\index!] = expr + assert @ == @@active_registry, "not the active registry!" + @@active_registry, @prev = @prev, nil + next_tag: => #@map + 1 +class SimpleRegistry extends Registry + new: => + @cnt = 1 + + init: (tag, expr) => + tag\set @cnt + @cnt += 1 + + last: (index) => + replace: (index, expr) => + finalize: => + assert @ == @@active_registry, "not the active registry!" + @@active_registry, @prev = @prev, nil + { - :Tag :Registry + :SimpleRegistry } |
