Skip to content

Commit

Permalink
Pass file to checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Oct 10, 2024
1 parent 22f2d87 commit b8c2bb0
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion benchmark/benches/typecheck_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub fn benchmark_type_checker(c: &mut Criterion) {
let builder = BuildManager::new(Settings::test_settings());
let file_path = PathBuf::from(path);
builder.build_one(&PathBuf::from("../../"), &file_path);
builder.type_check(&file_path);
let file = builder.get_state(&file_path);
builder.type_check(&file_path, &file);

0
});
Expand Down
3 changes: 2 additions & 1 deletion enderpy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ fn check(path: &Path) -> Result<()> {
let build_manager = BuildManager::new(settings);
build_manager.build(root);
build_manager.build_one(root, path);
build_manager.type_check(path);
let file = build_manager.get_state(path);
build_manager.type_check(path, &file);

if build_manager.diagnostics.is_empty() {
println!("zero errors");
Expand Down
3 changes: 2 additions & 1 deletion lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ impl<'a> Backend {
fn build(&self, path: PathBuf) {
let root = find_project_root(&path);
self.manager.build_one(root, &path);
self.manager.type_check(&path);
let file = self.manager.get_state(&path);
self.manager.type_check(&path, &file);
}
}

Expand Down
2 changes: 1 addition & 1 deletion parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ pub enum ConstantValue {

impl Constant {
pub fn get_value<'a>(&self, source: &'a str) -> &'a str {
return &source[self.node.start as usize..self.node.end as usize];
&source[self.node.start as usize..self.node.end as usize]
}
}

Expand Down
5 changes: 3 additions & 2 deletions typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ impl<'a> BuildManager {

// Performs type checking passes over the code
// This step happens after the binding phase
pub fn type_check(&self, path: &Path) -> TypeChecker {
pub fn type_check(&'a self, path: &Path, file: &'a EnderpyFile) -> TypeChecker<'a> {
let mut module_to_check = self.get_state(path);

let span = span!(Level::TRACE, "type check", path = %path.display());
let _guard = span.enter();
let mut checker = TypeChecker::new(
file,
self.get_symbol_table(path),
&self.symbol_tables,
&self.module_ids,
Expand All @@ -132,7 +133,7 @@ impl<'a> BuildManager {

pub fn get_hover_information(&self, path: &Path, line: u32, column: u32) -> String {
let module = self.get_state(path);
let checker = self.type_check(path);
let checker = self.type_check(path, &module);
let symbol_table = self.get_symbol_table(path);
let hovered_offset = module.line_starts[line as usize] + column;

Expand Down
7 changes: 5 additions & 2 deletions typechecker/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use enderpy_python_parser as parser;
use enderpy_python_parser::ast::{self, *};

use super::{type_evaluator::TypeEvaluator, types::PythonType};
use crate::file::EnderpyFile;
use crate::symbol_table::Id;
use crate::types::ModuleRef;
use crate::{ast_visitor::TraversalVisitor, diagnostic::CharacterSpan, symbol_table::SymbolTable};
Expand All @@ -28,13 +29,14 @@ pub struct TypeCheckError {
#[allow(unused)]
impl<'a> TypeChecker<'a> {
pub fn new(
file: &'a EnderpyFile,
symbol_table: SymbolTable,
symbol_tables: &'a DashMap<Id, SymbolTable>,
ids: &'a DashMap<PathBuf, Id>,
) -> Self {
TypeChecker {
errors: vec![],
type_evaluator: TypeEvaluator::new(symbol_table, symbol_tables, ids),
type_evaluator: TypeEvaluator::new(file, symbol_table, symbol_tables, ids),
types: Lapper::new(vec![]),
}
}
Expand Down Expand Up @@ -629,7 +631,8 @@ mod tests {
let root = &PathBuf::from("");
manager.build(root);
manager.build_one(root, &path);
let checker = manager.type_check(&path);
let module = manager.get_state(&path);
let checker = manager.type_check(&path, &module);
let module = manager.get_state(&path);

let result = checker.types;
Expand Down
9 changes: 6 additions & 3 deletions typechecker/src/type_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use super::{
},
};
use crate::{
file::EnderpyFile,
semantic_analyzer::get_member_access_info,
symbol_table::{
self, Class, Declaration, Id, LookupSymbolRequest, SymbolTable, SymbolTableNode,
TypeParameter,
},
types::CallableArgs,
};
Expand All @@ -40,6 +40,7 @@ const SPECIAL_FORM: &str = "_SpecialForm";
#[derive(Clone, Debug)]
pub struct TypeEvaluator<'a> {
// TODO: make this a reference to the symbol table in the checker
pub file: &'a EnderpyFile,
pub symbol_table: SymbolTable,
pub imported_symbol_tables: &'a DashMap<Id, SymbolTable>,
pub ids: &'a DashMap<PathBuf, Id>,
Expand Down Expand Up @@ -68,12 +69,14 @@ bitflags::bitflags! {
/// Struct for evaluating the type of an expression
impl<'a> TypeEvaluator<'a> {
pub fn new(
file: &'a EnderpyFile,
symbol_table: SymbolTable,
imported_symbol_tables: &'a DashMap<Id, SymbolTable>,
ids: &'a DashMap<PathBuf, Id>,
) -> Self {
TypeEvaluator {
symbol_table,
file,
imported_symbol_tables,
ids,
flags: Cell::new(GetTypeFlags::empty()),
Expand Down Expand Up @@ -145,7 +148,7 @@ impl<'a> TypeEvaluator<'a> {
};
let type_name = match first_arg {
ast::Expression::Constant(str) => match &str.value {
ast::ConstantValue::Str => str.get_value(""),
ast::ConstantValue::Str => str.get_value(&self.file.source),
_ => panic!("TypeVar first arg must be a string"),
},
_ => panic!("TypeVar must be called with at least one arg"),
Expand Down Expand Up @@ -562,7 +565,7 @@ impl<'a> TypeEvaluator<'a> {
// then local scope.
// https://peps.python.org/pep-0563/#backwards-compatibility
ast::ConstantValue::Str => {
let mut parser = Parser::new("");
let mut parser = Parser::new(&self.file.source);
// Wrap the parsing logic inside a `catch_unwind` block
let parse_result = catch_unwind(AssertUnwindSafe(|| parser.parse()));

Expand Down

0 comments on commit b8c2bb0

Please sign in to comment.