From e17822d1e548f96952efb95c64c69704fdfeaf76 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 29 May 2018 11:13:58 -0700 Subject: [PATCH] Cleaned up some asserts for performance. --- code_obj.lua | 13 +++++-------- code_obj.moon | 8 ++------ nomsu.lua | 10 +++++++--- nomsu.moon | 9 ++++++--- nomsu_tree.lua | 3 --- nomsu_tree.moon | 2 -- 6 files changed, 20 insertions(+), 25 deletions(-) diff --git a/code_obj.lua b/code_obj.lua index e77af5c..990f3bc 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -15,10 +15,8 @@ Source = immutable({ if not start then start, stop = 1, #FILE_CACHE[filename] end - if stop then - assert(start <= stop + 1, "Invalid range: " .. tostring(start) .. ", " .. tostring(stop)) - else - error("HUH?") + if stop and start > stop + 1 then + error("Invalid range: " .. tostring(start) .. ", " .. tostring(stop)) end return filename, start, stop end, @@ -56,7 +54,9 @@ Source = immutable({ if type(self) == 'number' then offset, self = self, offset else - assert(type(offset) == 'number', "Cannot add Source and " .. tostring(type(offset))) + if type(offset) ~= 'number' then + error("Cannot add Source and " .. tostring(type(offset))) + end end return Source(self.filename, self.start + offset, self.stop) end, @@ -109,8 +109,6 @@ do local match = string.match for i = 1, n do local b = select(i, ...) - assert(b ~= self, "No recursion please.") - assert(not Source:is_instance(b)) bits[#bits + 1] = b if type(b) == 'string' then do @@ -136,7 +134,6 @@ do end self.current_indent = 0 for i, b in ipairs(bits) do - assert(b ~= self, "No recursion please.") if type(b) == 'string' then do local spaces = b:match("\n([ ]*)[^\n]*$") diff --git a/code_obj.moon b/code_obj.moon index d764430..e821b7e 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -8,8 +8,7 @@ Source = immutable {"filename","start","stop"}, { __new: (filename, start, stop)=> if not start start, stop = 1, #FILE_CACHE[filename] - if stop then assert(start <= stop+1, "Invalid range: #{start}, #{stop}") - else error("HUH?") + if stop and start > stop+1 then error("Invalid range: #{start}, #{stop}") return filename, start, stop from_string: (str)=> filename,start,stop = str\match("^(.-)%[(%d+):(%d+)%]$") @@ -34,7 +33,7 @@ Source = immutable {"filename","start","stop"}, { __add: (offset)=> if type(self) == 'number' offset, self = self, offset - else assert(type(offset) == 'number', "Cannot add Source and #{type(offset)}") + else if type(offset) != 'number' then error("Cannot add Source and #{type(offset)}") return Source(@filename, @start+offset, @stop) sub: (start, stop)=> start or= 1 @@ -80,8 +79,6 @@ class Code match = string.match for i=1,n b = select(i, ...) - assert(b != self, "No recursion please.") - assert(not Source\is_instance(b)) bits[#bits+1] = b if type(b) == 'string' if spaces = match(b, "\n([ ]*)[^\n]*$") @@ -99,7 +96,6 @@ class Code bits[i] = select(i, ...) @current_indent = 0 for i,b in ipairs(bits) - assert(b != self, "No recursion please.") if type(b) == 'string' if spaces = b\match("\n([ ]*)[^\n]*$") @current_indent = #spaces diff --git a/nomsu.lua b/nomsu.lua index 2587080..10c907f 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -1,7 +1,7 @@ local _pairs, _ipairs = pairs, ipairs if jit then package.cpath = "./luajit_lpeg/?.so;" .. package.cpath - lpeg = require("lpeglj") + lpeg = require('lpeg') bit32 = require('bit') pairs = function(x) do @@ -269,7 +269,9 @@ do if is_compile_action == nil then is_compile_action = false end - assert(type(fn) == 'function', "Bad fn: " .. tostring(repr(fn))) + if type(fn) ~= 'function' then + error("Not a function: " .. tostring(repr(fn))) + end if type(signature) == 'string' then signature = { signature @@ -354,7 +356,9 @@ do return nil end local tree = self:parse(nomsu_code) - assert(tree, "Failed to parse: " .. tostring(nomsu_code)) + if not (tree) then + error("Failed to parse: " .. tostring(nomsu_code)) + end local lua = self:tree_to_lua(tree):as_statements() lua:declare_locals() lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n") diff --git a/nomsu.moon b/nomsu.moon index 7667600..883b702 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -15,7 +15,8 @@ _pairs, _ipairs = pairs, ipairs if jit package.cpath = "./luajit_lpeg/?.so;"..package.cpath --package.path = "./LPegLJ/src/?.lua;"..package.path - lpeg = require "lpeglj" + --lpeg = require "lpeglj" + lpeg = require 'lpeg' export bit32 bit32 = require('bit') @@ -302,7 +303,8 @@ class NomsuCompiler ]=], stub_defs var_pattern = re.compile "{| %space ((('%' {%varname}) / %word) %space)+ |}", stub_defs define_action: (signature, fn, is_compile_action=false)=> - assert(type(fn) == 'function', "Bad fn: #{repr fn}") + if type(fn) != 'function' + error("Not a function: #{repr fn}") if type(signature) == 'string' signature = {signature} elseif type(signature) != 'table' @@ -350,7 +352,8 @@ class NomsuCompiler run: (nomsu_code, compile_fn=nil)=> if #tostring(nomsu_code) == 0 then return nil tree = @parse(nomsu_code) - assert tree, "Failed to parse: #{nomsu_code}" + unless tree + error "Failed to parse: #{nomsu_code}" lua = @tree_to_lua(tree)\as_statements! lua\declare_locals! lua\prepend "-- File: #{nomsu_code.source or ""}\n" diff --git a/nomsu_tree.lua b/nomsu_tree.lua index 5b7a463..9a492f3 100644 --- a/nomsu_tree.lua +++ b/nomsu_tree.lua @@ -38,9 +38,6 @@ Tree = function(name, kind, methods) if not (next(fn)) then return self end - if type(next(fn)) == 'string' then - error("SHIT") - end local _replacements = fn fn = function(k) return _replacements[k] diff --git a/nomsu_tree.moon b/nomsu_tree.moon index 2d30486..5aa8f81 100644 --- a/nomsu_tree.moon +++ b/nomsu_tree.moon @@ -29,8 +29,6 @@ Tree = (name, kind, methods)-> .map = (fn)=> if type(fn) == 'table' return @ unless next(fn) - if type(next(fn)) == 'string' - error("SHIT") _replacements = fn fn = (k)-> _replacements[k] return @_map(fn)