Minor optimizations.

This commit is contained in:
Bruce Hill 2018-05-14 14:45:38 -07:00
parent 95ee15982b
commit 7410e42bc0
4 changed files with 49 additions and 50 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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