Commits

isop44  committed bc76be0

Add hex, and make sure assert is on for relevant tests

  • Participants
  • Parent commits 9aac607

Comments (0)

Files changed (2)

File Lib/test/parrot/builtins.py

 #!./parrot pynie.pbc
 # -*- coding: utf-8 -*-
 
+# turn on debug so assert works
+__debug__ = 1     # XXX not valid in real Python.
+
 #len
 print('test: len')
 n = len([1,2,3,4])
     print('success: map (python func)')
 except:
     print('failure: map (python func)')
+
+#hex
+print('test: hex')
+try:
+    a = [ 0, 5, -256, 1000 ]
+    b = [ '0x0', '0x5', '-0x100', '0x3e8' ]
+    res = map(hex, a)
+    for i in range(4):
+        assert res[i] == b[i]
+    print('success: hex')
+except:
+    print('failure: hex')

File src/builtins/funcs.pir

 .sub 'hex'
     .param pmc x
 
-    $P0 = new 'Exception'
-    $P0 = 'NotImplementedError'      # XXX
-    throw $P0
+    .local int digit
+    .local string digit_str, result, prefix
+    
+    $I0 = x
+    
+    unless $I0 == 0 goto is_nonzero
+    .return ('0x0')
+
+is_nonzero:
+    result = ''
+    unless $I0 < 0 goto is_positive
+    # is negative
+    prefix = '-0x'
+    $I0 = -$I0
+    goto loop
+
+is_positive:
+    prefix = '0x'
+
+loop:
+    digit = $I0 & 0xf
+    $I0 >>= 4
+    
+    digit_str = substr '0123456789abcdef', digit, 1
+    result = digit_str . result
+    if $I0 > 0 goto loop
+
+    result = prefix . result
+    .return (result)
 .end