Permalink
Browse files

Lua 5.2 updates, new example

  • Loading branch information...
1 parent cbe6020 commit 18ce8768acb262cd4ff574db74e4a09d3ab69ae4 @stevedonovan committed Jun 7, 2013
Showing with 131 additions and 27 deletions.
  1. +12 −14 luam
  2. +5 −11 macro.lua
  3. +88 −0 tests/atm.lua
  4. +12 −1 tests/dollar.lua
  5. +12 −0 tests/test-atm.lua
  6. +1 −0 tests/test-cskin.lua
  7. +1 −1 tests/test-dollar.lua
View
26 luam
@@ -11,18 +11,6 @@ The package loader is modified so that `require 'mod'` will preprocess `mod` if
Dumping is the only action available when preprocessing C code with `-C`.
-##Usage
-
- LuaMacro 2.4.0, a Lua macro preprocessor and runner
- -l require a library
- -e statement to be executed
- -c error context to be shown (default 2)
- -d dump preprocessed output to stdout
- -C C lexer
- -N No #line directives when generating C
- -i interactive prompt
- <input> Lua source file
-
@script luam
]]
@@ -35,9 +23,10 @@ require 'macro.builtin'
--- Using luam.
-- @usage follows
local usage = [[
-LuaMacro 2.4.0, a Lua macro preprocessor and runner
+LuaMacro 2.5.0, a Lua macro preprocessor and runner
-l require a library
-e statement to be executed
+ -D set a symbol (-DX or -DY=1)
-c error context to be shown (default 2)
-d dump preprocessed output to stdout
-o write to this file
@@ -49,7 +38,7 @@ LuaMacro 2.4.0, a Lua macro preprocessor and runner
]]
-- parsing the args, the hard way:
-local takes_value = {l = '', e = '', c = 2, o = ''}
+local takes_value = {l = '', e = '', c = 2, o = '',D = ''}
local args = {}
local idx,i = 1,1
@@ -262,6 +251,15 @@ macro.set_package_loader()
if args.l ~= '' then require(args.l) end
+if args.D ~= '' then
+ local sym,val = args.D:match '([^=]+)=(.+)'
+ if not sym then
+ sym = args.D
+ val = true
+ end
+ _G[sym] = val
+end
+
if args.e ~= '' then
subst_runstring(args.e,"<temp>")
else
View
@@ -629,16 +629,19 @@ function M.substitute_tostring(src,name,use_c,throw)
end
local lua52 = _VERSION:match '5.2'
-local loadin, searchpath
+local load, searchpath = load, package.searchpath
if not lua52 then -- Lua 5.1
- function loadin (env,src,name)
+ function load (env,src,name)
local chunk,err = loadstring(src,name)
if chunk and env then
setfenv(chunk,env)
end
return chunk,err
end
+end
+
+if not searchpath then
local sep = package.config:sub(1,1)
searchpath = function (mod,path)
mod = mod:gsub('%.',sep)
@@ -648,15 +651,6 @@ if not lua52 then -- Lua 5.1
if f then f:close(); return nm end
end
end
-else -- Lua 5.2
- function loadin(env,src,name)
- if env then
- return load(src,name,'t',env)
- else
- return load(src,name)
- end
- end
- searchpath = package.searchpath
end
--- load Lua code in a given envrionment after passing
View
@@ -0,0 +1,88 @@
+local M = require 'macro'
+
+local function get_args (get)
+ -- grab the expression
+ return (get:upto(function(t,v)
+ return (t=='space' and v:match '\n') or t == 'comment'
+ end))
+end
+
+local function eval (get,was_expr)
+ local expr = tostring(get_args(get))
+ if was_expr then expr = "return "..expr end
+ local chunk = M.assert(loadstring(expr))
+ local ok, res = pcall(chunk)
+ if not ok then M.error("error evaluating "..res) end
+ return res
+end
+
+local function next_at_macro (get)
+ local t,v = get()
+ while t ~= '@' do t = get() end
+ t,v = get()
+ return v
+end
+
+local function grab (get)
+ local ilevel = 0
+ while true do
+ local v = next_at_macro (get)
+ if v == 'if' then
+ ilevel = ilevel + 1
+ else -- 'end','elseif','else'
+ if ilevel > 0 and v == 'end' then
+ ilevel = ilevel - 1
+ elseif ilevel == 0 then return '@'..v end
+ end
+ end
+end
+
+M.define('@',function(get,put)
+ local t,v = get()
+--~ print('got',t,v)
+ return put:name(v..'_')
+end)
+
+local ifstack,push,pop = {},table.insert,table.remove
+
+local function push_if (res)
+--~ print 'push'
+ push(ifstack, not (res==false or res==nil))
+end
+
+local function pop_if ()
+--~ print 'pop'
+ pop(ifstack)
+end
+
+M.define('if_',function(get)
+ local res = eval(get,true)
+ push_if(res)
+ if not res then
+ return grab(get)
+ end
+end)
+
+M.define('elseif_',function(get)
+ local res = eval(get,true)
+ pop_if()
+ push_if(res)
+ if not res then
+ return grab(get)
+ end
+end)
+
+M.define('else_',function(get)
+ if #ifstack == 0 then M.error("mismatched else") end
+ if ifstack[#ifstack] then
+ return grab(get)
+ end
+end)
+
+M.define('end_',function(get)
+ pop_if()
+end)
+
+M.define('eval_',function(get)
+ eval(get)
+end)
View
@@ -6,6 +6,17 @@ macro.define('$',function(get)
return 'os.getenv("'..v..'")'
elseif t == '(' then
local rest = get:upto ')'
- return 'os.execute("'..tostring(rest)..'")'
+ return 'eval("'..tostring(rest)..'")'
end
end)
+
+return function()
+ return [[
+local function eval(cmd)
+ local f = io.popen(cmd,'r')
+ local res = f:read '*a'
+ f:close()
+ return res
+end
+]]
+end
View
@@ -0,0 +1,12 @@
+require_ 'atm'
+@eval env = os.getenv
+@if env 'P'
+print 'gotcha'
+@if A
+print 'A was true'
+@end
+@else
+print 'nada'
+@end
+
+
View
@@ -1,4 +1,5 @@
-- run like so: luam -lcskin test-cskin.lua
+
class Named {
def _init(self,name) { -- name for ctor
self.name = name
View
@@ -1,7 +1,7 @@
require_ 'dollar'
print($PATH)
if $(ls) ~= 0 then
- $(dir /B) -- so there!
+ print($(dir /B)) -- so there!
end

0 comments on commit 18ce876

Please sign in to comment.