8
8
use crate :: error:: { CompileError , CompileErrorType } ;
9
9
use crate :: output_stream:: { CodeObjectStream , OutputStream } ;
10
10
use crate :: peephole:: PeepholeOptimizer ;
11
- use crate :: symboltable:: { make_symbol_table, statements_to_symbol_table, Symbol , SymbolScope } ;
11
+ use crate :: symboltable:: {
12
+ make_symbol_table, statements_to_symbol_table, Symbol , SymbolScope , SymbolTable ,
13
+ } ;
12
14
use num_complex:: Complex64 ;
13
15
use rustpython_bytecode:: bytecode:: { self , CallType , CodeObject , Instruction , Varargs } ;
14
16
use rustpython_parser:: { ast, parser} ;
@@ -18,7 +20,7 @@ type BasicOutputStream = PeepholeOptimizer<CodeObjectStream>;
18
20
/// Main structure holding the state of compilation.
19
21
struct Compiler < O : OutputStream = BasicOutputStream > {
20
22
output_stack : Vec < O > ,
21
- scope_stack : Vec < SymbolScope > ,
23
+ symbol_table_stack : Vec < SymbolTable > ,
22
24
nxt_label : usize ,
23
25
source_path : Option < String > ,
24
26
current_source_location : ast:: Location ,
@@ -147,7 +149,7 @@ impl<O: OutputStream> Compiler<O> {
147
149
fn new ( optimize : u8 ) -> Self {
148
150
Compiler {
149
151
output_stack : Vec :: new ( ) ,
150
- scope_stack : Vec :: new ( ) ,
152
+ symbol_table_stack : Vec :: new ( ) ,
151
153
nxt_label : 0 ,
152
154
source_path : None ,
153
155
current_source_location : ast:: Location :: default ( ) ,
@@ -182,10 +184,10 @@ impl<O: OutputStream> Compiler<O> {
182
184
fn compile_program (
183
185
& mut self ,
184
186
program : & ast:: Program ,
185
- symbol_scope : SymbolScope ,
187
+ symbol_table : SymbolTable ,
186
188
) -> Result < ( ) , CompileError > {
187
189
let size_before = self . output_stack . len ( ) ;
188
- self . scope_stack . push ( symbol_scope ) ;
190
+ self . symbol_table_stack . push ( symbol_table ) ;
189
191
self . compile_statements ( & program. statements ) ?;
190
192
assert_eq ! ( self . output_stack. len( ) , size_before) ;
191
193
@@ -200,9 +202,9 @@ impl<O: OutputStream> Compiler<O> {
200
202
fn compile_program_single (
201
203
& mut self ,
202
204
program : & ast:: Program ,
203
- symbol_scope : SymbolScope ,
205
+ symbol_table : SymbolTable ,
204
206
) -> Result < ( ) , CompileError > {
205
- self . scope_stack . push ( symbol_scope ) ;
207
+ self . symbol_table_stack . push ( symbol_table ) ;
206
208
207
209
let mut emitted_return = false ;
208
210
@@ -239,9 +241,9 @@ impl<O: OutputStream> Compiler<O> {
239
241
fn compile_statement_eval (
240
242
& mut self ,
241
243
statements : & [ ast:: Statement ] ,
242
- symbol_table : SymbolScope ,
244
+ symbol_table : SymbolTable ,
243
245
) -> Result < ( ) , CompileError > {
244
- self . scope_stack . push ( symbol_table) ;
246
+ self . symbol_table_stack . push ( symbol_table) ;
245
247
for statement in statements {
246
248
if let ast:: StatementType :: Expression { ref expression } = statement. node {
247
249
self . compile_expression ( expression) ?;
@@ -265,12 +267,11 @@ impl<O: OutputStream> Compiler<O> {
265
267
266
268
fn scope_for_name ( & self , name : & str ) -> bytecode:: NameScope {
267
269
let symbol = self . lookup_name ( name) ;
268
- if symbol. is_global {
269
- bytecode:: NameScope :: Global
270
- } else if symbol. is_nonlocal {
271
- bytecode:: NameScope :: NonLocal
272
- } else {
273
- bytecode:: NameScope :: Local
270
+ match symbol. scope {
271
+ SymbolScope :: Global => bytecode:: NameScope :: Global ,
272
+ SymbolScope :: Nonlocal => bytecode:: NameScope :: NonLocal ,
273
+ SymbolScope :: Unknown => bytecode:: NameScope :: Local ,
274
+ SymbolScope :: Local => bytecode:: NameScope :: Local ,
274
275
}
275
276
}
276
277
@@ -1921,22 +1922,27 @@ impl<O: OutputStream> Compiler<O> {
1921
1922
1922
1923
// Scope helpers:
1923
1924
fn enter_scope ( & mut self ) {
1924
- // println!("Enter scope {:?}", self.scope_stack );
1925
+ // println!("Enter scope {:?}", self.symbol_table_stack );
1925
1926
// Enter first subscope!
1926
- let scope = self . scope_stack . last_mut ( ) . unwrap ( ) . sub_scopes . remove ( 0 ) ;
1927
- self . scope_stack . push ( scope) ;
1927
+ let table = self
1928
+ . symbol_table_stack
1929
+ . last_mut ( )
1930
+ . unwrap ( )
1931
+ . sub_tables
1932
+ . remove ( 0 ) ;
1933
+ self . symbol_table_stack . push ( table) ;
1928
1934
}
1929
1935
1930
1936
fn leave_scope ( & mut self ) {
1931
- // println!("Leave scope {:?}", self.scope_stack );
1932
- let scope = self . scope_stack . pop ( ) . unwrap ( ) ;
1933
- assert ! ( scope . sub_scopes . is_empty( ) ) ;
1937
+ // println!("Leave scope {:?}", self.symbol_table_stack );
1938
+ let table = self . symbol_table_stack . pop ( ) . unwrap ( ) ;
1939
+ assert ! ( table . sub_tables . is_empty( ) ) ;
1934
1940
}
1935
1941
1936
1942
fn lookup_name ( & self , name : & str ) -> & Symbol {
1937
1943
// println!("Looking up {:?}", name);
1938
- let scope = self . scope_stack . last ( ) . unwrap ( ) ;
1939
- scope . lookup ( name) . expect (
1944
+ let symbol_table = self . symbol_table_stack . last ( ) . unwrap ( ) ;
1945
+ symbol_table . lookup ( name) . expect (
1940
1946
"The symbol must be present in the symbol table, even when it is undefined in python." ,
1941
1947
)
1942
1948
}
0 commit comments