Exemple #1
0
def jmp(vm):
    if not (isinstance(vm.top, int) or isinstance(vm.top, long)):
        raise errors.MachineError("Jump address must be an integer: %s" %
                                  str(vm.top))
    addr = vm.pop()
    if 0 <= addr < len(vm.code):
        vm.instruction_pointer = addr
    else:
        raise errors.MachineError("Jump address out of range: %s" % str(addr))
Exemple #2
0
def mod(vm):
    a = vm.pop()
    b = vm.pop()
    _assert_number(a, b)
    if a == 0:
        raise errors.MachineError(ZeroDivisionError("Division by zero"))
    vm.push(b % a)
Exemple #3
0
def div(vm):
    divisor = vm.pop()
    dividend = vm.pop()
    _assert_number(dividend, divisor)
    if divisor == 0:
        raise errors.MachineError(ZeroDivisionError("Division by zero"))
    vm.push(dividend / divisor)
Exemple #4
0
def cast_int(vm):
    try:
        int(vm.top)
    except ValueError:
        raise errors.MachineError("Cannot be cast to int: '%s'" % str(vm.top))
    else:
        vm.push(int(vm.pop()))
Exemple #5
0
def cast_bool(vm):
    try:
        bool(vm.top)
    except ValueError:
        raise errors.MachineError("Cannot be cast to bool: '%s'" % str(vm.top))
    else:
        vm.push(bool(vm.pop()))
Exemple #6
0
 def pop(self):
     """Pops the data stack, returning the value."""
     try:
         return self.data_stack.pop()
     except errors.MachineError as e:
         raise errors.MachineError(
             "%s: At index %d in code: %s" %
             (e, self.instruction_pointer, self.code_string))
Exemple #7
0
def dot(vm, flush=True):
    write(vm, flush=False)
    try:
        if vm.output is not None:
            vm.output.write("\n")
            if flush:
                vm.output.flush()
    except IOError:
        raise errors.MachineError(IOError)
Exemple #8
0
def write(vm, flush=True):
    value = str(vm.pop())
    try:
        if vm.output is not None:
            vm.output.write(value)
            if flush:
                vm.output.flush()
    except IOError:
        raise errors.MachineError(StopIteration)
Exemple #9
0
def lookup(instruction, instructions=None):
    """Looks up instruction, which can either be a function or a string.
    If it's a string, returns the corresponding method.
    If it's a function, returns the corresponding name.
    """
    if instructions is None:
        instructions = default_instructions

    if isinstance(instruction, str):
        return instructions[instruction]
    elif hasattr(instruction, "__call__"):
        rev = dict(((v, k) for (k, v) in instructions.items()))
        return rev[instruction]
    else:
        raise errors.MachineError(
            KeyError("Unknown instruction: %s" % str(instruction)))
Exemple #10
0
def _assert_number(*args):
    for arg in args:
        if not interpreter.isnumber(arg):
            raise errors.MachineError("Not an integer: %s" % str(arg))
Exemple #11
0
def cast_str(vm):
    try:
        vm.push(str(vm.pop()))
    except ValueError as e:
        raise errors.MachineError(e)
Exemple #12
0
def _assert_binary(*args):
    for arg in args:
        if not interpreter.isbinary(arg):
            raise errors.MachineError("Not boolean or numerical: %s" %
                                      str(arg))
Exemple #13
0
def cast_float(vm):
    try:
        float(vm.top)
    except ValueError:
        raise errors.MachineError("Cannot be cast to float: '%s'" %
                                  str(vm.top))
Exemple #14
0
def _assert_bool(*args):
    for arg in args:
        if not interpreter.isbool(arg):
            raise errors.MachineError("Not a boolean: %s" % str(arg))