Class: Brainfuck::Main

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeMain

Returns a new instance of Main.



12
13
14
15
16
17
# File 'lib/brainfuck/main.rb', line 12

def initialize
  @print = Compiler::Print.new
  @compile_only = false
  @evals = []
  @rest = []
end

Instance Method Details

#compileObject

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

#evalsObject

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)
  options(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 options(argv)
  options = Rubinius::Options.new "Usage: brainfuck [options] [program]", 20
  options.doc "Brainfuck is a Brainfuck implementation for the Rubinius VM."
  options.doc ""
  options.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

  options.on "-C", "--compile", "Just batch compile dont execute." do
    @compile_only = true
  end
# 
#       options.on "-e", "CODE", "Execute CODE" do |e|
#         @evals << e
#       end

  options.on "-h", "--help", "Display this help" do
    puts options
    exit 0
  end

  options.doc ""

  @rest = options.parse(argv)
end

#scriptObject

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