-
Notifications
You must be signed in to change notification settings - Fork 18
/
cli.py
executable file
·146 lines (128 loc) · 5.01 KB
/
cli.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import subprocess
import pyjaco
import ast
import select
from pyjaco import Compiler
import platform
import sys, os
import readline
from optparse import OptionParser
def parse_args():
parser = OptionParser(usage="""%prog [options] [<file.py>]""",
description="Pyjaco command-line interface.")
parser.add_option("-d", "--debug",
action = "store_true",
dest = "debug",
help = "Debug mode. Show all communication with the JS interpreter.")
return parser.parse_args()
def pyjaco_compile(compiler, code, printoutput):
if printoutput:
return "try {print($PY.repr(%s), '\\n')} finally {print('\\nMARK\\n')};\n" % "\n".join(compiler._compile(code).split("\n"))[:-1]
else:
return "try {%s} finally {print('\\nMARK\\n')};\n" % "".join(compiler._compile(code).split("\n"))
def start_checks():
if not os.path.exists("py-builtins.js"):
print "Standard library (py-builtins.js) not found. Please 'make stdlib'"
sys.exit(1)
def start_js():
try:
proc = subprocess.Popen("js", stdin = subprocess.PIPE, stdout = subprocess.PIPE)
except OSError, E:
print "Could not execute 'js' javascript engine: %s" % E
sys.exit(1)
proc.stdin.write("load('py-builtins.js');\n")
proc.stdin.write("print('MARK');\n");
while True:
line = proc.stdout.readline()
if line.endswith("MARK\n"):
break
return proc
def print_banner():
print "Python 2.x (pyjaco)"
print "[google v8] on %s" % platform.system()
print 'Type "help", "copyright", "credits" or "license" for more information.'
def interactive_read():
while True:
lastline = raw_input(">>> ").rstrip()
if lastline:
code = lastline
while True:
tree = None
try:
tree = ast.parse(code)
except SyntaxError, E:
if not E.msg.startswith("unexpected EOF"):
print "SyntaxError in code: %s" % E.message
linetype = type(tree.body[0]) if tree and tree.body else None
if linetype in (ast.Expr, ast.Print, ast.Assign) or lastline == "":
break
else:
while True:
lastline = raw_input("... ").rstrip()
code += "\n" + lastline
try:
tree = ast.parse(code)
except SyntaxError, E:
if not E.msg.startswith("unexpected EOF"):
print "SyntaxError in code: %s" % E.message
else:
if lastline == "":
break
break
return code, linetype
def run_code(compiler, options, code, linetype):
jscode = pyjaco_compile(compiler, code, printoutput = linetype in (ast.Expr, ))
if options.debug:
print "[v8 input] >>>%s<<<" % jscode.rstrip()
proc.stdin.write(jscode)
while True:
output = proc.stdout.readline()
if options.debug:
print "-[v8 reply] >>>%r<<<" % output
if output.startswith("d8> "):
output = output[4:]
if output == "\n":
continue
# Some versions of the v8 shell produce spurious (but harmless) "undefined" messages
elif output == "undefined\n":
continue
elif output == "MARK\n":
proc.stdin.write("\nprint('\\nSENTINEL\\n');\n");
while True:
output = proc.stdout.readline()
if output.startswith("d8> "):
output = output[4:]
if options.debug:
print "--[v8 reply] >>>%r<<<" % output
if output == "SENTINEL\n":
break
# Some versions of the v8 shell produce spurious (but harmless) "undefined" messages
elif output == "undefined\n":
continue
elif output == "\n":
continue
else:
print "Error:", output,
break
elif linetype in (ast.Expr, ast.Print) and output.rstrip(" \n") <> "None":
print output,
if __name__ == "__main__":
start_checks()
options, args = parse_args()
compiler = Compiler()
proc = start_js()
if args:
run_code(compiler, options, file(args[0]).read(), ast.Print)
else:
print_banner()
while True:
try:
code, linetype = interactive_read()
run_code(compiler, options, code, linetype)
except KeyboardInterrupt:
print "\nKeyboardInterrupt"
continue
except EOFError:
print
break