Skip to content

Commit 6f856dc

Browse files
author
hartsantler
committed
Merge branch 'master' of https://github.com/PythonJS/PythonJS
2 parents e875860 + a89e797 commit 6f856dc

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
Introduction
22
------------
3-
PythonJS is a transpiler written in Python that converts Python into fast JavaScript. It can be run with regular Python, or fully self-hosted within NodeJS using Empythoned. PythonJS has been designed with speed and easy integration with existing JavaScript code in mind.
3+
PythonJS is a transpiler written in Python that converts Python into fast
4+
JavaScript. It can be run with regular Python, or fully self-hosted within
5+
NodeJS using Empythoned. PythonJS has been designed with speed and easy
6+
integration with existing JavaScript code in mind.
7+
8+
9+
Using translator.py Without Installing
10+
--------------------------------------
11+
To simply convert your python script into javascript, git clone this repo, and
12+
use translator.py located in the "pythonjs" directory. You can give it a list
13+
of python files to translate at once. It will output the translation to
14+
stdout. The default output type is JavaScript.
15+
16+
Usage::
17+
18+
translator.py [--dart|--coffee|--lua] file.py
19+
20+
Example::
21+
22+
cd pythonjs
23+
./translator.py myscript.py > myscript.js
24+
25+
426

527
Installing
628
-------------
7-
npm install python-js
29+
```
30+
npm install python-js
31+
```
832

933
NodeJS Quick Example
1034
--------------
@@ -71,20 +95,6 @@ A Python typed subset can be translated to a GLSL fragment shader to speed up ma
7195

7296

7397

74-
translator.py
75-
------------
76-
To simply convert your python script into javascript, git clone this repo, and use translator.py located in the "pythonjs" directory. You can give it a list of python files to translate at once. It will output the translation to stdout. The default output type is JavaScript.
77-
78-
Usage::
79-
80-
translator.py [--dart|--coffee|--lua] file.py
81-
82-
Example::
83-
84-
cd PythonJS/pythonjs
85-
./translator.py myscript.py > myscript.js
86-
87-
8898
Supported Features
8999
================
90100

pythonjs/python_to_pythonjs.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ def format_error(self, node):
125125
if self._line_number+1 < len(self._source):
126126
lines.append( self._source[self._line_number+1] )
127127

128-
return 'line %s\n%s\n%s' %(self._line_number, '\n'.join(lines), node)
128+
msg = 'line %s\n%s\n%s\n' %(self._line_number, '\n'.join(lines), node)
129+
msg += 'Depth Stack:\n'
130+
for l, n in enumerate(self._stack):
131+
#msg += str(dir(n))
132+
msg += '%s%s line:%s col:%s\n' % (' '*(l+1)*2, n.__class__.__name__, n.lineno, n.col_offset)
133+
return msg
129134

130135
def __init__(self, source=None, module=None, module_path=None, dart=False, coffee=False, lua=False):
131136

@@ -136,6 +141,7 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
136141

137142
self._line = None
138143
self._line_number = 0
144+
self._stack = [] ## current path to the root
139145

140146
self._direct_operators = set() ## optimize "+" and "*" operator
141147
self._with_ll = False ## lowlevel
@@ -247,6 +253,16 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
247253
else:
248254
self.visit(node)
249255

256+
def visit(self, node):
257+
"""Visit a node."""
258+
## modified code of visit() method from Python 2.7 stdlib
259+
self._stack.append(node)
260+
method = 'visit_' + node.__class__.__name__
261+
visitor = getattr(self, method, self.generic_visit)
262+
res = visitor(node)
263+
self._stack.pop()
264+
return res
265+
250266
def has_webworkers(self):
251267
return len(self._webworker_functions.keys())
252268

pythonjs/translator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
from pythonjs_to_lua import main as pythonjs_to_lua
99
from pythonjs_to_luajs import main as pythonjs_to_luajs
1010

11+
cmdhelp = """\
12+
usage: translator.py [--dart|--coffee|--lua] file.py
13+
translator.py --visjs file.py\
14+
"""
1115

1216
def main(script):
1317
if '--visjs' in sys.argv:
@@ -50,6 +54,10 @@ def main(script):
5054
return code
5155

5256
def command():
57+
if '-h' in sys.argv or '--help' in sys.argv:
58+
print(cmdhelp)
59+
return
60+
5361
scripts = []
5462
if len(sys.argv) > 1:
5563
for arg in sys.argv[1:]:

0 commit comments

Comments
 (0)