Permalink
Browse files

+ Starting out: Simple arithmetic.

An old friend
coming home; you know him well
he looks different
  • Loading branch information...
1 parent 2ee801d commit 03b0fe7c5ee94d193bc38163b15830e9fd4b80f7 @kschiess committed Dec 30, 2010
Showing with 77 additions and 1 deletion.
  1. 0 .rspec
  2. +6 −1 Gemfile
  3. +12 −0 Gemfile.lock
  4. +7 −0 lib/wt.rb
  5. +25 −0 lib/wt/parser.rb
  6. +6 −0 spec/spec_helper.rb
  7. +21 −0 spec/wt/parser_spec.rb
View
0 .rspec
No changes.
View
7 Gemfile
@@ -1,3 +1,8 @@
source "http://rubygems.org"
-gem 'parslet', '~> 1.0'
+gem 'parslet', '~> 1.0'
+
+group :development do
+ gem 'rspec', '~> 2'
+ gem 'flexmock'
+end
View
12 Gemfile.lock
@@ -2,11 +2,23 @@ GEM
remote: http://rubygems.org/
specs:
blankslate (2.1.2.3)
+ diff-lcs (1.1.2)
+ flexmock (0.8.11)
parslet (1.0.0)
blankslate (~> 2.1.2.3)
+ rspec (2.3.0)
+ rspec-core (~> 2.3.0)
+ rspec-expectations (~> 2.3.0)
+ rspec-mocks (~> 2.3.0)
+ rspec-core (2.3.1)
+ rspec-expectations (2.3.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.3.0)
PLATFORMS
ruby
DEPENDENCIES
+ flexmock
parslet (~> 1.0)
+ rspec (~> 2)
View
7 lib/wt.rb
@@ -0,0 +1,7 @@
+
+# A module that will hold the entire Wt language implementation.
+#
+module Wt
+end
+
+require 'wt/parser'
View
25 lib/wt/parser.rb
@@ -0,0 +1,25 @@
+
+require 'parslet'
+
+class Wt::Parser < Parslet::Parser
+ rule(:expression) { mexpression >> (c('+-') >> expression).maybe }
+ rule(:mexpression) { atom >> (c('*/') >> mexpression).maybe }
+ rule(:atom) { integer | s('(') >> expression >> s(')') }
+
+ rule(:integer) { c('0-9') }
+
+ rule(:space?) { space.maybe }
+ rule(:space) { match['\\s'].repeat }
+private
+ # Defines a string followed by any number of spaces.
+ #
+ def s(str)
+ str(str) >> space?
+ end
+
+ # Defines a set of characters followed by any number of spaces.
+ #
+ def c(chars)
+ match[chars] >> space?
+ end
+end
View
6 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+
+RSpec.configure do |config|
+ config.mock_with :flexmock
+end
+
+require 'wt'
View
21 spec/wt/parser_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Wt::Parser do
+ let(:parser) { described_class.new }
+
+ RSpec::Matchers.define :parse do |exp|
+ match do |parser|
+ parser.parse(exp)
+ end
+ end
+
+ context "simple arithmetic expressions" do
+ subject { parser.expression }
+ it { should parse("1+2") }
+ it { should parse("1-2") }
+ it { should parse("1*2") }
+ it { should parse("1/2") }
+ it { should parse('1*2 + 3') }
+ it { should parse('1 * (1 + 3)') }
+ end
+end

0 comments on commit 03b0fe7

Please sign in to comment.