From 7410e42bc00c8438884a475ccf8a9e07b73eaec3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 14 May 2018 14:45:38 -0700 Subject: [PATCH] Minor optimizations. --- code_obj.lua | 35 +++++++++++++++++------------------ code_obj.moon | 23 +++++++++++------------ nomsu.lua | 20 ++++++++++++-------- nomsu.moon | 21 +++++++++------------ 4 files changed, 49 insertions(+), 50 deletions(-) diff --git a/code_obj.lua b/code_obj.lua index c563a91..4c9eac0 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -151,10 +151,26 @@ do _class_0 = setmetatable({ __init = function(self, source, ...) self.source = source - self.indents = { } self.bits = { ... } + local indent, indents = 0, { } + local match = string.match + for i, b in ipairs(self.bits) do + if type(b) == 'string' then + do + local spaces = match(b, "\n([ ]*)[^\n]*$") + if spaces then + indent = #spaces + end + end + elseif indent ~= 0 then + indents[i] = indent + end + end + self.current_indent = indent + self.indents = indents + self.__str = nil if type(self.source) == 'string' then local filename, start, stop = self.source:match("^(.-)%[(%d+):(%d+)%]$") if not (filename) then @@ -166,23 +182,6 @@ do self.source = Source(self.source, 1, #tostring(self) + 1) end end - assert(self.source == nil or Source:is_instance(self.source)) - local indent = 0 - for i, b in ipairs(self.bits) do - assert(not Source:is_instance(b)) - if type(b) == 'string' then - do - local spaces = b:match("\n([ ]*)[^\n]*$") - if spaces then - indent = #spaces - end - end - elseif indent ~= 0 then - self.indents[i] = indent - end - end - self.current_indent = indent - self.__str = nil end, __base = _base_0, __name = "Code" diff --git a/code_obj.moon b/code_obj.moon index 3364cdf..caccd2b 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -62,8 +62,18 @@ Source = immutable {"filename","start","stop"}, { class Code new: (@source, ...)=> - @indents = {} @bits = {...} + indent, indents = 0, {} + match = string.match + for i,b in ipairs @bits + if type(b) == 'string' + if spaces = match(b, "\n([ ]*)[^\n]*$") + indent = #spaces + elseif indent != 0 + indents[i] = indent + @current_indent = indent + @indents = indents + @__str = nil if type(@source) == 'string' filename,start,stop = @source\match("^(.-)%[(%d+):(%d+)%]$") unless filename @@ -72,17 +82,6 @@ class Code @source = Source(filename, tonumber(start), tonumber(stop)) else @source = Source(@source, 1, #tostring(self)+1) - assert(@source == nil or Source\is_instance(@source)) - indent = 0 - for i,b in ipairs @bits - assert(not Source\is_instance(b)) - if type(b) == 'string' - if spaces = b\match("\n([ ]*)[^\n]*$") - indent = #spaces - elseif indent != 0 - @indents[i] = indent - @current_indent = indent - @__str = nil sub: (start,stop)=> -- TODO: implement this better diff --git a/nomsu.lua b/nomsu.lua index 9860772..472846a 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -131,12 +131,14 @@ do return self .. stringify(other) end STRING_METATABLE.__index = function(self, i) + local ret = string[i] + if ret ~= nil then + return ret + end if type(i) == 'number' then return string.sub(self, i, i) elseif type(i) == 'table' then return string.sub(self, i[1], i[2]) - else - return string[i] end end end @@ -1103,12 +1105,14 @@ OPTIONS print_err_msg(error_message) return os.exit(false, true) end - local ldt - ok, ldt = pcall(require, 'ldt') - if ok then - ldt.guard(run) - else - xpcall(run, err_hand) + do + local ldt + ok, ldt = pcall(require, 'ldt') + if ok then + ldt.guard(run) + else + xpcall(run, err_hand) + end end end return NomsuCompiler diff --git a/nomsu.moon b/nomsu.moon index 9b1ca59..64516e9 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -120,11 +120,10 @@ do STRING_METATABLE = getmetatable("") STRING_METATABLE.__add = (other)=> @ .. stringify(other) STRING_METATABLE.__index = (i)=> + ret = string[i] + if ret != nil then return ret if type(i) == 'number' then return string.sub(@, i, i) elseif type(i) == 'table' then return string.sub(@, i[1], i[2]) - else return string[i] - -- Can't use this because it breaks some LPEG stuff - --STRING_METATABLE.__mul = (other)=> string.rep(@, other) Types = require "nomsu_tree" @@ -309,6 +308,7 @@ class NomsuCompiler define_compile_action: (signature, fn)=> return @define_action(signature, fn, true) + _nomsu_chunk_counter = 0 parse: (nomsu_code)=> if type(nomsu_code) == 'string' _nomsu_chunk_counter += 1 @@ -333,7 +333,6 @@ class NomsuCompiler return tree - _nomsu_chunk_counter = 0 run: (nomsu_code, compile_fn=nil)=> if #tostring(nomsu_code) == 0 then return nil tree = @parse(nomsu_code) @@ -788,13 +787,11 @@ OPTIONS -- for both APIs -- TODO: revert back to old error handler - --ProFi = require 'ProFi' - --ProFi\start() - ok, ldt = pcall(require,'ldt') - if ok - ldt.guard run - else xpcall(run, err_hand) - --ProFi\stop() - --ProFi\writeReport( 'MyProfilingReport.txt' ) + --require('ProFi')\profile "scratch/profile.txt", (profi)-> + do + ok, ldt = pcall(require,'ldt') + if ok + ldt.guard run + else xpcall(run, err_hand) return NomsuCompiler