Skip to content
Browse files

Simple integer literal addition

  • Loading branch information...
1 parent d7fe531 commit a98af273d79760bac4f8ca8ba1655d98b8210b56 @txus committed Apr 21, 2011
Showing with 21 additions and 4 deletions.
  1. +12 −1 lib/brainscript/ast.rb
  2. +3 −1 lib/brainscript/compiler.rb
  3. +1 −1 test/brainscript/compiler_test.rb
  4. +5 −1 test/test_helper.rb
View
13 lib/brainscript/ast.rb
@@ -7,8 +7,19 @@ def compile
end
class Expression < Struct.new(:left, :op, :right)
+ OPS = {
+ '+' => 'add',
+ '-' => 'sub',
+ '*' => 'mul',
+ '/' => 'div'
+ }
+
def compile
- left.compile.send(op, right.compile)
+ self.send OPS[op.to_s], left.compile, right.compile
+ end
+
+ def add(l, r)
+ ">[-<+>]<"
end
end
View
4 lib/brainscript/compiler.rb
@@ -7,7 +7,9 @@ class Brainscript::Compiler
# @param <String> the source code.
def compile(source)
ast = transformer.apply(parser.parse(source))
- ast.compile
+ result = ast.compile.to_s
+ puts result
+ result
end
private
View
2 test/brainscript/compiler_test.rb
@@ -3,7 +3,7 @@
class CompilerTest < MiniTest::Unit::TestCase
def test_simple_addition
- assert_evaluates 3, '1 + 2'
+ assert_evaluates />\[-<\+>\]</, '1 + 2'
end
end
View
6 test/test_helper.rb
@@ -7,7 +7,11 @@
module CompilerAssertions
def assert_evaluates(expected, code)
- assert_equal expected, compiler.compile(code)
+ if expected.is_a?(Regexp)
+ assert_match expected, compiler.compile(code)
+ else
+ assert_equal expected, compiler.compile(code)
+ end
end
private

0 comments on commit a98af27

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