code = `Do{ },
after_success = mlp.gensym "_after_success" }
+
+ -- Some sharing issues occur when modifying term_seq,
+ -- so it's replaced by a copy new_term_seq.
+ -- TODO: clean that up, and re-suppress the useless copies
+ -- (cf. remarks about capture bug below).
+ local new_term_seq = { }
+
local match_locals
-- Make sure that all tested terms are variables or literals
table.insert(match_locals[1], v)
table.insert(match_locals[2], t)
end
- term_seq[i] = v
+ new_term_seq[i] = v
--end
end
+ term_seq = new_term_seq
if match_locals then acc_stat(match_locals, cfg) end
+
require 'metalua.extension.match'
+
module ('spmatch', package.seeall)
+
+require 'metalua.walk.id'
+
-{extension 'log'}
----------------------------------------------------------------------
--- Back-end for "match function ..." and "local match function..."
+-- Back-end for statements
+-- "match function ..." and "local match function...".
-- Tag must be either "Localrec" or "Set".
----------------------------------------------------------------------
named_match_function_builder = |tag| function (x)
----------------------------------------------------------------
{ 'function', mlp.expr, gg.optkeyword '|',
match_cases_list_parser, 'end',
- builder = match_function_builder 'Set' },
+ builder = named_match_function_builder 'Set' },
----------------------------------------------------------------
-- Reintroduce the original match statement:
mlp.stat:get'local'[2]:add{
'match', 'function', mlp.expr, gg.optkeyword '|',
match_cases_list_parser, 'end',
- builder = match_function_builder 'Localrec' }
+ builder = named_match_function_builder 'Localrec' }
----------------------------------------------------------------------
-- "match...with" expressions and "match function..."
return `Stat{ { `Local{{v}}; m }, v }
end } } }
-require 'walk.id'
-
function bind (x)
local patterns, values = unpack(x)
function p(msg) io.write(msg..' ':rep(WIDTH-#msg)) end
----------------------------------------------------------------------
-p"match as an expression"
+p "match as an expression"
print(match 1 with 1 -> 'ok' | 2 -> 'KO')
----------------------------------------------------------------------