]> git.lizzy.rs Git - metalua.git/commitdiff
fixed xmatch extension
authorFabien Fleutot <metalua@gmail.com>
Tue, 3 Feb 2009 19:42:14 +0000 (20:42 +0100)
committerFabien Fleutot <metalua@gmail.com>
Tue, 3 Feb 2009 19:42:14 +0000 (20:42 +0100)
src/lib/metalua/extension/match.mlua
src/lib/metalua/extension/xmatch.mlua
src/samples/xmatch_test.mlua

index 3581b0c48e8cf55cab6ef25111509a5786133332..6cceea7c4bc00aa4655c46c7c22ff9af150417a3 100644 (file)
@@ -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
 
index ce2e204f5760059926abfe509d25635deff6fca3..29dccd87ab5cb3473e69bb7085029e0163c21e1f 100644 (file)
@@ -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)
 
index f7c6d3e904ac55a65abb3c11ca678aee82530005..92897750636a640f80943b6f88a5e9490b4dbc5d 100755 (executable)
@@ -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')
 
 ----------------------------------------------------------------------