blob: f651db4aa09760a935e5e3922495d1bae57773d0 [file] [log] [blame]
-------------------------------------------------------------------------------
-- Copyright (c) 2006-2013 Fabien Fleutot and others.
--
-- All rights reserved.
--
-- This program and the accompanying materials are made available
-- under the terms of the Eclipse Public License v1.0 which
-- accompanies this distribution, and is available at
-- http://www.eclipse.org/legal/epl-v10.html
--
-- This program and the accompanying materials are also made available
-- under the terms of the MIT public license which accompanies this
-- distribution, and is available at http://www.lua.org/license.html
--
-- Contributors:
-- Fabien Fleutot - API and implementation
--
-------------------------------------------------------------------------------
-{ extension 'xmatch' }
WIDTH=60
function p(msg) io.write(msg..' ':rep(WIDTH-#msg)) end
----------------------------------------------------------------------
p "match as an expression"
print(match 1 with 1 -> 'ok' | 2 -> 'KO')
----------------------------------------------------------------------
p "global match function"
match function g
| x if x<10 -> return 'o'
| _ -> return 'k'
end
print(g(1)..g(11))
----------------------------------------------------------------------
p "global match function, multi-args"
match function cmp
| x, y if x<y -> return 'increasing'
| _, _ -> return 'decreasing'
end
if cmp(1,2)=='increasing' and cmp(2,1)=='decreasing' then
print "ok" else print "KO"
end
----------------------------------------------------------------------
p "local match function"
do
local match function x
| 1 -> print 'ok'
end
x(1)
end
assert(not x)
----------------------------------------------------------------------
p "global bind assignment"
bind {a, b} = {'o', 'k'}
print(a..b)
----------------------------------------------------------------------
p "local bind assignment"
c, d = 'k', 'o'
do
local bind {c, {d}} = {'o', {'k'}}
print(c..d)
end
----------------------------------------------------------------------
p "local bind assignment scope"
print(d..c)