Skip to content

Commit

Permalink
Remove useless abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Glyphack committed Jan 9, 2024
1 parent 47e10a8 commit dbfaad8
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 125 deletions.
13 changes: 4 additions & 9 deletions enderpy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn symbols(path: &Path) -> Result<()> {
manager.build();

let module = manager.get_state(path.to_path_buf()).unwrap();
println!("{}", module.file.module_name());
println!("{}", module.module_name());
println!("{}", module.get_symbol_table());

Ok(())
Expand Down Expand Up @@ -110,14 +110,9 @@ fn check(path: &Path) -> Result<()> {
let mut build_manager = BuildManager::new(vec![initial_source], settings);
build_manager.type_check();

for file_result in build_manager.get_result() {
for err in file_result.diagnostics {
println!(
"{:#?}: line {}: {}",
file_result.file.path(),
err.range.start.line,
err.body
);
for (path, errors) in build_manager.diagnostics {
for err in errors {
println!("{:#?}: line {}: {}", path, err.range.start.line, err.body);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Backend {
let mut diagnostics = Vec::new();
info!("path: {path:?}");

if let Some(state) = manager.get_state(path) {
for err in state.diagnostics.iter() {
if let Some(errors) = manager.diagnostics.get(&path) {
for err in errors {
diagnostics.push(from(err.clone()));
}
}
Expand Down
81 changes: 42 additions & 39 deletions typechecker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ use crate::{
module_descriptor::ImportModuleDescriptor, resolver,
},
settings::Settings,
state::State,
type_check::checker::TypeChecker,
};

#[derive(Debug)]
pub struct BuildManager {
pub errors: Vec<Diagnostic>,
pub modules: HashMap<String, State>,
pub modules: HashMap<String, EnderpyFile>,
build_sources: Vec<BuildSource>,
options: Settings,
// Map of file name to list of diagnostics
pub diagnostics: HashMap<PathBuf, Vec<Diagnostic>>,
}
#[allow(unused)]
impl BuildManager {
Expand All @@ -50,17 +51,18 @@ impl BuildManager {
build_sources: sources,
modules,
options,
diagnostics: HashMap::new(),
}
}

pub fn get_result(&self) -> Vec<State> {
pub fn get_result(&self) -> Vec<EnderpyFile> {
self.modules.values().cloned().collect()
}

pub fn get_state(&self, path: PathBuf) -> Option<&State> {
pub fn get_state(&self, path: PathBuf) -> Option<&EnderpyFile> {
for state in self.modules.values() {
info!("state: {:#?}", state.file.path());
if state.file.path() == path {
info!("state: {:#?}", state.path());
if state.path() == path {
return Some(state);
}
}
Expand All @@ -76,8 +78,8 @@ impl BuildManager {
fn populate_modules(&mut self) {
for build_source in self.build_sources.iter() {
let build_source: BuildSource = build_source.clone();
let state = State::new(build_source.into());
self.modules.insert(state.file.module_name(), state);
let state: EnderpyFile = build_source.into();
self.modules.insert(state.module_name(), state);
}
let (new_files, imports) = match self.options.follow_imports {
crate::settings::FollowImports::All => {
Expand All @@ -88,11 +90,11 @@ impl BuildManager {
}
};
for build_source in new_files {
let module = self.create_module(build_source);
self.modules.insert(module.file.module_name(), module);
let file: EnderpyFile = build_source.into();
self.modules.insert(file.module_name(), file);
}
for module in self.modules.values_mut() {
info!("file: {:#?}", module.file.module_name());
info!("file: {:#?}", module.module_name());
module.populate_symbol_table(&imports);
}
}
Expand All @@ -108,8 +110,8 @@ impl BuildManager {
}

for state in self.modules.iter_mut() {
if !state.1.file.errors.is_empty() {
for err in state.1.file.errors.iter() {
if !state.1.errors.is_empty() {
for err in state.1.errors.iter() {
match err {
ParsingError::InvalidSyntax {
msg,
Expand All @@ -121,43 +123,49 @@ impl BuildManager {
body: msg.to_string(),
suggestion: Some(advice.to_string()),
range: crate::diagnostic::Range {
start: state.1.file.get_position(span.0),
end: state.1.file.get_position(span.1),
},
});
state.1.diagnostics.push(Diagnostic {
body: msg.to_string(),
suggestion: Some(advice.to_string()),
range: crate::diagnostic::Range {
start: state.1.file.get_position(span.0),
end: state.1.file.get_position(span.1),
start: state.1.get_position(span.0),
end: state.1.get_position(span.1),
},
});
self.diagnostics
.entry(state.1.path())
.or_default()
.push(Diagnostic {
body: msg.to_string(),
suggestion: Some(advice.to_string()),
range: crate::diagnostic::Range {
start: state.1.get_position(span.0),
end: state.1.get_position(span.1),
},
});
}
}
}
}
let mut checker = TypeChecker::new(state.1, &self.options, all_symbol_tables.clone());
for stmt in &state.1.file.body {
for stmt in &state.1.body {
checker.type_check(stmt);
}
for error in checker.errors {
self.errors.push(Diagnostic {
body: error.msg.to_string(),
suggestion: Some("".into()),
range: crate::diagnostic::Range {
start: state.1.file.get_position(error.span.0),
end: state.1.file.get_position(error.span.1),
},
});
state.1.diagnostics.push(Diagnostic {
body: error.msg.to_string(),
suggestion: Some("".into()),
range: crate::diagnostic::Range {
start: state.1.file.get_position(error.span.0),
end: state.1.file.get_position(error.span.1),
start: state.1.get_position(error.span.0),
end: state.1.get_position(error.span.1),
},
});
self.diagnostics
.entry(state.1.path())
.or_default()
.push(Diagnostic {
body: error.msg.to_string(),
suggestion: Some("".into()),
range: crate::diagnostic::Range {
start: state.1.get_position(error.span.0),
end: state.1.get_position(error.span.1),
},
});
}
}
}
Expand Down Expand Up @@ -320,11 +328,6 @@ impl BuildManager {
(imported_sources, import_results)
}

// TODO: refactor to implement From/to trait
fn create_module(&self, build_source: BuildSource) -> State {
State::new(EnderpyFile::from(build_source))
}

fn resolve_file_imports(
&self,
file: EnderpyFile,
Expand Down
1 change: 0 additions & 1 deletion typechecker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod ast_visitor_generic;
mod nodes;
mod ruff_python_import_resolver;
mod semanal_utils;
mod state;
mod symbol_table;
mod type_check;

Expand Down
36 changes: 34 additions & 2 deletions typechecker/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
// here, so this has the minimum amount of nodes needed to
// get the type checker working. But can be expanded.

use std::path::PathBuf;
use std::{collections::HashMap, path::PathBuf};

use enderpy_python_parser as parser;
use enderpy_python_parser::ast::{Import, ImportFrom, Statement};
use parser::{error::ParsingError, Parser};

use crate::{ast_visitor::TraversalVisitor, build_source::BuildSource, diagnostic::Position};
use crate::{
ast_visitor::TraversalVisitor,
build_source::BuildSource,
diagnostic::Position,
ruff_python_import_resolver::{
import_result::ImportResult, module_descriptor::ImportModuleDescriptor,
},
semantic_analyzer::SemanticAnalyzer,
symbol_table::SymbolTable,
};

#[derive(Clone, Debug)]
pub enum ImportKinds {
Expand All @@ -31,6 +40,7 @@ pub struct EnderpyFile {
pub build_source: BuildSource,
// Parser Errors
pub errors: Vec<ParsingError>,
pub symbol_table: SymbolTable,
}

impl EnderpyFile {
Expand Down Expand Up @@ -63,6 +73,25 @@ impl EnderpyFile {
character: (pos - line_start - 1) as u32,
}
}

/// entry point to fill up the symbol table from the global definitions
pub fn populate_symbol_table(
&mut self,
imports: &HashMap<ImportModuleDescriptor, ImportResult>,
) {
let mut sem_anal = SemanticAnalyzer::new(self.clone(), imports.clone());
for stmt in &self.body {
sem_anal.visit_stmt(stmt)
}
// TODO: Hacky way to add the global scope to all scopes in symbol table after
// finishing
sem_anal.globals.exit_scope();
self.symbol_table = sem_anal.globals;
}

pub fn get_symbol_table(&self) -> SymbolTable {
self.symbol_table.clone()
}
}

impl From<BuildSource> for EnderpyFile {
Expand All @@ -72,13 +101,16 @@ impl From<BuildSource> for EnderpyFile {
build_source.path.as_path().to_str().unwrap().to_owned(),
);
let tree = parser.parse();
let symbol_table =
SymbolTable::global(build_source.module.clone(), build_source.path.clone());

let mut file = EnderpyFile {
defs: vec![],
imports: vec![],
body: vec![],
build_source,
errors: parser.errors,
symbol_table,
};

for stmt in &tree.body {
Expand Down
2 changes: 1 addition & 1 deletion typechecker/src/semantic_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct SemanticAnalyzer {
impl SemanticAnalyzer {
pub fn new(file: EnderpyFile, imports: HashMap<ImportModuleDescriptor, ImportResult>) -> Self {
log::debug!("Creating semantic analyzer for {}", file.module_name());
let globals = SymbolTable::global(file.clone());
let globals = SymbolTable::global(file.module_name(), file.path());
let is_pyi = file.path().ends_with(".pyi");
SemanticAnalyzer {
globals,
Expand Down
48 changes: 0 additions & 48 deletions typechecker/src/state.rs

This file was deleted.

11 changes: 4 additions & 7 deletions typechecker/src/symbol_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::{collections::HashMap, fmt::Display, path::PathBuf};

use enderpy_python_parser::ast::{self, Node};

use crate::{
nodes::EnderpyFile, ruff_python_import_resolver::import_result::ImportResult,
type_check::builtins,
};
use crate::{ruff_python_import_resolver::import_result::ImportResult, type_check::builtins};

#[derive(Debug, Clone)]
pub struct SymbolTable {
Expand Down Expand Up @@ -225,7 +222,7 @@ pub enum SymbolScope {
}

impl SymbolTable {
pub fn global(enderpy_file: EnderpyFile) -> Self {
pub fn global(module_name: String, file_path: PathBuf) -> Self {
let mut builtin_scope = SymbolTableScope {
id: get_id(),
symbol_table_type: SymbolTableType::BUILTIN,
Expand Down Expand Up @@ -317,8 +314,8 @@ impl SymbolTable {
scopes: vec![builtin_scope, global_scope],
all_scopes: vec![],
_locals: HashMap::new(),
module_name: enderpy_file.module_name(),
file_path: enderpy_file.path(),
module_name,
file_path,
}
}

Expand Down
10 changes: 7 additions & 3 deletions typechecker/src/type_check/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use enderpy_python_parser::ast::{self, *};

use super::{type_evaluator::TypeEvaluator, types::PythonType};
use crate::{
ast_visitor::TraversalVisitor, diagnostic::CharacterSpan, settings::Settings, state::State,
symbol_table::SymbolTable,
ast_visitor::TraversalVisitor, diagnostic::CharacterSpan, nodes::EnderpyFile,
settings::Settings, symbol_table::SymbolTable,
};

pub struct TypeChecker<'a> {
Expand All @@ -22,7 +22,11 @@ pub struct TypeCheckError {

#[allow(unused)]
impl<'a> TypeChecker<'a> {
pub fn new(module: &'a State, options: &'a Settings, symbol_tables: Vec<SymbolTable>) -> Self {
pub fn new(
module: &'a EnderpyFile,
options: &'a Settings,
symbol_tables: Vec<SymbolTable>,
) -> Self {
let symbol_table = module.get_symbol_table();
TypeChecker {
errors: vec![],
Expand Down
Loading

0 comments on commit dbfaad8

Please sign in to comment.