Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-V option, not -D; if/else test. Bumped to 2.5
- Loading branch information
1 parent
18ce876
commit 5ddbca4
Showing
6 changed files
with
135 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,90 @@ | ||
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) | ||
local M = require 'macro' | ||
|
||
local function eval (expr,was_expr) | ||
expr = tostring(expr) | ||
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 eval_line (get,was_expr) | ||
local args = get:line() | ||
return eval(args,was_expr) | ||
end | ||
|
||
local function grab (get) | ||
local ilevel = 0 | ||
while true do | ||
local t,v = get() | ||
while t ~= '@' do t = get() end | ||
t,v = 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_line(get,true) | ||
push_if(res) | ||
if not res then | ||
return grab(get) | ||
end | ||
end) | ||
|
||
M.define('elseif_',function(get) | ||
local res | ||
if ifstack[#ifstack] then | ||
res = false | ||
else | ||
res = eval_line(get,true) | ||
pop_if() | ||
push_if(res) | ||
end | ||
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('let_',function(get) | ||
eval_line(get) | ||
end) | ||
|
||
M.define('eval_(X)',function(X) | ||
return tostring(eval(X,true)) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
require_ 'atm' | ||
@eval env = os.getenv | ||
@if env 'P' | ||
print 'gotcha' | ||
@if A | ||
print 'A was true' | ||
@end | ||
@else | ||
print 'nada' | ||
@end | ||
|
||
|
||
@let env = os.getenv | ||
@if env 'P' | ||
print 'P env was set' | ||
@if A | ||
print 'Global A was true' | ||
@end | ||
@elseif A | ||
print 'Global A was true, no P' | ||
@else | ||
print 'Neither P or A' | ||
@end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters