Created
February 23, 2009 01:13
-
-
Save seivan/68715 to your computer and use it in GitHub Desktop.
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
module BCCode | |
def self.build(&block) | |
builder = Bc.new | |
builder.instance_eval(&block) | |
builder.codes | |
end | |
class Bc | |
attr_reader :code, :codes | |
def initialize | |
@code = @codes = "" | |
end | |
def method_missing(name) | |
table = { | |
:plus => "+", | |
:minus => "-", | |
:div => "/", | |
:times => "*", | |
#:modulos => "%" | |
} | |
@code += table.has_key?(name)? table[name] : name.to_s | |
self | |
end | |
def number(n) | |
@code += n.to_s | |
self | |
end | |
def set(variable, value) | |
@code += variable + "=" + value | |
self | |
end | |
def stop | |
build_code | |
end | |
private | |
def build_code | |
@codes += code + "\n" | |
@code = "" | |
#@codes = eval(@codes) | |
end | |
end | |
end | |
if $0 == __FILE__ | |
code = ARGV[0].to_s.gsub(/\"/, "") | |
block = Proc.new { instance_eval(code) } | |
puts BCCode::build(&block) | |
end | |
puts BCCode.build { number(4).plus.number(2).stop } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment