Updated and improve syntax for some stuff. Also added string indexing
like in python, including slices.
This commit is contained in:
parent
d173e9ae88
commit
f769351556
@ -81,7 +81,7 @@ compile [object %classname %class_body] to
|
||||
\%class_identifier.class = \%class_identifier;
|
||||
|
||||
-- Define the methods:
|
||||
\(join %methods with "\n")
|
||||
\(%methods joined with "\n")
|
||||
|
||||
-- Define class methods for instantiating and accessing instances:
|
||||
\%class_identifier.instance_metatable = {
|
||||
|
@ -37,18 +37,18 @@ action [%n to the nearest %rounder]
|
||||
|
||||
# Any/all/none
|
||||
compile [all of %items, all %items] to
|
||||
"(\(join ((% as lua) for all (%items' "value")) with " and "))"
|
||||
"(\(joined ((% as lua) for all (%items' "value")) with " and "))"
|
||||
..if ((%items' "type") is "List") else "utils.all(\(%items as lua))"
|
||||
parse [not all of %items, not all %items] as: not (all of %items)
|
||||
compile [any of %items, any %items] to
|
||||
"(\(join ((% as lua) for all (%items' "value")) with " or "))"
|
||||
"(\(joined ((% as lua) for all (%items' "value")) with " or "))"
|
||||
..if ((%items' "type") is "List") else "utils.any(\(%items as lua))"
|
||||
parse [none of %items, none %items] as: not (any of %items)
|
||||
compile [sum of %items, sum %items] to
|
||||
"(\(join ((% as lua) for all (%items' "value")) with " + "))"
|
||||
"(\(joined ((% as lua) for all (%items' "value")) with " + "))"
|
||||
..if ((%items' "type") is "List") else "utils.sum(\(%items as lua))"
|
||||
compile [product of %items, product %items] to
|
||||
"(\(join ((% as lua) for all (%items' "value")) with " * "))"
|
||||
"(\(joined ((% as lua) for all (%items' "value")) with " * "))"
|
||||
..if ((%items' "type") is "List") else "utils.product(\(%items as lua))"
|
||||
action [avg of %items, average of %items]
|
||||
=lua "(utils.sum(\%items)/#\%items)"
|
||||
|
@ -152,11 +152,7 @@ immediately
|
||||
=lua "nomsu:tree_to_value(\%tree)"
|
||||
compile [repr %obj] to
|
||||
"repr(\(%obj as lua))"
|
||||
compile [indented %obj] to
|
||||
"nomsu:indent(\(%obj as lua))"
|
||||
compile [dedented %obj] to
|
||||
"nomsu:dedent(\(%obj as lua))"
|
||||
compile [type %obj, type of %obj] to
|
||||
compile [type of %obj] to
|
||||
"type(\(%obj as lua))"
|
||||
|
||||
immediately
|
||||
@ -186,30 +182,16 @@ action [help %action]
|
||||
end
|
||||
|
||||
# Compiler tools
|
||||
parse [eval %code, run %code] as: nomsu "run" [%code]
|
||||
action [source code from tree %tree]
|
||||
lua> ".."
|
||||
local junk,junk,leading_space = \%tree.src:find("\\n(%s*)%S");
|
||||
if leading_space then
|
||||
local chunk1, chunk2 = \%tree.src:match(":%s*([^\\n]*)(\\n.*)");
|
||||
chunk2 = chunk2:gsub("\\n"..leading_space, "\\n");
|
||||
return chunk1..chunk2.."\\n";
|
||||
else
|
||||
return \%tree.src:match(":%s*(%S.*)").."\\n";
|
||||
end
|
||||
parse [source code %body] as: source code from tree \%body
|
||||
|
||||
parse [parse tree %code] as: nomsu "tree_to_str" [\%code]
|
||||
|
||||
parse [run %code] as: nomsu "run" [%code]
|
||||
parse [enable debugging] as: lua> "nomsu.debug = true"
|
||||
parse [disable debugging] as: lua> "nomsu.debug = false"
|
||||
|
||||
compile [say %str] to
|
||||
compile [say %message] to
|
||||
lua> ".."
|
||||
if \%str.type == "Text" then
|
||||
return "nomsu:writeln("..\(%str as lua)..")";
|
||||
if \%message.type == "Text" then
|
||||
return "nomsu:writeln("..\(%message as lua)..")";
|
||||
else
|
||||
return "nomsu:writeln(stringify("..\(%str as lua).."))";
|
||||
return "nomsu:writeln(stringify("..\(%message as lua).."))";
|
||||
end
|
||||
|
||||
# Error functions
|
||||
|
32
lib/text.nom
32
lib/text.nom
@ -5,28 +5,22 @@
|
||||
use "lib/metaprogramming.nom"
|
||||
|
||||
# Text functions
|
||||
action [join %strs with %glue]
|
||||
action [%texts joined with %glue]
|
||||
lua> ".."
|
||||
local str_bits = {}
|
||||
for i,bit in ipairs(\%strs) do str_bits[i] = stringify(bit) end
|
||||
return table.concat(str_bits, \%glue)
|
||||
parse [join %strs] as: join %strs with ""
|
||||
local text_bits = {}
|
||||
for i,bit in ipairs(\%texts) do text_bits[i] = stringify(bit) end
|
||||
return table.concat(text_bits, \%glue)
|
||||
parse [joined %texts, %texts joined] as: %texts joined with ""
|
||||
|
||||
compile [capitalize %str, %str capitalized] to
|
||||
"(\(%str as lua)):gsub('%l', string.upper, 1)"
|
||||
compile [capitalized %text capitalized] to
|
||||
"(\(%text as lua)):gsub('%l', string.upper, 1)"
|
||||
|
||||
compile [%str with %patt replaced with %sub, %str s/%patt/%sub] to
|
||||
"((\(%str as lua)):gsub(\(%patt as lua), \(%sub as lua)))"
|
||||
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 [%text with %sub instead of %patt, %text s/%patt/%sub] to
|
||||
"((\(%text as lua)):gsub(\(%patt as lua), \(%sub as lua)))"
|
||||
|
||||
compile [indent %str] to "\%str:gsub('\\n','\\n'..(' '))"
|
||||
compile [indent %str %n times, indent %str %n x] to "\%str:gsub('\\n','\\n'..(' '):rep(\%n))"
|
||||
|
||||
# Substring
|
||||
# TODO: improve this syntax
|
||||
compile [%str |%start|] to "\(%str as lua):sub(\(%start as lua), \(%start as lua))"
|
||||
compile [%str |%start - %stop|] to "\(%str as lua):sub(\(%start as lua), \(%stop as lua))"
|
||||
compile [indented %text, %text indented] to "\%text:gsub('\\n','\\n'..(' '))"
|
||||
compile [dedented %obj, %obj dedented] to "nomsu:dedent(\(%obj as lua))"
|
||||
compile [%text indented %n times] to "\%text:gsub('\\n','\\n'..(' '):rep(\%n))"
|
||||
|
||||
# Text literals
|
||||
lua do> ".."
|
||||
@ -46,7 +40,7 @@ lua do> ".."
|
||||
};
|
||||
for name, e in pairs(escapes) do
|
||||
local lua = "'"..e.."'";
|
||||
nomsu:define_compile_action(name, \(__line_no__), function() return {expr=str}; end, \(__src__ 1));
|
||||
nomsu:define_compile_action(name, \(__line_no__), function() return {expr=text}; end, \(__src__ 1));
|
||||
end
|
||||
for name, c in pairs(colors) do
|
||||
local color = "'"..c.."'";
|
||||
|
18
nomsu.lua
18
nomsu.lua
@ -26,6 +26,18 @@ do
|
||||
STRING_METATABLE.__add = function(self, other)
|
||||
return self .. stringify(other)
|
||||
end
|
||||
STRING_METATABLE.__index = function(self, i)
|
||||
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
|
||||
STRING_METATABLE.__mul = function(self, other)
|
||||
return string.rep(self, other)
|
||||
end
|
||||
end
|
||||
lpeg.setmaxstack(10000)
|
||||
local P, R, V, S, Cg, C, Cp, B, Cmt
|
||||
@ -1388,10 +1400,16 @@ do
|
||||
}
|
||||
end)
|
||||
self:define_compile_action("lua> %code", "nomsu.moon", function(_code)
|
||||
if _code.type == "Text" then
|
||||
local lua = nomsu_string_as_lua(_code)
|
||||
return {
|
||||
statements = lua
|
||||
}
|
||||
else
|
||||
return {
|
||||
statements = "nomsu:run_lua(" .. tostring(nomsu:tree_to_lua(_code).expr) .. ");"
|
||||
}
|
||||
end
|
||||
end)
|
||||
self:define_compile_action("=lua %code", "nomsu.moon", function(_code)
|
||||
local lua = nomsu_string_as_lua(_code)
|
||||
|
@ -20,10 +20,16 @@ colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..(msg or
|
||||
{:insert, :remove, :concat} = table
|
||||
|
||||
-- Use + operator for string coercive concatenation (note: "asdf" + 3 == "asdf3")
|
||||
-- Use [] for accessing string characters, or s[{3,4}] for s:sub(3,4)
|
||||
-- Note: This globally affects all strings in this instance of Lua!
|
||||
do
|
||||
STRING_METATABLE = getmetatable("")
|
||||
STRING_METATABLE.__add = (other)=> @ .. stringify(other)
|
||||
STRING_METATABLE.__index = (i)=>
|
||||
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]
|
||||
STRING_METATABLE.__mul = (other)=> string.rep(@, other)
|
||||
|
||||
-- TODO:
|
||||
-- consider non-linear codegen, rather than doing thunks for things like comprehensions
|
||||
@ -921,8 +927,11 @@ class NomsuCompiler
|
||||
return statements:lua_code, locals:lua.locals
|
||||
|
||||
@define_compile_action "lua> %code", "nomsu.moon", (_code)->
|
||||
if _code.type == "Text"
|
||||
lua = nomsu_string_as_lua(_code)
|
||||
return statements:lua
|
||||
else
|
||||
return statements:"nomsu:run_lua(#{nomsu\tree_to_lua(_code).expr});"
|
||||
|
||||
@define_compile_action "=lua %code", "nomsu.moon", (_code)->
|
||||
lua = nomsu_string_as_lua(_code)
|
||||
|
Loading…
Reference in New Issue
Block a user