Skip to content

Commit 4d0d9ff

Browse files
committed
Refactor symboltables and _ast to use codegen when possible
1 parent c7ad41e commit 4d0d9ff

File tree

11 files changed

+56
-55
lines changed

11 files changed

+56
-55
lines changed

Cargo.lock

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

compiler/codegen/src/compile.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use crate::{
99
error::{CompileError, CompileErrorType},
1010
ir,
11-
symboltable::{self, make_symbol_table, make_symbol_table_expr, SymbolScope, SymbolTable},
11+
symboltable::{self, SymbolScope, SymbolTable},
1212
IndexSet,
1313
};
1414
use itertools::Itertools;
@@ -149,7 +149,7 @@ pub fn compile_program(
149149
ast,
150150
source_path,
151151
opts,
152-
make_symbol_table,
152+
SymbolTable::scan_program,
153153
Compiler::compile_program,
154154
)
155155
}
@@ -164,7 +164,7 @@ pub fn compile_program_single(
164164
ast,
165165
source_path,
166166
opts,
167-
make_symbol_table,
167+
SymbolTable::scan_program,
168168
Compiler::compile_program_single,
169169
)
170170
}
@@ -178,7 +178,7 @@ pub fn compile_block_expression(
178178
ast,
179179
source_path,
180180
opts,
181-
make_symbol_table,
181+
SymbolTable::scan_program,
182182
Compiler::compile_block_expr,
183183
)
184184
}
@@ -192,7 +192,7 @@ pub fn compile_expression(
192192
ast,
193193
source_path,
194194
opts,
195-
make_symbol_table_expr,
195+
SymbolTable::scan_expr,
196196
Compiler::compile_eval,
197197
)
198198
}
@@ -2697,7 +2697,7 @@ fn compile_constant(value: &ast::Constant) -> ConstantData {
26972697
#[cfg(test)]
26982698
mod tests {
26992699
use super::{CompileOpts, Compiler};
2700-
use crate::symboltable::make_symbol_table;
2700+
use crate::symboltable::SymbolTable;
27012701
use rustpython_bytecode::CodeObject;
27022702
use rustpython_parser::parser;
27032703

@@ -2708,7 +2708,7 @@ mod tests {
27082708
"<module>".to_owned(),
27092709
);
27102710
let ast = parser::parse_program(source).unwrap();
2711-
let symbol_scope = make_symbol_table(&ast).unwrap();
2711+
let symbol_scope = SymbolTable::scan_program(&ast).unwrap();
27122712
compiler.compile_program(&ast, symbol_scope).unwrap();
27132713
compiler.pop_code_object()
27142714
}

compiler/codegen/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ pub mod error;
1313
pub mod ir;
1414
pub mod mode;
1515
pub mod symboltable;
16+
17+
pub use compile::{CompileOpts, Mode};

compiler/codegen/src/symboltable.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ use crate::{
1414
use rustpython_ast::{self as ast, Location};
1515
use std::{borrow::Cow, fmt};
1616

17-
pub fn make_symbol_table(program: &[ast::Stmt]) -> SymbolTableResult<SymbolTable> {
18-
let mut builder = SymbolTableBuilder::new();
19-
builder.scan_statements(program)?;
20-
builder.finish()
21-
}
22-
23-
pub fn make_symbol_table_expr(expr: &ast::Expr) -> SymbolTableResult<SymbolTable> {
24-
let mut builder = SymbolTableBuilder::new();
25-
builder.scan_expression(expr, ExpressionContext::Load)?;
26-
builder.finish()
27-
}
28-
2917
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.
3018
#[derive(Clone)]
3119
pub struct SymbolTable {
@@ -60,6 +48,18 @@ impl SymbolTable {
6048
sub_tables: vec![],
6149
}
6250
}
51+
52+
pub fn scan_program(program: &[ast::Stmt]) -> SymbolTableResult<Self> {
53+
let mut builder = SymbolTableBuilder::new();
54+
builder.scan_statements(program)?;
55+
builder.finish()
56+
}
57+
58+
pub fn scan_expr(expr: &ast::Expr) -> SymbolTableResult<Self> {
59+
let mut builder = SymbolTableBuilder::new();
60+
builder.scan_expression(expr, ExpressionContext::Load)?;
61+
builder.finish()
62+
}
6363
}
6464

6565
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

compiler/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ use rustpython_parser::{
66
};
77
use std::fmt;
88

9-
pub use compile::{CompileOpts, Mode};
10-
pub use symboltable::{Symbol, SymbolScope, SymbolTable, SymbolTableType};
11-
129
#[derive(Debug, thiserror::Error)]
1310
pub enum CompileErrorType {
1411
#[error(transparent)]
@@ -76,7 +73,7 @@ pub fn compile(
7673
source: &str,
7774
mode: compile::Mode,
7875
source_path: String,
79-
opts: CompileOpts,
76+
opts: compile::CompileOpts,
8077
) -> Result<CodeObject, CompileError> {
8178
let parser_mode = match mode {
8279
compile::Mode::Exec => parser::Mode::Module,
@@ -105,11 +102,11 @@ pub fn compile_symtable(
105102
let res = match mode {
106103
compile::Mode::Exec | compile::Mode::Single | compile::Mode::BlockExpr => {
107104
let ast = parser::parse_program(source).map_err(parse_err)?;
108-
symboltable::make_symbol_table(&ast)
105+
symboltable::SymbolTable::scan_program(&ast)
109106
}
110107
compile::Mode::Eval => {
111108
let expr = parser::parse_expression(source).map_err(parse_err)?;
112-
symboltable::make_symbol_table_expr(&expr)
109+
symboltable::SymbolTable::scan_expr(&expr)
113110
}
114111
};
115112
res.map_err(|e| CompileError::from_symtable(e, source, source_path.to_owned()))

derive/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ edition = "2021"
1111
proc-macro = true
1212

1313
[dependencies]
14+
rustpython-compiler = { path = "../compiler", version = "0.1.1" }
15+
rustpython-bytecode = { path = "../compiler/bytecode", version = "0.1.1" }
16+
rustpython-codegen = { path = "../compiler/codegen", version = "0.1.1" }
17+
1418
syn = { version = "1.0.91", features = ["full", "extra-traits"] }
1519
syn-ext = { version = "0.4.0", features = ["full"] }
1620
quote = "1.0.18"
1721
proc-macro2 = "1.0.37"
18-
rustpython-bytecode = { path = "../compiler/bytecode", version = "0.1.1" }
19-
rustpython-compiler = { path = "../compiler", version = "0.1.1" }
2022
maplit = "1.0.2"
2123
once_cell = "1.10.0"
2224
textwrap = { version = "0.15.0", default-features = false }

derive/src/compile_bytecode.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use once_cell::sync::Lazy;
1818
use proc_macro2::{Span, TokenStream};
1919
use quote::quote;
2020
use rustpython_bytecode::{CodeObject, FrozenModule};
21-
use rustpython_compiler as compile;
21+
use rustpython_codegen as codegen;
22+
use rustpython_compiler::compile;
2223
use std::{
2324
collections::HashMap,
2425
env, fs,
@@ -54,23 +55,21 @@ impl CompilationSource {
5455
fn compile_string<D: std::fmt::Display, F: FnOnce() -> D>(
5556
&self,
5657
source: &str,
57-
mode: compile::Mode,
58+
mode: codegen::Mode,
5859
module_name: String,
5960
origin: F,
6061
) -> Result<CodeObject, Diagnostic> {
61-
compile::compile(source, mode, module_name, compile::CompileOpts::default()).map_err(
62-
|err| {
63-
Diagnostic::spans_error(
64-
self.span,
65-
format!("Python compile error from {}: {}", origin(), err),
66-
)
67-
},
68-
)
62+
compile(source, mode, module_name, codegen::CompileOpts::default()).map_err(|err| {
63+
Diagnostic::spans_error(
64+
self.span,
65+
format!("Python compile error from {}: {}", origin(), err),
66+
)
67+
})
6968
}
7069

7170
fn compile(
7271
&self,
73-
mode: compile::Mode,
72+
mode: codegen::Mode,
7473
module_name: String,
7574
) -> Result<HashMap<String, FrozenModule>, Diagnostic> {
7675
match &self.kind {
@@ -88,7 +87,7 @@ impl CompilationSource {
8887

8988
fn compile_single(
9089
&self,
91-
mode: compile::Mode,
90+
mode: codegen::Mode,
9291
module_name: String,
9392
) -> Result<CodeObject, Diagnostic> {
9493
match &self.kind {
@@ -117,7 +116,7 @@ impl CompilationSource {
117116
&self,
118117
path: &Path,
119118
parent: String,
120-
mode: compile::Mode,
119+
mode: codegen::Mode,
121120
) -> Result<HashMap<String, FrozenModule>, Diagnostic> {
122121
let mut code_map = HashMap::new();
123122
let paths = fs::read_dir(path)
@@ -310,7 +309,7 @@ impl PyCompileInput {
310309

311310
Ok(PyCompileArgs {
312311
source,
313-
mode: mode.unwrap_or(compile::Mode::Exec),
312+
mode: mode.unwrap_or(codegen::Mode::Exec),
314313
module_name: module_name.unwrap_or_else(|| "frozen".to_owned()),
315314
crate_name: crate_name.unwrap_or_else(|| syn::parse_quote!(::rustpython_vm::bytecode)),
316315
})
@@ -351,7 +350,7 @@ impl Parse for PyCompileInput {
351350

352351
struct PyCompileArgs {
353352
source: CompilationSource,
354-
mode: compile::Mode,
353+
mode: codegen::Mode,
355354
module_name: String,
356355
crate_name: syn::Path,
357356
}

vm/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ pub use self::vm::{Context, Interpreter, Settings, VirtualMachine};
8787
pub use rustpython_bytecode as bytecode;
8888
pub use rustpython_common as common;
8989
#[cfg(feature = "rustpython-compiler")]
90-
pub use rustpython_compiler as compile;
90+
pub mod compile {
91+
pub use rustpython_codegen::{CompileOpts, Mode};
92+
pub use rustpython_compiler::*;
93+
}
9194

9295
#[doc(hidden)]
9396
pub mod __exports {

vm/src/stdlib/ast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::{
1414
use num_complex::Complex64;
1515
use num_traits::{ToPrimitive, Zero};
1616
use rustpython_ast as ast;
17-
#[cfg(feature = "rustpython-compiler")]
18-
use rustpython_compiler as compile;
17+
#[cfg(feature = "rustpython-codegen")]
18+
use rustpython_codegen as codegen;
1919
#[cfg(feature = "rustpython-parser")]
2020
use rustpython_parser::parser;
2121

@@ -269,16 +269,16 @@ pub(crate) fn parse(vm: &VirtualMachine, source: &str, mode: parser::Mode) -> Py
269269
Ok(top.ast_to_object(vm))
270270
}
271271

272-
#[cfg(feature = "rustpython-compiler")]
272+
#[cfg(feature = "rustpython-codegen")]
273273
pub(crate) fn compile(
274274
vm: &VirtualMachine,
275275
object: PyObjectRef,
276276
filename: &str,
277-
mode: compile::Mode,
277+
mode: codegen::Mode,
278278
) -> PyResult {
279279
let opts = vm.compile_opts();
280280
let ast = Node::ast_from_object(vm, object)?;
281-
let code = rustpython_codegen::compile::compile_top(&ast, filename.to_owned(), mode, opts)
281+
let code = codegen::compile::compile_top(&ast, filename.to_owned(), mode, opts)
282282
// TODO: use vm.new_syntax_error()
283283
.map_err(|err| vm.new_value_error(err.to_string()))?;
284284
Ok(vm.ctx.new_code(code).into())

vm/src/stdlib/symtable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ pub(crate) use symtable::make_module;
44
mod symtable {
55
use crate::{
66
builtins::PyStrRef,
7-
compile::{self, Symbol, SymbolScope, SymbolTable, SymbolTableType},
7+
compile::{self},
88
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
99
};
10+
use rustpython_codegen::symboltable::{Symbol, SymbolScope, SymbolTable, SymbolTableType};
1011
use std::fmt;
1112

1213
/// symtable. Return top level SymbolTable.

0 commit comments

Comments
 (0)