Commits

Anonymous committed 90160f5

Add 'type' type as a functor with a nice string representation. Make the 'str' and 'list' classes instances of this functor. (Though str/list instances are not instances of the 'type' objects yet.)

Comments (0)

Files changed (6)

     def test_class_str(self):
         """Lib/test/bootstrap/class-str.py"""
 
+    def test_class_type(self):
+        """Lib/test/bootstrap/class-type.py"""
+
     def test_grammar(self):
         """Lib/test/bootstrap/grammar.py"""
 
   'src/builtins/io.pir', 
   'src/builtins/lists.pir', 
   'src/builtins/oper.pir',
+  'src/classes/pynietype.pir',
   'src/classes/pynielist.pir',
-  'src/classes/pyniestr.pir'
+  'src/classes/pyniestr.pir',
   ]
 
 CLEANUPS = GEN_SOURCES + [

src/builtins/funcs.pir

 .end
 
 
-=item list([sequence])
-
-Return a list whose items are the same and in the same order as sequence's items.
-sequence may be either a sequence, a container that supports iteration, or an
-iterator object. If sequence is already a list, a copy is made and returned,
-similar to sequence[:]. For instance, list('abc') returns ['a', 'b', 'c'] and
-list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new
-empty list, [].
-
-=cut
-
-.sub 'list'
-    .param pmc sequence :optional
-    $P0 = 'listmaker'(sequence :flat)
-    .return ($P0)
-.end
-
 =item locals()
 
 Update and return a dictionary representing the current local symbol table.
 .end
 
 
-=item str([object])
-
-Return a string containing a nicely printable representation of an object.
-For strings, this returns the string itself. The difference with
-repr(object) is that str(object) does not always attempt to return
-a string that is acceptable to eval(); its goal is to return a
-printable string. If no argument is given, returns the empty string, ''.
-
-=cut
-
-.sub 'str'
-    .param pmc obj :optional
-    .param int has_obj :opt_flag
-    $P0 = new [ 'str' ]
-    unless has_obj goto done
-    $S0 = obj
-    $P0 = $S0
-done:
-    .return ($P0)
-.end
-
 =item sum(sequence[, start])
 
 Sums start and the items of a sequence, from left to right, and returns the

src/classes/pynielist.pir

 .sub '' :anon :init :load
     $P0 = subclass 'ResizablePMCArray', [ 'list' ]
 
+    $P1 = get_global [ 'type' ], 'wrap_class'
+    $P1('list', $P0)
+
     $P2 = get_class 'ResizablePMCArray'
     $P1 = getinterp
     $P1.'hll_map'($P2, $P0)
 .namespace [ 'list' ]
 
 
+=item list([sequence])
+
+Return a list whose items are the same and in the same order as sequence's items.
+sequence may be either a sequence, a container that supports iteration, or an
+iterator object. If sequence is already a list, a copy is made and returned,
+similar to sequence[:]. For instance, list('abc') returns ['a', 'b', 'c'] and
+list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, returns a new
+empty list, [].
+
+=cut
+
+.sub '__init__' :method
+    .param pmc sequence :optional
+    .param int has_seq :opt_flag
+
+    unless has_seq goto no_seq
+    self.'extend'(sequence)
+  no_seq:
+    # do nothing, just an empty list
+.end
+
 .sub 'get_string' :vtable
     .local pmc iterator
     .local string result

src/classes/pyniestr.pir

 .sub '' :anon :init :load
     $P0 = subclass 'String', [ 'str' ]
 
+    $P1 = get_global [ 'type' ], 'wrap_class'
+    $P1('str', $P0)
+    
     $P2 = get_class 'String'
     $P1 = getinterp
     $P1.'hll_map'($P2, $P0)
 .namespace [ 'str' ]
 
 
+=item str([object])
+
+Return a string containing a nicely printable representation of an object.
+For strings, this returns the string itself. The difference with
+repr(object) is that str(object) does not always attempt to return
+a string that is acceptable to eval(); its goal is to return a
+printable string. If no argument is given, returns the empty string, ''.
+
+=cut
+
+.sub '__init__' :method
+    .param pmc obj :optional
+    .param int has_obj :opt_flag
+    
+    unless has_obj goto done
+    $S0 = obj
+    self = $S0
+  done:
+.end
+
 .sub '__repr__' :method
     .local string result
     result = "'"

src/classes/pynietype.pir

+.sub '' :anon :init
+    # setup type Class
+    $P0 = newclass [ 'type' ]
+    addattribute $P0, '_class'
+
+    # XXX there should be a type object wrapping the type class
+.end
+
+.namespace [ 'type' ]
+
+.sub 'make_type_object'
+    .param pmc _class
+
+    .local pmc init_args
+    init_args = new 'Hash'
+    init_args['_class'] = _class
+
+    $P1 = new [ 'type' ], init_args
+
+    .return ($P1)
+.end
+
+.sub 'wrap_class'
+    .param string name
+    .param pmc _class
+    
+    .local pmc make_type_object
+    make_type_object = get_global 'make_type_object'
+    $P0 = make_type_object(_class)
+    
+    set_hll_global name, $P0
+.end
+
+.sub 'invoke' :vtable
+    .param pmc args :slurpy
+    .param pmc kwargs :named :slurpy
+
+    $P0 = getattribute self, '_class'
+    # XXX $P1 should be an instance of self
+    $P1 = new $P0
+
+    $I0 = can $P1, '__init__'
+    unless $I0 goto done
+    $P1.'__init__'(args :flat, kwargs :flat)
+
+done:
+    .return ($P1)
+.end
+
+.sub 'get_string' :vtable
+    $P0 = getattribute self, '_class'
+    $S0 = $P0.'name'()
+
+    $S1 = "<class '"
+    $S1 .= $S0
+    $S1 .= "'>"
+
+    .return ($S1)
+.end
+
+.namespace []