Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Lib/** linguist-vendored
Cargo.lock linguist-generated -merge
*.snap linguist-generated -merge
ast/src/ast_gen.rs linguist-generated -merge
vm/src/stdlib/ast/gen.rs linguist-generated -merge
parser/python.lalrpop text eol=LF
compiler/parser/python.lalrpop text eol=LF
Lib/*.py text working-tree-encoding=UTF-8 eol=LF
**/*.rs text working-tree-encoding=UTF-8 eol=LF
38 changes: 19 additions & 19 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: dtolnay/rust-toolchain@stable
- name: Set up the Windows environment
shell: bash
Expand Down Expand Up @@ -156,8 +156,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}

- uses: dtolnay/rust-toolchain@stable
with:
Expand Down Expand Up @@ -226,8 +226,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}

- uses: dtolnay/rust-toolchain@stable
- uses: actions/setup-python@v2
Expand Down Expand Up @@ -288,17 +288,17 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- name: Check if cached generated parser exists
id: generated_parser
uses: andstor/file-existence-action@v1
with:
files: "parser/python.rs"
files: "compiler/parser/python.rs"
- if: runner.os == 'Windows'
name: Force python.lalrpop to be lf # actions@checkout ignore .gitattributes
run: |
set file parser/python.lalrpop; ((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file
set file compiler/parser/python.lalrpop; ((Get-Content $file) -join "`n") + "`n" | Set-Content -NoNewline $file
- name: Install lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
uses: baptiste0928/cargo-install@v1
Expand All @@ -307,7 +307,7 @@ jobs:
version: "0.19.8"
- name: Run lalrpop
if: steps.generated_parser.outputs.files_exists == 'false'
run: lalrpop parser/python.lalrpop
run: lalrpop compiler/parser/python.lalrpop

lint:
name: Check Rust code with rustfmt and clippy
Expand All @@ -318,8 +318,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
Expand Down Expand Up @@ -351,8 +351,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
Expand All @@ -372,8 +372,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v1
- name: install wasm-pack
Expand Down Expand Up @@ -420,8 +420,8 @@ jobs:
- name: Cache generated parser
uses: actions/cache@v2
with:
path: parser/python.rs
key: lalrpop-${{ hashFiles('parser/python.lalrpop') }}
path: compiler/parser/python.rs
key: lalrpop-${{ hashFiles('compiler/parser/python.lalrpop') }}
- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-wasi
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ flamescope.json
extra_tests/snippets/resources
extra_tests/not_impl.py

parser/python.rs
compiler/parser/python.rs
37 changes: 18 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
[workspace]
resolver = "2"
members = [
".", "ast", "bytecode", "common", "compiler", "compiler/porcelain",
"derive", "jit", "parser", "vm", "vm/pylib-crate", "stdlib", "wasm/lib",
"compiler", "compiler/ast", "compiler/bytecode", "compiler/codegen", "compiler/parser",
".", "common", "derive", "jit", "vm", "vm/pylib-crate", "stdlib", "wasm/lib",
]

[features]
Expand All @@ -36,10 +36,10 @@ ssl-vendor = ["rustpython-stdlib/ssl-vendor"]
log = "0.4.16"
env_logger = { version = "0.9.0", default-features = false, features = ["atty", "termcolor"] }
clap = "2.34"
rustpython-compiler = { path = "compiler/porcelain", version = "0.1.1" }
rustpython-parser = { path = "parser", version = "0.1.1" }
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compile-parse"] }
rustpython-stdlib = {path = "stdlib", optional = true, default-features = false, features = ["compile-parse"]}
rustpython-compiler = { path = "compiler", version = "0.1.1" }
rustpython-parser = { path = "compiler/parser", version = "0.1.1" }
rustpython-vm = { path = "vm", version = "0.1.1", default-features = false, features = ["compiler"] }
rustpython-stdlib = {path = "stdlib", optional = true, default-features = false}
dirs = { package = "dirs-next", version = "2.0.0" }
num-traits = "0.2.14"
cfg-if = "1.0.0"
Expand Down
14 changes: 7 additions & 7 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ exists a raw html viewer which is currently broken, and we welcome a PR to fix i
Understanding a new codebase takes time. Here's a brief view of the
repository's structure:

- `bytecode/src`: python bytecode representation in rust structures
- `compiler/src`: python compilation to bytecode
- `bytecode/src`: python bytecode representation in rust structures
- `parser/src`: python lexing, parsing and ast
- `derive/src`: Rust language extensions and macros specific to rustpython
- `parser/src`: python lexing, parsing and ast
- `Lib`: Carefully selected / copied files from CPython sourcecode. This is
the python side of the standard library.
- `test`: CPython test suite
Expand All @@ -137,7 +137,7 @@ implementation is found in the `src` directory (specifically, `src/lib.rs`).

The top-level `rustpython` binary depends on several lower-level crates including:

- `rustpython-parser` (implementation in `parser/src`)
- `rustpython-parser` (implementation in `compiler/parser/src`)
- `rustpython-compiler` (implementation in `compiler/src`)
- `rustpython-vm` (implementation in `vm/src`)

Expand All @@ -155,15 +155,15 @@ enable a line of code to go through a series of steps:
This crate contains the lexer and parser to convert a line of code to
an Abstract Syntax Tree (AST):

- Lexer: `parser/src/lexer.rs` converts Python source code into tokens
- Parser: `parser/src/parser.rs` takes the tokens generated by the lexer and parses
- Lexer: `compiler/parser/src/lexer.rs` converts Python source code into tokens
- Parser: `compiler/parser/src/parser.rs` takes the tokens generated by the lexer and parses
the tokens into an AST (Abstract Syntax Tree) where the nodes of the syntax
tree are Rust structs and enums.
- The Parser relies on `LALRPOP`, a Rust parser generator framework. The
LALRPOP definition of Python's grammar is in `parser/src/python.lalrpop`.
LALRPOP definition of Python's grammar is in `compiler/parser/src/python.lalrpop`.
- More information on parsers and a tutorial can be found in the
[LALRPOP book](https://lalrpop.github.io/lalrpop/).
- AST: `ast/` implements in Rust the Python types and expressions
- AST: `compiler/ast/` implements in Rust the Python types and expressions
represented by the AST nodes.

### rustpython-compiler
Expand Down
22 changes: 6 additions & 16 deletions compiler/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
[package]
name = "rustpython-compiler-core"
name = "rustpython-compiler"
version = "0.1.2"
description = "Compiler for python code into bytecode for the rustpython VM."
description = "A usability wrapper around rustpython-parser and rustpython-compiler-core"
authors = ["RustPython Team"]
repository = "https://github.com/RustPython/RustPython"
license = "MIT"
edition = "2021"

[dependencies]
indexmap = "1.8.1"
itertools = "0.10.3"
rustpython-bytecode = { path = "../bytecode", version = "0.1.1" }
rustpython-ast = { path = "../ast", features = ["unparse"] }
num-complex = { version = "0.4.0", features = ["serde"] }
num-traits = "0.2.14"
log = "0.4.16"
ahash = "0.7.6"

[dev-dependencies]
rustpython-parser = { path = "../parser" }
insta = "1.14.0"
thiserror = "1.0"
rustpython-codegen = { path = "codegen" }
rustpython-parser = { path = "parser" }
rustpython-bytecode = { path = "bytecode" }
2 changes: 1 addition & 1 deletion ast/Cargo.toml → compiler/ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ unparse = ["rustpython-common"]

[dependencies]
num-bigint = "0.4.3"
rustpython-common = { path = "../common", optional = true }
rustpython-common = { path = "../../common", optional = true }
rustpython-bytecode = { path = "../bytecode"}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions compiler/codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "rustpython-codegen"
version = "0.1.2"
description = "Compiler for python code into bytecode for the rustpython VM."
authors = ["RustPython Team"]
repository = "https://github.com/RustPython/RustPython"
license = "MIT"
edition = "2021"

[dependencies]
indexmap = "1.8.1"
itertools = "0.10.3"
rustpython-bytecode = { path = "../bytecode", version = "0.1.1" }
rustpython-ast = { path = "../ast", features = ["unparse"] }
num-complex = { version = "0.4.0", features = ["serde"] }
num-traits = "0.2.14"
log = "0.4.16"
ahash = "0.7.6"

[dev-dependencies]
rustpython-parser = { path = "../parser" }
insta = "1.14.0"
20 changes: 10 additions & 10 deletions compiler/src/compile.rs → compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
//! <https://github.com/python/cpython/blob/main/Python/compile.c>
//! <https://github.com/micropython/micropython/blob/master/py/compile.c>

use crate::ir::{self, CodeInfo};
pub use crate::mode::Mode;
use crate::symboltable::{make_symbol_table, make_symbol_table_expr, SymbolScope, SymbolTable};
use crate::IndexSet;
use crate::{
error::{CompileError, CompileErrorType},
symboltable,
ir,
symboltable::{self, make_symbol_table, make_symbol_table_expr, SymbolScope, SymbolTable},
IndexSet,
};
use itertools::Itertools;
use num_complex::Complex64;
Expand All @@ -20,6 +18,8 @@ use rustpython_ast as ast;
use rustpython_bytecode::{self as bytecode, CodeObject, ConstantData, Instruction};
use std::borrow::Cow;

pub use crate::mode::Mode;

type CompileResult<T> = Result<T, CompileError>;

#[derive(PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -61,7 +61,7 @@ fn is_forbidden_name(name: &str) -> bool {

/// Main structure holding the state of compilation.
struct Compiler {
code_stack: Vec<CodeInfo>,
code_stack: Vec<ir::CodeInfo>,
symbol_table_stack: Vec<SymbolTable>,
source_path: String,
current_source_location: ast::Location,
Expand Down Expand Up @@ -199,7 +199,7 @@ pub fn compile_expression(

impl Compiler {
fn new(opts: CompileOpts, source_path: String, code_name: String) -> Self {
let module_code = CodeInfo {
let module_code = ir::CodeInfo {
flags: bytecode::CodeFlags::NEW_LOCALS,
posonlyarg_count: 0,
arg_count: 0,
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Compiler {

self.symbol_table_stack.push(table);

let info = CodeInfo {
let info = ir::CodeInfo {
flags,
posonlyarg_count,
arg_count,
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Compiler {
fn _name_inner(
&mut self,
name: &str,
cache: impl FnOnce(&mut CodeInfo) -> &mut IndexSet<String>,
cache: impl FnOnce(&mut ir::CodeInfo) -> &mut IndexSet<String>,
) -> bytecode::NameIdx {
let name = self.mangle(name);
let cache = cache(self.current_codeinfo());
Expand Down Expand Up @@ -2583,7 +2583,7 @@ impl Compiler {
self.emit(Instruction::LoadConst { idx })
}

fn current_codeinfo(&mut self) -> &mut CodeInfo {
fn current_codeinfo(&mut self) -> &mut ir::CodeInfo {
self.code_stack.last_mut().expect("no code on stack")
}

Expand Down
File renamed without changes.
File renamed without changes.
Loading