Permalink
Browse files

in the Schemer parser, EVERYTHING IS A LIST

  • Loading branch information...
1 parent 89f058b commit 495c3e2b31877d1973829da6ec492d1a5dfa695a @txus committed Mar 15, 2011
Showing with 18 additions and 40 deletions.
  1. +1 −1 lib/schemer/lexer.rb
  2. +1 −14 lib/schemer/parser.rb
  3. +16 −25 spec/schemer/parser_spec.rb
@@ -36,7 +36,7 @@ class Lexer < Parslet::Parser
rule(:operator) { `+` | `-` | `*` | `/` | `>=` | `<=` | `>` | `<` | `=` }
- rule(:arg) { (symbol | operator | quoted_list | literal | quoted_symbol | pair | vector | list) }
+ rule(:arg) { (symbol.as(:identifier) | operator | quoted_list | literal | quoted_symbol | pair | vector | list) }
rule(:args) { (arg >> space?).repeat }
rule(:comment) { `;`.repeat(1,3) >> (`\n`.absnt? >> any).repeat.as(:comment) }
View
@@ -1,7 +1,7 @@
module Schemer
class Parser < Parslet::Transform
- rule(:string => simple(:string)) { string }
+ rule(:string => simple(:string)) { string.to_s }
rule(:integer => simple(:integer)) { integer.to_i }
rule(:float => simple(:float)) { float.to_f }
@@ -20,19 +20,6 @@ class Parser < Parslet::Transform
rule(:vector => sequence(:vector)) { AST::Vector.new(vector) }
rule(:pair => sequence(:pair)) { AST::List.new(pair) }
- rule(:procedure => subtree(:procedure)) do
- puts procedure.inspect
- if procedure[:proc].is_a?(AST::Identifier) &&
- procedure[:proc].value == :define &&
- procedure[:args].any?{|a| a.is_a?(AST::Procedure)}
-
- procedure[:args].map! do |arg|
- AST::List.new(arg.args)
- end
- end
- AST::Procedure.new(procedure)
- end
-
rule(:comment => simple(:comment)) { AST::Comment.new(comment) }
rule(:comment => []) { AST::Comment.new("") }
@@ -8,76 +8,67 @@ module Schemer
describe "Strings" do
it "suffer no change" do
text = "(proc \"my string\")"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should eq("my string")
end
end
describe "Integers" do
it "are typecasted from string" do
text = "(proc 29)"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should eq(29)
end
end
describe "Floats" do
it "are typecasted from string" do
text = "(proc 29.42)"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should eq(29.42)
end
end
describe "Chars" do
it "are transformed into CharacterLiterals" do
text = "(proc #\\z)"
- parsed = subject.apply(lexer.parse text).first.args
- parsed.first.should be_an(AST::CharacterLiteral)
+ parsed = subject.apply(lexer.parse text).first.elements.last
+ parsed.should be_an(AST::CharacterLiteral)
end
end
describe "Booleans" do
it "are directly evaluated" do
text = "(proc #f #t)"
- parsed = subject.apply(lexer.parse text).first.args
- parsed.first.should be_false
- parsed.last.should be_true
+ parsed = subject.apply(lexer.parse text).first.elements
+ parsed[1].should be_false
+ parsed[2].should be_true
end
end
describe "Identifiers" do
describe "Regular identifiers" do
it "are transformed into Identifiers" do
text = "(proc my_identifier)"
- parsed = subject.apply(lexer.parse text).first.args
- parsed.first.should be_an(AST::Identifier)
+ parsed = subject.apply(lexer.parse text).first.elements.last
+ parsed.should be_an(AST::Identifier)
end
end
describe "Quoted identifiers" do
it "are transformed into QuotedIdentifiers" do
text = "(proc 'quoted_identifier)"
- parsed = subject.apply(lexer.parse text).first.args
- parsed.first.should be_an(AST::QuotedIdentifier)
+ parsed = subject.apply(lexer.parse text).first.elements.last
+ parsed.should be_an(AST::QuotedIdentifier)
end
end
end
- describe "Expressions" do
- it "are transformed into Expressions recursively" do
- text = "(lambda (my-proc))"
- parsed = subject.apply(lexer.parse text).first
- parsed.should be_an(AST::Procedure)
- parsed.args.first.should be_an(AST::Procedure)
- end
- end
-
describe "Lists" do
describe "Regular lists" do
it 'are transformed into Lists' do
text = "(lambda (1 2 3))"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should be_an(AST::List)
parsed.elements[0].should eq(1)
parsed.elements[1].should eq(2)
@@ -87,7 +78,7 @@ module Schemer
describe "Quoted lists" do
it 'are transformed into QuotedLists' do
text = "(lambda '(1 2 3))"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should be_an(AST::QuotedList)
parsed.elements[0].should eq(1)
parsed.elements[1].should eq(2)
@@ -97,7 +88,7 @@ module Schemer
describe "Vectors" do
it 'are transformed into Vectors' do
text = "(lambda #(1 2 3))"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should be_an(AST::Vector)
parsed.elements[0].should eq(1)
parsed.elements[1].should eq(2)
@@ -107,7 +98,7 @@ module Schemer
describe "Pairs" do
it 'are combined into Lists' do
text = "(lambda (1 . (2 3)))"
- parsed = subject.apply(lexer.parse text).first.args.first
+ parsed = subject.apply(lexer.parse text).first.elements.last
parsed.should be_an(AST::List)
parsed.elements.first.should == 1
parsed.elements.last.should be_an(AST::List)

0 comments on commit 495c3e2

Please sign in to comment.