diff --git a/compile_lib.sh b/compile_lib.sh index aad6de1..9bf0487 100755 --- a/compile_lib.sh +++ b/compile_lib.sh @@ -1,21 +1,7 @@ #!/bin/sh # This file is a script that converts the .nom files in lib/ into slightly more optimized # precompiled versions that are only lua> ".." and =lua ".." bits which are faster to load. -FLUSH=false -while getopts ":f" opt; do - case $opt in - f) - FLUSH=true - echo "flushing..." - ;; - esac -done -if [ "$FLUSH" = true ] ; then - rm core/*.lua - rm lib/*.lua - #rm tests/*.lua -fi - +moonc *.moon for file in core/*.nom; do printf "Compiling $file ..." ./nomsu.moon -o "core/$(basename $file .nom).lua" $file @@ -26,8 +12,3 @@ for file in lib/*.nom; do ./nomsu.moon -o "lib/$(basename $file .nom).lua" $file echo "done." done -#for file in tests/*.nom; do -# printf "Compiling $file ..." -# ./nomsu.moon -c $file -# echo "done." -#done diff --git a/core/control_flow.nom b/core/control_flow.nom index 9f23e07..6d93e8a 100644 --- a/core/control_flow.nom +++ b/core/control_flow.nom @@ -271,7 +271,7 @@ immediately %condition: %func_call.2 %action: %func_call.3 .. - assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." + assume (%star = "*") or barf ".." Invalid format for 'when' statement. Lines must begin with '*' assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else" @@ -279,7 +279,7 @@ immediately lua> "table.insert(\%fallthroughs, \(%condition as lua expr));" do next %func_call - if: (%condition.type is "Word") and (%condition.value is "else") + if: %condition = "else" assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block" to %code write "\nelse\n " to %code write: %action as lua statements @@ -315,7 +315,7 @@ immediately assume (%func_call.type is "Action") or barf ".." Invalid format for 'when' statement. Only '*' blocks are allowed. with {%star:%func_call.1, %condition:%func_call.2, %action:%func_call.3} - assume ((%star and (%star.type is "Word")) and (%star.value is "*")) or barf ".." + assume (%star = "*") or barf ".." Invalid format for 'when' statement. Lines must begin with '*' assume %condition or barf ".." Invalid format for 'when' statement. Lines must begin with '*' and have a condition or the word "else" @@ -323,7 +323,7 @@ immediately lua> "table.insert(\%fallthroughs, \(%condition as lua expr))" do next %func_call - if: (%condition.type is "Word") and (%condition.value is "else") + if: %condition = "else" assume (not %is_first) or barf "'else' clause cannot be first in 'when % = ?' block" to %code write "\nelse\n " to %code write: %action as lua statements diff --git a/nomsu.lua b/nomsu.lua index ca007e6..d05a5e6 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -480,7 +480,7 @@ do local _len_0 = 1 for _index_0 = 1, #tree do local arg = tree[_index_0] - if arg.type ~= "Word" then + if type(arg) ~= "string" then _accum_0[_len_0] = arg _len_0 = _len_0 + 1 end @@ -508,8 +508,8 @@ do local lua = Lua.Value() if not action and math_expression:match(stub) then for i, tok in ipairs(tree) do - if tok.type == "Word" then - lua:append(tok.value) + if type(tok) == 'string' then + lua:append(tok) else local tok_lua = self:tree_to_lua(tok, Tuple(i, path)) if not (tok_lua.is_value) then @@ -530,7 +530,7 @@ do for i, tok in ipairs(tree) do local _continue_0 = false repeat - if tok.type == "Word" then + if type(tok) == "string" then _continue_0 = true break end @@ -749,8 +749,6 @@ do return Lua.Value(nil, tostring(tree.value)) elseif "Var" == _exp_0 then return Lua.Value(nil, tree:as_lua_id()) - elseif "Word" == _exp_0 then - return error("Cannot convert a Word to lua") elseif "Comment" == _exp_0 then return Lua(nil, "--" .. tree.value:gsub("\n", "\n--") .. "\n") else @@ -769,11 +767,11 @@ do if inline then local nomsu = Nomsu() for i, bit in ipairs(tree) do - if bit.type == "Word" then + if type(bit) == "string" then if i > 1 then nomsu:append(" ") end - nomsu:append(bit.value) + nomsu:append(bit) else local arg_nomsu = self:tree_to_nomsu(bit, true) if not (arg_nomsu) then @@ -794,8 +792,8 @@ do local next_space = "" local last_colon = nil for i, bit in ipairs(tree) do - if bit.type == "Word" then - nomsu:append(next_space, bit.value) + if type(bit) == "string" then + nomsu:append(next_space, bit) next_space = " " else local arg_nomsu @@ -889,7 +887,7 @@ do else local interp_nomsu = self:tree_to_nomsu(bit, true) if interp_nomsu then - if bit.type ~= "Word" and bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then + if bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then interp_nomsu:parenthesize() end nomsu:append("\\", interp_nomsu) @@ -912,7 +910,7 @@ do else local interp_nomsu = self:tree_to_nomsu(bit, true) if interp_nomsu then - if bit.type ~= "Word" and bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then + if bit.type ~= "List" and bit.type ~= "Dict" and bit.type ~= "Text" then interp_nomsu:parenthesize() end nomsu:append("\\", interp_nomsu) @@ -1073,8 +1071,6 @@ do return Nomsu(nil, tostring(tree.value)) elseif "Var" == _exp_0 then return Nomsu(nil, "%", tree.value) - elseif "Word" == _exp_0 then - return Nomsu(nil, tree.value) elseif "Comment" == _exp_0 then if inline then return nil diff --git a/nomsu.moon b/nomsu.moon index 7648998..5890592 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -433,7 +433,7 @@ class NomsuCompiler stub = tree\get_stub! compile_action = @environment.COMPILE_ACTIONS[stub] if compile_action - args = [arg for arg in *tree when arg.type != "Word"] + args = [arg for arg in *tree when type(arg) != "string"] -- Force all compile-time actions to take a tree location args = [args[p-1] for p in *@environment.ARG_ORDERS[compile_action][stub]] -- Force Lua to avoid tail call optimization for debugging purposes @@ -448,8 +448,8 @@ class NomsuCompiler -- math expressions like 2*x + 3^2 without having to define a single -- action for every possibility. for i,tok in ipairs tree - if tok.type == "Word" - lua\append tok.value + if type(tok) == 'string' + lua\append tok else tok_lua = @tree_to_lua(tok, Tuple(i, path)) unless tok_lua.is_value @@ -463,7 +463,7 @@ class NomsuCompiler args = {} for i, tok in ipairs tree - if tok.type == "Word" then continue + if type(tok) == "string" then continue arg_lua = @tree_to_lua(tok, Tuple(i, path)) unless arg_lua.is_value error "Cannot use:\n#{colored.yellow repr(tok)}\nas an argument to #{stub}, since it's not an expression, it produces: #{repr arg_lua}", 0 @@ -625,9 +625,6 @@ class NomsuCompiler when "Var" Lua.Value(nil, tree\as_lua_id!) - when "Word" - error("Cannot convert a Word to lua") - when "Comment" Lua(nil, "--"..tree.value\gsub("\n","\n--").."\n") @@ -641,10 +638,10 @@ class NomsuCompiler if inline nomsu = Nomsu! for i,bit in ipairs tree - if bit.type == "Word" + if type(bit) == "string" if i > 1 nomsu\append " " - nomsu\append bit.value + nomsu\append bit else arg_nomsu = @tree_to_nomsu(bit,true) return nil unless arg_nomsu @@ -660,8 +657,8 @@ class NomsuCompiler -- TODO: track line length as we go and use 80-that instead of 80 for wrapping last_colon = nil for i,bit in ipairs tree - if bit.type == "Word" - nomsu\append next_space, bit.value + if type(bit) == "string" + nomsu\append next_space, bit next_space = " " else arg_nomsu = if last_colon == i-1 and bit.type == "Action" then nil @@ -736,7 +733,7 @@ class NomsuCompiler else interp_nomsu = @tree_to_nomsu(bit, true) if interp_nomsu - if bit.type != "Word" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" + if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" interp_nomsu\parenthesize! nomsu\append "\\", interp_nomsu else return nil @@ -753,7 +750,7 @@ class NomsuCompiler else interp_nomsu = @tree_to_nomsu(bit, true) if interp_nomsu - if bit.type != "Word" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" + if bit.type != "List" and bit.type != "Dict" and bit.type != "Text" interp_nomsu\parenthesize! nomsu\append "\\", interp_nomsu else @@ -865,9 +862,6 @@ class NomsuCompiler when "Var" return Nomsu(nil, "%", tree.value) - when "Word" - return Nomsu(nil, tree.value) - when "Comment" return nil if inline return Nomsu(nil, "#", tree.value\gsub("\n", "\n ")) diff --git a/nomsu.peg b/nomsu.peg index afc5580..b063ea0 100644 --- a/nomsu.peg +++ b/nomsu.peg @@ -50,7 +50,7 @@ inline_action (Action): action (Action): (expression (dotdot? %ws*))* word ((dotdot? %ws*) (expression / word))* -word (Word): { %operator / (!number plain_word) } +word: { %operator / (!number plain_word) } text_word (Text): {%operator / (!number plain_word)} diff --git a/nomsu_tree.lua b/nomsu_tree.lua index 4bacdb5..e7a5fce 100644 --- a/nomsu_tree.lua +++ b/nomsu_tree.lua @@ -103,7 +103,6 @@ Tree("Dict", 'multi') Tree("DictEntry", 'multi') Tree("IndexChain", 'multi') Tree("Number", 'single') -Tree("Word", 'single') Tree("Comment", 'single') Tree("Var", 'single', { as_lua_id = function(self) @@ -121,31 +120,29 @@ Tree("Action", 'multi', { if include_names == nil then include_names = false end - local bits if include_names then - do + return concat((function() local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #self do - local t = self[_index_0] - _accum_0[_len_0] = (t.type == "Word" and t.value or "%" .. tostring(t.value)) + local a = self[_index_0] + _accum_0[_len_0] = type(a) == "string" and a or "%" .. tostring(a.value) _len_0 = _len_0 + 1 end - bits = _accum_0 - end + return _accum_0 + end)(), " ") else - do + return concat((function() local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #self do - local t = self[_index_0] - _accum_0[_len_0] = (t.type == "Word" and t.value or "%") + local a = self[_index_0] + _accum_0[_len_0] = type(a) == "string" and a or "%" _len_0 = _len_0 + 1 end - bits = _accum_0 - end + return _accum_0 + end)(), " ") end - return concat(bits, " ") end }) return Types diff --git a/nomsu_tree.moon b/nomsu_tree.moon index 2f7c202..e261306 100644 --- a/nomsu_tree.moon +++ b/nomsu_tree.moon @@ -56,7 +56,6 @@ Tree "Dict", 'multi' Tree "DictEntry", 'multi' Tree "IndexChain", 'multi' Tree "Number", 'single' -Tree "Word", 'single' Tree "Comment", 'single' Tree "Var", 'single', @@ -65,9 +64,9 @@ Tree "Var", 'single', Tree "Action", 'multi', get_stub: (include_names=false)=> - bits = if include_names - [(t.type == "Word" and t.value or "%#{t.value}") for t in *@] - else [(t.type == "Word" and t.value or "%") for t in *@] - return concat(bits, " ") + if include_names + concat [type(a) == "string" and a or "%#{a.value}" for a in *@], " " + else + concat [type(a) == "string" and a or "%" for a in *@], " " return Types