Skip to content

Instantly share code, notes, and snippets.

@hedgehog
Last active May 9, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedgehog/de4b423f31451035514362a4a2e0f7c6 to your computer and use it in GitHub Desktop.
Save hedgehog/de4b423f31451035514362a4a2e0f7c6 to your computer and use it in GitHub Desktop.
Proof of concept simplying Machinetalk FSM & bindings
#
# Machinetalk FSM event runner: Ruby Proof-of-concept
#
#
# MIT License
#
# Copyright (c) 2017 Hedgehog
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and\/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
%%{
machine mt_event_runner;
#/* Machinetalk Generic actions */
action launch {
# Host language interface code here
mt_launch();
}
action terminate {
# Host language interface code here
mt_terminate();
}
action log {
# Host language interface code here
mt_log();
}
action log_to {
# Host language interface code here
mt_log_to();
}
action log_from {
# Host language interface code here
mt_log_from();
}
action lerror {
# Host language interface code here
mt_lerror();
}
action error {
# Host language interface code here
mt_error();
}
action eof_event {
# Host language interface code here
mt_eof_event();
}
action transition {
mt_run_event_name();
}
action event_text {
mt_build_event_name(data, fpc);
}
transition = '>';
event_text = [a-zA-Z][a-zA-Z_]+;
MtFsm =
start: (
'>' @transition -> two |
(any-'>') @event_text -> start
),
two: (
'>' @transition -> final |
(any-'>') @event_text -> start
);
main := ( MtFsm ) $log $to(log_to) $from(log_from) >launch %terminate %lerr(lerror) %err(error) %eof(eof_event);
}%%
%% write data;
def mt_build_event_name(data, fpc)
@event_name << data.pack("c*")[fpc]
end
def mt_run_event_name()
case @event_name
when 'test'
puts "running test"
when 'this'
puts "running this"
else
puts "unknown MT event"
end
@event_name= ""
end
def run_fsm(data)
@event_name = ""
data = data.unpack("c*") if(data.is_a?(String))
eof = data.length
token_array = []
ts = 0
te = 0
%% write init;
%% write exec;
end
#/* Machinetalk Generic actions */
def mt_launch()
# Host language interface code here
puts __callee__
end
def mt_terminate()
# Host language interface code here
puts __callee__
end
def eof_event()
# Host language interface code here
puts __callee__
end
def mt_error()
# Host language interface code here
puts __callee__
end
def mt_lerror()
# Host language interface code here
puts __callee__
end
def mt_log()
# Host language interface code here
puts __callee__
end
def mt_log_to()
# Host language interface code here
puts __callee__
end
def mt_log_from()
# Host language interface code here
puts __callee__
end
#!/usr/bin/env bash
# Make the Machinetalk FSM's.
fn='mt_ragel_poc'
fsm_type='G2'
ragel -${fsm_type} -o ${fn}.c ${fn}.rl
ragel -F1 -R ${fn}.rl
# Make a GraphViz representation of the FSM's.
ragel -Vp -o ${fn}.dot ${fn}.rl
# Make PNG image of the dot file.
dot -Tpng -o ${fn}.png ${fn}.dot
load './mt_ragel_poc.rb'; run_fsm("test>this>")
mt_log_from
mt_launch
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
running test
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
mt_log
mt_log_to
mt_log_from
running this
mt_log
mt_log_to
@hedgehog
Copy link
Author

hedgehog commented May 9, 2017

simple

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment