lua-users home
lua-l archive

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


In metalua, that would be:

$ cat awhile.lua
--macro back-end:
function awhile_builder(x)
   local expr, var, block = unpack(x)
   var = var or +{it}
   return +{ stat: do
      local -{var}
      while true do
         (-{var}) = -{expr}
         if -{var} then -{block} else break end
      end
   end }
end

mlp.lexer.register "awhile" -- register the new keyword
--syntactical front-end:
mlp.stat.add{ builder = awhile_builder,

   "awhile", mlp.expr, gg.onkeyword{ ",", mlp.id, consume=true },
   "do", mlp.block, "end" }


And here is a simplistic test program:

$ cat awhile_test.lua
-{ dofile "awhile.luac" }
x=0
awhile x do
   print(it)
   if x==10 then x=false else x=x+1 end
end

$ mlc awhile.lua
$ mlc -x awhile_test.lua