コード例 #1
0
    def swap_x_y(self, symbols, cmd, cmds):
        # exchanges top two values on the stack
        ret = 1

        a = variables.pop(symbols)
        b = variables.pop(symbols)

        variables.push(symbols, a)
        variables.push(symbols, b)

        return ret
コード例 #2
0
    def subtract(self, symbols, cmd, cmds):
        ret = 1
        a = variables.pop(symbols)
        b = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = b - a
        except:
            raise Exception("Error in subtraction: " + str(b) + " - " + str(a))

        variables.push(symbols, c)

        return ret
コード例 #3
0
    def mod(self, symbols, cmd, cmds):
        ret = 1
        a = variables.pop(symbols)
        b = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = b % a
        except:
            raise Exception("Error in mod: " + str(b) + " % " +
                            str(a))  # Errors are highly possible here

        variables.push(symbols, c)

        return ret
コード例 #4
0
    def multiply(self, symbols, cmd, cmds):
        ret = 1
        a = variables.pop(symbols)
        b = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = b * a
        except:
            raise Exception("Error in multiplication: " + str(b) + " * " +
                            str(a))

        variables.push(symbols, c)

        return ret
コード例 #5
0
    def y_to_x(self, symbols, cmd, cmds):
        # calculates the square
        ret = 1
        a = variables.pop(symbols)
        b = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = b**a
        except:
            raise Exception("Error raising: " + str(b) + " to the " + str(a) +
                            "th power")  # Errors are highly possible here

        variables.push(symbols, c)

        return ret
コード例 #6
0
    def chs(self, symbols, cmd, cmds):
        ret = 1
        a = variables.pop(symbols)

        try:
            variables.push(symbols, -a)
        except:
            raise Exception("Error in chs: " +
                            str(a))  # Errors are highly improbable here

        return ret
コード例 #7
0
    def one_on_x(self, symbols, cmd, cmds):
        ret = 1
        a = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            variables.push(symbols, 1 / a)
        except:
            raise Exception("Error in 1/x: " +
                            str(a))  # Errors are highly possible here

        return ret
コード例 #8
0
    def frac_x(self, symbols, cmd, cmds):
        # get the fractionasl part of x
        ret = 1
        a = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            variables.push(symbols, a - int(a))
        except:
            raise Exception("Error in '" + cmd + "' " +
                            str(a))  # Errors are highly unlikely here

        return ret
コード例 #9
0
    def sqr(self, symbols, cmd, cmds):
        # calculates the square
        ret = 1
        a = variables.pop(symbols)
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = a**2
        except:
            raise Exception("Error in squaring: " + str(a))

        variables.push(symbols, c)

        return ret
コード例 #10
0
    def add(
        self,
        symbols,  # the symbol table (stack, global vars, etc.)
        cmd,  # the current command
        cmds):  # the rest of the commands on the command line

        ret = 1  # always initialise ret to 1, because the default is to
        # step token by token along the expression

        a = variables.pop(
            symbols)  # add requires 2 params, pop them off the stack...
        b = variables.pop(symbols)  #
        symbols[SYM_LOCAL]['last x'] = a

        try:
            c = b + a  # RPN functions are defined as b (operator) a
        except:
            raise Exception("Error in addition: " + str(b) + " + " +
                            str(a))  # error message in case of problem

        variables.push(symbols, c)  # the result is pushed back on the stack

        return ret  # and we return the number of tokens to skip (normally 1)
コード例 #11
0
    def pop(self, symbols, cmd, cmds):
        # removes top item from the stack
        ret = 1
        variables.pop(symbols)

        return ret