def pairs_of_obj(args, context): o = args["obj"] pairs = [] for (key, value) in o.pairs.items(): pairs.append(obj.Tuple([key, value])) return obj.Array(pairs)
def start_to_end(args, context): start = args["start"] end = args["end"] if not start.is_integer(): return err("$start in `$start to $end` must be an integer") if not end.is_integer(): return err("$end in `$start to $end` must be an integer") s_val = int(start.value) e_val = int(end.value) if e_val < s_val: result = obj.Array([obj.Number(e + 1) for e in range(e_val, s_val)]) result.elements.reverse() return result elif e_val > s_val: return obj.Array([obj.Number(e) for e in range(s_val, e_val)]) else: return start
def evaluate(node, ctx): t = type(node) # Constructs if t == ast.Program: return eval_program(node, ctx) if t == ast.BlockStatement: return eval_block_stmt(node, ctx) if t == ast.ExpressionStatement: return evaluate(node.expr, ctx) if t == ast.IfExpression: return eval_if(node, ctx) if t == ast.WhileLoop: return eval_while_loop(node, ctx) if t == ast.ForLoop: return eval_for_loop(node, ctx) # Literals if t == ast.Null: return NULL if t == ast.Number: return obj.Number(node.value) if t == ast.String: return obj.String(node.value) if t == ast.Char: return obj.Char(node.value) if t == ast.Boolean: return bool_obj(node.value) if t == ast.Identifier: return eval_id(node, ctx) if t == ast.BlockLiteral: return eval_block(node, ctx) if t == ast.NextStatement: return NEXT if t == ast.BreakStatement: return BREAK # Functions if t == ast.FunctionDefinition: return eval_function_def(node, ctx) if t == ast.FunctionCall: return eval_function_call(node, ctx) if t == ast.Array: elements = eval_exprs(node.elements, ctx) if len(elements) == 1 and is_err(elements[0]): return elements[0] return obj.Array(elements) if t == ast.Object: keys = eval_exprs(node.pairs.keys(), ctx) if len(keys) == 1 and is_err(keys[0]): return keys[0] values = eval_exprs(node.pairs.values(), ctx) if len(values) == 1 and is_err(values[0]): return values[0] return obj.Object(list(zip(keys, values))) if t == ast.Tuple: elements = eval_exprs(node.value, ctx) if len(elements) == 1 and is_err(elements[0]): return elements[0] return obj.Tuple(elements) # More complex nodes if t == ast.ReturnStatement: if node.value == None: return obj.ReturnValue(NULL) val = evaluate(node.value, ctx) return val if is_err(val) else obj.ReturnValue(val) if t == ast.PrefixExpression: right = evaluate(node.right, ctx) return right if is_err(right) else eval_prefix(node.operator, right) if t == ast.InfixExpression: left = evaluate(node.left, ctx) if is_err(left): return left right = evaluate(node.right, ctx) if is_err(right): return right return eval_infix(node.operator, left, right, ctx) if t == ast.AssignExpression: right = evaluate(node.value, ctx) return right if is_err(right) else eval_assign(node.name, right, ctx) if t == ast.DeclareExpression: right = evaluate(node.value, ctx) return right if is_err(right) else eval_declare(node.name, right, ctx) return err("evaluation for %s not yet implemented" % t)
def indices_of_arr(args, context): collection = args["collection"].get_elements() result = [obj.Number(i) for i in range(len(collection))] return obj.Array(result)
def values_of_obj(args, context): return obj.Array(args["obj"].pairs.values())
def keys_of_obj(args, context): return obj.Array(args["obj"].pairs.keys())