def less(args): args = valueify(args) if len(args) != 2: raise MethodInputError("Incorrect number of inputs, should be 2, %s were given" % len(args)) elif args[0].type == "numb" and args[1].type == "numb": return tokenz.Token("bool", args[0].val < args[1].val) else: raise MethodInputError("Incorrect type of arguments for function: %s, %s" % (str(args[0].type), str(args[1].type))) return tokenz.Token("None", None)
def ask(args): if len(args) != 1: raise MethodInputError("Expected 1 input, got %s. %s" % (len(args), args)) if args[0].type == "str": return tokenz.Token( "str", tokenz.stringify(str(input(args[0].val[1:-1] + "\n")))) else: raise MethodInputError("Incorrect type of arguments for function: %s" % str(args[0].type)) return tokenz.Token("None", None)
def power(args): if len(args) != 2: raise MethodInputError( "Incorrect number of inputs, should be 2, %s were given" % len(args)) elif not all_type(args, "numb"): raise MethodInputError( "Incorrect type of arguments for function, should be all NUMB") else: return tokenz.Token("numb", pow(args[0].val, args[1].val))
def concat(args): if len(args) < 2: raise MethodInputError("Incorrect number of inputs, should be at least 2, %s were given" % len(args)) else: total = "" for tok in args: if tok.type == "str": total += str(tok.val[1:-1]) else: total += str(tok.val) return tokenz.Token("str", tokenz.stringify(total))
def toInt(args): if len(args) != 1: raise MethodInputError("Incorrect number of inputs, should be 1, %s were given" % len(args)) if args[0].type == "numb": return args[0] else: try: if args[0].type == "str": args[0].val = args[0].val[1:-1] return tokenz.Token("numb", int(args[0].val)) except ValueError: raise ConversionError("Attempted to convet %s type to numb" % str(args[0].val))
def define(args): if len(args) != 2: raise MethodInputError( "Incorrect number of inputs, should be 2, %s were given" % len(args)) elif args[0].type == "func" and args[1].type == "codeblock": funcs[str(args[0].val)] = args[1] else: raise MethodInputError( "Incorrect type of arguments for function: %s, %s" % (str(args[0].type), str(args[1].type))) return tokenz.Token("None", None)
def out(args): #[Token("num", 5), Token("num", 6)] if len(args) != 1: raise MethodInputError("Expected 1 input, got %s. %s" % (len(args), args)) a = args[0] #easier for typing if a.type == "str": print(a.val[1:-1].replace("//n", "\n")) elif a.type in ["numb", "bool", "value"]: print(str(tokenz.destringify(a)).replace("//n", "\n")) else: raise MethodInputError("Unreconized type to outupt: %s" % a.type) return tokenz.Token("None", None)
def div(args): if len(args) < 2: raise MethodInputError( "Incorrect number of inputs, should be at least 2, %s were given" % len(args)) elif not all_type(args, "numb"): raise MethodInputError( "Incorrect type of arguments for function, should be all NUMB") else: total = args[0].val for tok in args[1:]: total = total / tok.val return tokenz.Token("numb", total)
def rep(args): args = valueify(args) if len(args) != 2: raise MethodInputError( "Incorrect number of inputs, should be 2, %s were given" % len(args)) elif args[0].type == "numb" and args[1].type == "codeblock": for n in range(args[0].val): interpreter.Interpreter().call(args[1]) else: raise MethodInputError( "Incorrect type of arguments for function: %s, %s" % (str(args[0].type), str(args[1].type))) return tokenz.Token("None", None)
def call(args): if len(args) != 1: raise MethodInputError( "Incorrect number of inputs, should be 1, %s were given" % len(args)) elif args[0].type == "func": try: interpreter.Interpreter().call(funcs[str(args[0].val)]) except KeyError: raise FunctionError("Attempted to call undefined function %s" % args[0].val[1:]) else: raise MethodInputError("Incorrect type of arguments for function: %s" % str(args[0].type)) return tokenz.Token("None", None)
def toBool(args): if len(args) != 1: raise MethodInputError("Incorrect number of inputs, should be 1, %s were given" % len(args)) if args[0].type == "bool": return args[0] else: val = args[0].val if "False" in val: val = False elif "True" in val: val = True elif val == 0: val = False elif val == 1: val = True else: val = False return tokenz.Token("bool", val)
def eq(args): args = valueify(args) if len(args) != 2: raise MethodInputError("Incorrect number of inputs, should be 2, %s were given" % len(args)) return tokenz.Token("bool", args[0].val == args[1].val)
def notStm(args): if len(args) != 1: raise MethodInputError("Incorrect number of inputs, should be 1, %s were given" % len(args)) if args[0].type == "bool": return tokenz.Token("bool", not args[0].val) else: raise MethodInputError("Incorrect type of arguments for function: %s" % str(args[0].type))
def toString(args): if len(args) != 1: raise MethodInputError("Incorrect number of inputs, should be 1, %s were given" % len(args)) if args[0].type == "str": return args[0] else: return tokenz.Token("str", tokenz.stringify(str(args[0].val)))
def eval(self, code): # Code is string... self.tokens = tokenz.tokenize(str(code)) self.pos = 0 returns = [] while self.tokens != []: try: tok = self.tokens[0] if tok.type == "call": args = self.func( self.tokens ) # [isFunction, resulting token stream, arguments] if not args[0]: self.tokens = args[1][1:] that = tokenz.Token("ident", args[1][0].val) that.id = args[1][0].val this = tokenz.Token( "value", methodMang.Call("get", [that], True, False).run().val) returns.append(this) else: returns.append( methodMang.Call(tok.val, _2list(args[2]), False).run()) self.tokens = args[1] elif tok.type == "codeblock": self.crunch() returns.append(tok) elif tok.type == "ident": self.crunch() returns.append(tok) elif tok.type == "numb": self.crunch() returns.append(tok) elif tok.type == "str": self.crunch() returns.append(tok) elif tok.type == "bool": self.crunch() returns.append(tok) elif tok.type == "func": if len(self.tokens) > 2: if self.tokens[1].val == "(": i = 1 d = 1 while True: i += 1 if self.tokens[i].val == "(": d += 1 elif self.tokens[i].val == ")": d -= 1 if d == 0: break that = self.tokens[1:i + 1] that[0] = tok that.pop() # Get rid of ) methodMang.Call("call", that, True, False).run() self.tokens = self.tokens[i + 2:] returns.append(tokenz.Token("None", None)) self.crunch() returns.append(tok) elif tok.type == "codeblock": self.crunch() returns.append(tok) except IndexError: break return returns