Beispiel #1
0
 def apply(self, args, call_env):
     if len(args) != 1:
         raise Exception("Arity error")
     tgt = args[0].evaluate(call_env)
     if not isinstance(tgt, types.List):
         raise SyntaxError("rest only accepts a list")
     return types.List(*tgt.elements[1:])
Beispiel #2
0
 def apply(self, args, call_env):
     if len(args) != 2:
         raise Exception("Arity error")
     elem = args[0].evaluate(call_env)
     tgt = args[1].evaluate(call_env)
     if not isinstance(tgt, types.List):
         raise SyntaxError("the second argument of cons must be a list")
     return types.List(elem, *tgt.elements)
Beispiel #3
0
 def backquote_evaluate(self, expr, env):
     if isinstance(expr, types.List):
         if len(expr) > 0:
             if (isinstance(expr.elements[0], types.Symbol) and
                     isinstance(env.get_form(expr.elements[0]), UnQuoteForm)):
                 return expr.evaluate(env)
         ret = []
         for e in expr.elements:
             ret.append(self.backquote_evaluate(e, env))
         return types.List(*ret)
     return expr
Beispiel #4
0
 def parse_list(self, token):
     lst = []
     next_token = self.get_token()
     if next_token is None:
         raise self.ParseError("Expected end of list before end of input")
     while next_token.type != PLispTokens.END_EXPR:
         lst.append(self.parse_expression(next_token))
         next_token = self.get_token()
         if next_token is None:
             raise self.ParseError(
                 "Expected end of list before end of input")
     return types.List(*lst)
Beispiel #5
0
 def apply(self, args, call_env):
     res = types.List()
     for expr in args:
         res = expr.evaluate(call_env)
     return res
Beispiel #6
0
 def apply(self, args, call_env):
     if len(args) == 0:
         return types.List()
     return self.backquote_evaluate(args[0], call_env)
Beispiel #7
0
 def apply(self, args, call_env):
     if len(args) == 0:
         return types.List()
     return args[0].evaluate(call_env)
Beispiel #8
0
 def apply(self, args, call_env):
     string = ' '.join([str(a.evaluate(call_env)) for a in args])
     print(string)
     return types.List()
Beispiel #9
0
 def apply(self, args, call_env):
     return types.List(*[e.evaluate(call_env) for e in args])
Beispiel #10
0
 def parse_unquote(self, token):
     body = self.parse_expression(self.get_token())
     if body is None:
         raise self.ParseError("Invalid body for unquote")
     return types.List(types.Symbol("unquote"), body)