diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2018-11-09 16:40:36 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2018-11-09 16:41:19 -0800 |
| commit | 69aaea030e08e083151aa25b8080eddd0d4c1683 (patch) | |
| tree | 6cd90a65e6828f60cc052c4a1b33a4a4c0d65570 /code_obj.moon | |
| parent | a2f07415c5284bf94c146cea6eed4a15f171b9ab (diff) | |
No longer passing `tree` to every compile action. Now, you can just
return a LuaCode object, and it will automatically get a source from
`tree` if it didn't already have a source. Plus some fixes/cleanup.
Diffstat (limited to 'code_obj.moon')
| -rw-r--r-- | code_obj.moon | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/code_obj.moon b/code_obj.moon index fcf2217..e7fb5f3 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -43,13 +43,17 @@ class Source class Code is_code: true - new: (@source, ...)=> + new: (...)=> @bits = {} - if type(@source) == 'string' - @source = Source\from_string(@source) - --assert(@source and Source\is_instance(@source), "Source has the wrong type") @append(...) + @from: (source, ...)=> + inst = self(...) + if type(source) == 'string' + source = Source\from_string(source) + inst.source = source + return inst + @is_instance: (x)=> type(x) == 'table' and x.__class == @ text: => @@ -71,7 +75,10 @@ class Code __tostring: => @text! as_lua: => - "#{@__class.__name}(#{concat {tostring(@source)\as_lua!, unpack([b\as_lua! for b in *@bits])}, ", "})" + if @source + "#{@__class.__name}:from(#{concat {tostring(@source)\as_lua!, unpack([b\as_lua! for b in *@bits])}, ", "})" + else + "#{@__class.__name}(#{concat [b\as_lua! for b in *@bits], ", "})" __len: => #@text! @@ -214,18 +221,8 @@ class LuaCode extends Code @prepend "local #{concat to_declare, ", "};\n" return to_declare - as_statements: (prefix="", suffix=";")=> - if @text!\matches(";$") or @text! == "" - return self - statements = LuaCode(@source) - if prefix != "" - statements\append prefix - statements\append self - if suffix != "" - statements\append suffix - return statements - make_offset_table: => + assert @source, "This code doesn't have a source" -- Return a mapping from output (lua) character number to input (nomsu) character number lua_to_nomsu, nomsu_to_lua = {}, {} walk = (lua, pos)-> |
