Fixed some of secrets' functionality by rewriting rule % = %

This commit is contained in:
Bruce Hill 2017-09-28 18:25:50 -07:00
parent ac25e20b9f
commit 723a4c3871
2 changed files with 26 additions and 13 deletions

View File

@ -4,22 +4,37 @@
# Rule to make rules:
lua code ".."
|nomsu:def("escaped rule %rule_def = %body", function(nomsu, vars)
|nomsu:defmacro("rule %rule_def = %body", function(nomsu, vars)
| local aliases = nomsu:typecheck(vars, "rule_def", "Block").value
| local body = nomsu:typecheck(vars, "body", "Block")
| local thunk = nomsu:tree_to_value({type="Thunk", value={type="Statements", value=body.value, src=body.src}, src=body.src})
| local canonical = aliases[1]
| nomsu:def(canonical, thunk, body.src)
| local function rewriter(nomsu, vars)
| return nomsu:tree_to_lua(nomsu:replaced_vars(canonical, vars))
| local body = nomsu:typecheck(vars, "body", "Block")
| local thunk = nomsu:tree_to_lua({type="Thunk", value={type="Statements", value=body.value, src=body.src}, src=body.src})
| local lua = ([[
|nomsu:def(%s, %s, %s)
|]]):format(nomsu:repr(canonical.src), thunk, nomsu:repr(body.src))
| if #aliases > 1 then
| lua = lua .. ([[
|do
| local aliased = %s
| local src = %s
| local function dealiaser(nomsu, vars)
| return nomsu:tree_to_lua(nomsu:replaced_vars(aliased, vars))
| end
| for i=2,#aliases do
| nomsu:defmacro(aliases[i], rewriter, body.src)
|]]):format(nomsu:repr(canonical), nomsu:repr(canonical.src))
| for i=2,#aliases do
| lua = lua .. ([[
| nomsu:defmacro(%s, dealiaser, %s)
|]]):format(nomsu:repr(aliases[i].src), nomsu:repr(canonical.src))
| end
| lua = lua .. [[
|end
|]]
| end
|end)
| return nil, lua
|end, "<source can be found in lib/metaprogramming.nom>")
# Rule to make nomsu macros:
escaped rule \(escaped parse %shorthand as %longhand) = \:
rule (escaped parse %shorthand as %longhand) =:
lua code ".."
|local aliases = nomsu:typecheck(vars, "shorthand", "Block").value
|local template = nomsu:typecheck(vars, "longhand", "Block")
@ -31,7 +46,6 @@ escaped rule \(escaped parse %shorthand as %longhand) = \:
| nomsu:defmacro(call, parsing_as, template.src)
|end
escaped parse \(parse %shorthand as %longhand) as \: escaped parse \%shorthand as \%longhand
parse (rule %rule_def = %body) as: escaped rule \%rule_def = \%body
# Rule to make lua macros:
rule (escaped compile %macro_def to %body) =:

View File

@ -17,7 +17,6 @@ compile (secret %key = %new_value) to code:
|Wrong type, expected Var, but got: \(%key's "type")
"secrets[\(repr (%key's "value"))] = \(%new_value as lua)"
enable debugging
with secrets:
secret %foo = 5
rule (plumb %) =:
@ -26,5 +25,5 @@ with secrets:
secret %foo
say (frop)
pumb 99
plumb 99
say (frop)