futilehdl/src/frontend/callable.rs

73 lines
1.8 KiB
Rust

use super::types::{Type, TypingContext};
use std::collections::HashMap;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
pub struct CallableId(pub usize);
pub struct Callable {
pub name: String,
pub args: Vec<(Option<String>, Type)>,
pub ret_type: Option<Type>,
}
impl<'ty> Callable {
pub fn name(&self) -> &str {
&self.name
}
pub fn argcount(&self) -> usize {
self.args.len()
}
}
pub struct BuiltinCallables {
pub xor: CallableId,
pub bitnot: CallableId,
pub reduce_or: CallableId,
}
pub struct CallableContext {
pub builtins: BuiltinCallables,
callables: Vec<Callable>,
}
impl CallableContext {
pub fn new(typectx: &mut TypingContext) -> Self {
let builtins = BuiltinCallables {
xor: CallableId(0),
bitnot: CallableId(1),
reduce_or: CallableId(2),
};
Self {
callables: vec![
Callable {
name: "builtin::xor".to_string(),
args: vec![],
ret_type: Some(typectx.primitives.logic),
},
Callable {
name: "builtin::bitnot".to_string(),
args: vec![],
ret_type: Some(typectx.primitives.logic),
},
Callable {
name: "builtin::reduce_or".to_string(),
args: vec![],
ret_type: Some(typectx.primitives.logic),
},
],
builtins,
}
}
pub fn add(&mut self, callable: Callable) -> CallableId {
let id = self.callables.len();
self.callables.push(callable);
CallableId(id)
}
pub fn get(&self, id: CallableId) -> &Callable {
&self.callables[id.0]
}
}