Skip to content

Commit

Permalink
reassign duplicate tags (and print a warning)
Browse files Browse the repository at this point in the history
Close #14
  • Loading branch information
s-ol committed Apr 18, 2020
1 parent 2403b5b commit 80d568c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
29 changes: 14 additions & 15 deletions alv/registry.moon
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ class Registry

--- set the current registration.
--
-- @tparam string\number index the registration index
-- @tparam Tag tag the Tag to register
-- @tparam any expr the registration value
-- @tparam[default=false] boolean ignore_dup ignore duplicate registrations
register: (index, expr, ignore_dup=false) =>
L\trace "reg: setting #{index} to #{expr}"
if not ignore_dup and @map[index]
error Error 'tag', "duplicate tags [#{index}]!"
@map[index] = expr

--- request identity and registration for blank tag.
--
-- @tparam Tag tag the blank tag
-- @tparam any expr the registration value
init: (tag, expr) =>
L\trace "reg: init pending to #{expr}"
table.insert @pending, { :tag, :expr }
register: (tag, expr, ignore_dup=false) =>
index = tag\index!

if index and (not @map[index] or ignore_dup)
L\trace "reg: setting #{index} to #{expr}"
@map[index] = expr
else
if index
L\warn "duplicate tag [#{index}], reassigning repeated occurance"
tag\set nil
else
L\trace "reg: init #{tag} to #{expr}"
table.insert @pending, { :tag, :expr }

--- members
-- @section members
Expand All @@ -45,7 +45,6 @@ class Registry
-- All calls go `begin_eval` must be matched with either a call to
-- `end_eval` or `rollback_eval`.
begin_eval: =>
@latest_map = @last_map
@begin_tick!
@map, @pending = {}, {}

Expand Down
19 changes: 8 additions & 11 deletions alv/tag.moon
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ class Tag
-- Will mark blank tags for auto-assignment at the end of the eval cycle.
--
-- @tparam any expr the value to register
register: (expr) =>
if index = @index!
Registry.active!\register index, expr
else
Registry.active!\init @, expr
register: (expr) => Registry.active!\register @, expr

--- create a copy of this tag scoped to a `parent` tag.
--
Expand All @@ -52,10 +48,7 @@ class Tag
clone: (parent) =>
-- ensure this tag is registered for the current eval cycle,
-- even if it is blank and has no associated value
if index = @index!
Registry.active!\register index, dummy, true
else
Registry.active!\init @, dummy
Registry.active!\register @, dummy, true

assert parent, "need parent to clone!"
ClonedTag @, parent
Expand All @@ -77,9 +70,13 @@ class Tag
index: => @value

--- callback to set value for blank tags.
-- @tparam number value
--
-- `value` may be blank to reassign duplicate tags.
--
-- @tparam ?number value
set: (value) =>
assert not @value, "#{@} is not blank"
either_or = (@value or value) and not (@value and value)
assert either_or, "unexpected :set #{value} on #{@}"
@value = value

--- static functions
Expand Down

0 comments on commit 80d568c

Please sign in to comment.