X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flib%2Fmetalua%2Fextension%2Fanaphoric.mlua;fp=src%2Flib%2Fmetalua%2Fextension%2Fanaphoric.mlua;h=0000000000000000000000000000000000000000;hb=355ff0bc201e00856ba20d82c65b14ffa6fcfe4b;hp=2439fed481babb16de517352ddf7d26a4598be72;hpb=f998820846d157a0c28abfb71cfca4b273f45eb9;p=metalua.git diff --git a/src/lib/metalua/extension/anaphoric.mlua b/src/lib/metalua/extension/anaphoric.mlua deleted file mode 100644 index 2439fed..0000000 --- a/src/lib/metalua/extension/anaphoric.mlua +++ /dev/null @@ -1,54 +0,0 @@ --------------------------------------------------------------------------------- --- --- Anaphoric macros. --- --- This extension turns 'it' into a special variable, that's bound to --- an often used value: --- --- * in an 'if' statement, 'it' is bound, in a block, to the condition --- that triggered the block's execution: --- > if 1234 then y=it end; assert (y == 1234) --- --- * in a while loop, it's bound to the test: --- > while file:read "*a" do table.insert (lines, it) end --- --- 'it' is bound the the most closely surrounding structure. If you wanted to --- use its content at a deeper position in the AST, you would have to save it --- in a temporary variable. But what you should really do in such a case is --- avoiding to use anaphoric macros: they're fine for one-liner, but they --- reduce readability for bigger functions. --------------------------------------------------------------------------------- - --- TODO: 'and' operator could, and maybe should, be anaphoric as well --- TODO: anaphoric functions would be cool for recursive functions, but --- recursive calls are always in an 'if' statement, so the pronoun --- used for functions must not be the same as for 'if'. - -require 'freevars' - -local function anaphoric_if(ast) - local it_found = false - for i=2, #ast do - if freevars.block(ast[i])['it'] then - it_found = true - break - end - end - if it_found then - local cond = ast[1] - ast[1] = +{it} - return +{stat: do local it = -{cond}; -{ast} end } - end -end - -local function anaphoric_while(ast) - local it_found = false - if freevars.block(ast[2])['it'] then - local cond = ast[1] - ast[1] = +{it} - return +{stat: do local it = -{cond}; -{ast} end } - end -end - -mlp.stat:get'if'.transformers:add(anaphoric_if) -mlp.stat:get'while'.transformers:add(anaphoric_while) \ No newline at end of file