class ScriptParser(object):
    mapping = {'^': '**', '!': ' not ', '&': ' and '}

def to_python(self, calc):
    """
        Parse the calculation syntax from the script grammar to the python one.
        This could be grown to a more complex parser, if needed. For now it will
        simply assume any operator as defined in the grammar used for the script
        has an equivalent in python.
        """
for k, v in list(self.mapping.items()):
    calc = calc.replace(k, v)
return calc

def feed(self, lfs):
    """
        Parse a line of the script containing a function defintion
        """
signature, calc = lfs.split(':=')
funcname, variables = [s.strip() for s in signature.split('(')]
variables = variables[:-1]
setattr(self, funcname, eval('lambda ' + variables + ' : ' + self.to_python
    (calc)))

def main():
    lines = ['f0(x, y, z) := x^2', 'f1(x) := x**2 + x**3 + x*1000']
sp = ScriptParser()
for line in lines:
    sp.feed(line)
print('Script definition  : %s' % line)
for i in range(5):
    res0 = sp.f0(i, None, None)
res1 = sp.f1(i)
print('f0(%d) = %d' % (i, res0))
print('f1(%d) = %d' % (i, res1))
print('--------')
if __name__ == '__main__':
    main()
Реализация функций с очень простым скриптом