Changeset 3776:a4f2a72b483e in livinglogic.python.xist

Show
Ignore:
Timestamp:
05/10/09 18:00:15 (10 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add UL4 functions float() and iscolor().

Files:
4 modified

Legend:

Unmodified
Added
Removed
  • NEWS.rst

    r3775 r3776  
    3535*   The division operator is no longer implemented for :class:`Action` objects 
    3636    in :mod:`ll.make`. 
     37 
     38*   Two new UL4 functions have been added: ``float`` and ``iscolor``. 
    3739 
    3840*   A new script ``uls`` has been added. It can be used to list any directory 
  • docs/UL4.rst

    r3773 r3776  
    532532 
    533533``isdict`` 
    534 :::::::::::: 
     534:::::::::: 
    535535 
    536536``isdict(foo)`` returns ``True`` if ``foo`` is a dictionary object, else 
    537537``False`` is returned. 
     538 
     539 
     540``iscolor`` 
     541::::::::::: 
     542 
     543``iscolor(foo)`` returns ``True`` if ``foo`` is a color object, else ``False`` 
     544is returned. 
    538545 
    539546 
     
    552559case the first argument must be a string and the second is the number base for 
    553560the conversion. 
     561 
     562 
     563``float`` 
     564::::::: 
     565 
     566``float(foo)`` converts ``foo`` to a float. ``foo`` can be a string, a float, 
     567a boolean or an integer. 
    554568 
    555569 
  • src/ll/ul4c.py

    r3773 r3776  
    10281028    def _pythonsource_dispatch_callfunc1_int(self, opcode): 
    10291029        self._pythonsource_line(opcode.location, "r%d = int(r%d)" % (opcode.r1, opcode.r2)) 
     1030    def _pythonsource_dispatch_callfunc1_float(self, opcode): 
     1031        self._pythonsource_line(opcode.location, "r%d = float(r%d)" % (opcode.r1, opcode.r2)) 
    10301032    def _pythonsource_dispatch_callfunc1_bool(self, opcode): 
    10311033        self._pythonsource_line(opcode.location, "r%d = bool(r%d)" % (opcode.r1, opcode.r2)) 
     
    10471049        self._pythonsource_line(opcode.location, "r%d = isinstance(r%d, datetime.datetime)" % (opcode.r1, opcode.r2)) 
    10481050    def _pythonsource_dispatch_callfunc1_islist(self, opcode): 
    1049         self._pythonsource_line(opcode.location, "r%d = isinstance(r%d, (list, tuple))" % (opcode.r1, opcode.r2)) 
     1051        self._pythonsource_line(opcode.location, "r%d = isinstance(r%d, (list, tuple)) and not isinstance(r%d, color.Color)" % (opcode.r1, opcode.r2, opcode.r2)) 
    10501052    def _pythonsource_dispatch_callfunc1_isdict(self, opcode): 
    10511053        self._pythonsource_line(opcode.location, "r%d = isinstance(r%d, dict)" % (opcode.r1, opcode.r2)) 
    10521054    def _pythonsource_dispatch_callfunc1_istemplate(self, opcode): 
    10531055        self._pythonsource_line(opcode.location, "r%d = hasattr(r%d, '__call__')" % (opcode.r1, opcode.r2)) # this supports normal generators too 
     1056    def _pythonsource_dispatch_callfunc1_iscolor(self, opcode): 
     1057        self._pythonsource_line(opcode.location, "r%d = isinstance(r%d, color.Color)" % (opcode.r1, opcode.r2)) 
    10541058    def _pythonsource_dispatch_callfunc1_repr(self, opcode): 
    10551059        self._pythonsource_line(opcode.location, "r%d = ul4c._repr(r%d)" % (opcode.r1, opcode.r2)) 
  • test/test_ul4.py

    r3773 r3776  
    1414import py.test 
    1515 
    16 from ll import ul4c 
     16from ll import ul4c, color 
    1717 
    1818 
     
    597597 
    598598 
     599def test_function_float(): 
     600    checkrunerror("function u?'float' unknown", u"<?print float()?>") 
     601    checkrunerror("function u?'float' unknown", u"<?print float(1, 2, 3)?>") 
     602    checkrunerror("float\\(\\) argument must be a string or a number", u"<?print float(data)?>", data=None) 
     603    check("1.0", u"<?print float(data)?>", data=True) 
     604    check("0.0", u"<?print float(data)?>", data=False) 
     605    check("42.0", u"<?print float(data)?>", data=42) 
     606    check("4.2", u"<?print float(data)?>", data=4.2) 
     607    check("42.0", u"<?print float(data)?>", data="42") 
     608 
     609 
    599610def test_function_len(): 
    600611    checkrunerror("function u?'len' unknown", u"<?print len()?>") 
     
    639650    check("False", code, data={}) 
    640651    check("False", code, data=ul4c.compile(u"")) 
     652    check("False", code, data=color.red) 
    641653 
    642654 
     
    656668    check("False", code, data={}) 
    657669    check("False", code, data=ul4c.compile(u"")) 
     670    check("False", code, data=color.red) 
    658671 
    659672 
     
    673686    check("False", code, data={}) 
    674687    check("False", code, data=ul4c.compile(u"")) 
     688    check("False", code, data=color.red) 
    675689 
    676690 
     
    690704    check("False", code, data={}) 
    691705    check("False", code, data=ul4c.compile(u"")) 
     706    check("False", code, data=color.red) 
    692707 
    693708 
     
    707722    check("False", code, data={}) 
    708723    check("False", code, data=ul4c.compile(u"")) 
     724    check("False", code, data=color.red) 
    709725 
    710726 
     
    724740    check("False", code, data={}) 
    725741    check("False", code, data=ul4c.compile(u"")) 
     742    check("False", code, data=color.red) 
    726743 
    727744 
     
    741758    check("False", code, data={}) 
    742759    check("False", code, data=ul4c.compile(u"")) 
     760    check("False", code, data=color.red) 
    743761 
    744762 
     
    758776    check("True", code, data={}) 
    759777    check("False", code, data=ul4c.compile(u"")) 
     778    check("False", code, data=color.red) 
    760779 
    761780 
     
    775794    check("False", code, data={}) 
    776795    check("True", code, data=ul4c.compile(u"")) 
     796    check("False", code, data=color.red) 
     797 
     798 
     799def test_function_iscolor(): 
     800    checkrunerror("function u?'iscolor' unknown", u"<?print iscolor()?>") 
     801    checkrunerror("function u?'iscolor' unknown", u"<?print iscolor(1, 2)?>") 
     802    code = u"<?print iscolor(data)?>" 
     803    check("False", code, data=None) 
     804    check("False", code, data=True) 
     805    check("False", code, data=False) 
     806    check("False", code, data=42) 
     807    check("False", code, data=4.2) 
     808    check("False", code, data="foo") 
     809    check("False", code, data=datetime.datetime.now()) 
     810    check("False", code, data=()) 
     811    check("False", code, data=[]) 
     812    check("False", code, data={}) 
     813    check("False", code, data=ul4c.compile(u"")) 
     814    check("True", code, data=color.red) 
    777815 
    778816