forked from drewr/clojure-py
/
clojure.py
executable file
·99 lines (74 loc) · 2.27 KB
/
clojure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
try:
import readline
except ImportError:
pass
import sys
from py.clojure.lang.lispreader import read
from py.clojure.lang.fileseq import StringReader
from py.clojure.lang.globals import currentCompiler
import py.clojure.lang.rt as RT
from py.clojure.lang.compiler import Compiler
from py.clojure.lang.symbol import Symbol
def requireClj(filename, stopafter = None):
with open(filename) as fl:
r = StringReader(fl.read())
RT.init()
comp = Compiler()
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
def main():
requireClj("./clj/clojure/core.clj")
RT.init()
comp = Compiler()
currentCompiler.set(comp)
comp.setNS(Symbol.intern("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:
import traceback; traceback.print_exc()
def unbalanced(s):
return (s.count('(') != s.count(')')
or s.count('[') != s.count(']')
or s.count('{') != s.count('}'))
if __name__ == "__main__":
main()