Chervil is a toy Lisp interpreter written in Ruby
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parser_spec.rb 885B

123456789101112131415161718192021222324252627282930313233343536
  1. module Chervil
  2. RSpec.describe Parser do
  3. def parse(source)
  4. lexer = Lexer.new(source)
  5. parser = Parser.new(lexer)
  6. parser.parse
  7. end
  8. it 'parses a number' do
  9. expect(parse('1').first).to eq(AST::Number.new(1.0))
  10. end
  11. it 'parses a string' do
  12. expect(parse('"hello world"').first).to eq(AST::String.new('hello world'))
  13. end
  14. it 'parses an identifier' do
  15. expect(parse('+').first).to eq(AST::Identifier.new('+'))
  16. end
  17. it 'parses an application' do
  18. expect(parse('(+ 1 2)').first).to eq(
  19. AST::Application.new(
  20. AST::Identifier.new('+'),
  21. [AST::Number.new(1.0), AST::Number.new(2.0)]
  22. )
  23. )
  24. end
  25. it 'parses a definition' do
  26. expect(parse('(define x 5)').first).to eq(
  27. AST::Definition.new(AST::Identifier.new('x'), AST::Number.new(5.0))
  28. )
  29. end
  30. end
  31. end