lt-core/src/symbols.rs

72 lines
2 KiB
Rust
Raw Normal View History

2024-05-05 18:19:28 +02:00
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
use {
crate::expr::LTExpr
};
pub struct SymbolEntry {
link_addr: tisc::VM_Word,
in_types: Vec< laddertypes::TypeTerm >,
out_types: Vec< laddertypes::TypeTerm >
}
pub struct SymbolTable {
symbols: HashMap< String, SymbolEntry >,
linker: tisc::Linker
}
impl SymbolTable {
pub fn new() -> Self {
SymbolTable {
symbols: HashMap::new(),
linker: tisc::Linker::new(0x100)
}
}
pub fn get_link_addr(&self, symbol: &str) -> Option< tisc::VM_Word > {
self.symbols.get(&String::from(symbol)).map(|e| e.link_addr)
}
pub fn call_symbol(&self, typectx: &Arc<RwLock<laddertypes::TypeDict>>, symbol: &str) -> LTExpr {
let entry = self.symbols.get(&String::from(symbol)).unwrap();
LTExpr::call_symbol(
/*
&laddertypes::TypeTerm::App(vec![
typectx.write().unwrap().parse("Fn").expect("parse typeterm"),
laddertypes::TypeTerm::App( entry.in_types.clone() ),
laddertypes::TypeTerm::App( entry.out_types.clone() )
]),
*/
typectx,
entry.out_types.len(),
entry.link_addr
)
}
pub fn define(
&mut self,
vm: &mut tisc::VM,
typectx: &Arc<RwLock<laddertypes::TypeDict>>,
symbol: &str,
in_types: Vec<&str>,
out_types: Vec<&str>,
asm: tisc::Assembler
) {
self.linker.link( vm, String::from(symbol), asm.build() );
self.symbols.insert(
String::from(symbol),
SymbolEntry {
link_addr: self.linker.resolve_symbol(&String::from(symbol)).expect("cant find symbol"),
in_types: in_types.into_iter().map(|t| typectx.write().unwrap().parse(t).expect("parse typeterm")).collect(),
out_types: out_types.into_iter().map(|t| typectx.write().unwrap().parse(t).expect("parse typeterm")).collect()
}
);
}
}