forked from drewr/clojure-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clojure.py
executable file
·117 lines (92 loc) · 2.77 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
import sys
import os.path
import traceback
try:
import readline
except ImportError:
pass
else:
import os
import atexit
histfile = os.path.join(os.path.expanduser("~"), ".clojurepyhist")
try:
readline.read_history_file(histfile)
except IOError:
# Pass here as there isn't any history file, so one will be
# written by atexit
pass
atexit.register(readline.write_history_file, histfile)
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, symbol
VERSION = "0.0.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
def main():
requireClj(os.path.dirname(__file__) + "/clj/clojure/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)
def unbalanced(s):
return (s.count('(') != s.count(')')
or s.count('[') != s.count(']')
or s.count('{') != s.count('}'))
if __name__ == "__main__":
main()