def MAX(): """replaces top item of stack with largest of top 2""" global stack if len(stack) < 2: errors.stackArgumentLenError("MAX") else: stack = stack[:-2] + [max(stack[-2:])]
def NEG(): """negate top item of stack""" global stack if len(stack) < 1: errors.stackArgumentLenError("NEGATE") else: stack[-1] = -stack[-1]
def DUP(): """duplicates top item on stack""" global stack if len(stack) < 1: errors.stackArgumentLenError("DUP") else: stack.append(stack[-1])
def NIP(): """deletes 2nd top item from stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("NIP") else: del stack[-2]
def ABS(): """get absolute value of top item on stack""" global stack if len(stack) < 1: errors.stackArgumentLenError("ABS") else: stack[-1] = abs(stack[-1])
def MIN(): """replaces top item of stack with smallest of the top 2""" global stack if len(stack) < 2: errors.stackArgumentLenError("MIN") else: stack = stack[:-2] + [min(stack[-2:])]
def ROT(): global stack if len(stack) < 3: errors.stackArgumentLenError("ROT") else: temp = stack[-3] del stack[-3] stack.append(temp)
def WIPE(): global stack if len(stack) < 1: errors.stackArgumentLenError("WIPE") else: if type(stack[-1]) != list: errors.valueError() else: del stack[-1][:]
def ADD(): global stack if len(stack) < 2: errors.stackArgumentLenError("ADD") else: if type(stack[-2]) != list: errors.valueError() else: stack[-2].insert(0, stack[-1])
def APPEND(): global stack if len(stack) < 2: errors.stackArgumentLenError("APPEND") else: if type(stack[-2]) != list: errors.valueError() else: stack[-2].append(stack[-1])
def MULT(): """Multiply top 2 items of stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("MULTIPLY") else: temp = stack[-2] * stack[-1] stack = stack[:-2] stack.append(temp)
def MOD(): """Perform modulus of top 2 items of stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("MOD") else: temp = stack[-2] % stack[-1] stack = stack[:-2] stack.append(temp)
def LEN(): global stack if len(stack) < 1: errors.stackArgumentLenError("LEN") else: if type(stack[-1]) in [str, list]: stack.append(len(stack[-1])) else: errors.valueError()
def MINUS(): """Subtract top 2 items of stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("MINUS") else: temp = stack[-2] - stack[-1] stack = stack[:-2] stack.append(temp)
def MORETHANEQ(): global stack if len(stack) < 2: errors.stackArgumentLenError('MORETHANEQ') else: if stack[-2] >= stack[-1]: del stack[-2:] stack.append(1) else: del stack[-2:] stack.append(0)
def LESSTHAN(): global stack if len(stack) < 2: errors.stackArgumentLenError('LESSTHAN') else: if stack[-2] < stack[-1]: del stack[-2:] stack.append(1) else: del stack[-2:] stack.append(0)
def NOTEQUALS(): global stack if len(stack) < 2: errors.stackArgumentLenError('EQUALS') else: if stack[-2] != stack[-1]: del stack[-2:] stack.append(1) else: del stack[-2:] stack.append(0)
def DIV(): """Divide top 2 items of stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("DIVIDE") else: temp = float(stack[-2]) / float(stack[-1]) stack = stack[:-2] if str(temp)[-2:] == '.0': stack.append(int(temp)) else: stack.append(temp)
def DROP(): global stack if len(stack) < 1: errors.stackArgumentLenError("DROP") else: if type(stack[-1]) != list: errors.valueError() else: if len(stack[-1]) < 1: errors.indexError("DROP from empty list") else: del stack[-1][-1]
def PLUS(): """Add top 2 items of stack""" global stack if len(stack) < 2: errors.stackArgumentLenError("PLUS") else: try: temp = stack[-1] + stack[-2] except TypeError: errors.opError() stack = stack[:-2] stack.append(temp)
def GET(index): global stack if index in dep.globalVs: index = dep.globalVs[index] if len(stack) < 1: errors.stackArgumentLenError("GET") else: if type(stack[-1]) not in [list, str]: errors.valueError() else: try: stack.append(stack[-1][index]) except IndexError: errors.indexError("index out of range")
def DEL(index): global stack if index in dep.globalVs: index = dep.globalVs[index] if len(stack) < 1: errors.stackArgumentLenError("DEL") else: if type(stack[-1]) != list: errors.valueError() else: try: del stack[-1][index] except IndexError: errors.indexError("Array index out of range")
def INSERT(index): global stack if index in dep.globalVs: index = dep.globalVs[index] if len(stack) < 2: errors.stackArgumentLenError("INSERT") else: if type(stack[-2]) != list: errors.valueError() else: try: stack[-2][index] = stack[-1] except IndexError: errors.indexError("Array index out of range")
def NOT(): global stack if len(stack) < 1: errors.stackArgumentLenError('NOT') else: if type(stack[-1]) == str: if len(stack[-1]) > 0: del stack[-1] stack.append(0) else: del stack[-1] stack.append(1) elif type(stack[-1]) in [int, float]: if stack[-1] > 0: del stack[-1] stack.append(0) else: del stack[-1] stack.append(1)
def OR(): global stack if len(stack) < 2: errors.stackArgumentLenError('OR') else: if type(stack[-1]) == type(stack[-2]) == int or type( stack[-1]) == type(stack[-2]) == float: if stack[-1] > 0 or stack[-2] > 0: del stack[-2:] stack.append(1) else: del stack[-2:] stack.append(0) elif type(stack[-1]) == type(stack[-2]) == str: if len(stack[-1]) > 0 or len(stack[-2]) > 0: del stack[-2:] stack.append(1) else: del stack[-2:] stack.append(0) else: errors.valueError()
def CMD(): if len(stack) < 1: errors.stackArgumentLenError('CMD') else: os.system(stack[-1])
def TRIM(): global stack if len(stack) < 1: errors.stackArgumentLenError("TRIM") else: del stack[0]