nac3/nac3standalone/src/basic_symbol_resolver.rs

81 lines
2.4 KiB
Rust
Raw Normal View History

2021-10-16 18:08:13 +08:00
use nac3core::{
codegen::CodeGenContext,
symbol_resolver::{SymbolResolver, SymbolValue, ValueEnum},
2021-10-16 18:08:13 +08:00
toplevel::{DefinitionId, TopLevelDef},
typecheck::{
2021-08-19 15:30:52 +08:00
type_inferencer::PrimitiveStore,
typedef::{Type, Unifier},
2021-10-16 18:08:13 +08:00
},
};
use nac3parser::ast::{self, StrRef};
2021-11-20 19:50:25 +08:00
use parking_lot::{Mutex, RwLock};
use std::{collections::HashMap, sync::Arc};
2021-08-19 15:30:52 +08:00
pub struct ResolverInternal {
2021-09-22 17:19:27 +08:00
pub id_to_type: Mutex<HashMap<StrRef, Type>>,
pub id_to_def: Mutex<HashMap<StrRef, DefinitionId>>,
pub class_names: Mutex<HashMap<StrRef, Type>>,
pub module_globals: Mutex<HashMap<StrRef, SymbolValue>>,
2022-02-13 17:21:42 +08:00
pub str_store: Mutex<HashMap<String, i32>>,
2021-08-19 15:30:52 +08:00
}
impl ResolverInternal {
2021-09-22 17:19:27 +08:00
pub fn add_id_def(&self, id: StrRef, def: DefinitionId) {
self.id_to_def.lock().insert(id, def);
}
2021-09-22 17:19:27 +08:00
pub fn add_id_type(&self, id: StrRef, ty: Type) {
self.id_to_type.lock().insert(id, ty);
}
pub fn add_module_global(&self, id: StrRef, val: SymbolValue) {
self.module_globals.lock().insert(id, val);
}
}
pub struct Resolver(pub Arc<ResolverInternal>);
2021-08-19 15:30:52 +08:00
impl SymbolResolver for Resolver {
fn get_default_param_value(&self, expr: &ast::Expr) -> Option<SymbolValue> {
match &expr.node {
ast::ExprKind::Name { id, .. } => {
self.0.module_globals.lock().get(id).cloned()
}
_ => unimplemented!("other type of expr not supported at {}", expr.location)
}
}
2021-10-16 18:08:13 +08:00
fn get_symbol_type(
&self,
_: &mut Unifier,
_: &[Arc<RwLock<TopLevelDef>>],
_: &PrimitiveStore,
str: StrRef,
) -> Result<Type, String> {
self.0.id_to_type.lock().get(&str).cloned().ok_or(format!("cannot get type of {}", str))
2021-08-19 15:30:52 +08:00
}
2021-10-16 18:08:13 +08:00
fn get_symbol_value<'ctx, 'a>(
&self,
_: StrRef,
_: &mut CodeGenContext<'ctx, 'a>,
2021-11-20 19:50:25 +08:00
) -> Option<ValueEnum<'ctx>> {
2021-08-19 15:30:52 +08:00
unimplemented!()
}
fn get_identifier_def(&self, id: StrRef) -> Result<DefinitionId, String> {
self.0.id_to_def.lock().get(&id).cloned().ok_or_else(|| "Undefined identifier".to_string())
2021-08-19 15:30:52 +08:00
}
2022-02-12 21:21:56 +08:00
2022-02-13 17:21:42 +08:00
fn get_string_id(&self, s: &str) -> i32 {
let mut str_store = self.0.str_store.lock();
if let Some(id) = str_store.get(s) {
*id
} else {
let id = str_store.len() as i32;
str_store.insert(s.to_string(), id);
id
}
2022-02-12 21:21:56 +08:00
}
2021-08-19 15:30:52 +08:00
}