Refactor symboltables and _ast to use codegen when possible

This commit is contained in:
Jeong YunWon 2022-08-22 05:19:36 +09:00
parent 060d153bb3
commit 9c229ebb99
4 changed files with 24 additions and 25 deletions

View file

@ -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()
}

View file

@ -13,3 +13,5 @@ pub mod error;
pub mod ir;
pub mod mode;
pub mod symboltable;
pub use compile::{CompileOpts, Mode};

View file

@ -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)]

View file

@ -6,9 +6,6 @@ use rustpython_parser::{
};
use std::fmt;
pub use compile::{CompileOpts, Mode};
pub use symboltable::{Symbol, SymbolScope, SymbolTable, SymbolTableType};
#[derive(Debug, thiserror::Error)]
pub enum CompileErrorType {
#[error(transparent)]
@ -76,7 +73,7 @@ pub fn compile(
source: &str,
mode: compile::Mode,
source_path: String,
opts: CompileOpts,
opts: compile::CompileOpts,
) -> Result<CodeObject, CompileError> {
let parser_mode = match mode {
compile::Mode::Exec => parser::Mode::Module,
@ -105,11 +102,11 @@ pub fn compile_symtable(
let res = match mode {
compile::Mode::Exec | compile::Mode::Single | compile::Mode::BlockExpr => {
let ast = parser::parse_program(source).map_err(parse_err)?;
symboltable::make_symbol_table(&ast)
symboltable::SymbolTable::scan_program(&ast)
}
compile::Mode::Eval => {
let expr = parser::parse_expression(source).map_err(parse_err)?;
symboltable::make_symbol_table_expr(&expr)
symboltable::SymbolTable::scan_expr(&expr)
}
};
res.map_err(|e| CompileError::from_symtable(e, source, source_path.to_owned()))