Skip to content

Commit 32d63cc

Browse files
committed
Update symtable methods
1 parent c87884a commit 32d63cc

1 file changed

Lines changed: 47 additions & 59 deletions

File tree

vm/src/stdlib/symtable.rs

Lines changed: 47 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ mod symtable {
55
use crate::{
66
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine, builtins::PyStrRef, compiler,
77
};
8-
use rustpython_codegen::symboltable::{
9-
CompilerScope, Symbol, SymbolFlags, SymbolScope, SymbolTable,
10-
};
8+
use rustpython_codegen::symboltable::{Symbol, SymbolFlags, SymbolScope, SymbolTable};
119
use std::fmt;
1210

1311
// Consts as defined at
@@ -79,6 +77,30 @@ mod symtable {
7977
#[pyattr]
8078
pub const GENERATOR_EXPRESSION: i32 = 2;
8179

80+
#[pyattr]
81+
pub const SCOPE_OFF: i32 = SCOPE_OFFSET;
82+
83+
#[pyattr]
84+
pub const TYPE_FUNCTION: i32 = 0;
85+
86+
#[pyattr]
87+
pub const TYPE_CLASS: i32 = 1;
88+
89+
#[pyattr]
90+
pub const TYPE_MODULE: i32 = 2;
91+
92+
#[pyattr]
93+
pub const TYPE_ANNOTATION: i32 = 3;
94+
95+
#[pyattr]
96+
pub const TYPE_TYPE_VAR_BOUND: i32 = 4;
97+
98+
#[pyattr]
99+
pub const TYPE_TYPE_ALIAS: i32 = 5;
100+
101+
#[pyattr]
102+
pub const TYPE_TYPE_PARAM: i32 = 6;
103+
82104
#[pyfunction]
83105
fn symtable(
84106
source: PyStrRef,
@@ -117,57 +139,39 @@ mod symtable {
117139

118140
#[pyclass]
119141
impl PySymbolTable {
120-
#[pymethod]
121-
fn get_name(&self) -> String {
142+
#[pygetset]
143+
fn name(&self) -> String {
122144
self.symtable.name.clone()
123145
}
124146

125-
#[pymethod]
126-
fn get_type(&self) -> String {
147+
#[pygetset(name = "type")]
148+
fn typ(&self) -> String {
127149
self.symtable.typ.to_string()
128150
}
129151

130-
#[pymethod]
131-
const fn get_lineno(&self) -> u32 {
152+
#[pygetset]
153+
const fn lineno(&self) -> u32 {
132154
self.symtable.line_number
133155
}
134156

135-
#[pymethod]
136-
const fn is_nested(&self) -> bool {
137-
self.symtable.is_nested
138-
}
139-
140-
#[pymethod]
141-
fn is_optimized(&self) -> bool {
142-
matches!(
143-
self.symtable.typ,
144-
CompilerScope::Function | CompilerScope::AsyncFunction
145-
)
157+
#[pygetset]
158+
fn children(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
159+
let children = self
160+
.symtable
161+
.sub_tables
162+
.iter()
163+
.map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm))
164+
.collect();
165+
Ok(children)
146166
}
147167

148-
#[pymethod]
149-
fn lookup(&self, name: PyStrRef, vm: &VirtualMachine) -> PyResult<PyRef<PySymbol>> {
150-
let name = name.as_str();
151-
if let Some(symbol) = self.symtable.symbols.get(name) {
152-
Ok(PySymbol {
153-
symbol: symbol.clone(),
154-
namespaces: self
155-
.symtable
156-
.sub_tables
157-
.iter()
158-
.filter(|table| table.name == name)
159-
.cloned()
160-
.collect(),
161-
is_top_scope: self.symtable.name == "top",
162-
}
163-
.into_ref(&vm.ctx))
164-
} else {
165-
Err(vm.new_key_error(vm.ctx.new_str(format!("lookup {name} failed")).into()))
166-
}
168+
#[pygetset]
169+
fn id(&self) -> usize {
170+
self as *const Self as *const std::ffi::c_void as usize
167171
}
168172

169-
#[pymethod]
170-
fn get_identifiers(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
173+
#[pygetset]
174+
fn identifiers(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
171175
let symbols = self
172176
.symtable
173177
.symbols
@@ -177,8 +181,8 @@ mod symtable {
177181
Ok(symbols)
178182
}
179183

180-
#[pymethod]
181-
fn get_symbols(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
184+
#[pygetset]
185+
fn symbols(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
182186
let symbols = self
183187
.symtable
184188
.symbols
@@ -201,22 +205,6 @@ mod symtable {
201205
.collect();
202206
Ok(symbols)
203207
}
204-
205-
#[pymethod]
206-
const fn has_children(&self) -> bool {
207-
!self.symtable.sub_tables.is_empty()
208-
}
209-
210-
#[pymethod]
211-
fn get_children(&self, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
212-
let children = self
213-
.symtable
214-
.sub_tables
215-
.iter()
216-
.map(|t| to_py_symbol_table(t.clone()).into_pyobject(vm))
217-
.collect();
218-
Ok(children)
219-
}
220208
}
221209

222210
#[pyattr]

0 commit comments

Comments
 (0)