Array of functions, Pythonically

M

MartinRinehart

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?
 
D

Duncan Booth

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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)
 
C

castironpi

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

Then EXPR OPER EXPR -> ops[ OPER ]( EXPR, EXPR ), right?
 
P

Paul McGuire

 { '+': 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top