Skip to content

Commit 1edbfd8

Browse files
committed
Added the shinglyu/RustPython VM
1 parent cb394eb commit 1edbfd8

118 files changed

Lines changed: 3248 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# RustPython
22
A Python Interpreter written in Rust
33

4+
# Code organization
5+
The files in the top level directory are from [windelbouwman/rspython][rspython] which contains an implementation of the parser and vm in `src/`
6+
7+
An alternative implementation of python virtual machine that are compatible with CPython parser are from [shinglyu/RustPython][rustpython] and is located in the `VM/` folder.
8+
9+
We are in the process of merging the two implementation to form a single implementation.
10+
411
# Credit
512
The initial work was based on [windelbouwman/rspython](https://github.com/windelbouwman/rspython) and [shinglyu/RustPython](https://github.com/shinglyu/RustPython)
13+
14+
[rspython]: https://github.com/windelbouwman/rspython
15+
[rustpython]: https://github.com/shinglyu/RustPython
16+

VM/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Shing Lyu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

VM/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
RustPython
2+
==============
3+
4+
A Python interpreter written in Rust
5+
6+
# Installation
7+
8+
```
9+
bash init_env.sh
10+
```
11+
12+
# Run
13+
14+
```
15+
./test.sh <path/to/file.py> # compile and run
16+
./test.sh <path/to/file.py> --bytecode # print the bytecode in JSON
17+
./test.sh <path/to/file.py> --dis # Run python -m dis
18+
```
19+
20+
## Manual
21+
Given a python file `test.py`
22+
23+
```
24+
python compile_code.py test.py > test.bytecode
25+
26+
cd RustPython
27+
cargo run ../test.bytecode
28+
```
29+
30+
# Testing & debugging
31+
32+
```
33+
./test_all.sh # Run all tests under tests/
34+
```
35+
36+
* If a test is expected to fail or raise exception, add `xfail_*` prefix to the filename.
37+
38+
## Logging
39+
40+
```
41+
RUST_LOG=debug ./tests_all.sh
42+
```
43+
44+
# TODOs
45+
* Native types => Partial
46+
* Control flow => if(v)
47+
* assert => OK
48+
* Structural types (list, tuple, object)
49+
* Strings
50+
* Function calls => Blocked by bytecode serializer
51+
* Modules import
52+
* Generators
53+
54+
55+
# Goals
56+
* Support all builtin functions
57+
* Runs the [pybenchmark](https://pybenchmarks.org/) benchmark test
58+
* Run famous/popular python modules (which?)
59+
60+
* Compatible with CPython 3.6
61+
62+
# Rust version
63+
rustc 1.20.0-nightly
64+

VM/RustPython/Cargo.lock

Lines changed: 205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

VM/RustPython/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rustpython"
3+
version = "0.1.0"
4+
authors = ["Shing Lyu <shing.lyu@gmail.com>"]
5+
6+
[dependencies]
7+
log = "0.3"
8+
env_logger = "0.3"
9+
serde = "0.8.22"
10+
serde_derive = "0.8"
11+
serde_json = "0.8"

VM/RustPython/src/builtins.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use NativeType;
2+
use std::rc::Rc;
3+
use std::ops::Deref;
4+
5+
pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
6+
for elem in args {
7+
// TODO: figure out how python's print vectors
8+
match elem.deref() {
9+
&NativeType::NoneType => println!("None"),
10+
&NativeType::Boolean(ref b)=> {
11+
if *b {
12+
println!("True");
13+
} else {
14+
println!("False");
15+
}
16+
},
17+
&NativeType::Int(ref x) => println!("{}", x),
18+
&NativeType::Float(ref x) => println!("{}", x),
19+
&NativeType::Str(ref x) => println!("{}", x),
20+
&NativeType::Unicode(ref x) => println!("{}", x),
21+
_ => panic!("Print for {:?} not implemented yet", elem),
22+
/*
23+
List(Vec<NativeType>),
24+
Tuple(Vec<NativeType>),
25+
Iter(Vec<NativeType>), // TODO: use Iterator instead
26+
Code(PyCodeObject),
27+
Function(Function),
28+
#[serde(skip_serializing, skip_deserializing)]
29+
NativeFunction(fn(Vec<NativeType>) -> NativeType ),
30+
*/
31+
}
32+
}
33+
NativeType::NoneType
34+
}
35+
36+
pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
37+
if args.len() != 1 {
38+
panic!("len(s) expects exactly one parameter");
39+
}
40+
let len = match args[0].deref() {
41+
&NativeType::List(ref l) => l.borrow().len(),
42+
&NativeType::Tuple(ref t) => t.len(),
43+
&NativeType::Str(ref s) => s.len(),
44+
_ => panic!("TypeError: object of this type has no len()")
45+
};
46+
NativeType::Int(len as i32)
47+
}

0 commit comments

Comments
 (0)