Commits

isop44  committed b6c4368

Add simple str class and some tests

  • Participants
  • Parent commits c711819

Comments (0)

Files changed (8)

File Grammar/Actions.nqp

 }
 
 method shortstring($/) {
-    make PAST::Val.new( :value( ~$/[0] ), :node($/) );
+    make PAST::Val.new( :value( ~$/ ), :returns('str'), :node($/) );
 }
 
 method parenth_form($/) {

File Grammar/Grammar.pg

 }
 
 token shortstring {
-    [
-    | \" ( [ \\ . | <-["\n]> ]* ) \"
-    | \' ( [ \\ . | <-['\n]> ]* ) \'
-    ]
-    {*}
+    | \" ( [ \\ . | <-["\n]> ]* ) \"  {*}
+    | \' ( [ \\ . | <-['\n]> ]* ) \'  {*}
 }
 
 regex longstring {

File Lib/test/parrot/class-str.py

+#!./parrot
+
+print('test: str.split')
+if 'foobar'.split('o') == [ 'f', '', 'bar' ]:
+    print('success: str.split')
+else:
+    print('failure: str.split')
+
+print('test: str.join')
+if '---'.join(['hello', 'world']) == 'hello---world':
+    print('success: str.join')
+else:
+    print('failure: str.join')

File Lib/test/parrot/literals.py

 #3.14e-10j
 #1J
 print('success: imaginary literals')
+
+
+print('test: short string literals')
+'foo" '
+"Foo' "
+'\1\2\3'
+"\4\5\6"
+'\x5'
+print('success: short string literals')
+
+print('test: long string literals')
+#'''foo'''
+#"""foo"""
+#'''\x8'''
+print('xfail: long string literals')
     def test_class_list(self):
         """pynie.pbc Lib/test/parrot/class-list.py"""
 
+    def test_class_str(self):
+        """pynie.pbc Lib/test/parrot/class-str.py"""
+
     def test_grammar(self):
         """pynie.pbc Lib/test/parrot/grammar.py"""
 
   'src/builtins/io.pir', 
   'src/builtins/lists.pir', 
   'src/builtins/oper.pir',
-  'src/classes/pynielist.pir' ]
+  'src/classes/pynielist.pir',
+  'src/classes/pyniestr.pir'
+  ]
 
 CLEANUPS = GEN_SOURCES + [
   'pynie.pbc',

File src/builtins/funcs.pir

     if i < 0 goto valueerror
     if i > 255 goto valueerror
     .local pmc result
-    result = new 'String'
+    result = new [ 'str' ]
     $S0 = chr i
     result = $S0
     .return (result)
     .tailcall obj.'__repr__'()
 
 no_repr:
-    $I0 = isa obj, 'String'     # XXX use __repr__
-    unless $I0 goto not_a_string
-    result = "'"
-    $S0 = obj
-    result .= $S0
-    result .= "'"
-    .return (result)
-
-not_a_string:
     result = obj
     .return (result)
 .end
 .sub 'str'
     .param pmc obj :optional
     .param int has_obj :opt_flag
-    $P0 = new 'String'
+    $P0 = new [ 'str' ]
     unless has_obj goto done
     $S0 = obj
     $P0 = $S0

File src/classes/pyniestr.pir

+.sub '' :anon :init :load
+    $P0 = subclass 'String', [ 'str' ]
+
+    $P2 = get_class 'String'
+    $P1 = getinterp
+    $P1.'hll_map'($P2, $P0)
+.end
+
+
+.namespace [ 'str' ]
+
+
+.sub '__repr__' :method
+    .local string result
+    result = "'"
+    
+    # XXX escaping
+    $S0 = self
+    result .= $S0
+    
+    result .= "'"
+    .return (result)
+.end
+
+.sub 'split' :method
+    .param pmc sep          :optional :named('sep')
+    .param int has_sep      :opt_flag
+    .param int maxsplit     :optional :named('maxsplit')
+    .param int has_maxsplit :opt_flag
+
+    unless has_sep goto not_yet
+    if has_maxsplit goto not_yet
+
+    $S0 = sep
+    $S1 = self
+    $P0 = split $S0, $S1
+    # convert from ResizableStringArray to Pynie list
+    .tailcall 'listmaker'($P0 :flat)
+
+not_yet:
+    $P0 = new 'Exception'
+    $P0 = 'NotImplementedError'         # XXX
+    raise ($P0)
+.end
+
+.sub 'join' :method
+    .param pmc seq
+    $S0 = self
+    $S1 = join $S0, seq
+    .return ($S1)
+.end
+
+.namespace []