예제 #1
0
def requireClj(filename, stopafter=None):
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)

    pushThreadBindings({currentCompiler: comp})

    #o = open(filename+".cljc", "w")
    try:
        while True:
            EOF = object()
            s = read(r, False, EOF, True)
            if s is EOF:
                break
            #cPickle.dump(s, o)
            try:
                res = comp.compile(s)
                comp.executeCode(res)
                if stopafter is not None:
                    if hasattr(comp.getNS(), stopafter):
                        break
            except Exception as exp:
                print s, filename
                raise
    except IOError as e:
        pass
    finally:
        popThreadBindings()
예제 #2
0
def requireClj(filename, stopafter=None):
    """Compiles and executes the code in a clj file.
    
    If `stopafter` is given, then stop execution as soon as the `stopafter`
    name is defined in the current namespace of the compiler.
    """
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)

    with threadBindings({currentCompiler:
                         comp}):  #, open(filename + ".cljc", "w") as o:
        try:
            while True:
                EOF = object()
                s = read(r, False, EOF, True)
                if s is EOF:
                    break
                #cPickle.dump(s, o)
                try:
                    res = comp.compile(s)
                    comp.executeCode(res)
                    if stopafter is not None and hasattr(
                            comp.getNS(), stopafter):
                        break
                except Exception as exp:
                    print s, filename
                    raise
        except IOError as e:
            pass
예제 #3
0
def requireClj(filename, stopafter=None):
    """Compiles and executes the code in a clj file.
    
    If `stopafter` is given, then stop execution as soon as the `stopafter`
    name is defined in the current namespace of the compiler.
    """
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)

    with threadBindings({currentCompiler: comp}): #, open(filename + ".cljc", "w") as o:
        try:
            while True:
                EOF = object()
                s = read(r, False, EOF, True)
                if s is EOF:
                    break
                #cPickle.dump(s, o)
                try:
                    res = comp.compile(s)
                    comp.executeCode(res)
                    if stopafter is not None and hasattr(comp.getNS(), stopafter):
                        break
                except Exception as exp:
                    print s, filename
                    raise
        except IOError as e:
            pass
예제 #4
0
파일: main.py 프로젝트: aisk/clojure-py
def requireClj(filename, stopafter="concat"):
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)

    pushThreadBindings({currentCompiler: comp})

    #o = open(filename+".cljc", "w")
    try:
        while True:
            EOF = object()
            s = read(r, False, EOF, True)
            if s is EOF:
                break
            #cPickle.dump(s, o)
            try:
                res = comp.compile(s)
                print s, res

                comp.executeCode(res)
                if stopafter is not None and hasattr(comp.getNS(), stopafter):
                    break
            except Exception as exp:
                print s, filename
                raise
    except IOError as e:
        pass
    finally:
        popThreadBindings()
예제 #5
0
def requireClj(filename, stopafter=None):
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)
    currentCompiler.set(comp)

    try:
        while True:
            s = read(r, True, None, True)
            try:
                res = comp.compile(s)
                comp.executeCode(res)
                if stopafter is not None:
                    if hasattr(comp.getNS(), stopafter):
                        break
            except IOError as exp:
                print s
                raise exp

            while True:
                ch = r.read()

                if ch == "":
                    raise IOError()

                if ch not in [" ", "\t", "\n", "\r"]:
                    r.back()
                    break
    except IOError as e:
        pass
예제 #6
0
 def testIntegerReader_PASS(self):
     # base 8
     for k, v in base8IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base 10
     for k, v in base10IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base 16
     for k, v in base16IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base N
     for k, v in baseNIntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #7
0
 def testIntegerReader_PASS(self):
     # base 8
     for k, v in base8IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base 10
     for k, v in base10IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base 16
     for k, v in base16IntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
     # base N
     for k, v in baseNIntegerMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #8
0
 def compileActual(self, code):
     r = StringReader(code)
     s = read(r, True, None, True)
     res = self.comp.compile(s)
     fn = self.comp.executeCode(res)
     return [
         c for c in Code.from_code(fn.deref().func_code).code[:]
         if c[0] is not SetLineno
     ]
예제 #9
0
def main():
    requireClj(os.path.dirname(__file__) + "/core.clj")

    RT.init()
    comp = Compiler()
    currentCompiler.set(comp)
    comp.setNS(symbol("user"))

    if not sys.argv[1:]:
        while True:
            try:
                line = raw_input(comp.getNS().__name__ + "=> ")
            except EOFError:
                break

            if not line:
                continue

            while unbalanced(line):
                try:
                    line += raw_input('.' * len(comp.getNS().__name__) + '.. ')
                except EOFError:
                    break

            # Propogate break from above loop.
            if unbalanced(line):
                break

            r = StringReader(line)
            s = read(r, True, None, True)

            try:
                res = comp.compile(s)
                print comp.executeCode(res)
            except Exception:
                traceback.print_exc()
    else:
        for x in sys.argv[1:]:
            requireClj(x)
예제 #10
0
파일: main.py 프로젝트: bymerej/clojure-py
def requireClj(filename, stopafter=None):
    with open(filename) as fl:
        r = StringReader(fl.read())

    RT.init()
    comp = Compiler()
    comp.setFile(filename)
    currentCompiler.set(comp)

    # o = open(filename+".cljc", "w")
    try:
        while True:
            s = read(r, False, None, True)
            if s is None:
                break
            # cPickle.dump(s, o)
            try:
                res = comp.compile(s)
                comp.executeCode(res)
                if stopafter is not None:
                    if hasattr(comp.getNS(), stopafter):
                        break
            except Exception as exp:
                print s, filename
                raise

            while True:
                ch = r.read()

                if ch == "":
                    raise IOError()

                if ch not in [" ", "\t", "\n", "\r"]:
                    r.back()
                    break
    except IOError as e:
        pass
예제 #11
0
 def testRationalReader_PASS(self):
     for k, v in rationalMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #12
0
 def eval(self, code):
     r = StringReader(code)
     s = read(r, True, None, True)
     res = self.comp.compile(s)
     return self.comp.executeCode(res)
예제 #13
0
 def testFloatingPointReader_PASS(self):
     for k, v in floatingPointMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #14
0
파일: repl.py 프로젝트: bymerej/clojure-py
 def execute(string):
     r = StringReader(string)
     s = read(r, False, None, True)
     res = comp.compile(s)
     return comp.executeCode(res)
예제 #15
0
 def testReturnedType_PASS(self):
     for k, v in returnedType_PASS.items():
         r = StringReader(k)
         self.assertEqual(type(read(r, False, sentinal, False)), v)
예제 #16
0
파일: repl.py 프로젝트: yminer/clojure-py
def run_repl(opts, comp=None):
    """Initializes and runs the REPL. Assumes that RT.init has been called.

    Repeatedly reads well-formed forms from stdin (with an interactive prompt
    if a tty) and evaluates them (and prints the result if a tty). Exits on
    EOF.
    """
    if not opts.quiet and os.isatty(0):
        print VERSION_MSG

    if comp is None:
        curr = currentCompiler.get(lambda: None)
        if curr == None:
            comp = Compiler()
            currentCompiler.set(comp)
        else:
            comp = curr
    comp.setNS(Symbol("user"))
    core = sys.modules["clojure.core"]
    for i in dir(core):
        if not i.startswith("_"):
            setattr(comp.getNS(), i, getattr(core, i))

    line = opts.cmd
    last3 = [None, None, None]

    def firstLinePrompt():
        return comp.getNS().__name__ + "=> " if os.isatty(0) else ""

    def continuationLinePrompt():
        return "." * len(comp.getNS().__name__) + ".. " if os.isatty(0) else ""

    while True:
        # Evaluating before prompting caters for initially given forms.
        r = StringReader(line)
        while True:
            try:
                s = read(r, False, None, True)
                if s is None:
                    break
                res = comp.compile(s)
                out = comp.executeCode(res)
            except Exception:
                traceback.print_exc()
            else:
                if os.isatty(0):
                    RT.printTo(out)
                last3.pop()
                last3.insert(0, out)
                for i, value in enumerate(last3, 1):
                    v = findItem(Namespace("clojure.core"),
                                 Symbol("*{0}".format(i)))
                    if isinstance(value, Var):
                        v.bindRoot(value.deref())
                        v.setMeta(value.meta())
                    else:
                        v.bindRoot(value)
        try:
            line = raw_input(firstLinePrompt())
            while unbalanced(line):
                line += "\n" + raw_input(continuationLinePrompt())
        except BracketsException as exc:
            print exc
            continue
        except EOFError:
            print
            break
예제 #17
0
 def eval(self, code):
     r = StringReader(code)
     s = read(r, True, None, True)
     res = self.comp.compile(s)
     return self.comp.executeCode(res)
예제 #18
0
 def testRegexPattern_PASS(self):
     for k, v in regexPatternMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False).pattern, v.pattern)
예제 #19
0
 def testCharacterReader_PASS(self):
     for k, v in literalCharacterMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #20
0
 def testFloatingPointReader_PASS(self):
     for k, v in floatingPointMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #21
0
 def testRawRegexPattern_PASS(self):
     for k, v in rawRegexPatternMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False).pattern, v.pattern)
예제 #22
0
 def testReturnedType_PASS(self):
     for k, v in returnedType_PASS.items():
         r = StringReader(k)
         self.assertEqual(type(read(r, False, sentinal, False)), v)
예제 #23
0
 def testStringReader_PASS(self):
     for k, v in literalStringMap_PASS.items():
         r = StringReader('"' + k + '"')
         self.assertEqual(read(r, False, EOF, False), v)
예제 #24
0
 def testCharacterReader_PASS(self):
     for k, v in literalCharacterMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #25
0
 def testRationalReader_PASS(self):
     for k, v in rationalMap_PASS.items():
         r = StringReader(k)
         self.assertEqual(read(r, False, EOF, False), v)
예제 #26
0
 def testStringReader_PASS(self):
     for k, v in literalStringMap_PASS.items():
         r = StringReader('"' + k + '"')
         self.assertEqual(read(r, False, EOF, False), v)