diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-24 13:13:03 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-24 13:13:26 -0800 |
| commit | f769351556cceed58ab6bf844c671114ec1862c2 (patch) | |
| tree | dd7dcf811082cf4e4c4bb0b6b3cd3811cf4385e1 /lib | |
| parent | d173e9ae88380bb1217b3f775e6c3cec71a6606c (diff) | |
Updated and improve syntax for some stuff. Also added string indexing
like in python, including slices.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/class.nom | 2 | ||||
| -rw-r--r-- | lib/math.nom | 8 | ||||
| -rw-r--r-- | lib/metaprogramming.nom | 30 | ||||
| -rw-r--r-- | lib/text.nom | 32 |
4 files changed, 24 insertions, 48 deletions
diff --git a/lib/class.nom b/lib/class.nom index b871798..e65fb81 100644 --- a/lib/class.nom +++ b/lib/class.nom @@ -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 = { diff --git a/lib/math.nom b/lib/math.nom index 2af59eb..0210293 100644 --- a/lib/math.nom +++ b/lib/math.nom @@ -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)" diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom index a75336a..e7de602 100644 --- a/lib/metaprogramming.nom +++ b/lib/metaprogramming.nom @@ -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 diff --git a/lib/text.nom b/lib/text.nom index 2eb6b67..0bd7a8a 100644 --- a/lib/text.nom +++ b/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.."'"; |
