aboutsummaryrefslogtreecommitdiffstats
path: root/base.moon
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-02 18:51:13 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-02 18:51:13 +0000
commitf4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b (patch)
tree7d0760d51662af64219e5e388c794967081a526b /base.moon
parentclean out old patching implementation (diff)
downloadalive-f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b.tar.gz
alive-f4abdd9b5e5d52b515fed14ba9b46ed5d9f62e0b.zip
rename scopify to Const.wrap
Diffstat (limited to 'base.moon')
-rw-r--r--base.moon81
1 files changed, 58 insertions, 23 deletions
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: => "<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
}