From 69aaea030e08e083151aa25b8080eddd0d4c1683 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 9 Nov 2018 16:40:36 -0800 Subject: 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. --- code_obj.moon | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'code_obj.moon') 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)-> -- cgit v1.2.3