diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-14 14:45:38 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-05-14 14:45:45 -0700 |
| commit | 7410e42bc00c8438884a475ccf8a9e07b73eaec3 (patch) | |
| tree | fc8bec6c4c2a885aceb75fc784bdc4d07cd5392f | |
| parent | 95ee15982bb021e94a4976741a0bcdfe5f5d6992 (diff) | |
Minor optimizations.
| -rw-r--r-- | code_obj.lua | 33 | ||||
| -rw-r--r-- | code_obj.moon | 23 | ||||
| -rw-r--r-- | nomsu.lua | 20 | ||||
| -rwxr-xr-x | nomsu.moon | 21 |
4 files changed, 48 insertions, 49 deletions
diff --git a/code_obj.lua b/code_obj.lua index c563a91..4c9eac0 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -151,38 +151,37 @@ do _class_0 = setmetatable({ __init = function(self, source, ...) self.source = source - self.indents = { } self.bits = { ... } - if type(self.source) == 'string' then - local filename, start, stop = self.source:match("^(.-)%[(%d+):(%d+)%]$") - if not (filename) then - filename, start = self.source:match("^(.-)%[(%d+)%]$") - end - if start or stop then - self.source = Source(filename, tonumber(start), tonumber(stop)) - else - self.source = Source(self.source, 1, #tostring(self) + 1) - end - end - assert(self.source == nil or Source:is_instance(self.source)) - local indent = 0 + local indent, indents = 0, { } + local match = string.match 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]*$") + local spaces = match(b, "\n([ ]*)[^\n]*$") if spaces then indent = #spaces end end elseif indent ~= 0 then - self.indents[i] = indent + 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 + filename, start = self.source:match("^(.-)%[(%d+)%]$") + end + if start or stop then + self.source = Source(filename, tonumber(start), tonumber(stop)) + else + self.source = Source(self.source, 1, #tostring(self) + 1) + end + end 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 @@ -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 @@ -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 |
