You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
contributor_excerpt:""# TODO: write something here, goes right under "Contributors" heading
32
-
blog-intro:Create an issue if youread something wrong. Edit posts or create new ones via PR on <a href="https://github.com/RustPython/rustpython.github.io">github.com/RustPython/rustpython.github.io</a>
32
+
blog-intro:Create an issue if yousee something wrong. Edit posts or create new ones via PR on <a href="https://github.com/RustPython/rustpython.github.io">github.com/RustPython/rustpython.github.io</a>
Copy file name to clipboardExpand all lines: _posts/2020-03-12-thing-explainer-parser.markdown
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,21 @@ date: 2020-04-02 11:34:01 -0400
6
6
7
7
This post goes over the RustPython parser. You can see the source code at[RustPython/parser/](https://github.com/RustPython/RustPython/tree/master/parser).
8
8
9
-
When you write code inpython and run it, an interpreter, such as the RustPython interpreter, acts as the translator between you and your machine.
9
+
When you write code inPython and run it, an interpreter, such as the RustPython interpreter, acts as the translator between you and your machine.
10
10
11
-
The interpreter has the job of turning your human code intobyte codethat apython virtual machine can run. Bytecode is an intermediate code between source code and machine code. This makes it portable across multiple hardware and operating systems. Bytecode "works" as long as you implement a virtual machine(vm) that can run it. There is a performance penalty for this flexibility. RustPython has a vm under[RustPython/vm/](https://github.com/RustPython/RustPython/tree/master/vm). Other posts, will go into the details of that vm but now let's figure out how to turn code into bytecode.
11
+
The interpreter has the job of turning your human code intobytecodethat aPython virtual machine can run. Bytecode is an intermediate code between source code and machine code. This makes it portable across multiple hardware and operating systems. Bytecode "works" as long as you implement a virtual machine(vm) that can run it. There is a performance penalty for this flexibility. RustPython has a vm under[RustPython/vm/](https://github.com/RustPython/RustPython/tree/master/vm). Other posts will go into the details of that vm but now let's figure out how to turn code into bytecode.
12
12
13
13
14
-
##How doesbytecodelook like
14
+
##Whatbytecodelooks like
15
15
16
-
Seeing is believing. To see what bytecode looks like, you can use a Python module called[`dis`](https://docs.python.org/3/library/dis.html). dis standsfordisassembler. You can write source code then see how its bytecode looks like. Here is an example:
16
+
Seeing is believing. To see what bytecode looks like, you can use a Python module called[`dis`](https://docs.python.org/3/library/dis.html)."dis" is short offor_dis_assembler. You can write source code then see how its bytecode looks like. Here is an example:
17
17
18
18

19
19
20
20
21
-
##How RustPython turns your codeto bytecode
21
+
##How RustPython turns your codeinto bytecode
22
22
23
-
Here are the main steps that RustPython currentlydoes:
23
+
Here are the main steps that RustPython currentlygoes through:
24
24
- parse the line of source code into tokens
25
25
- determine if the tokens have a valid syntax
26
26
- create an Abstract Syntax Tree (AST)
@@ -31,7 +31,7 @@ This list of steps introduces some new concepts like: tokens and abstract syntax
31
31
32
32
###Step 1: parsing source code into tokens
33
33
34
-
The fastest way to understand what tokens are, is to see them. Conveniently, Python comes with a[tokenizer](https://docs.python.org/3/library/tokenize.html).Here is whathappen if I run the tokenizer on the function that I createdearlier.
34
+
The fastest way to understand what tokens are, is to see them. Conveniently, Python comes with a[tokenizer](https://docs.python.org/3/library/tokenize.html). Here is whathappens if I run the tokenizer on the function that I createdabove.
35
35
36
36
`$ python -m tokenize file.py`
37
37
@@ -46,20 +46,20 @@ def add(x,y):
46
46

47
47
48
48
49
-
A picture IS worth a thousandword 😛 Those are the tokens. They are the basic "units"in the programming language. They are the keywords and operators that you typed. Even new lines andidentations count.
49
+
A picture IS worth a thousandwords 😛 Those are the tokens. They are the basic "units"of the programming language. They are the keywords and operators that you typed. Even new lines andidentation count.
50
50
51
51
If you want to sound fancy:
52
52
- The tokens are the basic "lexical components"
53
53
- The parsing process is called "lexical analysis"
If you want dive into the details of lexical analysis, check out[Python in a nutshell / Lexical structure](https://learning.oreilly.com/library/view/python-in-a/9781491913833/ch03.html#python_language-id00003)
62
+
If you wanttodive into the details of lexical analysis, check out[Python in a nutshell / Lexical structure](https://learning.oreilly.com/library/view/python-in-a/9781491913833/ch03.html#python_language-id00003)
63
63
64
64
65
65
###Step 2 : determine if the tokens are valid syntax
@@ -70,8 +70,8 @@ In the previous step, if you add random stuff to your function and tokenize it,
70
70
71
71
So don't hate on the whole interpreter when you get error messages! or at least don't hate on the tokenizer!
72
72
73
-
To determine if the tokens are valid syntax, first you need a definition of what a valid syntax is. Python has a defined "grammar" or set of rules. The official reference is on[this link](https://docs.python.org/3/reference/grammar.html). There, you will find a machine readable file. You may read a book to know the rules ofpython, but words are too "fluffy", an algorithm that verifies if rules are followed needs a very strict set of rules encoded in a file.[This video](https://www.youtube.com/watch?v=KGMFvy2d5OI) explains the Python grammar and the file's notation.
74
-
As the presenter puts it, this is the spirit of the beast (python) and it is only~10KB 😭 (compare that to the size ofpython books you had to read!)
73
+
To determine if the tokens are valid syntax, first you need a definition of what a valid syntax is. Python has a defined "grammar" or set of rules. The official reference is on[this link](https://docs.python.org/3/reference/grammar.html). There, you will find a machine readable file. You may read a book to know the rules ofPython, but words are too "fluffy", an algorithm that verifies if the rules are followed needs a very strict set of rules encoded in a file.[This video](https://www.youtube.com/watch?v=KGMFvy2d5OI) explains the Python grammar and the file's notation.
74
+
As the presenter puts it, this is the spirit of the beast (Python) and it is only~10KB 😭 (compare that to the size ofthe Python books you had to read!)
75
75
76
76
So, we have the rules or grammar of a programming language in a machine encoded format... now we need to write something that verifies that those rules were followed... This sounds like something that other people could use and like something that should exist as an open source project! 🤔
77
77
@@ -91,7 +91,7 @@ You can do:
91
91
92
92
##Recap 🥴 🥵
93
93
94
-
As a recap, when you write a line ofpython code and "run it", here is what the RustPython interpreter does:
94
+
As a recap, when you write a line ofPython code and "run it", here is what the RustPython interpreter does:
95
95
96
96
**INPUT: your code** (in`file.py` or interactive shell)
At the very end of 2019, we finally reached one ofthe short-term goals: CPython unittest support. Due to this enhancement,finding CPython compatibilityis easier thanbefore.
10
-
Probably thiswill be the major source ofcontribution spotsfor the new contributors this year. Here is a simple guideline.
9
+
At the very end of 2019, we finally reached one ofour short-term goals: CPython`unittest` support which makesfinding CPython compatibilityerrors easier thanever.
10
+
Thiswillprobablybe the major source ofcontributionsfor new contributors this year. Here is a simple guideline.
11
11
12
12
##Fix known compatibility bugs
13
13
Let's find an incompatibility issue and fix it.
14
-
15
-
1. See`Lib/test` directory of the project. There are many`test_` prefixed files like`test_unicode.py`.
14
+
1. Look at the`Lib/test` directory of the project. There are many`test_` prefixed files like`test_unicode.py`.
16
15
2. Try to open one of them. It might look just fine at a glance - but search for`TODO: RUSTPYTHON` in the files. There are tons of skipped, marked as an expected failure or commented out tests.
17
-
1. Alternatively, try looking at the[regressiontests results]({% link pages/regression-tests-results.markdown %}) to find skipped or expected failure tests; some of them have
16
+
1. Alternatively, try looking at the[regressiontest results]({% link pages/regression-tests-results.markdown %}) to find skipped or expected failure tests; some of them have
18
17
notes for a way to resolve the issue.
19
-
3. Choose one or two interesting bugs. Remove the test blocker - skip,expectedFailure or comments.
18
+
3. Choose one or two interesting bugs. Remove the test blocker -`skip`,`expectedFailure` or comments.
20
19
4. Try to fix them.
21
20
22
-
Here is a quick tip torun single unittest file.
21
+
Here's how yourunasingle unittest file:
23
22
24
-
`$ RUSTPYTHONPATH=Lib cargo run --release Lib/test/test_unicode.py`
23
+
`$ RUSTPYTHONPATH=Lib cargo run --release Lib/test/test_unicode.py`
25
24
26
25
##Add a new unittest file
27
-
Because CPython unittestis notperfectlyworkingin RustPython, we aredoing thisone by one with editings.
28
-
1. Download CPython source code.
29
-
2. Check out a specific version of CPython.For now,3.8.2 is recommended. (We are testing against CPython3.8and 3.8.2 isthe most recent version for now)
30
-
3. Copy a file from CPython`Lib/test`
31
-
4. Commit the file without editing. Specifycopied CPython versionto commit message.
26
+
Because CPython unittestdoesn't work workperfectly in RustPython, we areadding test filesone by one. Here's how:
2. Check out a specific version of CPython.We test against CPython3.8, so the most recent release of3.8([currently 3.8.7](https://www.python.org/doc/versions/)) isrecommended.
29
+
3. Copy a file from CPython's`Lib/test`
30
+
4. Commit the file without editing it. Specifythe CPython versionyou copied from in the commit message.
32
31
5. Try to edit it until it runs without a crash or failure.
33
32
6. Commit the changes to make it run. This is the core contribution.
34
33
35
-
Because RustPython is not perfect, "try to edit it until it runs" doesn't mean to make 100%successful running. The commoneditingmethodshere:
36
-
1.At least it must be able to start to run thetest. Fix the test code or bug until it runs at least a single unit of the test. Typically, unimplemented stdlib or missing filesofunittest canmake issues. Sometimes RustPython bugsmake issues too.
37
-
2. If any testis not loadable by`SyntaxError`,that part is requiredtobe commented out.
38
-
3. If any test leads to a crash of RustPython, this codeis not possible torun. Mark the test to skip.
39
-
4. If any testis runbut fails, this is an incompatibility issue. Mark the test as an expected failure.
34
+
Because RustPython is not perfect, "try to edit it until it runs" doesn't mean to makeit run100%of the tests successfully. The common methodsto make the test file pass are:
35
+
1.It must at least be able to start to run thetests. Fix the test code or bug until it runs at least a single unit of the test. Typically, unimplemented stdlib or missing filesin`unittest` cancause issues. Sometimes RustPython bugscause issues too.
36
+
2. If any testcan't be loaded because of a`SyntaxError`,you'll havetocomment that part out.
37
+
3. If any test leads to a crash of RustPython, this codecan't berun. Mark the testwith`@unittest.skip('TODO: RUSTPYTHON')`to skip it.
38
+
4. If any testrunsbut fails, this is an incompatibility issue. Mark the test as an expected failure with`@unittest.expectedFailure`.
40
39
41
-
We prefer the reversed order ofupper methods. The later the more strict soeasy to detect any progress or regression.
42
-
When we temporarily disable parts of unittest due to RustPython caveats, we mark them tofind itout easilylater. Pleasecheck the examples below or search for`TODO: RUSTPYTHON` in`Lib/test` directory to check actual usage.
40
+
We prefer the reversed order ofabove methods. The later, the more strict, soit's easier to detect any progress or regression.
41
+
When we temporarily disable parts of`unittest` due to RustPython caveats, we mark them tomake iteasier to find (and re-enable!) themlater. Pleasesee the examples below or search for`TODO: RUSTPYTHON` in the`Lib/test` directory to check actual usage.
43
42
44
43
Comment out:
45
44
```python
@@ -48,18 +47,18 @@ Comment out:
48
47
# def ... # commented out tests
49
48
```
50
49
51
-
skip:
50
+
`skip`:
52
51
```python
53
52
@unittest.skip("TODO: RUSTPYTHON")
54
53
def...# skipped tests
55
54
```
56
55
57
-
expectedFailure:
56
+
`expectedFailure`:
58
57
```python
59
58
#TODO: RUSTPYTHON
60
59
@unittest.expectedFailure
61
60
def...# failed tests
62
61
```
63
62
64
63
##Development guide
65
-
Forthe generalsource of the development, please visit the[RustPython development guide](https://github.com/RustPython/RustPython/blob/master/DEVELOPMENT.md)
64
+
Fora generalintroduction to RustPython development, please visit the[RustPython development guide](https://github.com/RustPython/RustPython/blob/master/DEVELOPMENT.md)
Copy file name to clipboardExpand all lines: index.markdown
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ installation:
26
26
27
27
goals:
28
28
-goal:
29
-
Full Python-3 environment entirely in Rust (not CPython bindings), with
29
+
Full Python3 environment entirely in Rust (not CPython bindings), with
30
30
a clean implementation and no compatiblity hacks.
31
31
# TODO: integrate this into the "goals" boxes
32
32
progress:
@@ -53,10 +53,10 @@ There are many implementations of Python. For example:
53
53
-[PyPy](https://www.pypy.org/) (Python)
54
54
-[Stackless](http://www.stackless.com/)
55
55
56
-
Each of these implementations offer some benefits: Jython, for example, compiles Python source code to Java byte code, then routes it to the Java Virtual Machine. Because Python code is translated to Javabyte code, it looks and feels like a true Java program at runtime and so it integrates well with Java applications.
56
+
Each of these implementations offer some benefits: Jython, for example, compiles Python2source code to Java byte code, then routes it to the Java Virtual Machine. Because Python code is translated to Javabytecode, it looks and feels like a true Java program at runtime and so it integrates well with Java applications.
57
57
58
58
IronPython is well-integrated with .NET, which means IronPython can use the .NET framework and Python libraries or vice versa.
59
59
60
60
We want to unlock the same possibilities that Jython and IronPython enable, but for the Rust programming language. In addition, thanks to Rusts' minimal runtime, we're able to compile RustPython to WebAssembly and allow users to run their Python code easily in the browser.
61
61
62
-
Check the "learn more" section for an explainer of all those jargon-y words, or read the blog for more in-depth technical discussion.
62
+
Checkoutthe "learn more" section for an explainer of all those jargon-y words, or read the blog for more in-depth technical discussion.