Class: Brainfuck::Main
- Inherits:
-
Object
- Object
- Brainfuck::Main
- Defined in:
- lib/brainfuck/main.rb
Overview
Command line interface to Brainfuck.
Currently we only take a brainfuck source name to compile and run.
Instance Method Summary collapse
-
#compile ⇒ Object
Batch compile all brainfuck files given as arguments.
-
#evals ⇒ Object
Evaluate code given on command line.
-
#initialize ⇒ Main
constructor
A new instance of Main.
- #main(argv = ARGV) ⇒ Object
-
#options(argv) ⇒ Object
Parse command line options.
-
#script ⇒ Object
Run the given script if any.
Constructor Details
Instance Method Details
#compile ⇒ Object
Batch compile all brainfuck files given as arguments.
27 28 29 30 31 32 33 34 35 |
# File 'lib/brainfuck/main.rb', line 27 def compile @rest.each do |bf| begin Compiler.compile_file bf, nil, @print rescue Compiler::Error => e e.show end end end |
#evals ⇒ Object
Evaluate code given on command line
38 39 40 41 42 43 44 45 46 |
# File 'lib/brainfuck/main.rb', line 38 def evals bnd = Object.new def bnd.get; binding; end bnd = bnd.get mod = nil @evals.each do |code| CodeLoader.execute_code code, bnd, mod, @print end end |
#main(argv = ARGV) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/brainfuck/main.rb', line 19 def main(argv=ARGV) (argv) evals unless @evals.empty? script unless @rest.empty? compile if @compile_only end |
#options(argv) ⇒ Object
Parse command line options
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/brainfuck/main.rb', line 54 def (argv) = Rubinius::Options.new "Usage: brainfuck [options] [program]", 20 .doc "Brainfuck is a Brainfuck implementation for the Rubinius VM." .doc "" .doc "OPTIONS:" # options.on "-", "Read and evalute code from STDIN" do # @evals << STDIN.read # end # options.on "--print-ast", "Print the Brainfuck AST" do # @print.ast = true # end # options.on "--print-asm", "Print the Rubinius ASM" do # @print.asm = true # end # options.on "--print-sexp", "Print the Brainfuck Sexp" do # @print.sexp = true # end # options.on "--print-all", "Print Sexp, AST and Rubinius ASM" do # @print.ast = @print.asm = @print.sexp = true # end .on "-C", "--compile", "Just batch compile dont execute." do @compile_only = true end # # options.on "-e", "CODE", "Execute CODE" do |e| # @evals << e # end .on "-h", "--help", "Display this help" do puts exit 0 end .doc "" @rest = .parse(argv) end |
#script ⇒ Object
Run the given script if any
49 50 51 |
# File 'lib/brainfuck/main.rb', line 49 def script CodeLoader.execute_file @rest.first, nil, @print end |