Transition := Object clone
Transition set := method(input, state,
self input := input
self state := state
self)
State := Object clone
State init := method(
self transitions := List clone)
State addTransition := method(input, state,
self transitions append(Transition clone set(input, state)))
State nextState := method(input,
transitions foreach(trans,
if(trans input == input,
return(trans state)))
Exception raise("undefined behaviour"))
stop := State clone
State run := method(inputs,
if(inputs isEmpty, "reject",
next := self nextState(inputs at(0))
if(next != stop, next run(inputs slice(1)),
if(inputs size == 1, "accept", "early accept"))))
State run := method(inputs,
if(inputs isEmpty, "reject",
next := self nextState(inputs at(0))
if(next != "stop", doString(next) run(inputs slice(1)),
if(inputs size == 1, "accept", "early accept"))))
State defineMultiple := method(
names := call message arguments foreach(name,
doString("call sender " .. name .. " := self clone")))
State defineMultiple(start, foo, bar, baz)
start addTransition(0, foo)
start addTransition(1, bar)
foo addTransition(0, foo)
foo addTransition(1, baz)
bar nextState := method(input, if(input == 0, bar, foo))
baz addTransition(0, stop)
start run(list(1, 0, 1, 1, 0)) start run(list(0, 1, 0, 0, 1)) start run(list(1, 0, 1, 0, 0)) start run(list(1, 0, 1, 1, 1))
start := State clone
start addTransition(0, "foo")
start addTransition(1, "bar")
foo := State clone
foo addTransition(0, "foo")
foo addTransition(1, "baz")
bar := State clone
bar nextState := method(input, if(input == 0, "bar", "foo"))
baz := State clone
baz addTransition(0, "stop")
start run(list(1, 0, 1, 1, 0)) start run(list(0, 1, 0, 0, 1)) start run(list(1, 0, 1, 0, 0)) start run(list(1, 0, 1, 1, 1))