2022-02-16 21:17:25 +00:00
|
|
|
use super::callable::CallableId;
|
2022-02-16 15:37:12 +00:00
|
|
|
use super::types::{ElabData, Type};
|
2022-04-05 21:31:20 +00:00
|
|
|
use std::collections::HashMap;
|
2022-02-15 22:56:52 +00:00
|
|
|
use std::fmt::Debug;
|
2022-02-15 20:32:55 +00:00
|
|
|
|
|
|
|
/// ID of a definition (e.g. variable, block, function)
|
2022-02-15 22:56:52 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2022-02-15 20:32:55 +00:00
|
|
|
pub struct DefId(pub u32);
|
2022-04-05 21:31:20 +00:00
|
|
|
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
|
2022-02-15 20:32:55 +00:00
|
|
|
pub struct ExprId(pub u32);
|
2022-01-30 22:54:19 +00:00
|
|
|
|
2022-02-15 22:56:52 +00:00
|
|
|
// more compact Debug impl
|
|
|
|
impl Debug for DefId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "DefId({})", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Debug for ExprId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "ExprId({})", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 22:19:55 +00:00
|
|
|
/// an abstract element that performs some kind of computation on inputs
|
2022-02-15 20:32:55 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Expr {
|
|
|
|
pub id: ExprId,
|
|
|
|
pub kind: ExprKind,
|
|
|
|
pub typ: Type,
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:40:08 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Call {
|
|
|
|
pub called: CallableId,
|
2022-04-05 21:31:20 +00:00
|
|
|
pub args: Vec<ExprId>,
|
2022-04-04 19:40:08 +00:00
|
|
|
pub genargs: Vec<Type>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Match {
|
2022-04-05 21:31:20 +00:00
|
|
|
pub expr: ExprId,
|
|
|
|
pub arms: Vec<(ExprId, ExprId)>,
|
2022-04-04 19:40:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:32:55 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum ExprKind {
|
2022-02-16 15:37:12 +00:00
|
|
|
Literal(ElabData),
|
2022-02-15 20:32:55 +00:00
|
|
|
Path(DefId),
|
2022-04-04 19:40:08 +00:00
|
|
|
Call(Call),
|
|
|
|
Match(Box<Match>),
|
2022-01-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 20:32:55 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Signal {
|
|
|
|
pub id: DefId,
|
|
|
|
pub typ: Type,
|
2022-01-30 22:54:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-06 22:19:55 +00:00
|
|
|
/// A block of HDL code, e.g. comb block
|
2022-02-15 20:32:55 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2022-04-05 14:42:30 +00:00
|
|
|
pub struct Body {
|
2022-02-23 22:36:01 +00:00
|
|
|
pub signature: CallableId,
|
2022-02-15 20:32:55 +00:00
|
|
|
pub signals: Vec<Signal>,
|
2022-04-05 21:31:20 +00:00
|
|
|
pub exprs: HashMap<ExprId, Expr>,
|
|
|
|
pub expr: ExprId,
|
2022-02-06 22:19:55 +00:00
|
|
|
}
|
2022-02-20 16:49:28 +00:00
|
|
|
|
|
|
|
impl Expr {
|
|
|
|
pub fn with_type(mut self, typ: Type) -> Self {
|
|
|
|
self.typ = typ;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|