From 01aa199f7acfe9af57c5970793259b36e060fd11 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 15 May 2018 16:36:21 -0700 Subject: Adding support for coroutines, and cleaning up comment syntax. --- nomsu.lua | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'nomsu.lua') diff --git a/nomsu.lua b/nomsu.lua index e835562..3c2fc4e 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -1,7 +1,7 @@ +local _pairs, _ipairs = pairs, ipairs if jit then package.cpath = "./luajit_lpeg/?.so;" .. package.cpath bit32 = require('bit') - local _pairs, _ipairs = pairs, ipairs pairs = function(x) do local mt = getmetatable(x) @@ -486,7 +486,7 @@ do end return nil end, - tree_with_replaced_vars = function(self, tree, replacements) + tree_with_replacements = function(self, tree, replacements) if not (next(replacements)) then return tree end @@ -669,6 +669,42 @@ do return #x end) end + self.environment.ipairs = function(x) + if type(x) == 'function' then + return coroutine.wrap(x) + elseif type(x) == 'thread' then + return coroutine.resume, x, nil + else + do + local mt = getmetatable(x) + if mt then + if mt.__ipairs then + return mt.__ipairs(x) + end + else + return _ipairs(x) + end + end + end + end + self.environment.pairs = function(x) + if type(x) == 'function' then + return coroutine.wrap(x) + elseif type(x) == 'thread' then + return coroutine.resume, x, nil + else + do + local mt = getmetatable(x) + if mt then + if mt.__pairs then + return mt.__pairs(x) + end + else + return _pairs(x) + end + end + end + end for k, v in pairs(Types) do self.environment[k] = v end -- cgit v1.2.3