2022-02-18 11:59:53 +00:00
|
|
|
use super::types::{GenericArg, Type, TypingContext};
|
2022-01-23 20:04:19 +00:00
|
|
|
|
2022-02-16 21:17:25 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
|
|
|
|
pub struct CallableId(pub usize);
|
2022-02-16 16:38:56 +00:00
|
|
|
|
2022-02-15 20:32:55 +00:00
|
|
|
pub struct Callable {
|
2022-01-23 20:04:19 +00:00
|
|
|
pub name: String,
|
2022-02-15 20:32:55 +00:00
|
|
|
pub args: Vec<(Option<String>, Type)>,
|
|
|
|
pub ret_type: Option<Type>,
|
2022-01-23 20:04:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:32:55 +00:00
|
|
|
impl<'ty> Callable {
|
2022-01-23 20:04:19 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn argcount(&self) -> usize {
|
|
|
|
self.args.len()
|
|
|
|
}
|
|
|
|
}
|
2022-02-16 21:17:25 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
2022-02-18 11:59:53 +00:00
|
|
|
let logic1 = typectx.make_logic_size(1);
|
2022-02-16 21:17:25 +00:00
|
|
|
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(),
|
2022-02-18 11:59:53 +00:00
|
|
|
args: vec![(Some("a".to_string()), typectx.primitives.logic)],
|
|
|
|
ret_type: Some(logic1),
|
2022-02-16 21:17:25 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
}
|