Commits

Lucian Brănescu-Mihăilă committed 9032260

Implement dict(), dict({'one':1, 'two':2}) and dict([ ('one',1), ('two',2) ]). Still left to implement dict(one=1, two=2).

Comments (0)

Files changed (1)

src/builtins/funcs.pir

 
 =cut
 
-.sub 'dict'
-    .param pmc sequence :optional
-    .param pmc mapping :slurpy :named :optional
 
+.sub 'dict' :multi()
+    $P0 = new 'dict'
+    .return ($P0)
+.end
+
+.sub 'dict' :multi('dict')
+    .param pmc dict
+    $P0 = clone dict
+    .return ($P0)
+.end
+
+.sub 'dict' :multi('list')
+    .param pmc sequence
+
+    .local pmc result, iterator
+
+    result = new 'dict'
+    iterator = iter sequence
+
+loop:
+    unless iterator goto done
+    
+    $P0 = shift iterator
+    $I0 = 'len'($P0)
+    unless $I0 == 2 goto type_error
+    # unpack the 2 element tuple
+    $P1 = $P0.'shift'()
+    $P2 = $P0.'shift'()
+    
+    # put entry in dict
+    result[$P1] = $P2
+
+    goto loop
+
+done:
+    .return (result)
+
+type_error:
     $P0 = new 'Exception'
-    $P0 = 'NotImplementedError'      # XXX
+    $P0 = 'TypeError'
     throw $P0
 .end