From f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b Mon Sep 17 00:00:00 2001 From: s-ol Date: Sun, 2 Feb 2020 19:51:13 +0100 Subject: rename scopify to Const.wrap --- base.moon | 81 +++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 23 deletions(-) (limited to 'base.moon') diff --git a/base.moon b/base.moon index 334431c..fc9f885 100644 --- a/base.moon +++ b/base.moon @@ -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: => "" __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 } -- cgit v1.2.3