Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/argcheck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRonan Collobert <ronan@collobert.com>2014-11-09 04:50:27 +0300
committerRonan Collobert <ronan@collobert.com>2014-11-09 04:50:27 +0300
commita9d57b109962887f36a4b85015bb4049f0e07678 (patch)
tree599c8012ddf3d739804344f1356497da2bf72c48
parent4d146767a6ff015d1bbc637e648c53d523ab65e4 (diff)
fix support for opt=true (nil are now properly handled)
-rw-r--r--graph.lua21
-rw-r--r--init.lua54
2 files changed, 44 insertions, 31 deletions
diff --git a/graph.lua b/graph.lua
index f78e591..a8f1371 100644
--- a/graph.lua
+++ b/graph.lua
@@ -44,10 +44,15 @@ function ACN:match(rules, rulemask, named)
local head = self
local nmatched = 0
for _,idx in ipairs(rulemask) do
+ local isnil
+ if idx < 0 then
+ idx = -idx
+ isnil = true
+ end
local rule = rules[idx]
local matched = false
for n=1,head.n do
- if head.next[n].type == rule.type
+ if head.next[n].type == (isnil and 'nil' or rule.type)
and head.next[n].check == rule.check
and (not named or (named and head.next[n].name == rule.name)) then
head = head.next[n]
@@ -97,10 +102,16 @@ function ACN:addpath(rules, rulemask, named)
head.rulemask = rulemask
end
for n=n+1,#rulemask do
- local rule = rules[rulemask[n]]
- local node = ACN.new(rule.type,
+ local idx = rulemask[n]
+ local isnil
+ if idx < 0 then
+ idx = -idx
+ isnil = true
+ end
+ local rule = rules[idx]
+ local node = ACN.new(isnil and 'nil' or rule.type,
named and rule.name or nil,
- rule.check,
+ (not isnil) and rule.check or nil, -- nil -> no check at all (beware: not...)
n == #rulemask and rules or nil,
n == #rulemask and rulemask or nil)
head:add(node)
@@ -178,7 +189,7 @@ function ACN:generate_ordered_or_named(code, upvalues, named, depth)
local argidx
for i=1,#self.rulemask do -- DEBUG: bourrin
- if ridx == self.rulemask[i] then
+ if ridx == math.abs(self.rulemask[i]) then
argidx = i
break
end
diff --git a/init.lua b/init.lua
index b6625e1..55cd725 100644
--- a/init.lua
+++ b/init.lua
@@ -18,15 +18,6 @@ end
local function generaterules(rules)
- local nopt = 0
- local nrule = 0
- for _, rule in ipairs(rules) do
- if rule.default ~= nil or rule.defaulta or rule.defaultf or rule.opt then
- nopt = nopt + 1
- end
- nrule = nrule + 1
- end
-
local graph
if rules.chain or rules.overload then
local status
@@ -39,26 +30,37 @@ local function generaterules(rules)
end
local upvalues = {istype=env.istype, graph=graph}
- for optmask=0,2^nopt-1 do
- local rulemask = {}
- local ridx = 1
- local optidx = 0
- while ridx <= nrule do
- local rule = rules[ridx]
- local skiprule = false
+ local optperrule = {}
+ for ridx, rule in ipairs(rules) do
+ if rule.default ~= nil or rule.defaulta or rule.defaultf then
+ optperrule[ridx] = 2 -- here or not here
+ elseif rule.opt then
+ optperrule[ridx] = 3 -- here, nil or not here
+ else
+ optperrule[ridx] = 1 -- here
+ end
+ end
- if rule.default ~= nil or rule.defaulta or rule.defaultf or rule.opt then
- optidx = optidx + 1
- if bit.band(2^(optidx-1), optmask) == 0 then
- skiprule = true
- end
- end
-
- if not skiprule then
+ local optperrulestride = {}
+ local nvariant = 1
+ for ridx=#rules,1,-1 do
+ optperrulestride[ridx] = nvariant
+ nvariant = nvariant * optperrule[ridx]
+ end
+
+ for variant=1,nvariant do
+ local r = variant
+ local rulemask = {}
+ for ridx=1,#rules do
+ local f = math.floor((r-1)/optperrulestride[ridx]) + 1
+ if f == 1 then -- here
table.insert(rulemask, ridx)
+ elseif f == 2 then -- not here
+ elseif f == 3 then -- opt
+ table.insert(rulemask, -ridx)
end
-
- ridx = ridx + 1
+ r = (r-1) % optperrulestride[ridx] + 1
+ local rule = rules[ridx]
end
if not rules.noordered then