From 222d4d07271a3296175d55d09b0bfbbd72613dd8 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 13 Apr 2018 14:54:35 -0700 Subject: More correct and more working. --- lua_obj.moon | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'lua_obj.moon') diff --git a/lua_obj.moon b/lua_obj.moon index 6cc3e00..1979c94 100644 --- a/lua_obj.moon +++ b/lua_obj.moon @@ -3,29 +3,26 @@ immutable = require 'immutable' local Lua, LuaValue, Location export LINE_STARTS -Location = immutable {"text_name","text","start","stop"}, { +Location = immutable {"filename","start","stop"}, { name:"Location" - __new: (text_name, text, start, stop)=> - assert(type(text_name) == 'string') - assert(type(text) == 'string') - assert(type(start) == 'number') - assert(type(stop or start) == 'number') - return text_name, text, start, stop or start - __tostring: => "#{@text_name}[#{@start}:#{@stop}]" + __new: (filename, start, stop)=> + assert(type(filename) == 'string' and type(start) == 'number' and type(stop) == 'number') + return filename, start, stop or start + __tostring: => "Location(\"#{@filename}\", #{@start}, #{@stop})" __lt: (other)=> - assert(@text == other.text, "Cannot compare sources from different texts") + assert(@filename == other.filename, "Cannot compare sources from different files") return if @start == other.start @stop < other.stop else @start < other.start __le: (other)=> - assert(@text == other.text, "Cannot compare sources from different texts") + assert(@filename == other.filename, "Cannot compare sources from different files") return if @start == other.start @stop <= other.stop else @start <= other.start - get_text: => @text\sub(@start,@stop) + get_text: => FILE_CACHE[@filename]\sub(@start,@stop) get_line_number: => -- TODO: do a binary search if this is actually slow, which I doubt - line_starts = LINE_STARTS[@text] + line_starts = LINE_STARTS[FILE_CACHE[@filename]] start_line = 1 while (line_starts[start_line+1] or (#src+1)) <= @start start_line += 1 @@ -33,12 +30,12 @@ Location = immutable {"text_name","text","start","stop"}, { while (line_starts[stop_line+1] or (#src+1)) <= @stop stop_line += 1 return start_line, stop_line - get_line: => "#{@text_name}:#{@get_line_number!}" + get_line: => "#{@filename}:#{@get_line_number!}" get_line_range: => start_line, stop_line = @get_line_number! return if stop_line == start_line - "#{text_name}:#{start_line}" - else "#{text_name}:#{start_line}-#{stop_line}" + "#{@filename}:#{start_line}" + else "#{@filename}:#{start_line}-#{stop_line}" } class Lua @@ -46,6 +43,9 @@ class Lua is_value: false new: (@source, ...)=> + if type(@source) == 'string' + filename,start,stop = @source\match("^(.-)[(%d+):(%d+)]$") + @source = Location(filename, tonumber(start), tonumber(stop)) for i=1,select("#",...) x = select(i,...) assert(type(x) != 'table' or getmetatable(x)) @@ -109,7 +109,7 @@ class Lua -- Return a mapping from output (lua) character number to input (nomsu) character number lua_str = tostring(self) metadata = { - nomsu_filename:@source.text_name, nomsu_file:@source.text, + nomsu_filename:@source.filename lua_filename:lua_chunkname, lua_file:lua_str lua_to_nomsu: {}, nomsu_to_lua: {} } @@ -122,7 +122,7 @@ class Lua for b in *lua.bits walk b lua_stop = lua_offset - nomsu_src, lua_src = lua.source, Location(lua_chunkname, lua_str, lua_start, lua_stop) + nomsu_src, lua_src = lua.source, Location(lua_chunkname, lua_start, lua_stop) metadata.lua_to_nomsu[lua_src] = nomsu_src metadata.nomsu_to_lua[nomsu_src] = lua_src walk self @@ -143,9 +143,9 @@ class LuaValue extends Lua assert(not ret\match(".*table: 0x.*")) return ret - as_statements: => - bits = {unpack @bits} - bits[#bits+1] = ";" + as_statements: (prefix="", suffix=";")=> + bits = {prefix, unpack @bits} + bits[#bits+1] = suffix return Lua(@source, unpack(bits)) parenthesize: => -- cgit v1.2.3