82 lines
2.3 KiB
Rust
82 lines
2.3 KiB
Rust
use super::types::{Type, TypingContext};
|
|
|
|
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
|
|
pub struct CallableId(pub usize);
|
|
|
|
#[derive(Debug)]
|
|
pub struct Callable {
|
|
pub name: String,
|
|
pub args: Vec<(Option<String>, Type)>,
|
|
pub genargs: Vec<(Option<String>, Type)>,
|
|
pub ret_type: 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),
|
|
};
|
|
let logic1 = typectx.make_logic_size(1);
|
|
let logic_tvar0 = typectx.make_typevar(0, 0);
|
|
Self {
|
|
callables: vec![
|
|
Callable {
|
|
name: "builtin::xor".to_string(),
|
|
args: vec![
|
|
(Some("a".to_string()), logic_tvar0),
|
|
(Some("b".to_string()), logic_tvar0),
|
|
],
|
|
genargs: vec![(Some("T".to_string()), typectx.primitives.logic)],
|
|
ret_type: logic_tvar0,
|
|
},
|
|
Callable {
|
|
name: "builtin::bitnot".to_string(),
|
|
args: vec![(Some("a".to_string()), logic_tvar0)],
|
|
genargs: vec![(Some("T".to_string()), typectx.primitives.logic)],
|
|
ret_type: logic_tvar0,
|
|
},
|
|
Callable {
|
|
name: "builtin::reduce_or".to_string(),
|
|
args: vec![(Some("a".to_string()), typectx.primitives.logic)],
|
|
genargs: vec![],
|
|
ret_type: logic1,
|
|
},
|
|
],
|
|
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]
|
|
}
|
|
}
|