Esempio n. 1
0
def psFor():

    codeArray = assignment1Functions.op_pop()
    finalValue = assignment1Functions.op_pop()
    incrementValue = assignment1Functions.op_pop()
    initialValue = assignment1Functions.op_pop()

    if incrementValue < 0:
        while (initialValue >= finalValue):
            assignment1Functions.op_push(initialValue)
            interpretSPS(codeArray)
            initialValue += incrementValue

    elif incrementValue > 0:
        while (initialValue <= finalValue):
            assignment1Functions.op_push(initialValue)
            interpretSPS(codeArray)
            initialValue += incrementValue
Esempio n. 2
0
def new_ps_def():

    # Check if there are enough values on the operand stack and if values are in the correct format
    if len(assignment1Functions.op_stack) < 2:
        print(
            "There are not enough values on the operand stack to perform the operation"
        )
        return

    # Get the first two variables from the operand stack
    value_1 = assignment1Functions.op_pop()
    value_2 = assignment1Functions.op_pop()

    if value_2[0] != '/':
        assignment1Functions.op_push(value_2)
        assignment1Functions.op_push(value_1)
        print("The entry is not in the correct format")

        # Take the '/' from the name and then add to the dictionary at the top of the dict stack
    else:
        newDef(value_2, value_1)
Esempio n. 3
0
def interpretSPS(code, scope):

    postscriptOperations = [
        'add', 'sub', 'mul', 'div', 'eq', 'lt', 'gt', 'and', 'or', 'not', 'if',
        'ifelse', 'for', 'forall', 'length', 'get', 'dup', 'exch', 'pop',
        'copy', 'clear', 'def', 'stack'
    ]

    for item in code:
        if isinstance(item, str):
            # If the item is a name declaration
            if item[0] == '/':
                assignment1Functions.op_push(item)
                continue

            # If the item is a postscript operation, call correct function
            elif item in postscriptOperations:
                if item == 'add':
                    assignment1Functions.add()
                elif item == 'sub':
                    assignment1Functions.sub()
                elif item == 'mul':
                    assignment1Functions.mul()
                elif item == 'div':
                    assignment1Functions.div()
                elif item == 'eq':
                    assignment1Functions.eq()
                elif item == 'lt':
                    assignment1Functions.lt()
                elif item == 'gt':
                    assignment1Functions.gt()
                elif item == 'and':
                    assignment1Functions.ps_and()
                elif item == 'or':
                    assignment1Functions.ps_or()
                elif item == 'not':
                    assignment1Functions.ps_not()
                # Recursive
                elif item == 'if':
                    psIf()
                    continue
                # Recursive
                elif item == 'ifelse':
                    psIfElse()
                    continue
                # Recursive
                elif item == 'for':
                    psFor()
                    continue
                # Recursive
                elif item == 'forall':
                    psForAll()
                    continue

                elif item == 'length':
                    assignment1Functions.length()
                elif item == 'get':
                    assignment1Functions.get()
                elif item == 'dup':
                    assignment1Functions.dup()
                elif item == 'exch':
                    assignment1Functions.exch()
                elif item == 'pop':
                    assignment1Functions.pop()
                elif item == 'copy':
                    assignment1Functions.copy()
                elif item == 'clear':
                    assignment1Functions.clear()
                elif item == 'def':
                    new_ps_def()
                elif item == 'stack':
                    newStack(scope)

            # If the item is a name lookup
            # If lookup yields a code array, then execute that code array.
            # Otherwise, push the value to the operand stack
            else:
                if scope == 'dynamic':
                    value = newDynamicLookup(item)
                    if type(value) == list:
                        # If it is an integer array
                        if all(isinstance(x, int) for x in value):
                            assignment1Functions.op_push(value)
                        # If it is a code array
                        else:
                            # Push new tuple to dict stack with correct static link
                            assignment1Functions.dict_push(({}, 0))

                            interpretSPS(value, scope)
                            assignment1Functions.dict_pop()
                    #If the lookup yields a non-list or non-code array then push it to operand stack
                    else:
                        assignment1Functions.op_push(value)

                elif scope == 'static':
                    # Do the static lookup starting at the top value in the dictionary stack
                    value = newStaticLookup(
                        assignment1Functions.dict_stack[-1], item)
                    if type(value) == list:
                        # If it is an integer array
                        if all(isinstance(x, int) for x in value):
                            assignment1Functions.op_push(value)
                        # If it is a code array
                        else:
                            # Get the static link
                            staticLink = findStaticLink(
                                assignment1Functions.dict_stack[-1], item)
                            # Push new tuple to dict stack with correct static link
                            assignment1Functions.dict_push(({}, staticLink))
                            interpretSPS(value, scope)
                            # Pop the top dictionary from the stack once function execution has completed
                            assignment1Functions.dict_pop()

                    #If the lookup yields a non-list or non-code array then push it to operand stack
                    else:
                        assignment1Functions.op_push(value)

        # If the item is a list (int array or code array), then push to op stack
        elif type(item) == list:
            assignment1Functions.op_push(item)
            continue

        # If the item is an integer or floating point value, then push to op stack
        elif type(item) == int or type(item) == float:
            assignment1Functions.op_push(item)
            continue

        # If the item is a boolean value, then push to op stack
        elif type(item) == bool:
            assignment1Functions.op_push(item)
            continue

    return
Esempio n. 4
0
def psForAll():
    procedure = assignment1Functions.op_pop()
    intArray = assignment1Functions.op_pop()
    for item in intArray:
        assignment1Functions.op_push(item)
        interpretSPS(procedure)
Esempio n. 5
0
def interpretSPS(code):

    postscriptOperations = [
        'add', 'sub', 'mul', 'div', 'eq', 'lt', 'gt', 'and', 'or', 'not', 'if',
        'ifelse', 'for', 'forall', 'length', 'get', 'dup', 'exch', 'pop',
        'copy', 'clear', 'def', 'stack', 'dict', 'begin', 'end'
    ]

    for item in code:
        if isinstance(item, str):
            # If the item is a name declaration
            if item[0] == '/':
                assignment1Functions.op_push(item)
                continue

            # If the item is a postscript operation, call correct function
            elif item in postscriptOperations:
                if item == 'add':
                    assignment1Functions.add()
                elif item == 'sub':
                    assignment1Functions.sub()
                elif item == 'mul':
                    assignment1Functions.mul()
                elif item == 'div':
                    assignment1Functions.div()
                elif item == 'eq':
                    assignment1Functions.eq()
                elif item == 'lt':
                    assignment1Functions.lt()
                elif item == 'gt':
                    assignment1Functions.gt()
                elif item == 'and':
                    assignment1Functions.ps_and()
                elif item == 'or':
                    assignment1Functions.ps_or()
                elif item == 'not':
                    assignment1Functions.ps_not()
                # Recursive
                elif item == 'if':
                    psIf()
                    continue
                # Recursive
                elif item == 'ifelse':
                    psIfElse()
                    continue
                # Recursive
                elif item == 'for':
                    psFor()
                    continue
                # Recursive
                elif item == 'forall':
                    psForAll()
                    continue

                elif item == 'length':
                    assignment1Functions.length()
                elif item == 'get':
                    assignment1Functions.get()
                elif item == 'dup':
                    assignment1Functions.dup()
                elif item == 'exch':
                    assignment1Functions.exch()
                elif item == 'pop':
                    assignment1Functions.pop()
                elif item == 'copy':
                    assignment1Functions.copy()
                elif item == 'clear':
                    assignment1Functions.clear()
                elif item == 'def':
                    assignment1Functions.ps_def()
                elif item == 'stack':
                    assignment1Functions.stack()
                elif item == 'dict':
                    assignment1Functions.ps_dict()
                elif item == 'begin':
                    assignment1Functions.begin()
                elif item == 'end':
                    assignment1Functions.end()

            # If the item is a name lookup
            # If lookup yields a code array, then execute that code array.
            # Otherwise, push the value to the operand stack
            else:
                value = assignment1Functions.lookup(item)
                if type(value) == list:
                    if all(isinstance(x, int) for x in value):
                        assignment1Functions.op_push(value)
                    else:
                        interpretSPS(value)

                else:
                    assignment1Functions.op_push(value)

        # If the item is a list (int array or code array), then push to op stack
        elif type(item) == list:
            assignment1Functions.op_push(item)
            continue

        # If the item is an integer or floating point value, then push to op stack
        elif type(item) == int or type(item) == float:
            assignment1Functions.op_push(item)
            continue

        # If the item is a boolean value, then push to op stack
        elif type(item) == bool:
            assignment1Functions.op_push(item)
            continue

    return