-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTranslator.html
More file actions
167 lines (163 loc) · 7.14 KB
/
Translator.html
File metadata and controls
167 lines (163 loc) · 7.14 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Pyjs</title>
<link rel="stylesheet" type="text/css" media="screen" href="assets/main.css"/>
</head>
<body>
<div id="menu">
<div id="logo"><a href="./" title="Pyjs!"><img src="assets/images/pyjs.128x128.png" alt="Pyjs Logo" /></a></div>
<ul class="simple">
<li class="section">About</li>
<li><a class="reference external" href="About.html">About</a></li>
<li><a class="reference external" href="Overview.html">Overview</a></li>
<li><a class="reference external" href="Translator.html">Translator</a></li>
<li><a class="reference external" href="Download.html">Download</a></li>
<li><a class="reference external" href="GettingHelp.html">Getting Help</a></li>
<li class="section">Documentation</li>
<li><a class="reference external" href="examples">Examples</a></li>
<li><a class="reference external" href="UIHierarchy.html">UI Hierarchy</a></li>
<li><a class="reference external" href="api">API Docs</a></li>
<li><a class="reference external" href="book/Bookreader.html">Book</a></li>
<li><a class="reference external" href="https://github.com/pyjs/pyjs/wiki">Wiki</a></li>
<li class="section">Development</li>
<li><a class="reference external" href="Developing.html">Develop</a></li>
<li><a class="reference external" href="Optimize.html">Optimize</a></li>
<li><a class="reference external" href="Contribute.html">Contribute</a></li>
<li><a class="reference external" href="Roadmap.html">Roadmap</a></li>
</ul>
</div>
<div id="body">
<h1 class="title">pyjs Python-to-JavaScript Translator</h1>
<p>The pyjs Python-to-JavaScript compiler is actually a <a class="reference external" href="http://en.wikipedia.org/wiki/Translator_(computing)">language translator</a>. It has two main modes: <tt class="docutils literal"><span class="pre">-O</span></tt> and <tt class="docutils literal"><span class="pre">--strict</span></tt>. The <tt class="docutils literal"><span class="pre">--strict</span></tt> mode aims for <em>full</em> Python interoperability, even at the expense of performance, whilst the <tt class="docutils literal"><span class="pre">-O</span></tt> mode is, like gcc's <tt class="docutils literal"><span class="pre">-O</span></tt> option, "optimised" for speed, even at the expense of missing out certain Python language features and relying on JavaScript instead. The <a class="reference external" href="https://github.com/pyjs/pyjs/wiki/migrationguide">Migration Guide</a> best describes the differences.</p>
<div class="section" id="supported-syntax">
<h1>Supported Syntax</h1>
<p>The majority of Python 2.5, and parts of Python 2.6 syntax are supported, including yield <em>despite</em> only one web browser engine having support for ECMAScript "yield" (which is <strong>not</strong> used). The yield support is
implemented as a state machine, rewriting the code so that it is re-entrant, and can resume on each yield. The features <em>not</em> supported are pretty small and obscure, and include for example, at the time of writing,
assignment to variables of list items in for-loop syntax is not (<tt class="docutils literal">for [a, b] in enumerate(l)</tt>), whereas
assignment to tuples is (<tt class="docutils literal">for (a, b) in enumerate(l)</tt>). Overall it is best to read the <a class="reference external" href="https://github.com/pyjs/pyjs/wiki/migrationguide">Migration Guide</a> and go from there.</p>
<p>Multiple inheritance is also supported, as is the creation of metaclasses using three arguments to the builtin "type" function (but a call to "type" with only one argument is not supported, and you should be using isinstance for that, anyway). Object introspection is supported, as are decorators, static methods,
exceptions and so on. __new__ however, at the time of writing, is a little iffy. unicode is <em>not</em> supported, as it would be horrendous to implement in JavaScript, but the long type <em>is</em> supported, emulated correctly as JavaScript.</p>
<p>One particular feature worth noting: the pyjs Python-to-JavaScript compiler uses the internal features of Python (its compile module) to turn Python into an Abstract Syntax Tree. This had the disadvantage that the compiler was tied to one specific version of Python (2.5). As of pyjs 0.6, it has become possible to use anything from Python 2.4 and upwards to compile code with Python 2.5 syntax, using the
<tt class="docutils literal"><span class="pre">--internal-ast</span></tt> option. A port of lib2to3 was used as the basis for an AST which is identical to that created by the C code in the <a class="reference external" href="http://python.org">CPython</a> compiler. The plan is to use this at some point to implement "exec" and "eval" builtins, but it has the nice side-effect of allowing people who can only use specific versions of Python to reliably compile pyjs Python into JavaScript.</p>
<p>Overall, the general rule is that whoever wants or needs something, they either work around the limitations of JavaScript and the pyjs implementation, or they <a class="reference external" href="Contribute.html">contribute code</a> to improve the compiler/translator.</p>
</div>
<div class="section" id="supported-modules">
<h1>Supported Modules</h1>
<p>Additionally, the Python-to-JavaScript compiler has available to it some
implementations of standard Python modules, either in pure Python or in
hybrid JavaScript and using web browser built-ins. Here is a list of the
current Python modules that are partially supported. They have all been
contributed by pyjs developers, and anyone wishing to have particular
features that are not yet included should simply write them and
<a class="reference external" href="Contribute.html">make a Pull Request</a>:</p>
<pre class="literal-block">
base64.py
binascii.py
csv.py
datetime.py
math.py
md5.py
os.py
_random.py
random.py
re.py
sets.py
struct.py
sys.py
time.py
urllib.py
</pre>
</div>
<div class="section" id="supported-built-ins">
<h1>Supported Built-ins</h1>
<p>Here, also, is a list of supported built-in functions, classes and types.
Again: these have been contributed by various pyjs developers, and again,
if there is a particular function or feature missing, it can be submitted
as a <a class="reference external" href="Contribute.html">contribution</a> on GitHub.</p>
<p>Functions:</p>
<pre class="literal-block">
__import__
abs
all
any
bool
callable
chr
cmp
delattr
dir
divmod
enumerate
filter
float
getattr
hasattr
hash
hex
isinstance
issubclass
iter
len
map
max
min
oct
open
ord
pow
property
range
reduce
repr
reversed
round
setattr
sorted
staticmethod
str
sum
super
type
xrange
zip
</pre>
<p>Classes:</p>
<pre class="literal-block">
ArithmeticError
AttributeError
BaseException
Exception
GeneratorExit
ImportError
IndexError
KeyError
LookupError
NameError
NotImplemented
NotImplementedError
NotImplementedType
RuntimeError
StandardError
StopIteration
TypeError
ValueError
ZeroDivisionError
</pre>
<p>Types:</p>
<pre class="literal-block">
dict
frozenset
int
list
long
object
set
tuple
</pre>
</div>
</div>
</body>
</html>