def run_block(args, context): block = args["block"] if type(block) != obj.Block: return err("the $block parameter in `run $block` must be of type <block>") if len(block.params) > 0: return err("since no arguments are provided, $block of `run $block` must have no parameters") ctx = context.enclose() return evaluate(block.body, ctx)
def run_block(args, context): block = args["block"] if type(block) != obj.Block: return err( "the $block parameter in `run $block` must be of type <block>") if len(block.params) > 0: return err( "since no arguments are provided, $block of `run $block` must have no parameters" ) ctx = context.enclose() return evaluate(block.body, ctx)
def new_fn(args, context): if not isinstance(args[name], expected_type): return err( context, "the $%s parameter must be of type %s, not %s" % (name, expected_type.t, args[name].type), "TypeError") return fn(args, context)
def format_string_with_args(args, context): fmt = args["format"].value items = tuple(args["args"].get_elements()) try: return obj.String(fmt % items) except TypeError: return err("Wrong number of arguments to format `%s`" % fmt)
def new_fn(args, context): if isinstance(args[name].type, expected_type): return err( "the $%s parameter in `%s` must be of type %s, not %s" % (name, getattr(fn, "pattern"), expected_type, args[name].type)) return fn(args, context)
def key_of_obj(args, context): key = args["key"] obj = args["obj"] if key not in obj.pairs.keys(): return err("key %s not found" % key) return obj.pairs[key]
def index_i_of_array(args, context): i = args["i"] array = args["array"] if not i.is_integer() or not i.is_positive() or not int(i.value) < len(array.get_elements()): return err("invalid index: %s" % i) return array.get_elements()[int(i.value)]
def do_block(args, context): block = args["block"] if len(block.params) > 0: return err( "since no arguments are provided, $block of `do $block` must have no parameters", "TypeError") return _run_block(block, [], context)
def index_i_of_array(args, context): i = args["i"] array = args["array"] if not i.is_integer() or not i.is_positive() or not int(i.value) < len( array.get_elements()): return err("invalid index: %s" % i) return array.get_elements()[int(i.value)]
def do_block_with_args(args, context): block = args["block"] b_args = args["args"].get_elements() if len(block.params) != len(b_args): return err( "the amount of arguments provided in `do $block with $args` should match the number of parameters in the block", "TypeError") return _run_block(block, b_args, context)
def new_fn(args, context): if isinstance(args[name].type, expected_type): return err("the $%s parameter in `%s` must be of type %s, not %s" % ( name, getattr(fn, "pattern"), expected_type, args[name].type )) return fn(args, context)
def printf_format_with_args(args, context): fmt = args["format"].value items = tuple(args["args"].get_elements()) try: print(obj.String(fmt % items)) except TypeError: return err("Wrong number of arguments to format `%s`" % fmt, "TypeError") return NULL
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 run_block_with_args(args, context): block = args["block"] b_args = args["args"].get_elements() if len(block.params) != len(b_args): return err("the amount of arguments provided in `run $block with $args` should match the number of parameters in the block") params = [param.value for param in block.params] args_dictionary = dict(zip(params, b_args)) ctx = context.enclose_with_args(args_dictionary) return evaluate(block.body, ctx)
def run_block_with_args(args, context): block = args["block"] b_args = args["args"].get_elements() if len(block.params) != len(b_args): return err( "the amount of arguments provided in `run $block with $args` should match the number of parameters in the block" ) params = [param.value for param in block.params] args_dictionary = dict(zip(params, b_args)) ctx = context.enclose_with_args(args_dictionary) return evaluate(block.body, ctx)