Fixed some codegen stuff, optimized "say" for the common case of string
literals.
This commit is contained in:
parent
cbd876673a
commit
f1a2c936de
@ -3,4 +3,5 @@ require "lib/utils.nom"
|
||||
require "lib/operators.nom"
|
||||
require "lib/control_flow.nom"
|
||||
require "lib/collections.nom"
|
||||
require "lib/utils2.nom"
|
||||
require "lib/permissions.nom"
|
||||
|
@ -6,14 +6,14 @@ require "lib/collections.nom"
|
||||
# Permission functions
|
||||
rule [standardize rules %rules] =:
|
||||
if ((type of %rules) == "string"): %rules = [%rules]
|
||||
(nomsu "get_stub" [%]) for all %rules
|
||||
%stubs = (nomsu "get_stubs" [%rules])
|
||||
%result = []
|
||||
for %rule in %rules:
|
||||
%stub = (nomsu "get_stub" [%rule])
|
||||
for %stub in %stubs:
|
||||
%def = ((nomsu's "defs")->%stub)
|
||||
assert %def "Undefined rule: \(%stub)"
|
||||
%aliases = (%def's "aliases")
|
||||
for all %aliases: add % to %result
|
||||
if %def:
|
||||
%aliases = (%def's "aliases")
|
||||
for all %aliases: add % to %result
|
||||
..else: add %def to %result
|
||||
unique %result
|
||||
|
||||
rule [restrict %rules to within %elite_rules] =:
|
||||
@ -52,3 +52,7 @@ rule [forbid %pleb_rules to use %rules] =:
|
||||
|Cannot individually restrict permissions for \(%) because it is currently
|
||||
|available to everyone. Perhaps you meant to use "restrict % to within %" instead?
|
||||
for all %pleb_rules: %whiteset's % = (nil)
|
||||
|
||||
parse [rule %spec = %body restricted to %elite_rules] as:
|
||||
rule %spec = %body
|
||||
restrict (nomsu "get_stubs" [\%spec]) to within %elite_rules
|
||||
|
@ -27,9 +27,6 @@ compile [%str with %patt replaced with %sub, %str s/%patt/%sub] to:
|
||||
compile [%str with %patt replaced with %sub %n times, %str s/%patt/%sub/%n] to:
|
||||
"((\(%str as lua)):gsub(\(%patt as lua), \(%sub as lua), \(%n as lua)))"
|
||||
|
||||
compile [say %str] to: ".."
|
||||
|nomsu:writeln(nomsu:stringify(\(%str as lua)))
|
||||
|
||||
# Number ranges
|
||||
compile [%start to %stop by %step, %start to %stop via %step] to: ".."
|
||||
|nomsu.utils.range(\(%start as lua), \(%stop as lua), \(%step as lua))
|
||||
|
10
lib/utils2.nom
Normal file
10
lib/utils2.nom
Normal file
@ -0,0 +1,10 @@
|
||||
require "lib/metaprogramming.nom"
|
||||
require "lib/utils.nom"
|
||||
require "lib/control_flow.nom"
|
||||
require "lib/operators.nom"
|
||||
|
||||
compile [say %str] to:
|
||||
if ((%str's "type") == "String"):
|
||||
"nomsu:writeln(\(%str as lua))"
|
||||
..else:
|
||||
"nomsu:writeln(nomsu:stringify(\(%str as lua)))"
|
11
nomsu.lua
11
nomsu.lua
@ -479,7 +479,12 @@ end);]]):format(statements or "", expr or "ret")
|
||||
self:errorln(debug.traceback())
|
||||
self:error(ret)
|
||||
end
|
||||
insert(buffer, tostring(statements or '') .. "\n" .. tostring(expr and "ret = " .. tostring(expr) .. ";" or ''))
|
||||
if statements then
|
||||
insert(buffer, statements)
|
||||
end
|
||||
if expr then
|
||||
insert(buffer, "ret = " .. tostring(expr) .. ";")
|
||||
end
|
||||
end
|
||||
if max_operations then
|
||||
debug.sethook()
|
||||
@ -488,7 +493,7 @@ end);]]):format(statements or "", expr or "ret")
|
||||
local ret;
|
||||
%s
|
||||
return ret;
|
||||
end);]]):format(concat(buffer, ""))
|
||||
end);]]):format(concat(buffer, "\n"))
|
||||
return return_value, lua_code, vars
|
||||
end,
|
||||
tree_to_value = function(self, tree, vars)
|
||||
@ -911,7 +916,7 @@ end)]]):format(concat(lua_bits, "\n"))
|
||||
end
|
||||
return concat(stub, " "), arg_names
|
||||
else
|
||||
return self:error("Unsupported get stub type: " .. tostring(x.type))
|
||||
return self:error("Unsupported get stub type: " .. tostring(x.type) .. " for " .. tostring(repr(x)))
|
||||
end
|
||||
end,
|
||||
get_stubs = function(self, x)
|
||||
|
@ -365,7 +365,10 @@ end);]])\format(statements or "", expr or "ret")
|
||||
@errorln "#{colored.red "Error occurred in statement:"}\n#{colored.yellow statement.src}"
|
||||
@errorln debug.traceback!
|
||||
@error(ret)
|
||||
insert buffer, "#{statements or ''}\n#{expr and "ret = #{expr};" or ''}"
|
||||
if statements
|
||||
insert buffer, statements
|
||||
if expr
|
||||
insert buffer, "ret = #{expr};"
|
||||
|
||||
if max_operations
|
||||
debug.sethook!
|
||||
@ -374,7 +377,7 @@ return (function(nomsu, vars)
|
||||
local ret;
|
||||
%s
|
||||
return ret;
|
||||
end);]])\format(concat(buffer, ""))
|
||||
end);]])\format(concat(buffer, "\n"))
|
||||
return return_value, lua_code, vars
|
||||
|
||||
tree_to_value: (tree, vars)=>
|
||||
@ -675,7 +678,7 @@ end)]])\format(concat(lua_bits, "\n"))
|
||||
insert stub, "%"
|
||||
arg_names = nil
|
||||
return concat(stub," "), arg_names
|
||||
else @error "Unsupported get stub type: #{x.type}"
|
||||
else @error "Unsupported get stub type: #{x.type} for #{repr x}"
|
||||
|
||||
get_stubs: (x)=>
|
||||
if type(x) != 'table' then return {{@get_stub(x)}}
|
||||
|
Loading…
Reference in New Issue
Block a user