Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve class scopes in their own symbol table #221

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion typechecker/src/build_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl BuildSource {
}

pub fn get_module_name(path: &Path) -> String {
path.to_str().unwrap_or_default().replace(['/', '\\'], ".")
path.to_str().unwrap().replace(['/', '\\'], ".")
}

// impl Into<EnderpyFile> for BuildSource {
Expand Down
3 changes: 2 additions & 1 deletion typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ impl TraversalVisitor for SemanticAnalyzer {

self.symbol_table.exit_scope();

let class_declaration = Declaration::Class(Class::new(c.clone(), methods));
let class_declaration =
Declaration::Class(Class::new(c.clone(), methods, self.file.path()));
let flags = SymbolFlags::empty();
self.create_symbol(c.name.clone(), class_declaration, flags);
}
Expand Down
4 changes: 2 additions & 2 deletions typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ pub struct Class {
}

impl Class {
pub fn new(class_node: ast::ClassDef, methods: Vec<String>) -> Self {
pub fn new(class_node: ast::ClassDef, methods: Vec<String>, file_path: PathBuf) -> Self {
Class {
name: class_node.name.clone(),
declaration_path: DeclarationPath {
module_name: PathBuf::new(),
module_name: file_path,
node: Node {
start: class_node.node.start,
end: class_node.node.end,
Expand Down
1,452 changes: 1,452 additions & 0 deletions typechecker/src/type_check/.type_evaluator.rs.rustfmt

Large diffs are not rendered by default.

29 changes: 22 additions & 7 deletions typechecker/src/type_check/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(unused_variables)]

use core::panic;
use std::{path::Path};
use std::path::Path;

use enderpy_python_parser as parser;
use enderpy_python_parser::ast;
Expand Down Expand Up @@ -239,15 +239,10 @@ impl TypeEvaluator {
return Ok(PythonType::Unknown);
}
};
log::debug!("checking type for expression: {:#?}", a);
log::debug!("looking up attribute in type: {:?}", value_type);
match value_type {
PythonType::Class(c) => {
let class_scope = self
.symbol_table
.get_scope(&c.details.declaration_path.node)
.expect("cannot find class scope for attribute access");

let class_scope = self.get_scope_of(&c);
log::debug!("class scope for the attribute: {:#?}", class_scope);
let symbol_table_node =
self.symbol_table.lookup_attribute(&a.attr, class_scope);
Expand Down Expand Up @@ -878,6 +873,26 @@ impl TypeEvaluator {
.find(|symbol_table| symbol_table.file_path == path);
symbol_table
}

fn get_scope_of(&self, c: &ClassType) -> &symbol_table::SymbolTableScope {
let symbol_table = self
.imported_symbol_tables
.iter()
.find(|symbol_table| {
symbol_table
.file_path
.ends_with(&c.details.declaration_path.module_name)
})
.unwrap_or_else(|| {
panic!(
"Declaration path not found: {:?}",
c.details.declaration_path,
)
});
symbol_table
.get_scope(&c.details.declaration_path.node)
.unwrap_or_else(|| panic!("Scope not found for: {:?}", c.details.declaration_path))
}
}

impl TraversalVisitorImmutGeneric<PythonType> for TypeEvaluator {
Expand Down
2 changes: 2 additions & 0 deletions typechecker/test_data/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ def get_x(self) -> float:
d = {"a": 1, "b": 2}
s = {1,2,3}

l.append(4)

Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ SymbolFlags(
--: Class {
name: "c",
declaration_path: DeclarationPath {
module_name: "",
module_name: "[TYPECHECKER]/test_data/inputs/symbol_table/class_definition.py",
node: Node {
start: 0,
end: 80,
Expand Down

Large diffs are not rendered by default.

Loading