mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	* test/ruby/sentence.rb: new method Sentence().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13110 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									98e7aa679a
								
							
						
					
					
						commit
						a854a4358f
					
				
					 2 changed files with 53 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
Sun Aug 19 12:36:11 2007  Tanaka Akira  <akr@fsij.org>
 | 
			
		||||
 | 
			
		||||
	* test/ruby/sentence.rb: new method Sentence().
 | 
			
		||||
 | 
			
		||||
Sun Aug 19 12:32:39 2007  Koichi Sasada  <ko1@atdot.net>
 | 
			
		||||
 | 
			
		||||
	* insnhelper.ci (vm_callee_setup_arg): fix to mark enough VM stack.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,7 +34,7 @@
 | 
			
		|||
#
 | 
			
		||||
# Some arithmetic expressions including parenthesis can be generated as follows.
 | 
			
		||||
# 
 | 
			
		||||
#  synax = {
 | 
			
		||||
#  syntax = {
 | 
			
		||||
#    :factor => [["n"],
 | 
			
		||||
#                ["(", :exp, ")"]],
 | 
			
		||||
#    :term => [[:factor],
 | 
			
		||||
| 
						 | 
				
			
			@ -74,6 +74,18 @@
 | 
			
		|||
#  ...
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
# Sentence() instantiates a sentence object.
 | 
			
		||||
#
 | 
			
		||||
#  Sentence("foo", "bar")
 | 
			
		||||
#  #=> #<Sentence: "foo" "bar">
 | 
			
		||||
#
 | 
			
		||||
#  Sentence("foo", ["bar", "baz"])
 | 
			
		||||
#  #=> #<Sentence: "foo" ("bar" "baz")>
 | 
			
		||||
#
 | 
			
		||||
def Sentence(*ary)
 | 
			
		||||
  Sentence.new(ary)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# Sentence class represents a tree with string leaves.
 | 
			
		||||
#
 | 
			
		||||
class Sentence
 | 
			
		||||
| 
						 | 
				
			
			@ -96,10 +108,10 @@ class Sentence
 | 
			
		|||
  # returns a string which is concatenation of all strings.
 | 
			
		||||
  # No separator is used.
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new(["2", "+", "3"]).to_s
 | 
			
		||||
  #  Sentence("2", "+", "3").to_s
 | 
			
		||||
  #  "2+3"
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new(["2", "+", ["3", "*", "5"]]).to_s
 | 
			
		||||
  #  Sentence("2", "+", ["3", "*", "5"]).to_s
 | 
			
		||||
  #  "2+3*5"
 | 
			
		||||
  #
 | 
			
		||||
  def to_s
 | 
			
		||||
| 
						 | 
				
			
			@ -109,13 +121,13 @@ class Sentence
 | 
			
		|||
  # returns a string which is concatenation of all strings separated by _sep_.
 | 
			
		||||
  # If _sep_ is not given, single space is used.
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new(["I", "have", ["a", "pen"]]).join
 | 
			
		||||
  #  Sentence("I", "have", ["a", "pen"]).join
 | 
			
		||||
  #  "I have a pen"
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new(["I", "have", ["a", "pen"]]).join("/")
 | 
			
		||||
  #  Sentence("I", "have", ["a", "pen"]).join("/")
 | 
			
		||||
  #  "I/have/a/pen"
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new(["a", [], "b"]).join("/")
 | 
			
		||||
  #  Sentence("a", [], "b").join("/")
 | 
			
		||||
  #  "a/b"
 | 
			
		||||
  #
 | 
			
		||||
  def join(sep=' ')
 | 
			
		||||
| 
						 | 
				
			
			@ -127,7 +139,7 @@ class Sentence
 | 
			
		|||
  # Note that the result is not copied.
 | 
			
		||||
  # Don't modify the result.
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new([["foo", "bar"], "baz"]).to_a
 | 
			
		||||
  #  Sentence(["foo", "bar"], "baz").to_a
 | 
			
		||||
  #  #=> [["foo", "bar"], "baz"]
 | 
			
		||||
  #
 | 
			
		||||
  def to_a
 | 
			
		||||
| 
						 | 
				
			
			@ -136,7 +148,7 @@ class Sentence
 | 
			
		|||
 | 
			
		||||
  # returns <i>i</i>th element as a sentence or string.
 | 
			
		||||
  #
 | 
			
		||||
  #  s = Sentence.new([["foo", "bar"], "baz"])
 | 
			
		||||
  #  s = Sentence(["foo", "bar"], "baz")
 | 
			
		||||
  #  s    #=> #<Sentence: ("foo" "bar") "baz">
 | 
			
		||||
  #  s[0] #=> #<Sentence: "foo" "bar">
 | 
			
		||||
  #  s[1] #=> "baz"
 | 
			
		||||
| 
						 | 
				
			
			@ -151,7 +163,7 @@ class Sentence
 | 
			
		|||
  #  Sentence.new(%w[foo bar]).length   
 | 
			
		||||
  #  #=> 2
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new([%w[2 * 7], "+", %w[3 * 5]]).length                         
 | 
			
		||||
  #  Sentence(%w[2 * 7], "+", %w[3 * 5]).length                         
 | 
			
		||||
  #  #=> 3
 | 
			
		||||
  #
 | 
			
		||||
  def length
 | 
			
		||||
| 
						 | 
				
			
			@ -216,7 +228,7 @@ class Sentence
 | 
			
		|||
  # The block is invoked for each subsentence in preorder manner.
 | 
			
		||||
  # The first subsentence which the block returns true is returned.
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.new([%w[2 * 7], "+", %w[3 * 5]]).find_subtree {|s| s[1] == "*" }
 | 
			
		||||
  #  Sentence(%w[2 * 7], "+", %w[3 * 5]).find_subtree {|s| s[1] == "*" }
 | 
			
		||||
  #  #=> #<Sentence: "2" "*" "7">
 | 
			
		||||
  #
 | 
			
		||||
  def find_subtree(&b)
 | 
			
		||||
| 
						 | 
				
			
			@ -247,7 +259,7 @@ class Sentence
 | 
			
		|||
  # The subsentences which the block returns true are
 | 
			
		||||
  # expanded into parent.
 | 
			
		||||
  #
 | 
			
		||||
  #  s = Sentence.new([%w[2 * 7], "+", %w[3 * 5]])
 | 
			
		||||
  #  s = Sentence(%w[2 * 7], "+", %w[3 * 5])
 | 
			
		||||
  #  #=> #<Sentence: ("2" "*" "7") "+" ("3" "*" "5")>
 | 
			
		||||
  #
 | 
			
		||||
  #  s.expand { true }
 | 
			
		||||
| 
						 | 
				
			
			@ -349,6 +361,32 @@ class Sentence
 | 
			
		|||
  #   :just_empty1=>[],
 | 
			
		||||
  #   :just_empty2=>[]}
 | 
			
		||||
  #
 | 
			
		||||
  #
 | 
			
		||||
  #  Sentence.expand_syntax({      
 | 
			
		||||
  #    :factor => [["n"],
 | 
			
		||||
  #                ["(", :exp, ")"]],
 | 
			
		||||
  #    :term => [[:factor],
 | 
			
		||||
  #              [:term, "*", :factor],
 | 
			
		||||
  #              [:term, "/", :factor]],
 | 
			
		||||
  #    :exp => [[:term],
 | 
			
		||||
  #             [:exp, "+", :term],
 | 
			
		||||
  #             [:exp, "-", :term]]
 | 
			
		||||
  #  })
 | 
			
		||||
  #  #=>
 | 
			
		||||
  #  {:exp=> [["n"],
 | 
			
		||||
  #           ["(", :exp, ")"],
 | 
			
		||||
  #           [:exp, "+", :term],
 | 
			
		||||
  #           [:exp, "-", :term],
 | 
			
		||||
  #           [:term, "*", :factor],
 | 
			
		||||
  #           [:term, "/", :factor]],
 | 
			
		||||
  #   :factor=> [["n"],
 | 
			
		||||
  #              ["(", :exp, ")"]],
 | 
			
		||||
  #   :term=> [["n"],
 | 
			
		||||
  #            ["(", :exp, ")"],
 | 
			
		||||
  #            [:term, "*", :factor],
 | 
			
		||||
  #            [:term, "/", :factor]]
 | 
			
		||||
  #  }
 | 
			
		||||
  #
 | 
			
		||||
  def Sentence.expand_syntax(syntax)
 | 
			
		||||
    Sentence::Gen.expand_syntax(syntax)
 | 
			
		||||
  end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue