Added support for $(foo 1 baz 2) as a way to access (foo 1 baz 2)'s

meaning.
This commit is contained in:
Bruce Hill 2019-01-01 15:52:56 -08:00
parent b6d3cbd61c
commit a82b0d9d24
6 changed files with 19 additions and 9 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env nomsu -V6.13.12.8 #!/usr/bin/env nomsu -V6.13.12.8
# How do I... # How do I...
# Write a comment? Put a # and go till the end of the line # Write a comment? Put a # and go till the end of the line
# How do I write a multi-line comment? # How do I write a multi-line comment?
@ -313,9 +314,9 @@ debug only:
# Function literals look like: $x -> ($x * $x) # Function literals look like: $x -> ($x * $x)
say (best of [2, -3, 4, -8] according to ($x -> ($x * $x))) say (best of [2, -3, 4, -8] according to ($x -> ($x * $x)))
# Or, you can use ((foo $)'s meaning) to access the function that gets called by (foo $) # Or, you can use $(foo $) to access the function that gets called by (foo $)
($x squared) means ($x * $x) ($x squared) means ($x * $x)
say (best of [2, -3, 4, -8] according to (($ squared)'s meaning)) say (best of [2, -3, 4, -8] according to $($ squared)
# But nomsu was designed with flexible alternatives that are often better than passing functions. # But nomsu was designed with flexible alternatives that are often better than passing functions.
For example, instead of calling a key function on every item, you could instead define a macro For example, instead of calling a key function on every item, you could instead define a macro

View File

@ -190,7 +190,7 @@ real_number (Number) <-
(("-"? [0-9]+ "." [0-9]+)-> tonumber) (("-"? [0-9]+ "." [0-9]+)-> tonumber)
variable (Var) <- "$" ({ident_char+} / "(" {(ws+ / operator_char+ / ident_char+)*} ")" / {''}) variable (Var) <- "$" ({ident_char+} / "(" ws* inline_action ws* ")" / {''})
inline_list (List) <- inline_list (List) <-

View File

@ -473,7 +473,12 @@ local compile = setmetatable({
elseif "Number" == _exp_0 then elseif "Number" == _exp_0 then
return LuaCode:from(tree.source, tostring(tree[1])) return LuaCode:from(tree.source, tostring(tree[1]))
elseif "Var" == _exp_0 then elseif "Var" == _exp_0 then
return LuaCode:from(tree.source, (concat(tree, " ")):as_lua_id()) if type(tree[1]) == 'string' then
return LuaCode:from(tree.source, (concat(tree, " ")):as_lua_id())
else
assert(tree[1].type == 'Action')
return LuaCode:from(tree.source, tree[1]:get_stub():as_lua_id())
end
elseif "FileChunks" == _exp_0 then elseif "FileChunks" == _exp_0 then
return error("Can't convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks") return error("Can't convert FileChunks to a single block of lua, since each chunk's " .. "compilation depends on the earlier chunks")
elseif "Comment" == _exp_0 then elseif "Comment" == _exp_0 then

View File

@ -370,7 +370,11 @@ compile = setmetatable({
return LuaCode\from(tree.source, tostring(tree[1])) return LuaCode\from(tree.source, tostring(tree[1]))
when "Var" when "Var"
return LuaCode\from(tree.source, (concat(tree, " "))\as_lua_id!) if type(tree[1]) == 'string'
return LuaCode\from(tree.source, (concat(tree, " "))\as_lua_id!)
else
assert(tree[1].type == 'Action')
return LuaCode\from(tree.source, tree[1]\get_stub!\as_lua_id!)
when "FileChunks" when "FileChunks"
error("Can't convert FileChunks to a single block of lua, since each chunk's ".. error("Can't convert FileChunks to a single block of lua, since each chunk's "..

View File

@ -220,10 +220,10 @@ tree_to_inline_nomsu = function(tree)
return NomsuCode:from(tree.source, s) return NomsuCode:from(tree.source, s)
elseif "Var" == _exp_0 then elseif "Var" == _exp_0 then
local varname = tree[1] local varname = tree[1]
if varname == "" or is_identifier(varname) then if type(varname) == "string" then
return NomsuCode:from(tree.source, "$", varname) return NomsuCode:from(tree.source, "$", varname)
else else
return NomsuCode:from(tree.source, "$(", varname, ")") return NomsuCode:from(tree.source, "$(", tree_to_inline_nomsu(varname), ")")
end end
elseif "FileChunks" == _exp_0 then elseif "FileChunks" == _exp_0 then
return error("Can't inline a FileChunks") return error("Can't inline a FileChunks")

View File

@ -165,10 +165,10 @@ tree_to_inline_nomsu = (tree)->
when "Var" when "Var"
varname = tree[1] varname = tree[1]
if varname == "" or is_identifier(varname) if type(varname) == "string"
return NomsuCode\from(tree.source, "$", varname) return NomsuCode\from(tree.source, "$", varname)
else else
return NomsuCode\from(tree.source, "$(", varname, ")") return NomsuCode\from(tree.source, "$(", tree_to_inline_nomsu(varname), ")")
when "FileChunks" when "FileChunks"
error("Can't inline a FileChunks") error("Can't inline a FileChunks")