Skip to content

Instantly share code, notes, and snippets.

View aarongough's full-sized avatar

Aaron Gough (He/Him) aarongough

View GitHub Profile
%%{
machine test_lexer;
integer = <token_description>;
float = <token_description>;
main := |*
integer => {<action>};
float => {<action>};
%%{
machine test_lexer;
<scanner_name> := |*
<token_description> => {<action>};
<token_description> => {<action>};
*|;
}%%
# lexer.rl
%%{
machine test_lexer;
}%%
%% write data;
blank_object = {
:parent => nil,
:slots => {},
:size => 0
}
def send(receiver, message, *params, &block)
method_owner = receiver
while(method_owner[:slots][:lookup].nil?)
puts "ERR: lookup failed for ':lookup' on object:\n#{receiver.inspect}" and break if(method_owner[:parent].nil?)
send(object_with_size, :size)
# => 1
object_with_size = derive_from(basic_object)
send(object_with_size, :add_method, :size) do |this, params|
this[:size]
end
basic_object[:size] = 2
basic_object[:slots][:add_method] = Proc.new do |this, params|
puts "ERR: add_method called without method key" and return unless(params.first.is_a?(String) || params.first.is_a?(Symbol))
puts "ERR: add_method called without block" and return unless(params.last.is_a?(Proc))
this[:slots][params.first.to_sym] = params.last
this[:size] += 1
end
basic_object = derive_from(blank_object)
basic_object[:slots][:lookup] = Proc.new do |this, message|
method_owner = this
while(!method_owner.nil? && method_owner[:slots][message.to_sym].nil?)
puts "ERR: lookup failed for '#{message}' on object:\n#{this.inspect}" and break if(method_owner[:parent].nil?)
method_owner = method_owner[:parent]
end
method_owner[:slots][message.to_sym] unless(method_owner.nil?)
end