diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-02-02 18:51:13 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-02-02 18:51:13 +0000 |
| commit | f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b (patch) | |
| tree | 7d0760d51662af64219e5e388c794967081a526b | |
| parent | clean out old patching implementation (diff) | |
| download | alive-f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b.tar.gz alive-f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b.zip | |
rename scopify to Const.wrap
| -rw-r--r-- | base.moon | 81 | ||||
| -rw-r--r-- | scope.moon | 45 |
2 files changed, 63 insertions, 63 deletions
@@ -1,26 +1,5 @@ import is_object from require 'moon' - -class Const - types = { - sym: true - scope: true - str: true - num: true - op: true - opdef: true - macrodef: true - } - new: (@type, @value) => - assert types[@type], "invalid Const type: #{@type}" - - get: => @value - getc: => @value - - destroy: => - - __tostring: => - value = if @type\match 'def$' then @value.__name else @value - "<#{@type}: #{value}>" +import Scope from require 'scope' class Op new: (...) => @@ -55,8 +34,64 @@ class Macro __tostring: => "<macro: #{@@__name}>" __inherited: (cls) => cls.__base.__tostring = @__tostring +class Const + types = { + sym: true + scope: true + str: true + num: true + op: true + opdef: true + macrodef: true + } + new: (@type, @value) => + assert types[@type], "invalid Const type: #{@type}" + + get: => @value + getc: => @value + + destroy: => + + __tostring: => + value = if @type\match 'def$' then @value.__name else @value + "<#{@type}: #{value}>" + + ancestor = (klass) -> + assert klass, "cant find the ancestor of nil" + while klass.__parent + klass = klass.__parent + klass + wrap: (val, name='(unknown)') -> + typ = switch type val + when 'number' then 'num' + when 'string' then 'str' + when 'table' + if base = rawget val, '__base' + -- a class + switch ancestor val + when Op then 'opdef' + when Macro then 'macrodef' + else + error "#{name}: cannot wrap class '#{val.__name}'" + elseif klass = val.__class + -- an instance + switch ancestor klass + when Op then 'op' + when Scope then 'scope' + when Const + return val + else + error "#{name}: cannot wrap '#{klass.__name}' instance" + else + -- plain table + return Const 'scope', Scope.from_table val + else + error "#{name}: cannot wrap Lua type '#{type val}'" + + Const typ, val + { - :Const :Op :Macro + :Const } @@ -1,46 +1,12 @@ -import Const, Op, Macro from require 'base' - -ancestor = (klass) -> - assert klass, "cant find the ancestor of nil" - while klass.__parent - klass = klass.__parent - klass - -local Scope - -constify = (val, key) -> - typ = switch type val - when 'number' then 'num' - when 'string' then 'str' - when 'table' - if base = rawget val, '__base' - -- a class - switch ancestor val - when Op then 'opdef' - when Macro then 'macrodef' - else - error "#{key}: cannot constify klass '#{val.__name}'" - elseif klass = val.__class - -- an instance - switch ancestor klass - when Op then 'op' - when Scope then 'scope' - when Const - return val - else - error "#{key}: cannot constify '#{klass.__name}' instance" - else - return Const 'scope', Scope.from_table val - else - error "#{key}: cannot constify Lua type '#{type val}'" - - Const typ, val +local Const class Scope new: (@node, @parent) => + import Const from require 'base' + @values = {} - set_raw: (key, val) => @values[key] = constify val, key + set_raw: (key, val) => @values[key] = Const.wrap val, key set: (key, val) => L\trace "setting #{key} = #{val}" @values[key] = val @@ -66,7 +32,7 @@ class Scope from_table: (tbl) -> with Scope! - .values = { k, constify v, k for k,v in pairs tbl } + .values = { k, Const.wrap v, k for k,v in pairs tbl } __tostring: => buf = "<Scope" @@ -84,7 +50,6 @@ class Scope buf ..= ">" buf - { :Scope } |
