Exemplo n.º 1
0
Arquivo: expr.py Projeto: zr8091/fun
 def __str__(self):
     function = str(self.operator)
     args = '(' + comma_separated(self.operands) + ')'
     if isinstance(self.operands, LambdaExpr):
         return '(' + function + ')' + args
     else:
         return function + args
Exemplo n.º 2
0
Arquivo: expr.py Projeto: zr8091/fun
 def apply(self, arguments):
     """
     将参数绑定到lambda 的 变量中。
     :param arguments:
     :return:
     """
     if len(self.parameters) != len(arguments):
         raise TypeError(
             "Cannot match parameters {} to arguments {}".format(
                 comma_separated(self.parameters), arguments))
     env = self.parent.copy()
     for p, a in zip(self.parameters, arguments):
         env[p] = a
     return self.body.eval(env)
Exemplo n.º 3
0
Arquivo: expr.py Projeto: zr8091/fun
 def __str__(self):
     body = str(self.body)
     if not self.parameters:
         return 'lambda: ' + body
     else:
         return 'lambda ' + comma_separated(body)
Exemplo n.º 4
0
Arquivo: expr.py Projeto: zr8091/fun
 def __repr__(self):
     args = '(' + comma_separated([repr(a) for a in self.args]) + ')'
     return type(self).__name__ + args
Exemplo n.º 5
0
Arquivo: expr.py Projeto: zr8091/fun
 def apply(self, arguments):
     for a in arguments:
         if not isinstance(a, Number):
             raise TypeError("Invalid argument {} to {}".format(
                 comma_separated(arguments), self))
     return Number(self.operator(*[a.value for a in arguments]))
Exemplo n.º 6
0
Arquivo: expr.py Projeto: zr8091/fun
 def apply(self, arguments):
     raise TypeError("Cannot apply number{} to arguments {}".format(
         self.value, comma_separated(arguments)))