1. allison
  2. pynie

Commits

blairuk  committed 5a03fba

Applied patch from isop44++; adds cmp, max, min, ord, pow.
Also added fast algorithm for pow(a,x,y).

  • Participants
  • Parent commits aac405e
  • Branches default

Comments (0)

Files changed (2)

File Lib/test/parrot/builtins.py

View file
 #!./parrot pynie.pbc
+# -*- coding: utf-8 -*-
 
 #len
 print 'test: len'
 print 'test: len/str'
 if len(str(861)) == 3: print 'success: len/str'
 else: print 'failure: len/str'
+
+#cmp
+print 'test: cmp (equal)'
+if cmp(5, 5) == 0: print 'success: cmp (equal)'
+else: print 'failure: cmp (equal)'
+
+print 'test: cmp (gt)'
+if cmp(5, 0) > 0: print 'success: cmp (gt)'
+else: print 'failure: cmp (gt)'
+
+print 'test: cmp (lt)'
+if cmp(0, 5) < 0: print 'success: cmp (lt)'
+else: print 'failure: cmp (lt)'
+
+#max
+print 'test: max (simple)'
+if max(range(5)) == 4: print 'success: max (simple)'
+else: print 'failure: max (simple)'
+
+print 'test: max (mixed)'
+if max([1,98,3,8,32,8]) == 98: print 'success: max (mixed)'
+else: print 'failure: max (mixed)'
+
+#min
+print 'test: min (simple)'
+if min(range(5)) == 0: print 'success: min (simple)'
+else: print 'failure: min (simple)'
+
+print 'test: min (mixed)'
+if min([98,1,3,8,32,8]) == 1: print 'success: min (mixed)'
+else: print 'failure: min (mixed)'
+
+#ord
+print 'test: ord'
+if ord('a') == 97: print 'success: ord'
+else: print 'failure: ord'
+
+#pow
+print 'test: pow (2 args)'
+if pow(2,3) == 8: print 'success: pow (2 args)'
+else: print 'failure: pow (2 args)'
+
+print 'test: pow (3 args)'
+if pow(2,3,3) == 2: print 'success: pow (3 args)'
+else: print 'failure: pow (3 args)'
+
+#sum
+print 'test: sum'
+if sum(range(5)) == 10: print 'success: sum'
+else: print 'failure: sum'

File src/builtins/funcs.pir

View file
 .sub 'cmp'
     .param pmc x
     .param pmc y
+	$I0 = cmp x, y
+	$P0 = new 'Integer'
+	$P0 = $I0
+	.return ($P0)
 .end
 
 
 .sub 'isinstance'
     .param pmc obj
     .param pmc classinfo
+    
+    $I0 = isa obj, classinfo
+    .return ($I0)
 .end
 
 
 
 .sub 'max'
     .param pmc s
-    .param pmc args :optional
-    .param pmc key :optional
+    .param pmc args :slurpy
+	#.param pmc key :named :optional
+
+	.local pmc iter, result
+	
+	$I0 = elements args
+	if $I0 > 0 goto multiple_args
+	
+	iter = new 'Iterator', s
+	result = shift iter
+	goto loop_check
+
+multiple_args:
+	iter = new 'Iterator', args
+	result = s
+
+loop_check:
+	.local pmc i
+	unless iter goto loop_end
+	i = shift iter
+	unless i > result goto loop_check
+	set result, i
+	goto loop_check
+
+loop_end:
+	.return (result)
 .end
 
 
 
 .sub 'min'
     .param pmc s
-    .param pmc args :optional
-    .param pmc key :optional
+    .param pmc args :slurpy
+	#.param pmc key :named :optional
+
+	.local pmc iter, result
+
+	$I0 = elements args
+	if $I0 > 0 goto multiple_args
+
+	iter = new 'Iterator', s
+	result = shift iter
+	goto loop_check
+
+multiple_args:
+	iter = new 'Iterator', args
+	result = s
+
+loop_check:
+	.local pmc i
+	unless iter goto loop_end
+	i = shift iter
+	unless i < result goto loop_check
+	set result, i
+	goto loop_check
+
+loop_end:
+	.return (result)
 .end
 
+
 =item object()
 
 Return a new featureless object. object is a base for all new style classes.
 
 .sub 'ord'
     .param pmc c
+	
+	$S0 = c
+	$I0 = length $S0
+	if $I0 != 1 goto typeerror 
+	$I1 = ord $S0
+	
+    .return ($I1)
+
+  typeerror:
+    $P0 = new 'Exception'
+    $P0 = "TypeError"
+    throw $P0
+
 .end
 
 
     .param pmc x
     .param pmc y
     .param pmc z :optional
+    .param int has_z :opt_flag
+
+	if has_z goto when_has_z
+    
+	$P0 = pow x, y
+	.return ($P0)
+
+when_has_z:
+
+    # use a fast exponentiation method
+    .local int X, Y, Z
+    X = x
+    Y = y
+    Z = z
+    
+    $I0 = 1
+    loop:
+        unless Y > 0 goto endloop
+        $I1 = band Y, 1
+        bitcheck:
+            unless $I1 == 1 goto endbitcheck
+            $I1 = $I0 * X
+            $I0 = mod $I1, Z
+        endbitcheck:
+        
+        Y = shr Y, 1
+        $I1 = X * X
+        X = mod $I1, Z
+        
+        goto loop
+    endloop:
+
+	.return ($I0)
 .end
 
 
 .sub 'sum'
     .param pmc sequence
     .param pmc start :optional
+	.param int has_start :opt_flag
+	
+	.local num result
+	unless has_start goto no_start
+	result = start
+	goto setup_loop
+no_start:
+	result = 0
+
+setup_loop:
+    .local pmc iter
+    iter = new 'Iterator', sequence
+
+    .local int i
+loop_check:
+    unless iter goto loop_end
+    i = shift iter
+	result += i
+    goto loop_check
+
+loop_end:
+    .return (result)
 .end
 
 =item super(type[, object-or-type])