Bruker:Jeblad/Module:FSM
Utseende
Dokumentasjon
[opprett]
-- don't pollute with globals
require('Module:No globals')
local SINGLE = 1
local MULTIPLE = 2
local ALTERNATE = 4
local fsm = {}
function fsm.next( index, current, tape, transitions )
local state
if not current then
return nil
elseif transitions[current] == true then
return true
elseif transitions[current][1] == SINGLE then
return mw.ustring.sub(tape, index, index) == transitions[current][3] and transitions[current][2] or nil
elseif transitions[current][1] == MULTIPLE then
for _,transition in ipairs( transitions[current][2] ) do
if mw.ustring.sub(tape, index, index) == transition[2] then
return transition[1]
end
end
return nil
else
throw('Wrong state transfer type')
end
return state
end
function fsm.drecognize( tape, transitions )
local index = 1
local current = 1
local tapeLength = mw.ustring.len( tape )
repeat
if index > tapeLength then
local lookahead = fsm.next( index, current, tape, transitions )
return (lookahead == true) and true or false
elseif not current then
return false
else
current = fsm.next( index, current, tape, transitions )
index = 1+index
end
until false
end
function fsm.run( frame )
local lookup = {}
local str = frame:getParent().args[1]
--local machine = frame:getParent().args[2]
local engines = mw.loadData( 'Module:FSM/analyze' )
lookup['sheeptalk'] = {
func = fsm.drecognize,
set = engines.sheeptalk_dfsa
}
local pair = lookup['sheeptalk']
if pair then
result = pair.func( str, pair.set.transitions)
end
if result == true then
return 'accept'
end
if result == false then
return 'reject'
end
return 'unknown'
end
return fsm