Skip to content

Commit

Permalink
Everything's leaf green
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed May 15, 2011
1 parent 3d3bb6e commit 14a95b4
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 168 deletions.
6 changes: 0 additions & 6 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ PATH
remote: . remote: .
specs: specs:
brainfuck (0.2.0) brainfuck (0.2.0)
highline
parslet parslet


GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
blankslate (2.1.2.4) blankslate (2.1.2.4)
highline (1.6.2)
minitest (2.0.2) minitest (2.0.2)
mocha (0.9.12) mocha (0.9.12)
parslet (1.2.0) parslet (1.2.0)
blankslate (~> 2.0) blankslate (~> 2.0)
simplecov (0.3.7)
simplecov-html (>= 0.3.7)
simplecov-html (0.3.9)


PLATFORMS PLATFORMS
java java
Expand All @@ -26,4 +21,3 @@ DEPENDENCIES
brainfuck! brainfuck!
minitest minitest
mocha mocha
simplecov
6 changes: 2 additions & 4 deletions brainfuck.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ Gem::Specification.new do |s|
s.authors = ["Josep M. Bach"] s.authors = ["Josep M. Bach"]
s.email = ["josep.m.bach@gmail.com"] s.email = ["josep.m.bach@gmail.com"]
s.homepage = "http://github.com/txus/brainfuck" s.homepage = "http://github.com/txus/brainfuck"
s.summary = %q{Another Brainfuck interpreter in Ruby} s.summary = %q{An implementation of Brainfuck on the Rubinius VM.}
s.description = %q{Another Brainfuck interpreter in Ruby} s.description = %q{An implementation of Brainfuck on the Rubinius VM.}


s.rubyforge_project = "brainfuck" s.rubyforge_project = "brainfuck"


s.add_runtime_dependency "highline"
s.add_runtime_dependency "parslet" s.add_runtime_dependency "parslet"


s.add_development_dependency "minitest" s.add_development_dependency "minitest"
s.add_development_dependency "mocha" s.add_development_dependency "mocha"
s.add_development_dependency "simplecov"


s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
6 changes: 4 additions & 2 deletions lib/brainfuck/ast.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ def bytecode(g)
end end
class PutsNode class PutsNode
def bytecode(g) def bytecode(g)
g.push_const :STDOUT

g.push_local 0 g.push_local 0
g.push_local 1 g.push_local 1
g.send :[], 1, false g.send :[], 1, false
g.send :chr, 0, true
g.send :puts, 1, true g.send :putc, 1, true
end end
end end
class GetsNode class GetsNode
Expand Down
6 changes: 1 addition & 5 deletions lib/brainfuck/compiler.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def self.compile_for_eval(code, variable_scope, file = "(eval)", line = 0, print
end end
end end


class Print < Struct.new(:sexp, :ast, :heap, :asm) class Print < Struct.new(:sexp, :ast, :asm)
def sexp? def sexp?
@sexp @sexp
end end
Expand All @@ -72,10 +72,6 @@ def ast?
@ast @ast
end end


def heap?
@heap
end

def asm? def asm?
@asm @asm
end end
Expand Down
4 changes: 0 additions & 4 deletions lib/brainfuck/main.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ def options(argv)
@print.sexp = true @print.sexp = true
end end


options.on "--print-heap", "Print the heap and the pointer at the end" do
@print.heap = true
end

options.on "--print-all", "Print Sexp, AST and Rubinius ASM" do options.on "--print-all", "Print Sexp, AST and Rubinius ASM" do
@print.ast = @print.asm = @print.sexp = true @print.ast = @print.asm = @print.sexp = true
end end
Expand Down
5 changes: 3 additions & 2 deletions lib/brainfuck/stages.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def run
bottom.set! bottom.set!


@output.use_detected @output.use_detected
@output.push_nil # Return the heap
@output.push_local 0
@output.ret @output.ret
@output.close @output.close


Expand Down Expand Up @@ -130,7 +131,7 @@ def input(code, filename = "eval", line = 1)
end end


def run def run
code = Lexer.clean(File.read(@filename)) code = Lexer.clean(@code)
@output = Lexer.new.tokenize(code) @output = Lexer.new.tokenize(code)
pp(@output) if @print.sexp? pp(@output) if @print.sexp?
run_next run_next
Expand Down
16 changes: 11 additions & 5 deletions test/acceptance/acceptance_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@


class BrainfuckAcceptanceTest < MiniTest::Unit::TestCase class BrainfuckAcceptanceTest < MiniTest::Unit::TestCase


def test_ok
assert_evaluates [2,1], "++++>++++---.<--."
end

def test_without_loops_nor_user_input def test_without_loops_nor_user_input
$stdout.expects(:print).times(2) STDOUT.expects(:putc).times(2)
assert_evaluates [2,1], "++++>++++---.<--." assert_evaluates [2,1], "++++>++++---.<--."
end end


def test_with_user_input def test_with_user_input
stack = Brainfuck::Stack.new STDIN.expects(:getc).returns 97
Brainfuck::Interpreter.stubs(:stack).returns stack
stack.expects(:get_character).returns 97


assert_evaluates [101], ",++++" assert_evaluates [101], ",++++"
end end
Expand Down Expand Up @@ -64,6 +66,10 @@ def test_hello_world
private private


def assert_evaluates(expected, code) def assert_evaluates(expected, code)
assert_equal expected, Brainfuck.run(code) bnd = Object.new
def bnd.get; binding; end
bnd = bnd.get
mod = nil
assert_equal expected, Brainfuck::CodeLoader.execute_code(code, bnd, mod)
end end
end end
19 changes: 0 additions & 19 deletions test/brainfuck/ast_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,24 +3,5 @@
module Brainfuck module Brainfuck
class ASTTest < MiniTest::Unit::TestCase class ASTTest < MiniTest::Unit::TestCase


%w{fwd bwd inc dec puts gets}.each do |node|
define_method("test_ast_#{node}_node_eval_delegates_to_the_stack") do
subject = eval("AST::#{node.capitalize}Node").new stub(:stack)
subject.stack.expects(node)
subject.eval
end
end

def test_iteration_node_evaluates_until_cell_is_zero
nodes = [stub(:node), stub(:node)]
subject = AST::IterationNode.new stub(:stack), nodes
subject.stack.stubs(:current).returns 3, 2, 1, 0

nodes.each do |node|
node.expects(:eval).times(3)
end
subject.eval
end

end end
end end
22 changes: 0 additions & 22 deletions test/brainfuck/interpreter_test.rb

This file was deleted.

21 changes: 21 additions & 0 deletions test/brainfuck/lexer_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

module Brainfuck
class LexerTest < MiniTest::Unit::TestCase

def test_INSTRUCTIONS_returns_valid_symbols
assert_equal %w{> < + - [ ] . ,}, Lexer::INSTRUCTIONS
end

def test_clean_cleans_all_invalid_symbols
assert_equal '><+-[-].,', Lexer.clean(">3< 223+fn - ()()[r23-] .bdn*& ,")
end

%w{lparen rparen space space? fwd bwd inc dec puts gets iteration expression}.each do |rule|
define_method("test_implements_a_#{rule}_rule") do
assert Lexer.new.respond_to?(rule), "Parser should implement a #{rule} rule"
end
end

end
end
17 changes: 6 additions & 11 deletions test/brainfuck/parser_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
module Brainfuck module Brainfuck
class ParserTest < MiniTest::Unit::TestCase class ParserTest < MiniTest::Unit::TestCase


def test_INSTRUCTIONS_returns_valid_symbols %w{fwd bwd inc dec puts gets iteration exp}.each do |rule|
assert_equal %w{> < + - [ ] . ,}, Parser::INSTRUCTIONS define_method "test_implements_a_#{rule}_rule" do
end rules = Parser.rules.map(&:first).map do |pattern|

pattern.instance_variable_get(:@pattern)
def test_clean_cleans_all_invalid_symbols end.map(&:keys).flatten
assert_equal '><+-[-].,', Parser.clean(">3< 223+fn - ()()[r23-] .bdn*& ,") assert_includes rules, rule.to_sym
end

%w{lparen rparen space space? fwd bwd inc dec puts gets iteration expression}.each do |rule|
define_method("test_implements_a_#{rule}_rule") do
assert Parser.new.respond_to?(rule), "Parser should implement a #{rule} rule"
end end
end end


Expand Down
65 changes: 0 additions & 65 deletions test/brainfuck/stack_test.rb

This file was deleted.

18 changes: 0 additions & 18 deletions test/brainfuck_test.rb

This file was deleted.

5 changes: 0 additions & 5 deletions test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@


require 'bundler/setup' require 'bundler/setup'


require 'simplecov'
SimpleCov.start do
add_group "Lib", "lib"
end

gem 'minitest' gem 'minitest'
require 'minitest/unit' require 'minitest/unit'
require 'minitest/autorun' require 'minitest/autorun'
Expand Down

0 comments on commit 14a95b4

Please sign in to comment.