Fix class scopes by modifying symboltable

This commit is contained in:
coolreader18 2019-09-07 01:29:11 -05:00
parent acce23b79b
commit d7f99dc7b1
2 changed files with 7 additions and 9 deletions

View file

@ -975,7 +975,7 @@ impl<O: OutputStream> Compiler<O> {
// Turn code object into function object: // Turn code object into function object:
self.emit(Instruction::MakeFunction { self.emit(Instruction::MakeFunction {
flags: bytecode::FunctionOpArg::IS_CLASS, flags: bytecode::FunctionOpArg::empty(),
}); });
self.emit(Instruction::LoadConst { self.emit(Instruction::LoadConst {

View file

@ -61,7 +61,7 @@ impl SymbolTable {
} }
} }
#[derive(Clone)] #[derive(Clone, PartialEq)]
pub enum SymbolTableType { pub enum SymbolTableType {
Module, Module,
Class, Class,
@ -241,12 +241,10 @@ impl SymbolTableAnalyzer {
} else { } else {
// Interesting stuff about the __class__ variable: // Interesting stuff about the __class__ variable:
// https://docs.python.org/3/reference/datamodel.html?highlight=__class__#creating-the-class-object // https://docs.python.org/3/reference/datamodel.html?highlight=__class__#creating-the-class-object
let found_in_outer_scope = (symbol.name == "__class__") let found_in_outer_scope = symbol.name == "__class__"
|| self || self.tables.iter().skip(1).any(|t| {
.tables t.typ != SymbolTableType::Class && t.symbols.contains_key(&symbol.name)
.iter() });
.skip(1)
.any(|t| t.symbols.contains_key(&symbol.name));
if found_in_outer_scope { if found_in_outer_scope {
// Symbol is in some outer scope. // Symbol is in some outer scope.
@ -387,7 +385,6 @@ impl SymbolTableBuilder {
keywords, keywords,
decorator_list, decorator_list,
} => { } => {
self.register_name(name, SymbolUsage::Assigned)?;
self.enter_scope(name, SymbolTableType::Class, statement.location.row()); self.enter_scope(name, SymbolTableType::Class, statement.location.row());
self.scan_statements(body)?; self.scan_statements(body)?;
self.leave_scope(); self.leave_scope();
@ -396,6 +393,7 @@ impl SymbolTableBuilder {
self.scan_expression(&keyword.value, &ExpressionContext::Load)?; self.scan_expression(&keyword.value, &ExpressionContext::Load)?;
} }
self.scan_expressions(decorator_list, &ExpressionContext::Load)?; self.scan_expressions(decorator_list, &ExpressionContext::Load)?;
self.register_name(name, SymbolUsage::Assigned)?;
} }
Expression { expression } => { Expression { expression } => {
self.scan_expression(expression, &ExpressionContext::Load)? self.scan_expression(expression, &ExpressionContext::Load)?