summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/yarp.rb24
-rw-r--r--test/yarp/ruby_api_test.rb11
2 files changed, 35 insertions, 0 deletions
diff --git a/lib/yarp.rb b/lib/yarp.rb
index aa92113d2c..b761d2e4a5 100644
--- a/lib/yarp.rb
+++ b/lib/yarp.rb
@@ -313,6 +313,30 @@ module YARP
end
end
+ class FloatNode < Node
+ def value
+ Float(slice)
+ end
+ end
+
+ class ImaginaryNode < Node
+ def value
+ Complex(0, numeric.value)
+ end
+ end
+
+ class IntegerNode < Node
+ def value
+ Integer(slice)
+ end
+ end
+
+ class RationalNode < Node
+ def value
+ Rational(numeric.value)
+ end
+ end
+
# Load the serialized AST using the source as a reference into a tree.
def self.load(source, serialized)
Serialize.load(source, serialized)
diff --git a/test/yarp/ruby_api_test.rb b/test/yarp/ruby_api_test.rb
index ea18fbb35a..a26b971b82 100644
--- a/test/yarp/ruby_api_test.rb
+++ b/test/yarp/ruby_api_test.rb
@@ -19,4 +19,15 @@ class YARPRubyAPITest < Test::Unit::TestCase
assert_equal_nodes ast1, ast2
assert_equal_nodes ast2, ast3
end
+
+ def test_literal_value_method
+ assert_equal 123, YARP.parse("123").value.statements.body.first.value
+ assert_equal 3.14, YARP.parse("3.14").value.statements.body.first.value
+ assert_equal 42i, YARP.parse("42i").value.statements.body.first.value
+ assert_equal 3.14i, YARP.parse("3.14i").value.statements.body.first.value
+ assert_equal 42r, YARP.parse("42r").value.statements.body.first.value
+ assert_equal 0.5r, YARP.parse("0.5r").value.statements.body.first.value
+ assert_equal 42ri, YARP.parse("42ri").value.statements.body.first.value
+ assert_equal 0.5ri, YARP.parse("0.5ri").value.statements.body.first.value
+ end
end