lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Mon, May 16, 2011 at 08:47:15PM +0200, Michael Rose wrote:
> * Every entry has now an implicit 'break' statement at the end unless
>   an 'and' keyword is placed between two entries. E.g.
>     jump chr in "0z" do
>        "aeiou"     do print("vocal")        end   and
>        "a" .. "z"  do print("lower letter") end
>        "0" .. "9"  do print("digit")        end   and
>        do print("no lower letter") end
>     end
>   works now as expected. 
This syntax is much cleaner than the first proposal.  I'll feel happier
with a hyphen for range, and included in the string: "aeiou" stays the 
same but now "a-z", "0-9".  Patterns, sort of, but not the same as that
of the string library.  You are matching only one character and don't need
that generality.  You could define e.g. "a-z^rs" to mean "a to z but not 
r or s".  The price paid the pattern-like notation is that '%', '-' and 
'^' are now 'magic' characters, to be escaped with '%'.

If you are willing to drop the fall-through, i.e. no "and", and the 
duplication (each character appears only once in a key), it would no 
longer matter in what order the options are specified, and it would 
become possible to implement 'jump' as a function that constructs 
a table of functions.  No language extension would be necessary: 
everything is done by the function.

require "jump"
-- The above example would be coded as
jump { "0-z", 
    aeiou = 'print("vocal")', 
    ['a-z^aeiou']='print("lower letter")',
    ['0-9'] = 'print("digit")', 
    'print("no lower letter")' } [chr] ()
-- But one could also say
JT = jump { "0-z",
    aeiou = 'print("vocal")',
    ['a-z^aeiou']='print("lower letter")',
    ['0-9'] = 'print("digit")',
    'print("no lower letter")' }
-- which allows the jump table to be reused and updated
JT.e()      --> vocal
JT['7']()   --> digit
JT['A']()   --> no lower letter
JT['y']()   --> lower letter
JT['ü']()   --> error, out of range
JT['ü']= JT.u   --> extra case not covered by "a-z" 
JT['ü']()   --> vocal

So one would need:
1. A system by which a string is interpreted as a pattern defining
    one character.
2. A function 'jump' that accepts a table T, with T[1] defining the 
    range of allowable characters, T[2] if given the default action, 
    T[pat] the action for characters that satisfy pat; and returns 
    a table J in which an action is specified for all characters defined 
    by T[1].  One could even code this function in Lua, but of course
    it will be more efficient if done in C.