Created
November 20, 2011 19:09
-
-
Save benjaminplee/1380717 to your computer and use it in GitHub Desktop.
Playing with Ruby for QuickPiet interpreter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
class Recorder | |
def push amt | |
puts "push: #{amt}" | |
end | |
def add | |
puts "add" | |
end | |
def macro name, ¯o_def | |
puts "macro" | |
# first way; didn't work | |
# self.define_method name ¯o_def | |
# second way; works | |
self.class.send(:define_method, name.to_s, ¯o_def) | |
end | |
end | |
def quickpiet &script | |
recorder = Recorder.new | |
recorder.instance_eval &script | |
end | |
quickpiet do | |
macro plus2 do | |
push 2 | |
add | |
end | |
push 2 | |
plus2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Figured it out, error above was red herring b/c I didn't use a string or symbol for the macro method invocation.
next problem was accessing define_method correctly (As @adkron pointed out) since it is private to the class and not an instance method