]> git.lizzy.rs Git - metalua.git/commitdiff
fixed xmatch and xloop
authorFabien Fleutot <fabien@macfabien.local>
Fri, 22 Feb 2008 00:13:54 +0000 (01:13 +0100)
committerFabien Fleutot <fabien@macfabien.local>
Fri, 22 Feb 2008 00:13:54 +0000 (01:13 +0100)
src/lib/extension/xloop.mlua
src/lib/extension/xmatch.mlua
src/samples/xloop_test.mlua
src/samples/xmatch_test.mlua

index 166c89f676bb3978d0aaf8f545bc9353ac386149..9964504ae7d1623c2b1728f5cd1b13d523fc2fbc 100644 (file)
@@ -49,7 +49,7 @@ function xloop_builder(x)
       | `Break -> x <- exit()
       | `Forin{ ... } | `Fornum{ ... } | `While{ ... } | `Repeat{ ... } -> 
          return 'break'
-      | _ -> _
+      | _ -> -- pass
       end
    end
    function cfg.expr.down(x) if x.tag=='Function' then return 'break' end end
index 3fe781d516b47e23a441de3afb7edbe755630d91..8201beb50da80e85031b23c01443abd2c12926d3 100755 (executable)
@@ -2,6 +2,18 @@ require 'extension.match'
 module ('spmatch', package.seeall)\r
 -{extension 'log'}\r
 \r
+match_function_builder = |tag| function (x)                      \r
+   local func_name, _, cases = unpack(x)\r
+   local arity = #cases[1][1][1]\r
+   if arity==0 then \r
+      error "There must be at least 1 case in match function" \r
+   end\r
+   local args = { }\r
+   for i=1, arity do args[i] = mlp.gensym("arg."..i) end\r
+   local body = match_builder{args, cases}\r
+   return { tag=tag, {func_name}, { `Function{ args, {body} } } }\r
+end\r
+\r
 -- Get rid of the former parser, it will be blended in a multiseq:\r
 mlp.stat:del 'match'\r
 \r
@@ -14,29 +26,26 @@ mlp.stat:add{ 'match',
       -- "match function f $2 end"\r
       ----------------------------------------------------------------\r
       { 'function', mlp.expr, gg.optkeyword '|',\r
-         match_cases_list_parser,\r
-         'end',\r
-         builder = function(x)                      \r
-            local func_name, _, cases = unpack(x)\r
-            local arity = #cases[1][1][1]\r
-            if arity==0 then \r
-               error "There must be at least 1 case in match function" \r
-            end\r
-            local args = { }\r
-            for i=1, arity do args[i] = mlp.gensym("arg."..i) end\r
-            local body = match_builder{args, cases}\r
-            return `Set{ {func_name}, { `Function{ args, {body} } } }\r
-         end },\r
+         match_cases_list_parser, 'end', \r
+         builder = match_function_builder 'Set' },\r
 \r
       ----------------------------------------------------------------\r
       -- Reintroduce the original match statement:\r
       ----------------------------------------------------------------\r
       default = gg.sequence{\r
          mlp.expr_list, 'with', gg.optkeyword '|',\r
-         match_cases_list_parser,\r
-         'end',\r
+         match_cases_list_parser, 'end',\r
          builder = |x| match_builder{ x[1], x[3] } } } }\r
 \r
+----------------------------------------------------------------------\r
+-- Shortcut: "local match function f $cases end" translates to:\r
+-- "local function f($args) match $args with $cases end end"\r
+----------------------------------------------------------------------\r
+mlp.stat:get'local'[2]:add{\r
+   'match', 'function', mlp.expr, gg.optkeyword '|',\r
+   match_cases_list_parser, 'end',\r
+   builder = match_function_builder 'Localrec' }\r
+\r
 mlp.expr:add{ 'match', builder = |x| x[1], gg.multisequence{\r
 \r
       ----------------------------------------------------------------\r
index be57bb24aa616c4f448dcc3e6fc8e72fa181ee67..395a07407c8dc775f2d2ada849b9f1f7efc89d83 100644 (file)
@@ -1,4 +1,4 @@
 -{ extension 'xloop' }
 for i=1,9 for j=10,90,10 if i~=3 while i<8 do
-   print(i+j)
+   io.write(i+j, ' ')
 end
\ No newline at end of file
index a81fb914ac3769f7ee643499a063d5eb51e6851d..ab8d6260638bfb3793d963fb7e1077d07cb6a03e 100755 (executable)
@@ -1,37 +1,54 @@
 -{ extension 'xmatch' }\r
 \r
-print(match 1 with 1 -> 'ok' | 2 -> 'KO' end)\r
+WIDTH=60\r
+function p(msg) io.write(msg..' ':rep(WIDTH-#msg)) end\r
 \r
-function f(x)\r
-   match x with\r
-   | y if y<10 -> print 'small'\r
-   | _ -> print 'big'\r
-   end\r
-end\r
+----------------------------------------------------------------------\r
+p"match as an expression"\r
+print(match 1 with 1 -> 'ok' | 2 -> 'KO' end)\r
 \r
+----------------------------------------------------------------------\r
+p "global match function"\r
 match function g\r
-| x if x<10 -> print 'small'\r
-| _         -> print 'big'\r
+| x if x<10 -> return 'o'\r
+| _         -> return 'k'\r
 end\r
+print(g(1)..g(11))\r
 \r
+----------------------------------------------------------------------\r
+p "global match function, multi-args"\r
 match function cmp\r
-| x, y if x<y -> print 'increasing'\r
-| _, _        -> print 'decreasing'\r
-end\r
+| x, y if x<y -> return 'increasing'\r
+| _, _        -> return 'decreasing'\r
+      end\r
 \r
-f(1) f(11) g(1) g(11)\r
+if cmp(1,2)=='increasing' and cmp(2,1)=='decreasing' then\r
+   print "ok" else print "KO"\r
+end\r
 \r
-cmp(1,2) cmp(2,1)\r
+----------------------------------------------------------------------\r
+p "local match function"\r
+do\r
+   local match function x\r
+   | 1 -> print 'ok'\r
+   end\r
+   x(1)\r
+end\r
+assert(not x)\r
 \r
+----------------------------------------------------------------------\r
+p "global bind assignment"\r
 bind {a, b} = {'o', 'k'}\r
-\r
 print(a..b)\r
 \r
+----------------------------------------------------------------------\r
+p "local bind assignment"\r
 c, d = 'k', 'o'\r
-\r
 do\r
    local bind {c, {d}} = {'o', {'k'}}\r
    print(c..d)\r
 end\r
 \r
+----------------------------------------------------------------------\r
+p "local bind assignment scope"\r
 print(d..c)\r