Class: Brainfuck::Compiler

Inherits:
Rubinius::Compiler
  • Object
show all
Defined in:
lib/brainfuck/compiler.rb

Defined Under Namespace

Classes: Print

Class Method Summary collapse

Class Method Details

.always_recompile=(flag) ⇒ Object



12
13
14
# File 'lib/brainfuck/compiler.rb', line 12

def self.always_recompile=(flag)
  @always_recompile = flag
end

.compile_file(file, output = nil, print = Print.new) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brainfuck/compiler.rb', line 28

def self.compile_file(file, output = nil, print = Print.new)
  compiler = new :brainfuck_file, :compiled_file
  parser = compiler.parser

  parser.input file

  compiler.generator = Rubinius::Generator.new
  compiler.writer.name = output || compiled_filename(file)

  parser.print = print
  compiler.packager.print.bytecode = true if print.asm?

  begin
    compiler.run

  rescue Exception => e
    compiler_error "Error trying to compile brainfuck: #{file}", e
  end
end

.compile_for_eval(code, variable_scope, file = "(eval)", line = 0, print = Print.new) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/brainfuck/compiler.rb', line 48

def self.compile_for_eval(code, variable_scope, file = "(eval)", line = 0, print = Print.new)
  compiler = new :brainfuck_code, :compiled_method
  parser = compiler.parser

  parser.input code, file, line
  compiler.generator.root = Rubinius::AST::EvalExpression
  compiler.generator.variable_scope = variable_scope

  parser.print = print
  compiler.packager.print.bytecode = true if print.asm?

  begin
    compiler.run
  rescue Exception => e
    compiler_error "Error trying to compile brainfuck: #{file}", e
  end
end

.compile_if_needed(file, output = nil, print = Print.new) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/brainfuck/compiler.rb', line 16

def self.compile_if_needed(file, output = nil, print = Print.new)
  compiled = output || compiled_filename(file)
  needed = @always_recompile || !File.exists?(compiled) ||
    File.stat(compiled).mtime < File.stat(file).mtime
  if needed
    compile_file(file, compiled, print)
  else
    Brainfuck::CodeLoader.new(compiled).load_compiled_file(compiled, 0)
  end
end

.compiled_filename(filename) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/brainfuck/compiler.rb', line 4

def self.compiled_filename(filename)
  if filename =~ /.bf$/
    filename + "c"
  else
    filename + ".compiled.bfc"
  end
end