FAQ
My parser has found an expression of the form CONSTANT_INTEGER
OPERATOR CONSTANT_INTEGER. I want to fold this into a single
CONSTANT_INTEGER.

The OPERATOR token has an intValue attribute, '+' == 0, '-'== 1, etc.
In C I'd put functions Add, Subtract, ... into an array and call
ArithmeticFunctions[ intValue ] to perform the operation. Would I
index into a list of functions in Python, or would IF/ELIF/ELIF ... be
the way to go?

Search Discussions

  • Duncan Booth at Feb 25, 2008 at 2:56 pm

    MartinRinehart at gmail.com wrote:

    My parser has found an expression of the form CONSTANT_INTEGER
    OPERATOR CONSTANT_INTEGER. I want to fold this into a single
    CONSTANT_INTEGER.

    The OPERATOR token has an intValue attribute, '+' == 0, '-'== 1, etc.
    In C I'd put functions Add, Subtract, ... into an array and call
    ArithmeticFunctions[ intValue ] to perform the operation. Would I
    index into a list of functions in Python, or would IF/ELIF/ELIF ... be
    the way to go?
    Well, you could add another attribute to the OPERATOR token:

    token.operation = operator.add

    or you could have an a list of operations keyed in token.intValue:

    [ operator.add, operator.sub, ...]

    or you could use a dictionary (that intValue attribute is just so C):

    { '+': operator.add, '-': operator.sub, ... }

    I don't know the structure of your program, but I'd go for subclassing
    your operator. So you would have classes OperatorAdd, OperatorSub, etc.
    and then you can make the operation a method of the class. Seomthing
    like:

    class Token(object): ...
    def foldConstants(self): pass

    class Operator(Token): ...
    def foldConstants(self):
    self.left.foldConstants()
    self.right.foldConstants()
    if self.left.isConstant and self.right.isConstant:
    self.value, self.isConstant = self.doFold()


    class OperatorAdd(Operator): ...
    def doFold(self):
    return self.left.value + self.right.value, True
  • Castironpi at Feb 25, 2008 at 5:53 pm
    ?{ '+': operator.add, '-': operator.sub, ... }
    Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?
  • Paul McGuire at Feb 25, 2008 at 6:03 pm

    On Feb 25, 11:53?am, castiro... at gmail.com wrote:
    ?{ '+': operator.add, '-': operator.sub, ... }
    Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?
    I think this is the most Pythonic idiom. You can even define your own
    custom binary operators, such as '$' to convert dollars and cents to a
    float:
    ops = { '+': operator.add, '-': operator.sub, '$' : lambda a,b: a + b/100.0 }
    ops['$'](1,2) # convert dollars and cents to float
    1.02

    -- Paul
  • Bruno Desthuilliers at Feb 25, 2008 at 4:16 pm

    MartinRinehart at gmail.com a ?crit :
    My parser has found an expression of the form CONSTANT_INTEGER
    OPERATOR CONSTANT_INTEGER. I want to fold this into a single
    CONSTANT_INTEGER.

    The OPERATOR token has an intValue attribute, '+' == 0, '-'== 1, etc.
    In C I'd put functions Add, Subtract, ... into an array and call
    ArithmeticFunctions[ intValue ] to perform the operation. Would I
    index into a list of functions in Python, or would IF/ELIF/ELIF ... be
    the way to go?
    Python functions are first class objects, so you can indeed have a list
    (or dict etc) of functions:

    funcs = [lambda x : x+1, lambda x : x * 42]
    funcs[0](1)
    funcs[1](1)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 25, '08 at 2:22p
activeFeb 25, '08 at 6:03p
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase