From b2a84ddc6905db7e09499e2833b29dd9352ed703 Mon Sep 17 00:00:00 2001 From: James Moriarty Date: Sun, 25 Sep 2016 13:50:14 +1000 Subject: [PATCH] feature: strings. --- Gemfile | 1 + lib/lisp.rb | 8 +++++--- test/test_lisp.rb | 5 +++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index cca4fab..7c31bcd 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source "https://rubygems.org" group :test do + gem "pry" gem "simplecov" gem "codeclimate-test-reporter", require: nil end diff --git a/lib/lisp.rb b/lib/lisp.rb index e0b3b4a..3356111 100644 --- a/lib/lisp.rb +++ b/lib/lisp.rb @@ -31,10 +31,12 @@ def self.parse tokens, tree = [] def self.atom token case token - when /\d/ - token.to_f % 1 > 0 ? token.to_f : token.to_i + when /^[0-9]+$/ + token.to_i + when /[0-9]+\.?[0-9]+?$/ + token.to_f else - token.to_sym + token[0] == token[-1] && token[0] == ?' ? token.slice(1..-2) : token.to_sym end end diff --git a/test/test_lisp.rb b/test/test_lisp.rb index 2c07862..e1311b7 100755 --- a/test/test_lisp.rb +++ b/test/test_lisp.rb @@ -22,8 +22,9 @@ def test_parse # representation def test_representation - assert_equal [:*, 2, [:+, 1, 0]], Lisp.parse(Lisp.tokenize("(* 2 (+ 1 0) )")) + assert_equal [:*, 2, [:+, 1.5, 0]], Lisp.parse(Lisp.tokenize("(* 2 (+ 1.5 0) )")) assert_equal [:lambda, [:r], [:*, 3.141592653, [:*, :r, :r]]], Lisp.parse(Lisp.tokenize("(lambda (r) (* 3.141592653 (* r r)))")) + assert_equal [:+, "hello", "world"], Lisp.parse(Lisp.tokenize("(+ 'hello' 'world')")) end # execution @@ -112,7 +113,7 @@ def test_repl Process.kill("INT", pid) thread.join end - + assert_output("ctrl-c to exit\n") do subject.run end