lua-users home
lua-l archive

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


Hello all,

Here's my semi-regular posting about erlang style numbers... this time
as an excuse to play with Luiz' token filter. Below is the code to
support this interesting (to me at least) extension:

-- my first hack at token filtering
-- accept erlang style numeric constants
--
-- tested very weakly, use at your own risk.
--
-- 2#1001 means base "2" numeric constant "1001" or 9 in decimal
-- 8#666 means base "8" numeric constant "666" or 438 in decimal
-- 16#deadbeef means base "16" numeric constant "deadbeef" or
3735928559 in decimal
-- 36#luiz means base "36" numeric constant "luiz" or 1019339 in decimal

local function F(get,put)
   put("F init")
   while true do
       local line1,token1,value1=get()
       if token1 == "<number>" then
           local line2,token2,value2=get()
           if token2 == "#" then
               local line3,token3,value3=get()
               if value3 then
                   put(line1,"<number>",tonumber(value3,value1))
               else
                   put(line1,token1,value1)
                   put(line2,token2,value2)
                   put(line3,token3,value3)
               end
           else
               put(line1,token1,value1)
               put(line2,token2,value2)
           end
       else
           put(line1,token1,value1)
       end
   end
end

function FILTER(get,source)
   FILTER=coroutine.wrap(F)
   FILTER(get,coroutine.yield)
end