From: Fabien Fleutot Date: Tue, 3 Feb 2009 19:42:14 +0000 (+0100) Subject: fixed xmatch extension X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=d69775ede2a525bea03b505c70494b3aef2be6e3;p=metalua.git fixed xmatch extension --- diff --git a/src/lib/metalua/extension/match.mlua b/src/lib/metalua/extension/match.mlua index 3581b0c..6cceea7 100644 --- a/src/lib/metalua/extension/match.mlua +++ b/src/lib/metalua/extension/match.mlua @@ -295,6 +295,13 @@ function match_builder (x) 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 @@ -310,9 +317,10 @@ function match_builder (x) 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 diff --git a/src/lib/metalua/extension/xmatch.mlua b/src/lib/metalua/extension/xmatch.mlua index ce2e204..29dccd8 100644 --- a/src/lib/metalua/extension/xmatch.mlua +++ b/src/lib/metalua/extension/xmatch.mlua @@ -1,9 +1,15 @@ + 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) @@ -34,7 +40,7 @@ mlp.stat:add{ 'match', ---------------------------------------------------------------- { '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: @@ -51,7 +57,7 @@ mlp.stat:add{ 'match', 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..." @@ -103,8 +109,6 @@ mlp.expr:add{ 'match', builder = |x| x[1], gg.multisequence{ return `Stat{ { `Local{{v}}; m }, v } end } } } -require 'walk.id' - function bind (x) local patterns, values = unpack(x) diff --git a/src/samples/xmatch_test.mlua b/src/samples/xmatch_test.mlua index f7c6d3e..9289775 100755 --- a/src/samples/xmatch_test.mlua +++ b/src/samples/xmatch_test.mlua @@ -4,7 +4,7 @@ WIDTH=60 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') ----------------------------------------------------------------------