def execute_extend(self, exec_context):
     first_list = exec_context.symbol_table.get("first_list")
     end_list = exec_context.symbol_table.get("second_list")
     if not isinstance(first_list, List):
         return RuntimeResult().failure(
             ActiveRuntimeError("First argument must be list",
                                self.start_pos, self.end_pos, exec_context))
     if not isinstance(end_list, List):
         return RuntimeResult().failure(
             ActiveRuntimeError("Second argument must be list",
                                self.start_pos, self.end_pos, exec_context))
     first_list.elements.extend(end_list.elements)
     return RuntimeResult().success(Number(0))
 def execute_len(self, exec_context):
     list_ = exec_context.symbol_table.get("list")
     if not isinstance(list_, List):
         return RuntimeResult().failure(
             ActiveRuntimeError("Argument must be list", self.start_pos,
                                self.end_pos, exec_context))
     return RuntimeResult().success(Number(len(list_.elements)))
Beispiel #3
0
 def divide_by(self, other):
     """
     Get value from the List instance.
     :param other: Index of value to fetch from List.
     :return: Value requested from the List.
     """
     if isinstance(other, Number):
         try:  # Try indexing on the list()
             return self.elements[other.value], None
         except IndexError:  # Catch Python's attempt at pop()
             return None, ActiveRuntimeError('Index not found',
                                             self.start_pos, other.end_pos,
                                             self.context)
     else:  # Index of List value must be a Number
         return ActiveRuntimeError('Illegal operation performed',
                                   self.start_pos, other.end_pos,
                                   self.context)
 def execute_append(self, exec_context):
     list_ = exec_context.symbol_table.get("list")
     value = exec_context.symbol_table.get("value")
     if not isinstance(list_, List):
         return RuntimeResult().failure(
             ActiveRuntimeError("First argument must be list",
                                self.start_pos, self.end_pos, exec_context))
     list_.elements.append(value)
     return RuntimeResult().success(Number(0))
 def execute_pop(self, exec_context):
     list_ = exec_context.symbol_table.get("list")
     index = exec_context.symbol_table.get("index")
     if not isinstance(list_, List):
         return RuntimeResult().failure(
             ActiveRuntimeError("First argument must be list",
                                self.start_pos, self.end_pos, exec_context))
     if not isinstance(index, Number):
         return RuntimeResult().failure(
             ActiveRuntimeError("Second argument must be number",
                                self.start_pos, self.end_pos, exec_context))
     try:  # Try pop() command in Python
         element = list_.elements.pop(index.value)
     except IndexError:
         return RuntimeResult().failure(
             ActiveRuntimeError(
                 'Element at this index could not be removed from list because index is out of bounds',
                 self.start_pos, self.end_pos, exec_context))
     return RuntimeResult().success(element)
Beispiel #6
0
 def subtract_by(self, other):
     """
     Subtract value from List.
     :param other: Index of the value to extract.
     :return: New List instance without desired index value.
     """
     if isinstance(other, Number):
         new_list = self.copy()
         try:  # Try pop() call on the list()
             new_list.elements.pop(other.value)
             return new_list, None
         except IndexError:  # Catch Python's attempt at pop()
             return None, ActiveRuntimeError('Index not found',
                                             self.start_pos, other.end_pos,
                                             self.context)
     else:  # Index of List value must be a Number
         return ActiveRuntimeError('Illegal operation performed',
                                   self.start_pos, other.end_pos,
                                   self.context)
Beispiel #7
0
 def illegal_operation(self, other=None):
     """
     Processes illegal operations against Values.
     :param other: Other values being acted on.
     :return: RuntimeError with illegal operands.
     """
     if not other:
         other = self
     return ActiveRuntimeError('Illegal operation performed',
                               self.start_pos, other.end_pos, self.context)
 def check_args(self, arg_names, args):
     """
     Checks that correct number of args are present.
     :param arg_names: List of argument names.
     :param args: List of arguments passed into func.
     :return: None, if there are no issues.
     """
     runtime_result = RuntimeResult()
     if len(args) > len(arg_names):
         return runtime_result.failure(
             ActiveRuntimeError(
                 'Too many arguments'.format(len(args) - len(arg_names)),
                 self.start_pos, self.end_pos, self.context))
     if len(args) < len(arg_names):
         return runtime_result.failure(
             ActiveRuntimeError(
                 'Too few arguments'.format(len(arg_names) - len(args)),
                 self.start_pos, self.end_pos, self.context))
     return runtime_result.success(None)
Beispiel #9
0
 def multiply_by(self, other):
     """
     Join to List instances together.
     :param other: Another List instance.
     :return: The resulting List from the join operation.
     """
     if isinstance(other, List):
         new_list = self.copy()
         new_list.elements.extend(other.elements)
         return new_list, None
     else:  # Cannot join List to any other data type
         return ActiveRuntimeError('Illegal operation performed',
                                   self.start_pos, other.end_pos,
                                   self.context)
 def execute_run(self, exec_context):
     file_name = exec_context.symbol_table.get("fn")
     if not isinstance(file_name, String):
         return RuntimeResult().failure(
             ActiveRuntimeError("Second argument must be string",
                                self.start_pos, self.end_pos, exec_context))
     file_name = file_name.value
     try:
         with open(file_name, "r") as f:
             script = f.read()
     except Exception as exception:
         return RuntimeResult().failure(
             ActiveRuntimeError(
                 "Failed to load script \"{}\"\n".format(file_name) +
                 str(exception), self.start_pos, self.end_pos,
                 exec_context))
     _, error = run(file_name, script)
     if error:
         return RuntimeResult().failure(
             ActiveRuntimeError(
                 "Failed to finish executing script \"{}\"\n".format(
                     file_name) + error.as_string(), self.start_pos,
                 self.end_pos, exec_context))
     return RuntimeResult().success(Number(0))
Beispiel #11
0
 def modulo_by(self, other):
     """
     Takes modulo of two Number values.
     :param other: Number instance.
     :return: Number instance with the modulo remainder value.
     """
     if isinstance(other, Number):
         if other.value == 0:
             return None, ActiveRuntimeError('Division by 0 not allowed',
                                             other.start_pos, other.end_pos,
                                             self.context)
         return Number(self.value % other.value).set_context(
             self.context), None
     else:
         return None, Value.illegal_operation(other)
 def visit_varaccessnode(self, node, context):
     """
     Accesses the value of variables in the stream.
     :param node: Node with which to fetch the variable.
     :param context: Context of the caller.
     :return: Value of fetching a variable's value and executing it.
     """
     runtime_result = RuntimeResult()
     var_name = node.var_name.value
     var_value = context.symbol_table.get(var_name)
     if var_value is None:
         runtime_result.failure(
             ActiveRuntimeError('VAR "{}" not defined'.format(var_name),
                                node.start_pos, node.end_pos, context))
     var_value = var_value.copy().set_position(
         node.start_pos, node.end_pos).set_context(context)
     return runtime_result.success(var_value)
Beispiel #13
0
 def divide_by(self, other, clean=False):
     """
     Divide two Number values together.
     :param other: Number instance.
     :param clean: True to perform a clean division.
     :return: Number instance with the divided value.
     """
     if isinstance(other, Number):
         if other.value == 0:
             return None, ActiveRuntimeError('Division by 0 not allowed',
                                             other.start_pos, other.end_pos,
                                             self.context)
         if clean:  # Perform integer division
             return Number(self.value // other.value).set_context(
                 self.context), None
         else:  # Perform regular floating point division
             return Number(self.value / other.value).set_context(
                 self.context), None
     else:
         return None, Value.illegal_operation(other)