(105 lines)
1 -- This file defines some methods on Lua numbers, bools, actions, nil, and coroutines2 require "text"4 number_mt =5 __type: "a Number"6 as_lua: tostring7 as_nomsu: tostring8 as_text: tostring9 as_a_number: => @10 rounded: => math.floor(@ + .5)11 rounded_down: math.floor12 rounded_up: math.ceil13 to_the_nearest: (rounder)=> rounder * math.floor(@/rounder + 0.5)14 base16: => ("%X")\format(@)15 number_mt.__index = number_mt16 debug.setmetatable 0, number_mt18 bool_mt =19 __type: "a Boolean"20 __len: => @ and 1 or 021 as_lua: tostring22 as_nomsu: => @ and "yes" or "no"23 as_text: => @ and "yes" or "no"24 _and: (cond)=> @ and cond25 _or: (cond)=> @ or cond26 xor: (cond)=> @ == (not cond)27 bool_mt.__index = bool_mt28 debug.setmetatable true, bool_mt30 fn_mt =31 __type: "an Action"32 as_text: => (tostring(@)\gsub("function", "Action"))33 __add: (other)=>34 if type(@) == 'function' and type(other) == 'function'35 (...)-> (@(...) + other(...))36 elseif type(@) == 'function' then (...)-> (@(...) + other)37 else (...)-> (@ + other(...))38 __sub: (other)=>39 if type(@) == 'function' and type(other) == 'function'40 (...)-> (@(...) - other(...))41 elseif type(@) == 'function' then (...)-> (@(...) - other)42 else (...)-> (@ - other(...))43 __mul: (other)=>44 if type(@) == 'function' and type(other) == 'function'45 (...)-> (@(...) * other(...))46 elseif type(@) == 'function' then (...)-> (@(...) * other)47 else (...)-> (@ * other(...))48 __div: (other)=>49 if type(@) == 'function' and type(other) == 'function'50 (...)-> (@(...) other(...))51 elseif type(@) == 'function' then (...)-> (@(...) other)52 else (...)-> (@ other(...))53 __mod: (other)=>54 if type(@) == 'function' and type(other) == 'function'55 (...)-> (@(...) % other(...))56 elseif type(@) == 'function' then (...)-> (@(...) % other)57 else (...)-> (@ % other(...))58 __band: (other)=>59 if type(@) == 'function' and type(other) == 'function'60 (...)-> (@(...) and other(...))61 elseif type(@) == 'function' then (...)-> (@(...) and other)62 else (...)-> (@ and other(...))63 __bor: (other)=>64 if type(@) == 'function' and type(other) == 'function'65 (...)-> (@(...) or other(...))66 elseif type(@) == 'function' then (...)-> (@(...) or other)67 else (...)-> (@ or other(...))68 __bxor: (other)=>69 if type(@) == 'function' and type(other) == 'function'70 (...)-> (@(...) != other(...))71 elseif type(@) == 'function' then (...)-> (@(...) != other)72 else (...)-> (@ != other(...))73 fn_mt.__index = fn_mt74 debug.setmetatable (->), fn_mt76 _last_co_i = setmetatable({}, {__mode:'k'})77 local co_mt78 co_mt =79 __type: "a Coroutine"80 as_text: => (tostring(@)\gsub("thread", "Coroutine")).." ("..coroutine.status(@)..")"81 __len: => math.huge82 __call: coroutine.resume83 __inext: (k)=>84 ok, val = coroutine.resume(@)85 return if coroutine.status(@) == 'dead'86 if ok then return (k or 0) + 1, val87 __index: (k)=>88 if k == (_last_co_i[@] or 0) + 189 ret = {coroutine.resume(@, k)}90 _last_co_i[@] = k91 if ret[1]92 return table.unpack(ret, 2)93 else94 return nil95 return co_mt[k]96 co_mt.__next = co_mt.__inext97 debug.setmetatable(coroutine.create(->), co_mt)99 nil_mt =100 __type: "Nil"101 as_lua: => "nil"102 as_nomsu: => "nil"103 as_text: => "nil"104 nil_mt.__index = nil_mt105 debug.setmetatable nil, nil_mt