diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-03-22 11:44:49 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-03-22 11:44:49 +0000 |
| commit | 1c6df2515f4a221edf3374aec44de7bcdca7c90c (patch) | |
| tree | e3d8fe1038c542cec2ed3d908fbe11dcb0f8f78f | |
| parent | more Errors, doc (diff) | |
| download | alive-1c6df2515f4a221edf3374aec44de7bcdca7c90c.tar.gz alive-1c6df2515f4a221edf3374aec44de7bcdca7c90c.zip | |
error handling: scope + registry
| -rw-r--r-- | core/init.moon | 2 | ||||
| -rw-r--r-- | core/registry.moon | 4 | ||||
| -rw-r--r-- | core/scope.moon | 20 | ||||
| -rw-r--r-- | core/value.moon | 2 |
4 files changed, 18 insertions, 10 deletions
diff --git a/core/init.moon b/core/init.moon index 1aa0d2d..8a0154c 100644 --- a/core/init.moon +++ b/core/init.moon @@ -57,7 +57,7 @@ globals = Scope.from_table require 'core.builtin' scope = Scope nil, globals scope\use inject if inject - ast = assert (cell\match str), "failed to parse: #{str}" + ast = assert (program\match str), "failed to parse" result = ast\eval scope result\const! } diff --git a/core/registry.moon b/core/registry.moon index 1183a0d..332c9e1 100644 --- a/core/registry.moon +++ b/core/registry.moon @@ -5,6 +5,7 @@ import Value from require 'core.value' import Result from require 'core.result' +import Error from require 'core.error' unpack or= table.unpack @@ -25,7 +26,8 @@ class Registry -- @tparam[default=false] boolean ignore_dup ignore duplicate registrations register: (index, expr, ignore_dup=false) => L\trace "reg: setting #{index} to #{expr}" - assert not @map[index] or ignore_dup, "duplicate tags with index #{index}!" + if not ignore_dup and @map[index] + error Error 'tag', "duplicate tags [#{index}]!" @map[index] = expr --- request identity and registration for blank tag. diff --git a/core/scope.moon b/core/scope.moon index f3699d7..37a1ba3 100644 --- a/core/scope.moon +++ b/core/scope.moon @@ -4,6 +4,7 @@ -- @classmod Scope import Value from require 'core.value' import Result from require 'core.result' +import Error from require 'core.error' class Scope --- members @@ -26,33 +27,38 @@ class Scope set: (key, val) => L\trace "setting #{key} = #{val} in #{@}" assert val.__class == Result, "expected #{key}=#{val} to be Result" - assert not @values[key], "cannot redefine symbol #{key}!" + assert val.value, Error 'type', "cannot define symbol to nil" + assert not @values[key], Error 'type', "cannot redefine symbol '#{key}'!" @values[key] = val recurse: (key) => parent = if key\match '^%*.*%*$' then @dynamic_parent else @parent parent or= @parent - return parent and L\push parent\get, key + if parent + L\push parent\get, key + else + error Error 'reference', "undefined symbol '#{key}'" --- resolve a key in this Scope. -- -- @tparam string key the key to resolve - -- @tparam[opt] string prefix a prefix (for internal use with nested scopes) -- @treturn ?Result the value of the definition that was found, or `nil` - get: (key, prefix='') => + get: (key) => L\debug "checking for #{key} in #{@}" if val = @values[key] L\trace "found #{val} in #{@}" return val start, rest = key\match '^(.-)/(.+)' - if not start return @recurse key child = @get start - assert child and child.value.type == 'scope', "#{start} is not a scope (looking for #{key})" - child.value\unwrap!\get rest, "#{prefix}#{start}/" + if not child + error Error 'reference', "undefined symbol '#{start}'" + if child\type! != 'scope' + error Error 'reference', "'#{start}' is not a scope" + child.value!\get rest, while_msg --- copy definitions from another scope. -- diff --git a/core/value.moon b/core/value.moon index da7f6d0..bdc4b39 100644 --- a/core/value.moon +++ b/core/value.moon @@ -107,7 +107,7 @@ class Value when 'num', 'str' Result value: @ when 'sym' - assert (scope\get @value), Error 'reference', "undefined symbol '#{@value}'" + Error.wrap "resolving symbol '#{@value}'", scope\get, @value else error "cannot evaluate #{@}" |
