def items(self): final = _List(self.evaluator, self.context, []) for key, value in self.value.items(): final.append( _List(self.evaluator, self.context, [ create_token(self.context, BasicToken, ClassInstance, key), create_token(self.context, BasicToken, ClassInstance, value) ])) return final
def append(self, *items): extend_by = [] for item in items: extend_by.append( create_token(self.context, BasicToken, ClassInstance, item)) self.value.extend(extend_by) self.length += len(items)
def evaluate_op(op, context): if not isinstance(op, list): op = [op] # to avoid multiple getitems first = op[0] if hasattr(first, 'primary_type') and first.type == FCALL: function_response = first.execute(context) function_response_as_token = create_token(context, BasicToken, ClassInstance, function_response, unary=first.unary, exclam=first.exclam) process_token_exclam(function_response_as_token) apply_token_unary(function_response_as_token) return function_response_as_token elif len(op) == 1: if first.primary_type == PARENTHESIS: first = evaluate(first.value, context, return_token=True) return process_token(first, context) left, op, right = op if op.type == POWER: return evaluate_pow(left, right, context) left = process_token(left, context).value right = process_token(right, context).value executor = executors[op.value] result = executor(left, right) return create_token(context, BasicToken, ClassInstance, result)
def evaluate_pow(left, right, context): post_unary = None if left.primary_type == PARENTHESIS: left_value = evaluate(left.value, context) else: post_unary = left.unary left_value = process_token(left, context).value right_value = process_token(right, context).value result = left_value**right_value if isinstance(result, complex): result = result.imag if post_unary is not None: result = -result if post_unary == '-' else +result return create_token(context, BasicToken, ClassInstance, result)
def execute(self, context): value = self.value if hasattr(value, 'execute'): value = value.execute(context) if not hasattr(value, 'primary_type') and not isinstance( value, ModuleType): value = create_token(context, BasicToken, ClassInstance, value) if hasattr(value, 'primary_type') and value.type == MATHEXPR: value = self.evaluator(value.value, context, return_token=True) if hasattr(self.name, 'primary_type') and self.name.type == TUPLE: if value.type not in (LIST, TUPLE): raise TypeError('only lists and tuples can be unpacked') to_assign = zip(self.name.value, value.value) else: to_assign = [[self.name, value]] for var, val in to_assign: if var.type != VARIABLE: raise SyntaxError('assigning value to non-variable') elif hasattr(val, 'primary_type') and val.type == MATHEXPR: val = val.value if hasattr(val, 'primary_type') and val.type in (CLASSASSIGN, CLASSINSTANCE): result = val else: if not isinstance(val, list): val = [val] result = self.evaluator(val, context) context[var.value] = result