Skip to content
Browse files

Everything's leaf green

  • Loading branch information...
1 parent 3d3bb6e commit 14a95b4ad69f2fb6903a8d4cd45fb8149ab43409 @txus committed May 16, 2011
View
6 Gemfile.lock
@@ -2,21 +2,16 @@ PATH
remote: .
specs:
brainfuck (0.2.0)
- highline
parslet
GEM
remote: http://rubygems.org/
specs:
blankslate (2.1.2.4)
- highline (1.6.2)
minitest (2.0.2)
mocha (0.9.12)
parslet (1.2.0)
blankslate (~> 2.0)
- simplecov (0.3.7)
- simplecov-html (>= 0.3.7)
- simplecov-html (0.3.9)
PLATFORMS
java
@@ -26,4 +21,3 @@ DEPENDENCIES
brainfuck!
minitest
mocha
- simplecov
View
6 brainfuck.gemspec
@@ -9,17 +9,15 @@ Gem::Specification.new do |s|
s.authors = ["Josep M. Bach"]
s.email = ["josep.m.bach@gmail.com"]
s.homepage = "http://github.com/txus/brainfuck"
- s.summary = %q{Another Brainfuck interpreter in Ruby}
- s.description = %q{Another Brainfuck interpreter in Ruby}
+ s.summary = %q{An implementation of Brainfuck on the Rubinius VM.}
+ s.description = %q{An implementation of Brainfuck on the Rubinius VM.}
s.rubyforge_project = "brainfuck"
- s.add_runtime_dependency "highline"
s.add_runtime_dependency "parslet"
s.add_development_dependency "minitest"
s.add_development_dependency "mocha"
- s.add_development_dependency "simplecov"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
6 lib/brainfuck/ast.rb
@@ -83,11 +83,13 @@ def bytecode(g)
end
class PutsNode
def bytecode(g)
+ g.push_const :STDOUT
+
g.push_local 0
g.push_local 1
g.send :[], 1, false
- g.send :chr, 0, true
- g.send :puts, 1, true
+
+ g.send :putc, 1, true
end
end
class GetsNode
View
6 lib/brainfuck/compiler.rb
@@ -63,7 +63,7 @@ def self.compile_for_eval(code, variable_scope, file = "(eval)", line = 0, print
end
end
- class Print < Struct.new(:sexp, :ast, :heap, :asm)
+ class Print < Struct.new(:sexp, :ast, :asm)
def sexp?
@sexp
end
@@ -72,10 +72,6 @@ def ast?
@ast
end
- def heap?
- @heap
- end
-
def asm?
@asm
end
View
4 lib/brainfuck/main.rb
@@ -79,10 +79,6 @@ def options(argv)
@print.sexp = true
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
@print.ast = @print.asm = @print.sexp = true
end
View
5 lib/brainfuck/stages.rb
@@ -56,7 +56,8 @@ def run
bottom.set!
@output.use_detected
- @output.push_nil
+ # Return the heap
+ @output.push_local 0
@output.ret
@output.close
@@ -130,7 +131,7 @@ def input(code, filename = "eval", line = 1)
end
def run
- code = Lexer.clean(File.read(@filename))
+ code = Lexer.clean(@code)
@output = Lexer.new.tokenize(code)
pp(@output) if @print.sexp?
run_next
View
16 test/acceptance/acceptance_test.rb
@@ -2,15 +2,17 @@
class BrainfuckAcceptanceTest < MiniTest::Unit::TestCase
+ def test_ok
+ assert_evaluates [2,1], "++++>++++---.<--."
+ end
+
def test_without_loops_nor_user_input
- $stdout.expects(:print).times(2)
+ STDOUT.expects(:putc).times(2)
assert_evaluates [2,1], "++++>++++---.<--."
end
def test_with_user_input
- stack = Brainfuck::Stack.new
- Brainfuck::Interpreter.stubs(:stack).returns stack
- stack.expects(:get_character).returns 97
+ STDIN.expects(:getc).returns 97
assert_evaluates [101], ",++++"
end
@@ -64,6 +66,10 @@ def test_hello_world
private
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
View
19 test/brainfuck/ast_test.rb
@@ -3,24 +3,5 @@
module Brainfuck
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
View
22 test/brainfuck/interpreter_test.rb
@@ -1,22 +0,0 @@
-require 'test_helper'
-
-module Brainfuck
- class InterpreterTest < MiniTest::Unit::TestCase
-
- def test_stack_returns_a_new_or_cached_stack
- stack = Interpreter.stack
- assert_kind_of Stack, stack
- assert Interpreter.stack === stack
- end
-
- %w{fwd bwd inc dec puts gets iteration exp}.each do |rule|
- define_method "test_implements_a_#{rule}_rule" do
- rules = Interpreter.rules.map(&:first).map do |pattern|
- pattern.instance_variable_get(:@pattern)
- end.map(&:keys).flatten
- assert_includes rules, rule.to_sym
- end
- end
-
- end
-end
View
21 test/brainfuck/lexer_test.rb
@@ -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
View
17 test/brainfuck/parser_test.rb
@@ -3,17 +3,12 @@
module Brainfuck
class ParserTest < MiniTest::Unit::TestCase
- def test_INSTRUCTIONS_returns_valid_symbols
- assert_equal %w{> < + - [ ] . ,}, Parser::INSTRUCTIONS
- end
-
- def test_clean_cleans_all_invalid_symbols
- assert_equal '><+-[-].,', Parser.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 Parser.new.respond_to?(rule), "Parser should implement a #{rule} rule"
+ %w{fwd bwd inc dec puts gets iteration exp}.each do |rule|
+ define_method "test_implements_a_#{rule}_rule" do
+ rules = Parser.rules.map(&:first).map do |pattern|
+ pattern.instance_variable_get(:@pattern)
+ end.map(&:keys).flatten
+ assert_includes rules, rule.to_sym
end
end
View
65 test/brainfuck/stack_test.rb
@@ -1,65 +0,0 @@
-require 'test_helper'
-
-module Brainfuck
- class StackTest < MiniTest::Unit::TestCase
- def setup
- @stack = Stack.new
- end
-
- def test_current_returns_the_current_value_of_the_stack
- assert_equal 0, @stack.current
- end
-
- def test_fwd_advances_the_pointer
- @stack.fwd
- assert_equal 1, @stack.instance_variable_get(:@pointer)
- end
-
- def test_bwd_decreases_the_pointer
- @stack.instance_variable_set(:@pointer, 4)
- @stack.bwd
- assert_equal 3, @stack.instance_variable_get(:@pointer)
- end
-
- def test_raises_if_the_pointer_tries_to_go_below_zero
- @stack.instance_variable_set(:@pointer, 0)
- assert_raises RuntimeError, "Tried to access cell -1" do
- @stack.bwd
- end
- end
-
- def test_inc_increases_the_current_cell
- @stack.instance_variable_set(:@stack, [4])
- @stack.inc
- assert_equal 5, @stack.current
- end
-
- def test_dec_decreases_the_current_cell
- @stack.instance_variable_set(:@stack, [4])
- @stack.dec
- assert_equal 3, @stack.current
- end
-
- def test_puts_prints_the_current_character
- $stdout.expects(:print).with 'A'
-
- @stack.stubs(:current).returns 65
- @stack.puts
- end
-
- def test_gets_fetches_from_stdin
- @stack.expects(:get_character).returns 97
- @stack.gets
- assert_equal 97, @stack.current
- end
-
- def test_to_a_returns_the_stack_array
- assert_equal [0], @stack.to_a
- end
-
- def test_clear_clears_the_stack
- @stack.expects(:initialize)
- @stack.clear
- end
- end
-end
View
18 test/brainfuck_test.rb
@@ -1,18 +0,0 @@
-require 'test_helper'
-
-class BrainfuckTest < MiniTest::Unit::TestCase
-
- def test_run
- code, parsed = stub(:code), stub(:parsed)
- ast = [stub(:node), stub(:node)]
-
- Brainfuck::Parser.expects(:clean).with("code").returns code
- Brainfuck::Parser.any_instance.stubs(:parse).with(code).returns parsed
- Brainfuck::Interpreter.any_instance.stubs(:apply).with(parsed).returns ast
-
- ast.each { |node| node.expects(:eval) }
-
- assert_equal Brainfuck::Interpreter.stack.to_a, Brainfuck.run("code")
- end
-
-end
View
5 test/test_helper.rb
@@ -2,11 +2,6 @@
require 'bundler/setup'
-require 'simplecov'
-SimpleCov.start do
- add_group "Lib", "lib"
-end
-
gem 'minitest'
require 'minitest/unit'
require 'minitest/autorun'

0 comments on commit 14a95b4

Please sign in to comment.
Something went wrong with that request. Please try again.