Ejemplo n.º 1
0
 def call_external(self, val):
     if hasattr(val, "nustack"):
         # This function was marked by the extension module register, so call with the interpreter
         log("call_extenal: calling registered function", val)
         val(self)
     else:
         numargs = getnumargs(val)
         log("call_extenal: calling unregisted function", val)
         log("call_extenal: num args", numargs)
         func_args = [arg.val for arg in self.stack.popN(numargs)]
         log("call_extenal: function args", func_args)
         ret = val(*func_args)
         tok = tokenize.Token("any", ret)
         if ret is not None: self.stack.push(tok)
Ejemplo n.º 2
0
def tokenize(code):
    tokens = []
    while code:
        commentmatch = COMMENT.match(code)
        intmatch = INT.match(code)
        floatmatch = FLOAT.match(code)
        boolmatch = BOOL.match(code)
        stringmatch = STRING.match(code)
        bytematch = BYTE.match(code)
        symbolmatch = SYMBOL.match(code)
        callmatch = CALL.match(code)
        if commentmatch:
            log("Parsing: Found comment/whitespace")
            commentend = commentmatch.span()[1]
            code = code[commentend:]
        elif intmatch:
            span = intmatch.span()[1]
            n = code[:span]
            n = int(n)
            tokens.append(Token("lit_int", n))
            code = code[span:]
            log("Parsing: Found int", n)
        elif floatmatch:
            span = floatmatch.span()[1]
            n = code[:span]
            n = float(n)
            tokens.append(Token("lit_float", n))
            code = code[span:]
            log("Parsing: Found float", n)
        elif boolmatch:
            span = boolmatch.span()[1]
            b = code[:span]
            if b == "#t":
                bool = True
            else:
                bool = False
            code = code[span:]
            tokens.append(Token("lit_bool", bool))
            log("Parsing: Found bool", b)
        elif stringmatch:
            span = stringmatch.span()[1]
            s = code[:span]
            s = addescapes(s)
            tokens.append(Token("lit_string", s[1:-1]))
            code = code[span:]
            log("Parsing: Found string", s[1:-1])
        elif bytematch:
            span = bytematch.span()[1]
            s = code[:span]
            s = addescapes(s)
            tokens.append(Token("lit_bytes", bytes(s[2:-1], "utf8")))
            code = code[span:]
            log("Parsing: Found bytes", bytes(s[2:-1], "utf8"))
        elif code[0] == "[":
            tokens.append(Token("lit_liststart","["))
            code = code[1:]
            log("Parsing: Found list start")
        elif code[0] == "]":
            tokens.append(Token("listend", "]"))
            code = code[1:]
            log("Parsing: Found list end")
        elif code[0] == "{":
            tokens.append(Token("codestart", "{"))
            code = code[1:]
            log("Parsing: Found code start")
        elif code[0] == "}":
            subcode = []
            while True:
                t = tokens.pop()
                if t.type == "codestart":
                    break
                subcode.append(t)
            tokens.append(Token("lit_code", list(reversed(subcode))))
            code = code[1:]
            log("Parsing: Found code end")
        elif callmatch:
            span = callmatch.span()[1]
            sym = code[:span]
            code = code[span:]
            tokens.append(Token("call", sym))
            log("Parsing: Found call", sym)
        elif symbolmatch:
            span = symbolmatch.span()[1]
            sym = code[1:span]
            code = code[span:]
            tokens.append(Token("lit_symbol", sym))
            log("Parsing: Found symbol", sym)
        else:
            print(code)
            raise TokenizeError("Can not find a token!")
    return tokens
Ejemplo n.º 3
0
def loadModule(env, name):
    "Returns a module"
    curdir = env.getDir()
    if name.startswith("std::"):
        usestd = True
        name = name[5:]
    else:
        usestd = False
    namesplit = name.split("::")
    try:
        if usestd:
            log("loadModule: Force loading stdlib",
                os.path.join(stddir, *namesplit) + ".nu")
            f = open(os.path.join(stddir, *namesplit) + '.nu', "r")
            code = f.read()
            f.close()
            interp = nustack.interpreter.Interpreter()
            _, s = interp.run(code)
            scope = ScopeWrapper(s._scopes[0])
            return namesplit, scope
        nupath = getsearchpath(env)
        log("loadModule: Module Search Path =", nupath)
        for pth in nupath:
            pth = os.path.join(pth, *namesplit) + ".nu"
            log("loadModule: Trying to load", pth)
            if os.path.exists(pth):
                f = open(pth, "r")
                code = f.read()
                f.close()
                interp = nustack.interpreter.Interpreter()
                _, s = interp.run(code)
                scope = ScopeWrapper(s._scopes[0])
                return namesplit, scope
        raise IOError()
    except IOError:
        log("loadModule: Couldn't find nustack module, trying extensions...")
        name = ".".join(namesplit)
        if usestd:
            m = importlib.import_module("nustack.stdlib.%s" % name)
            log("loadModule: Force loading stdlib", name)
        else:
            try:
                m = importlib.import_module("nu_ext.%s" % name)
                log("loadModule: Loading extension module", name,
                    "in nu_ext package")
            except ImportError:
                try:
                    m = importlib.import_module("nu_ext_%s" % name)
                    log("loadModule: Loading extension module", name,
                        "with nu_ext prefix")
                except ImportError:
                    m = importlib.import_module("nustack.stdlib.%s" % name)
                    log("loadModule: Loading stdlib", name)
        return namesplit, m.module
Ejemplo n.º 4
0
def loadModule(env, name):
    "Returns a module"
    curdir = env.getDir()
    if name.startswith("std::"):
        usestd = True
        name = name[5:]
    else:
        usestd = False
    namesplit = name.split("::")
    try:
        if usestd:
            log("loadModule: Force loading stdlib", os.path.join(stddir, *namesplit) + ".nu")
            f = open(os.path.join(stddir, *namesplit) + '.nu', "r")
            code = f.read()
            f.close()
            interp = nustack.interpreter.Interpreter()
            _, s = interp.run(code)
            scope = ScopeWrapper(s._scopes[0])
            return namesplit, scope
        nupath = getsearchpath(env)
        log("loadModule: Module Search Path =", nupath)
        for pth in nupath:
            pth = os.path.join(pth, *namesplit) + ".nu"
            log("loadModule: Trying to load", pth)
            if os.path.exists(pth):
                f = open(pth, "r")
                code = f.read()
                f.close()
                interp = nustack.interpreter.Interpreter()
                _, s = interp.run(code)
                scope = ScopeWrapper(s._scopes[0])
                return namesplit, scope
        raise IOError()
    except IOError:
        log("loadModule: Couldn't find nustack module, trying extensions...")
        name = ".".join(namesplit)
        if usestd:
            m = importlib.import_module("nustack.stdlib.%s" % name)
            log("loadModule: Force loading stdlib", name)
        else:
            try:
                m = importlib.import_module("nu_ext.%s" % name )
                log("loadModule: Loading extension module", name, "in nu_ext package")
            except ImportError:
                try:
                    m = importlib.import_module("nu_ext_%s" % name)
                    log("loadModule: Loading extension module", name, "with nu_ext prefix")
                except ImportError:
                    m = importlib.import_module("nustack.stdlib.%s" % name)
                    log("loadModule: Loading stdlib", name)
        return namesplit, m.module
Ejemplo n.º 5
0
def tokenize(code):
    tokens = []
    while code:
        commentmatch = COMMENT.match(code)
        intmatch = INT.match(code)
        floatmatch = FLOAT.match(code)
        boolmatch = BOOL.match(code)
        stringmatch = STRING.match(code)
        bytematch = BYTE.match(code)
        symbolmatch = SYMBOL.match(code)
        callmatch = CALL.match(code)
        if commentmatch:
            log("Parsing: Found comment/whitespace")
            commentend = commentmatch.span()[1]
            code = code[commentend:]
        elif intmatch:
            span = intmatch.span()[1]
            n = code[:span]
            n = int(n)
            tokens.append(Token("lit_int", n))
            code = code[span:]
            log("Parsing: Found int", n)
        elif floatmatch:
            span = floatmatch.span()[1]
            n = code[:span]
            n = float(n)
            tokens.append(Token("lit_float", n))
            code = code[span:]
            log("Parsing: Found float", n)
        elif boolmatch:
            span = boolmatch.span()[1]
            b = code[:span]
            if b == "#t":
                bool = True
            else:
                bool = False
            code = code[span:]
            tokens.append(Token("lit_bool", bool))
            log("Parsing: Found bool", b)
        elif stringmatch:
            span = stringmatch.span()[1]
            s = code[:span]
            s = addescapes(s)
            tokens.append(Token("lit_string", s[1:-1]))
            code = code[span:]
            log("Parsing: Found string", s[1:-1])
        elif bytematch:
            span = bytematch.span()[1]
            s = code[:span]
            s = addescapes(s)
            tokens.append(Token("lit_bytes", bytes(s[2:-1], "utf8")))
            code = code[span:]
            log("Parsing: Found bytes", bytes(s[2:-1], "utf8"))
        elif code[0] == "[":
            tokens.append(Token("lit_liststart", "["))
            code = code[1:]
            log("Parsing: Found list start")
        elif code[0] == "]":
            tokens.append(Token("listend", "]"))
            code = code[1:]
            log("Parsing: Found list end")
        elif code[0] == "{":
            tokens.append(Token("codestart", "{"))
            code = code[1:]
            log("Parsing: Found code start")
        elif code[0] == "}":
            subcode = []
            while True:
                t = tokens.pop()
                if t.type == "codestart":
                    break
                subcode.append(t)
            tokens.append(Token("lit_code", list(reversed(subcode))))
            code = code[1:]
            log("Parsing: Found code end")
        elif callmatch:
            span = callmatch.span()[1]
            sym = code[:span]
            code = code[span:]
            tokens.append(Token("call", sym))
            log("Parsing: Found call", sym)
        elif symbolmatch:
            span = symbolmatch.span()[1]
            sym = code[1:span]
            code = code[span:]
            tokens.append(Token("lit_symbol", sym))
            log("Parsing: Found symbol", sym)
        else:
            print(code)
            raise TokenizeError("Can not find a token!")
    return tokens