From b98059261c173dab8d919e7f9a93d44c5de0329d Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 19 Oct 2017 17:00:10 -0700 Subject: [PATCH] Abbreviated "lua code" to "lua>" and "lua expr" to "=lua" and "lua block" to "lua do>" --- examples/how_do_i.nom | 8 ++++---- examples/sample_code.nom | 12 +++++------- examples/sample_game.nom | 14 +++++++------- lib/collections.nom | 12 ++++++------ lib/control_flow.nom | 22 +++++++++++----------- lib/metaprogramming.nom | 28 ++++++++++++++-------------- lib/moonscript.nom | 4 ++-- lib/operators.nom | 6 +++--- lib/permissions.nom | 2 +- lib/plurals.nom | 2 +- lib/utils.nom | 14 +++++++------- nomsu.moon | 6 +++--- 12 files changed, 64 insertions(+), 66 deletions(-) diff --git a/examples/how_do_i.nom b/examples/how_do_i.nom index 2365627..78a11e2 100644 --- a/examples/how_do_i.nom +++ b/examples/how_do_i.nom @@ -246,17 +246,17 @@ say both (..) # Macros: -# The "lua block %" and "lua expr %" macros can be used to write raw lua code: +# The "lua> %" and "=lua %" macros can be used to write raw lua code: rule [say the time] =: - lua block ".." + lua> ".." |nomsu:writeln("The OS time is: "..os.time()); say the time -say "Math expression result is: \(lua expr "(1 + 2*3 + 3*4)^2")" +say "Math expression result is: \(=lua "(1 + 2*3 + 3*4)^2")" #.. In the lua environment, "vars" can be used to get local variables/function args, and "nomsu" can be used to access the compiler, function defs, and other things rule [square root of %n] =: - lua expr "math.sqrt(vars.n)" + =lua "math.sqrt(vars.n)" say "The square root of 2 is \(square root of 2)" # Macros can be defined to transform one bit of nomsu code into another using "parse % as %": diff --git a/examples/sample_code.nom b/examples/sample_code.nom index 1e1ccc6..08f2af5 100644 --- a/examples/sample_code.nom +++ b/examples/sample_code.nom @@ -131,14 +131,12 @@ say ".." | and an interpolated expression: \2 + 5\ rule [%n bottles] =: - lua block ".." - |do - | print("running raw lua code...") - | for i=\(%n),1,-1 do - | print(tostring(i).." bottles of beer on the wall. Take one down, pass it around,") - | end - | print("no more bottles of beer on the wall.") + lua do> ".." + |print("running raw lua code...") + |for i=\(%n),1,-1 do + | print(tostring(i).." bottles of beer on the wall. Take one down, pass it around,") |end + |print("no more bottles of beer on the wall.") nil 9 bottles diff --git a/examples/sample_game.nom b/examples/sample_game.nom index a6a792d..d740655 100644 --- a/examples/sample_game.nom +++ b/examples/sample_game.nom @@ -6,15 +6,15 @@ require "lib/plurals.nom" # Users: with secrets: secret %users = (..) - lua expr ".." + =lua ".." |setmetatable({}, {__index={ | by_name=function(self, key) return "User<"..key..">"; end, | add=function(self, key) self[key] = self:by_name(key); end, |}}) rule [find user %name, @%name] =: - lua expr "secrets.users:by_name(vars.name) or nomsu:error('Failed to find user: '..tostring(vars.name))" + =lua "secrets.users:by_name(vars.name) or nomsu:error('Failed to find user: '..tostring(vars.name))" rule [add user %name] =: - lua expr "secrets.users:add(vars.name)" + =lua "secrets.users:add(vars.name)" rule [everybody, everyone] =: (%entry's "key") for %entry in (entries in (secret %users)) @@ -23,7 +23,7 @@ with secrets: # Inventory: with secrets: secret %inventory = (..) - lua expr ".." + =lua ".." |setmetatable({}, {__index=function(self,key) | local t = setmetatable({}, {__index=function() return 0; end}); | self[key] = t; @@ -69,14 +69,14 @@ with secrets: "inventory", "%'s inventory", "%'s stock of %", "%'s stock of % as str" rule [you] =: - lua expr "(nomsu.you or 'Anonymous')" + =lua "(nomsu.you or 'Anonymous')" rule [make %person %action, make %person do %action] =: - lua code ".." + lua> ".." |local old_you = nomsu.you |nomsu.you = vars.person do %action - lua code ".." + lua> ".." |nomsu.you = old_you say "====================================================" diff --git a/lib/collections.nom b/lib/collections.nom index 9432baa..1d22bb6 100644 --- a/lib/collections.nom +++ b/lib/collections.nom @@ -101,10 +101,10 @@ compile [dict %items] to: |Invalid format for 'dict' expression. Only literals are allowed. %tokens = (%func_call's "value") %equals = (%tokens -> 2) - assert (lua expr "#\(%tokens) == 3 and \(%equals) and \(%equals).type == 'Word' and \(%equals).value == '='") ".." + assert (=lua "#\(%tokens) == 3 and \(%equals) and \(%equals).type == 'Word' and \(%equals).value == '='") ".." |Invalid format for 'dict' expression. Lines must only have the "% = %" format, not \(%func_call's "src") %key = (%tokens -> 1) - lua code ".." + lua> ".." |if \(%key).type == "Word" and \(%key).value:match("^[a-zA-Z_][a-zA-Z0-9_]*$") then | \(%key_code) = \(%key).value; |elseif \(%key).type == "Word" then @@ -117,7 +117,7 @@ compile [dict %items] to: ..else: return (..) (..) - nomsu "replaced_vars" [\(dict from entries %items), lua expr "vars"] + nomsu "replaced_vars" [\(dict from entries %items), =lua "vars"] ..as lua rule [entries in %dict] =: @@ -171,11 +171,11 @@ compile [default dict] to: ".." rule [chain %dict to %fallback] =: when (type of %fallback) == ?: * "table": - lua expr "setmetatable(\(%dict), \(%fallback))" + =lua "setmetatable(\(%dict), \(%fallback))" * "function": - lua expr "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback))(nomsu, {['']=key, self=self}); end})" + =lua "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback))(nomsu, {['']=key, self=self}); end})" * else: - lua expr "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback)); end})" + =lua "setmetatable(\(%dict), {__index=function(self, key) return (\(%fallback)); end})" # TODO: maybe make a generator/coroutine? diff --git a/lib/control_flow.nom b/lib/control_flow.nom index 84aca6c..c16b19d 100644 --- a/lib/control_flow.nom +++ b/lib/control_flow.nom @@ -30,7 +30,7 @@ compile [go to %label] to code: ".." |goto label_\(nomsu "var_to_lua_identifier" [%label]); rule [tree %tree has function call %call] =: - lua code ".." + lua> ".." |local target = (\(%call)).value; |for subtree,_ in coroutine.wrap(function() nomsu:walk_tree(\(%tree)); end) do; | if type(subtree) == 'table' and subtree.type == "FunctionCall" @@ -76,7 +76,7 @@ compile [..] %continue_labels = "" if (tree %body has function call \(do next for-loop)): %continue_labels join= "\n::continue_for::;" - if (tree %body has function call (nomsu "replaced_vars" [\(do next %), lua expr "{['']=\(%var)}"])): + if (tree %body has function call (nomsu "replaced_vars" [\(do next %), =lua "{['']=\(%var)}"])): %continue_labels join= "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" %code = ".." |for i=\(%start as lua),\(%stop as lua),\(%step as lua) do; @@ -87,7 +87,7 @@ compile [..] %stop_labels = "" if (tree %body has function call \(stop for-loop)): %stop_labels join= "\n::stop_for::;" - if (tree %body has function call (nomsu "replaced_vars" [\(stop %), lua expr "{['']=\(%var)}"])): + if (tree %body has function call (nomsu "replaced_vars" [\(stop %), =lua "{['']=\(%var)}"])): %stop_labels join= "\n::stop_\(nomsu "var_to_lua_identifier" [%var])::;" if (%stop_labels != ""): ".." |do;--for-loop label scope @@ -105,7 +105,7 @@ compile [for %var in %iterable %body] to code: %continue_labels = "" if (tree %body has function call \(do next for-loop)): %continue_labels join= "\n::continue_for::;" - if (tree %body has function call (nomsu "replaced_vars" [\(do next %), lua expr "{['']=\(%var)}"])): + if (tree %body has function call (nomsu "replaced_vars" [\(do next %), =lua "{['']=\(%var)}"])): %continue_labels join= "\n::continue_\(nomsu "var_to_lua_identifier" [%var])::;" %code = ".." |for i,value in ipairs(\(%iterable as lua)) do; @@ -116,7 +116,7 @@ compile [for %var in %iterable %body] to code: %stop_labels = "" if (tree %body has function call \(stop for-loop)): %stop_labels join= "\n::stop_for::;" - if (tree %body has function call (nomsu "replaced_vars" [\(stop %), lua expr "{['']=\(%var)}"])): + if (tree %body has function call (nomsu "replaced_vars" [\(stop %), =lua "{['']=\(%var)}"])): %stop_labels join= "\n::stop_\(nomsu "var_to_lua_identifier" [%var])::;" if (%stop_labels != ""): ".." |do;--for-loop label scope @@ -136,7 +136,7 @@ compile [when %body] to code: |Invalid format for 'when' statement. Only '*' blocks are allowed. %tokens = (%func_call's "value") %star = (%tokens -> 1) - assert (lua expr "vars.star and vars.star.type == 'Word' and vars.star.value == '*'") ".." + assert (=lua "vars.star and vars.star.type == 'Word' and vars.star.value == '*'") ".." |Invalid format for 'when' statement. Lines must begin with '*' %condition = (%tokens -> 2) @@ -145,10 +145,10 @@ compile [when %body] to code: %action = (%tokens -> 3) if (%action == (nil)): - lua block "table.insert(vars.fallthroughs, vars.condition)" + lua do> "table.insert(vars.fallthroughs, vars.condition)" do next %func_call - if (lua expr "vars.condition.type == 'Word' and vars.condition.value == 'else'"): + if (=lua "vars.condition.type == 'Word' and vars.condition.value == 'else'"): %result join= ".." | |else; @@ -180,7 +180,7 @@ compile [when %branch_value == ? %body] to code: |Invalid format for 'when' statement. Only '*' blocks are allowed. %tokens = (%func_call's "value") %star = (%tokens -> 1) - assert (lua expr "vars.star and vars.star.type == 'Word' and vars.star.value == '*'") ".." + assert (=lua "vars.star and vars.star.type == 'Word' and vars.star.value == '*'") ".." |Invalid format for 'when' statement. Lines must begin with '*' %condition = (%tokens -> 2) @@ -189,10 +189,10 @@ compile [when %branch_value == ? %body] to code: %action = (%tokens -> 3) if (%action == (nil)): - lua block "table.insert(vars.fallthroughs, vars.condition)" + lua> "table.insert(vars.fallthroughs, vars.condition)" do next %func_call - if (lua expr "vars.condition.type == 'Word' and vars.condition.value == 'else'"): + if (=lua "vars.condition.type == 'Word' and vars.condition.value == 'else'"): %result join= ".." | |else; diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom index bf4d79a..d167fef 100644 --- a/lib/metaprogramming.nom +++ b/lib/metaprogramming.nom @@ -3,7 +3,7 @@ that easier. # Rule to make rules: -lua code ".." +lua> ".." |nomsu:defmacro("rule %signature = %body", (function(nomsu, vars) | local signature = nomsu:get_stubs(nomsu:typecheck(vars, "signature", "List").value); | local body = nomsu:typecheck(vars, "body", "Thunk"); @@ -14,7 +14,7 @@ lua code ".." # Rule to make nomsu macros: rule [escaped parse %shorthand as %longhand] =: - lua code ".." + lua> ".." |local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "shorthand", "List").value); |if #vars.longhand.value ~= 1 then; | nomsu:error("Expected only 1 line to parse to, but got "..tostring(#vars.longhand.value)); @@ -29,13 +29,13 @@ escaped parse \[parse %shorthand as %longhand] as \: escaped parse \%shorthand a # Rule to make lua macros: rule [escaped compile %macro_def to %body] =: - lua code ".." + lua> ".." |local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "macro_def", "List").value); |local body = nomsu:typecheck(vars, "body", "Thunk"); |local thunk = nomsu:tree_to_value(body); |nomsu:defmacro(aliases, thunk, body.src); rule [escaped compile %macro_def to code %body] =: - lua code ".." + lua> ".." |local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "macro_def", "List").value); |local body = nomsu:typecheck(vars, "body", "Thunk"); |local thunk = nomsu:tree_to_value(body); @@ -45,27 +45,27 @@ parse [compile %macro_def to %body] as: escaped compile \%macro_def to \%body parse [compile %macro_def to code %body] as: escaped compile \%macro_def to code \%body rule [do %] =: - lua expr "\(%)(nomsu, vars)" + =lua "\(%)(nomsu, vars)" rule [%tree as lua] =: - lua expr "nomsu:tree_to_lua(\(%tree))" + =lua "nomsu:tree_to_lua(\(%tree))" rule [%tree as value] =: - lua expr "nomsu:tree_to_value(\(%tree), vars)" + =lua "nomsu:tree_to_value(\(%tree), vars)" compile [repr %obj] to: "nomsu:repr(\(%obj as lua))" compile [type %obj, type of %obj] to: "type(\(%obj as lua))" -parse [lua block %block] as: lua code ".." +parse [lua do> %block] as: lua> ".." |do; | \(%block) |end; rule [%tree as lua statement] =: - lua block ".." + lua do> ".." |local _,statement = nomsu:tree_to_lua(\(%tree)); |return statement; rule [%tree as lua statements] =: - lua block ".." + lua do> ".." |local lua_bits = {}; |local statements = nomsu:typecheck(vars, "tree", "Thunk").value; |for _,bit in ipairs(statements) do; @@ -81,7 +81,7 @@ compile [nomsu %method %args] to: "nomsu[\(%method as lua)](nomsu, unpack(\(%arg # Get the source code for a function rule [help %rule] =: - lua block ".." + lua do> ".." |local fn_def = nomsu.defs[nomsu:get_stub(vars.rule)] |if not fn_def then; | nomsu:writeln("Rule not found: "..nomsu:repr(vars.rule)); @@ -95,7 +95,7 @@ rule [help %rule] =: # Compiler tools parse [eval %code, run %code] as: nomsu "run" [%code] rule [source code from tree %tree] =: - lua block ".." + lua do> ".." |local _,_,leading_space = vars.tree.src:find("\\n(%s*)%S"); |if leading_space then; | local chunk1, chunk2 = vars.tree.src:match(":%s*([^\\n]*)(\\n.*)"); @@ -108,5 +108,5 @@ parse [source code %body] as: source code from tree \%body parse [parse tree %code] as: nomsu "tree_to_str" [\%code] -parse [enable debugging] as: lua code "nomsu.debug = true" -parse [disable debugging] as: lua code "nomsu.debug = false" +parse [enable debugging] as: lua> "nomsu.debug = true" +parse [disable debugging] as: lua> "nomsu.debug = false" diff --git a/lib/moonscript.nom b/lib/moonscript.nom index 9aa9575..4610eac 100644 --- a/lib/moonscript.nom +++ b/lib/moonscript.nom @@ -2,7 +2,7 @@ require "lib/metaprogramming.nom" # Moonscript! parse [moonscript block %moonscript_code] as: - lua block ".." + lua do> ".." |local parse, compile = require('moonscript.parse'), require('moonscript.compile'); |local moon_code = nomsu:tree_to_value(vars.moonscript_code, vars); |local tree, err = parse.string(moon_code); @@ -16,7 +16,7 @@ parse [moonscript block %moonscript_code] as: |return "do\\n"..lua_code.."\\nend"; parse [moonscript %moonscript_code] as: - lua block ".." + lua do> ".." |local parse, compile = require('moonscript.parse'), require('moonscript.compile'); |local moon_code = nomsu:tree_to_value(vars.moonscript_code, vars); |local tree, err = parse.string(moon_code); diff --git a/lib/operators.nom b/lib/operators.nom index c254861..5b390e5 100644 --- a/lib/operators.nom +++ b/lib/operators.nom @@ -44,7 +44,7 @@ compile [%var join= %val] to code: "\(%var as lua) = \(%var as lua) .. \(%val as compile [%var mod= %val] to code: "\(%var as lua) = \(%var as lua) % \(%val as lua);" # Binary Operators -lua block ".." +lua do> ".." |local binops = {"-","/","<","<=",">",">=","^",{"===","=="},{"!==","~="},{"mod","%"}}; |for _,op in ipairs(binops) do; | local nomsu_alias = op; @@ -69,7 +69,7 @@ compile [%a != %b] to: "(not nomsu.utils.equivalent(\(%a as lua), \(%b as lua))) # Commutative Operators defined for up to 8 operands # TODO: work out solution for commutative operators using more clever macros -lua block ".." +lua do> ".." |local max_operands = 8; |local comops = {"+","*","and","or"}; |for _,_op in ipairs(comops) do; @@ -88,7 +88,7 @@ lua block ".." |end; # Chained compairsions (e.g. x < y <= z) are defined up to 3 operands -lua block ".." +lua do> ".." |local max_operands = 3; |for _,chainers in ipairs({{"<","<="},{">",">="}}) do; | local function recurse(chainers, chain) diff --git a/lib/permissions.nom b/lib/permissions.nom index 3231b25..5669366 100644 --- a/lib/permissions.nom +++ b/lib/permissions.nom @@ -5,7 +5,7 @@ require "lib/collections.nom" # Permission functions rule [standardize rules %rules] =: - if (lua expr "type(vars.rules) == 'string'"): %rules = [%rules] + if (=lua "type(vars.rules) == 'string'"): %rules = [%rules] (nomsu "get_stub" [%]) for all %rules %set = [] for %rule in %rules: diff --git a/lib/plurals.nom b/lib/plurals.nom index da0fa4d..52a3294 100644 --- a/lib/plurals.nom +++ b/lib/plurals.nom @@ -3,7 +3,7 @@ require "lib/secrets.nom" # Plurals with secrets: - lua block ".." + lua do> ".." |local endings = setmetatable({x="es",c="es",s="es"}, {__index=function() return "s"; end}); |secrets.plurals = setmetatable({}, {__index=function(self,key) | return key..endings[key:sub(-1)]; diff --git a/lib/utils.nom b/lib/utils.nom index f0a7342..54fde72 100644 --- a/lib/utils.nom +++ b/lib/utils.nom @@ -13,7 +13,7 @@ parse [assert %condition] as: assert %condition (nil) # String functions rule [join %strs with glue %glue] =: - lua block ".." + lua do> ".." |local str_bits = {} |for i,bit in ipairs(vars.strs) do str_bits[i] = nomsu:stringify(bit) end |return table.concat(str_bits, vars.glue) @@ -31,7 +31,7 @@ compile [%start to %stop by %step, %start to %stop via %step] to: ".." parse [%start to %stop] as: %start to %stop by 1 # Random functions -lua code ".." # Seed +lua> ".." # Seed |math.randomseed(os.time()); |for i=1,20 do; math.random(); end; compile [random number, random, rand] to: "math.random()" @@ -39,7 +39,7 @@ compile [random int %n, random integer %n, randint %n] to: "math.random(\(%n as compile [random from %low to %high, random number from %low to %high, rand %low %high] to: "math.random(\(%low as lua), \(%high as lua))" rule [random choice from %elements, random choice %elements, random %elements] =: - lua expr "\(%elements)[math.random(#\(%elements))]" + =lua "\(%elements)[math.random(#\(%elements))]" # Math functions compile [abs %, absolute value of %, | % |] to: "math.abs(\(% as lua))" @@ -61,7 +61,7 @@ compile [log % base %base] to: "math.log(\(% as lua), \(%base as lua))" compile [floor %] to: "math.floor(\(% as lua))" compile [round %, % rounded] to: "math.floor(\(% as lua) + .5)" rule [%n rounded to the nearest %rounder] =: - lua expr "(\(%rounder))*math.floor(\(%n)/\(%rounder) + .5)" + =lua "(\(%rounder))*math.floor(\(%n)/\(%rounder) + .5)" # Common utility functions compile [sum of %items, sum %items] to: "nomsu.utils.sum(\(%items as lua))" @@ -69,7 +69,7 @@ compile [product of %items, product %items] to: "nomsu.utils.product(\(%items as compile [all of %items] to: "nomsu.utils.all(\(%items as lua))" compile [any of %items] to: "nomsu.utils.any(\(%items as lua))" rule [avg of %items, average of %items] =: - lua expr "(nomsu.utils.sum(\(%items))/#\(%items))" + =lua "(nomsu.utils.sum(\(%items))/#\(%items))" compile [min of %items, smallest of %items, lowest of %items] to: "nomsu.utils.min(\(%items as lua))" compile [max of %items, biggest of %items, largest of %items, highest of %items] to: @@ -87,7 +87,7 @@ compile [max of %items by %value_expr] to: | return \(%value_expr as lua) |end) compile [sort %items] to: "table.sort(\(%items as lua))" -rule [sort %items by %key] =: lua expr ".." +rule [sort %items by %key] =: =lua ".." |nomsu.utils.sort(\(%items), function(x) | return (\(%key))(nomsu, {['']=x}); |end) @@ -108,7 +108,7 @@ compile [form feed, formfeed] to: ".." compile [vertical tab] to: ".." |"\v" -lua code ".." +lua> ".." |do; | local colors = { | ["reset color"] = 0, bright = 1, dim = 2, underscore = 4, blink = 5, diff --git a/nomsu.moon b/nomsu.moon index a188d1b..0642392 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -542,7 +542,7 @@ end)]])\format(concat(lua_bits, "\n")) -- Returns a single stub ("say %"), and list of arg names ({"msg"}) from a single rule def -- (e.g. "say %msg") or function call (e.g. FunctionCall({Word("say"), Var("msg"))) if type(x) == 'string' - stub = x\gsub("([#{wordbreaker}]+)"," %1 ")\gsub("%%%S+","%%")\gsub("%s+"," ")\gsub("%s*$","") + stub = x\gsub("([#{wordbreaker}]+)"," %1 ")\gsub("%%%S+","%%")\gsub("%s+"," ")\gsub("^%s*","")\gsub("%s*$","") arg_names = [arg for arg in x\gmatch("%%([^%s']*)")] return stub, arg_names switch x.type @@ -605,13 +605,13 @@ end)]])\format(concat(lua_bits, "\n")) inner_vars = setmetatable({}, {__index:(_,key)-> "vars[#{repr(key)}]"}) lua = @tree_to_value(vars.code, inner_vars) return nil, lua - @defmacro "lua code %code", lua_code + @defmacro "lua > %code", lua_code lua_value = (vars)=> inner_vars = setmetatable({}, {__index:(_,key)-> "vars[#{repr(key)}]"}) lua = @tree_to_value(vars.code, inner_vars) return lua, nil - @defmacro "lua expr %code", lua_value + @defmacro "= lua %code", lua_value run_file = (vars)=> if vars.filename\match(".*%.lua")