mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-31 07:38:04 +00:00
Refactor symboltables and _ast to use codegen when possible
This commit is contained in:
parent
060d153bb3
commit
9c229ebb99
4 changed files with 24 additions and 25 deletions
|
@ -8,7 +8,7 @@
|
|||
use crate::{
|
||||
error::{CompileError, CompileErrorType},
|
||||
ir,
|
||||
symboltable::{self, make_symbol_table, make_symbol_table_expr, SymbolScope, SymbolTable},
|
||||
symboltable::{self, SymbolScope, SymbolTable},
|
||||
IndexSet,
|
||||
};
|
||||
use itertools::Itertools;
|
||||
|
@ -149,7 +149,7 @@ pub fn compile_program(
|
|||
ast,
|
||||
source_path,
|
||||
opts,
|
||||
make_symbol_table,
|
||||
SymbolTable::scan_program,
|
||||
Compiler::compile_program,
|
||||
)
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ pub fn compile_program_single(
|
|||
ast,
|
||||
source_path,
|
||||
opts,
|
||||
make_symbol_table,
|
||||
SymbolTable::scan_program,
|
||||
Compiler::compile_program_single,
|
||||
)
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ pub fn compile_block_expression(
|
|||
ast,
|
||||
source_path,
|
||||
opts,
|
||||
make_symbol_table,
|
||||
SymbolTable::scan_program,
|
||||
Compiler::compile_block_expr,
|
||||
)
|
||||
}
|
||||
|
@ -192,7 +192,7 @@ pub fn compile_expression(
|
|||
ast,
|
||||
source_path,
|
||||
opts,
|
||||
make_symbol_table_expr,
|
||||
SymbolTable::scan_expr,
|
||||
Compiler::compile_eval,
|
||||
)
|
||||
}
|
||||
|
@ -2697,7 +2697,7 @@ fn compile_constant(value: &ast::Constant) -> ConstantData {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{CompileOpts, Compiler};
|
||||
use crate::symboltable::make_symbol_table;
|
||||
use crate::symboltable::SymbolTable;
|
||||
use rustpython_bytecode::CodeObject;
|
||||
use rustpython_parser::parser;
|
||||
|
||||
|
@ -2708,7 +2708,7 @@ mod tests {
|
|||
"<module>".to_owned(),
|
||||
);
|
||||
let ast = parser::parse_program(source).unwrap();
|
||||
let symbol_scope = make_symbol_table(&ast).unwrap();
|
||||
let symbol_scope = SymbolTable::scan_program(&ast).unwrap();
|
||||
compiler.compile_program(&ast, symbol_scope).unwrap();
|
||||
compiler.pop_code_object()
|
||||
}
|
||||
|
|
|
@ -13,3 +13,5 @@ pub mod error;
|
|||
pub mod ir;
|
||||
pub mod mode;
|
||||
pub mod symboltable;
|
||||
|
||||
pub use compile::{CompileOpts, Mode};
|
||||
|
|
|
@ -14,18 +14,6 @@ use crate::{
|
|||
use rustpython_ast::{self as ast, Location};
|
||||
use std::{borrow::Cow, fmt};
|
||||
|
||||
pub fn make_symbol_table(program: &[ast::Stmt]) -> SymbolTableResult<SymbolTable> {
|
||||
let mut builder = SymbolTableBuilder::new();
|
||||
builder.scan_statements(program)?;
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub fn make_symbol_table_expr(expr: &ast::Expr) -> SymbolTableResult<SymbolTable> {
|
||||
let mut builder = SymbolTableBuilder::new();
|
||||
builder.scan_expression(expr, ExpressionContext::Load)?;
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.
|
||||
#[derive(Clone)]
|
||||
pub struct SymbolTable {
|
||||
|
@ -60,6 +48,18 @@ impl SymbolTable {
|
|||
sub_tables: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scan_program(program: &[ast::Stmt]) -> SymbolTableResult<Self> {
|
||||
let mut builder = SymbolTableBuilder::new();
|
||||
builder.scan_statements(program)?;
|
||||
builder.finish()
|
||||
}
|
||||
|
||||
pub fn scan_expr(expr: &ast::Expr) -> SymbolTableResult<Self> {
|
||||
let mut builder = SymbolTableBuilder::new();
|
||||
builder.scan_expression(expr, ExpressionContext::Load)?;
|
||||
builder.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue