Move a bunch of fields to AnalysisState instead

This commit is contained in:
Sofia 2025-08-03 22:37:28 +03:00
parent 3b3b21d4dc
commit 5706fd99e3
2 changed files with 36 additions and 36 deletions

View File

@ -120,6 +120,11 @@ pub struct AnalysisState {
symbol_table: Vec<Symbol>, symbol_table: Vec<Symbol>,
/// SymbolID -> Symbol /// SymbolID -> Symbol
pub symbol_to_token: HashMap<SymbolId, usize>, pub symbol_to_token: HashMap<SymbolId, usize>,
functions: HashMap<String, SymbolId>,
associated_functions: HashMap<(TypeKind, String), SymbolId>,
properties: HashMap<(TypeKind, String), SymbolId>,
types: HashMap<TypeKind, SymbolId>,
} }
impl AnalysisState { impl AnalysisState {
@ -203,10 +208,6 @@ pub struct AnalysisScope<'a> {
state: &'a mut AnalysisState, state: &'a mut AnalysisState,
tokens: &'a Vec<FullToken>, tokens: &'a Vec<FullToken>,
variables: HashMap<String, SymbolId>, variables: HashMap<String, SymbolId>,
functions: HashMap<String, SymbolId>,
associated_functions: HashMap<(TypeKind, String), SymbolId>,
properties: HashMap<(TypeKind, String), SymbolId>,
types: HashMap<TypeKind, SymbolId>,
} }
impl<'a> AnalysisScope<'a> { impl<'a> AnalysisScope<'a> {
@ -215,10 +216,6 @@ impl<'a> AnalysisScope<'a> {
state: self.state, state: self.state,
tokens: self.tokens, tokens: self.tokens,
variables: self.variables.clone(), variables: self.variables.clone(),
functions: self.functions.clone(),
associated_functions: self.associated_functions.clone(),
properties: self.properties.clone(),
types: self.types.clone(),
} }
} }
@ -344,16 +341,16 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti
map: HashMap::new(), map: HashMap::new(),
symbol_table: Vec::new(), symbol_table: Vec::new(),
symbol_to_token: HashMap::new(), symbol_to_token: HashMap::new(),
functions: HashMap::new(),
associated_functions: HashMap::new(),
properties: HashMap::new(),
types: HashMap::new(),
}; };
let mut scope = AnalysisScope { let mut scope = AnalysisScope {
state: &mut state, state: &mut state,
tokens: &module.tokens, tokens: &module.tokens,
variables: HashMap::new(), variables: HashMap::new(),
functions: HashMap::new(),
associated_functions: HashMap::new(),
properties: HashMap::new(),
types: HashMap::new(),
}; };
for (i, token) in module.tokens.iter().enumerate() { for (i, token) in module.tokens.iter().enumerate() {
@ -458,7 +455,7 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti
.state .state
.new_symbol(struct_idx, SemanticKind::Struct, module.module_id); .new_symbol(struct_idx, SemanticKind::Struct, module.module_id);
scope.state.set_symbol(struct_idx, struct_symbol); scope.state.set_symbol(struct_idx, struct_symbol);
scope.types.insert( scope.state.types.insert(
TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)), TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)),
struct_symbol, struct_symbol,
); );
@ -485,7 +482,7 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti
.new_symbol(field_idx, SemanticKind::Property, module.module_id); .new_symbol(field_idx, SemanticKind::Property, module.module_id);
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
scope.properties.insert( scope.state.properties.insert(
( (
TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)), TypeKind::CustomType(CustomTypeKey(typedef.name.clone(), typedef.source_module)),
field.0.clone(), field.0.clone(),
@ -524,6 +521,7 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti
let symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id); let symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id);
scope.state.set_symbol(idx, symbol); scope.state.set_symbol(idx, symbol);
scope scope
.state
.associated_functions .associated_functions
.insert((ty.clone(), function.name.clone()), symbol); .insert((ty.clone(), function.name.clone()), symbol);
@ -559,7 +557,7 @@ pub fn analyze_context(context: &mir::Context, module: &mir::Module, error: Opti
.unwrap_or(function.signature().range.end); .unwrap_or(function.signature().range.end);
let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id); let function_symbol = scope.state.new_symbol(idx, SemanticKind::Function, module.module_id);
scope.state.set_symbol(idx, function_symbol); scope.state.set_symbol(idx, function_symbol);
scope.functions.insert(function.name.clone(), function_symbol); scope.state.functions.insert(function.name.clone(), function_symbol);
} }
for function in &module.functions { for function in &module.functions {
@ -700,18 +698,19 @@ pub fn analyze_expr(
.token_idx(&meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(meta.range.end); .unwrap_or(meta.range.end);
let field_symbol = let field_symbol = if let Some(symbol_id) =
if let Some(symbol_id) = scope.properties.get(&(accessed_type.clone(), name.clone())) { scope.state.properties.get(&(accessed_type.clone(), name.clone()))
scope.state.new_symbol( {
field_idx, scope.state.new_symbol(
SemanticKind::Reference(*symbol_id), field_idx,
source_module.module_id, SemanticKind::Reference(*symbol_id),
) source_module.module_id,
} else { )
scope } else {
.state scope
.new_symbol(field_idx, SemanticKind::Property, source_module.module_id) .state
}; .new_symbol(field_idx, SemanticKind::Property, source_module.module_id)
};
scope.state.set_symbol(field_idx, field_symbol); scope.state.set_symbol(field_idx, field_symbol);
if let Some(typedef) = typedef { if let Some(typedef) = typedef {
@ -744,7 +743,7 @@ pub fn analyze_expr(
.token_idx(&expr.1, |t| matches!(t, Token::Identifier(_))) .token_idx(&expr.1, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(expr.1.range.end); .unwrap_or(expr.1.range.end);
let struct_symbol = if let Some(symbol_id) = scope.types.get(&struct_type) { let struct_symbol = if let Some(symbol_id) = scope.state.types.get(&struct_type) {
scope scope
.state .state
.new_symbol(struct_idx, SemanticKind::Reference(*symbol_id), source_module.module_id) .new_symbol(struct_idx, SemanticKind::Reference(*symbol_id), source_module.module_id)
@ -761,7 +760,7 @@ pub fn analyze_expr(
.unwrap_or(field_meta.range.end); .unwrap_or(field_meta.range.end);
let field_symbol = let field_symbol =
if let Some(symbol_id) = scope.properties.get(&(struct_type.clone(), field_name.clone())) { if let Some(symbol_id) = scope.state.properties.get(&(struct_type.clone(), field_name.clone())) {
scope scope
.state .state
.new_symbol(field_idx, SemanticKind::Reference(*symbol_id), source_module.module_id) .new_symbol(field_idx, SemanticKind::Reference(*symbol_id), source_module.module_id)
@ -806,7 +805,7 @@ pub fn analyze_expr(
let idx = scope let idx = scope
.token_idx(&meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(meta.range.end); .unwrap_or(meta.range.end);
let symbol = if let Some(symbol_id) = scope.functions.get(name) { let symbol = if let Some(symbol_id) = scope.state.functions.get(name) {
scope scope
.state .state
.new_symbol(idx, SemanticKind::Reference(*symbol_id), source_module.module_id) .new_symbol(idx, SemanticKind::Reference(*symbol_id), source_module.module_id)
@ -832,7 +831,7 @@ pub fn analyze_expr(
ty.clone() ty.clone()
}; };
let type_symbol = if let Some(symbol_id) = scope.types.get(&invoked_ty) { let type_symbol = if let Some(symbol_id) = scope.state.types.get(&invoked_ty) {
scope scope
.state .state
.new_symbol(type_idx, SemanticKind::Reference(*symbol_id), source_module.module_id) .new_symbol(type_idx, SemanticKind::Reference(*symbol_id), source_module.module_id)
@ -846,7 +845,10 @@ pub fn analyze_expr(
let fn_idx = scope let fn_idx = scope
.token_idx(&meta, |t| matches!(t, Token::Identifier(_))) .token_idx(&meta, |t| matches!(t, Token::Identifier(_)))
.unwrap_or(meta.range.end); .unwrap_or(meta.range.end);
let fn_symbol = if let Some(symbol_id) = scope.associated_functions.get(&(invoked_ty.clone(), name.clone())) let fn_symbol = if let Some(symbol_id) = scope
.state
.associated_functions
.get(&(invoked_ty.clone(), name.clone()))
{ {
scope scope
.state .state

View File

@ -399,15 +399,13 @@ impl Backend {
let module_id = lock.increment(); let module_id = lock.increment();
drop(lock); drop(lock);
self.url_to_module.insert(path.clone(), module_id); self.url_to_module.insert(path.clone(), module_id);
self.module_to_url.insert(module_id, path.clone());
module_id module_id
}; };
let parse_res = parse(&params.text, path.clone(), &mut map, module_id); let parse_res = parse(&params.text, path.clone(), &mut map, module_id);
let (tokens, result) = match parse_res { let (tokens, result) = match parse_res {
Ok((module_id, tokens)) => { Ok((module_id, tokens)) => (tokens.clone(), analyze(module_id, tokens, path, &mut map)),
dbg!("compiled: ", module_id);
(tokens.clone(), analyze(module_id, tokens, path, &mut map))
}
Err(e) => (Vec::new(), Err(e)), Err(e) => (Vec::new(), Err(e)),
}; };