1use std::fmt;
11use std::hash::Hash;
12
13use chandeliers_err::EAccum;
14
15use crate::causality::depends::Depends;
16
17pub mod options;
18
19crate::sp::derive_with_span!(Tuple<T> where <T>);
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub struct Tuple<T> {
23 elems: Vec<T>,
25}
26
27impl<T> Default for Tuple<T> {
28 fn default() -> Self {
30 Self {
31 elems: Vec::default(),
32 }
33 }
34}
35
36impl<T> Tuple<T> {
37 pub fn map<F, U>(self, f: F) -> Tuple<U>
39 where
40 F: Fn(T) -> U,
41 {
42 Tuple {
43 elems: self.elems.into_iter().map(f).collect(),
44 }
45 }
46
47 pub fn map_ref<F, U>(&self, f: F) -> Tuple<U>
49 where
50 F: Fn(&T) -> U,
51 {
52 Tuple {
53 elems: self.elems.iter().map(f).collect(),
54 }
55 }
56
57 pub fn try_map<F, U>(&self, eaccum: &mut EAccum, mut f: F) -> Option<Tuple<U>>
61 where
62 F: FnMut(&mut EAccum, &T) -> Option<U>,
63 {
64 let mut elems = Vec::new();
65 for e in &self.elems {
66 elems.push(f(&mut *eaccum, e)?);
67 }
68 Some(Tuple { elems })
69 }
70
71 pub fn iter(&self) -> impl Iterator<Item = &T> {
73 self.elems.iter()
74 }
75
76 pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
78 self.elems.iter_mut()
79 }
80
81 #[must_use]
83 pub fn len(&self) -> usize {
84 self.elems.len()
85 }
86
87 #[must_use]
89 pub fn is_empty(&self) -> bool {
90 self.elems.is_empty()
91 }
92
93 pub fn push(&mut self, e: T) {
95 self.elems.push(e);
96 }
97}
98
99impl<T> IntoIterator for Tuple<T> {
100 type Item = T;
101 type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
102 fn into_iter(self) -> Self::IntoIter {
104 self.elems.into_iter()
105 }
106}
107
108impl<T: Depends> Depends for Tuple<T> {
110 type Output = T::Output;
111 fn provides(&self, v: &mut Vec<Self::Output>) {
112 self.elems.provides(v);
113 }
114 fn requires(&self, v: &mut Vec<Self::Output>) {
115 self.elems.requires(v);
116 }
117}
118
119pub mod past {
121 use std::fmt;
122
123 crate::sp::derive_with_span!(Depth);
124 #[derive(Debug, Clone, Copy)]
126 pub struct Depth {
127 pub dt: usize,
129 }
130
131 impl fmt::Display for Depth {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 write!(f, "{}", self.dt)
134 }
135 }
136}
137
138impl<T> fmt::Display for Tuple<T>
139where
140 T: fmt::Display,
141{
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "(")?;
144 let mut es = self.elems.iter();
145 if let Some(e) = es.next() {
146 write!(f, "{e}")?;
147 }
148 for e in es {
149 write!(f, ", {e}")?;
150 }
151 write!(f, ")")
152 }
153}
154
155pub mod ty {
157 use std::fmt;
158
159 use crate::ast::past;
160 use crate::sp::Sp;
161
162 crate::sp::derive_with_span!(Base);
163 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
165 pub enum Base {
166 Int,
168 Float,
170 Bool,
172 Other(Sp<String>),
174 }
175
176 crate::sp::derive_with_span!(Clock);
177 #[derive(Debug, Clone)]
179 pub enum Clock {
180 Explicit {
182 activation: bool,
184 id: Sp<String>,
186 },
187 Implicit,
189 Adaptative,
191 }
192
193 impl fmt::Display for Base {
194 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
195 match self {
196 Self::Int => write!(f, "int"),
197 Self::Float => write!(f, "float"),
198 Self::Bool => write!(f, "bool"),
199 Self::Other(t) => write!(f, "{t}"),
200 }
201 }
202 }
203
204 crate::sp::derive_with_span!(Clocked<T> where <T>);
205 #[derive(Debug, Clone)]
207 pub struct Clocked<T> {
208 pub inner: Sp<T>,
210 pub clk: Sp<Clock>,
212 }
213
214 impl<T: fmt::Display> fmt::Display for Clocked<T> {
215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 write!(f, "{}{}", self.inner, self.clk)
217 }
218 }
219
220 #[derive(Debug, Clone)]
223 pub struct Stream {
224 pub ty: Sp<Clocked<Base>>,
226 pub depth: Sp<past::Depth>,
229 }
230
231 impl fmt::Display for Stream {
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233 write!(f, "{}", self.ty)
234 }
235 }
236
237 crate::sp::derive_with_span!(Tuple);
238 #[derive(Debug, Clone)]
240 pub enum Tuple {
241 Single(Sp<Base>),
244 Multiple(Sp<super::Tuple<Sp<Tuple>>>),
247 }
248
249 impl fmt::Display for Tuple {
250 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251 match self {
252 Self::Single(t) => write!(f, "{t}"),
253 Self::Multiple(ts) => write!(f, "{ts}"),
254 }
255 }
256 }
257
258 impl fmt::Display for Clock {
259 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260 match self {
261 Self::Explicit { activation, id } => write!(
262 f,
263 " {when} {id}",
264 when = if *activation { "when" } else { "whenot" }
265 ),
266 _ => Ok(()),
267 }
268 }
269 }
270}
271
272pub mod var {
274 use std::fmt;
275
276 use crate::sp::Sp;
277 use chandeliers_err::Transparent;
278
279 crate::sp::derive_with_span!(Local);
280 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
282 pub struct Local {
283 pub repr: Sp<String>,
285 }
286
287 crate::sp::derive_with_span!(Global);
288 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
290 pub struct Global {
291 pub repr: Sp<String>,
293 pub run_uid: Transparent<usize>,
295 }
296
297 impl fmt::Display for Local {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 write!(f, "{}", self.repr)
300 }
301 }
302
303 impl fmt::Display for Global {
304 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
305 write!(f, "{}", self.repr)
306 }
307 }
308
309 crate::sp::derive_with_span!(Past);
310 #[derive(Debug, Clone)]
312 pub struct Past {
313 pub var: Sp<Local>,
315 pub depth: Sp<super::past::Depth>,
317 }
318
319 crate::sp::derive_with_span!(Node);
320 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
322 pub struct Node {
323 pub id: Sp<usize>,
325 pub repr: Sp<String>,
327 }
328
329 crate::sp::derive_with_span!(Register);
330 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
332 pub struct Register {
333 pub id: Sp<usize>,
335 }
336
337 impl fmt::Display for Register {
338 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
339 write!(f, "reg{}", self.id)
340 }
341 }
342
343 crate::sp::derive_with_span!(Flip);
344 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
346 pub struct Flip {
347 pub id: Sp<usize>,
349 }
350
351 impl fmt::Display for Flip {
352 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353 write!(f, "flip{}", self.id)
354 }
355 }
356
357 impl fmt::Display for Past {
358 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
359 if self.depth.t.dt > 0 {
360 write!(f, "{}@{}", self.var, self.depth)
361 } else {
362 write!(f, "{}", self.var)
363 }
364 }
365 }
366
367 impl fmt::Display for Node {
368 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
369 write!(f, "{}", self.repr)
370 }
371 }
372
373 crate::sp::derive_with_span!(Reference);
374 #[derive(Debug, Clone)]
377 pub enum Reference {
378 Global(Sp<Global>),
380 Var(Sp<Past>),
382 }
383
384 impl fmt::Display for Reference {
385 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386 match self {
387 Self::Var(v) => write!(f, "{v}"),
388 Self::Global(g) => write!(f, "{g}"),
389 }
390 }
391 }
392}
393
394pub mod op {
396 use std::fmt;
397
398 #[derive(Debug, Clone, Copy)]
400 pub enum Bin {
401 Add,
403 Mul,
405 Sub,
407 Div,
409 Rem,
411 BitAnd,
413 BitOr,
415 BitXor,
417 }
418
419 impl fmt::Display for Bin {
420 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
421 match self {
422 Self::Add => write!(f, "+"),
423 Self::Mul => write!(f, "*"),
424 Self::Sub => write!(f, "-"),
425 Self::Div => write!(f, "/"),
426 Self::Rem => write!(f, "%"),
427 Self::BitAnd => write!(f, "and"),
428 Self::BitOr => write!(f, "or"),
429 Self::BitXor => write!(f, "^"),
430 }
431 }
432 }
433
434 #[derive(Debug, Clone, Copy)]
436 pub enum Un {
437 Neg,
439 Not,
441 }
442
443 impl fmt::Display for Un {
444 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445 match self {
446 Self::Neg => write!(f, "-"),
447 Self::Not => write!(f, "not"),
448 }
449 }
450 }
451
452 #[derive(Debug, Clone, Copy)]
454 pub enum Cmp {
455 Le,
457 Ge,
459 Lt,
461 Gt,
463 Eq,
465 Ne,
467 }
468
469 impl fmt::Display for Cmp {
470 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
471 match self {
472 Self::Le => write!(f, "<="),
473 Self::Ge => write!(f, ">="),
474 Self::Lt => write!(f, "<"),
475 Self::Gt => write!(f, ">"),
476 Self::Eq => write!(f, "="),
477 Self::Ne => write!(f, "<>"),
478 }
479 }
480 }
481
482 crate::sp::derive_with_span!(Clock);
483 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
485 pub enum Clock {
486 When,
488 Whenot,
490 }
491
492 impl fmt::Display for Clock {
493 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
494 match self {
495 Self::When => write!(f, "when"),
496 Self::Whenot => write!(f, "whenot"),
497 }
498 }
499 }
500}
501
502pub mod expr {
504 use super::var;
505
506 use std::fmt;
507
508 use crate::ast::{op, past, Tuple};
509 use crate::sp::Sp;
510
511 crate::sp::derive_with_span!(Lit);
512 #[derive(Debug, Clone, Copy)]
514 pub enum Lit {
515 Int(i64),
517 Float(f64),
519 Bool(bool),
521 }
522
523 impl fmt::Display for Lit {
524 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
525 match self {
526 Self::Int(i) => write!(f, "{i}"),
527 Self::Float(x) => write!(f, "{x}"),
528 Self::Bool(b) => write!(f, "{b}"),
529 }
530 }
531 }
532
533 crate::sp::derive_with_span!(Expr);
534 #[derive(Debug, Clone)]
536 pub enum Expr {
537 Lit(Sp<Lit>),
539 Reference(Sp<super::var::Reference>),
541 Tuple(Sp<Tuple<Sp<Expr>>>),
543 DummyPre(Sp<Box<Expr>>),
546 DummyParen(Sp<Box<Expr>>),
549 Bin {
551 op: op::Bin,
553 lhs: Sp<Box<Expr>>,
555 rhs: Sp<Box<Expr>>,
557 },
558 Un {
560 op: op::Un,
562 inner: Sp<Box<Expr>>,
564 },
565 Cmp {
567 op: op::Cmp,
569 lhs: Sp<Box<Expr>>,
571 rhs: Sp<Box<Expr>>,
573 },
574 Clock {
576 op: op::Clock,
578 inner: Sp<Box<Expr>>,
580 activate: Sp<Box<Expr>>,
582 },
583 Later {
586 delay: Sp<past::Depth>,
588 before: Sp<Box<Expr>>,
590 after: Sp<Box<Expr>>,
592 },
593 FetchRegister {
596 id: Sp<var::Register>,
598 dummy_init: Option<Sp<Box<Expr>>>,
600 dummy_followed_by: Sp<Box<Expr>>,
602 },
603 Flip {
606 id: Sp<var::Flip>,
608 initial: Sp<Box<Expr>>,
610 continued: Sp<Box<Expr>>,
612 },
613 Ifx {
615 cond: Sp<Box<Expr>>,
617 yes: Sp<Box<Expr>>,
619 no: Sp<Box<Expr>>,
621 },
622 Merge {
624 switch: Sp<Box<Expr>>,
626 on: Sp<Box<Expr>>,
628 off: Sp<Box<Expr>>,
630 },
631 Substep {
633 delay: usize,
635 id: Sp<super::var::Node>,
637 args: Sp<Box<Expr>>,
639 },
640 }
641
642 impl fmt::Display for Expr {
643 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
644 match self {
645 Self::Lit(l) => write!(f, "{l}"),
646 Self::Reference(r) => write!(f, "{r}"),
647 Self::Tuple(t) => write!(f, "{t}"),
648 Self::DummyParen(inner) => write!(f, "({inner})"),
649 Self::DummyPre(t) => write!(f, "{t}"),
650 Self::Bin { op, lhs, rhs } => write!(f, "{lhs} {op} {rhs}"),
651 Self::Un { op, inner } => write!(f, "{op} {inner}"),
652 Self::Cmp { op, lhs, rhs } => write!(f, "{lhs} {op} {rhs}"),
653 Self::FetchRegister { id, .. } => write!(f, "!{id}"),
654 Self::Flip {
655 id,
656 initial,
657 continued,
658 } => write!(f, "{initial} ->@{id} {continued}"),
659 Self::Later {
660 delay,
661 before,
662 after,
663 } => write!(f, "({before} ->{delay} {after})"),
664 Self::Ifx { cond, yes, no } => {
665 write!(f, "if {cond} {{ {yes} }} else {{ {no} }}")
666 }
667 Self::Substep { delay, id, args } => {
668 if *delay > 0 {
669 write!(f, "{id}@{delay}{args}")
670 } else {
671 write!(f, "{id}{args}")
672 }
673 }
674 Self::Clock {
675 op,
676 inner,
677 activate,
678 } => write!(f, "({inner} {op} {activate})"),
679 Self::Merge { switch, on, off } => write!(f, "(merge {switch} {on} {off})"),
680 }
681 }
682 }
683}
684
685pub mod stmt {
687 use std::fmt;
688
689 use crate::ast::{expr, var, Tuple};
690 use crate::sp::Sp;
691
692 #[derive(Debug, Clone)]
695 pub enum VarTuple {
696 Single(Sp<var::Local>),
698 Multiple(Sp<Tuple<Sp<VarTuple>>>),
700 }
701
702 impl VarTuple {
703 #[must_use]
706 pub fn is_empty(&self) -> bool {
707 match self {
708 Self::Single(_) => false,
709 Self::Multiple(tup) => tup.t.is_empty(),
710 }
711 }
712 }
713
714 impl fmt::Display for VarTuple {
715 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
716 match self {
717 Self::Single(v) => write!(f, "{v}"),
718 Self::Multiple(vs) => write!(f, "{vs}"),
719 }
720 }
721 }
722
723 crate::sp::derive_with_span!(Statement);
724 #[derive(Debug, Clone)]
726 pub enum Statement {
727 Let {
729 target: Sp<VarTuple>,
731 source: Sp<expr::Expr>,
733 },
734 Assert(Sp<expr::Expr>),
736 InitRegister {
738 id: Sp<var::Register>,
740 val: Option<Sp<expr::Expr>>,
742 clk: Option<Sp<expr::Expr>>,
744 },
745 UpdateRegister {
747 id: Sp<var::Register>,
749 val: Sp<expr::Expr>,
751 },
752 }
753}
754
755pub mod decl {
757 use std::fmt;
758
759 use crate::ast::{expr, options, stmt, ty, var, Tuple};
760 use crate::sp::Sp;
761 use chandeliers_err::Transparent;
762
763 #[derive(Debug, Clone)]
765 pub struct TyVar {
766 pub name: Sp<var::Local>,
768 pub ty: Sp<ty::Stream>,
770 }
771
772 impl fmt::Display for TyVar {
773 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
774 write!(f, "{}: {}", self.name, self.ty)
775 }
776 }
777
778 crate::sp::derive_with_span!(NodeName);
779 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
781 pub struct NodeName {
782 pub repr: Sp<String>,
784 pub run_uid: Transparent<usize>,
786 }
787
788 impl fmt::Display for NodeName {
789 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
790 write!(f, "{}", self.repr)
791 }
792 }
793
794 #[derive(Debug, Clone)]
796 pub struct NodeInstance {
797 pub name: Sp<NodeName>,
799 pub generics: Option<Vec<Sp<ty::Base>>>,
801 }
802
803 #[derive(Debug, Clone)]
805 pub struct RegisterInstance {
806 pub id: Sp<var::Register>,
808 pub typ: Option<Sp<ty::Tuple>>,
811 }
812
813 #[derive(Debug, Clone)]
815 pub struct FlipInstance {
816 pub id: Sp<var::Flip>,
818 }
819
820 #[derive(Debug, Clone)]
822 pub struct Node {
823 pub name: Sp<NodeName>,
825 pub options: options::Node,
827 pub inputs: Sp<Tuple<Sp<TyVar>>>,
829 pub outputs: Sp<Tuple<Sp<TyVar>>>,
831 pub locals: Sp<Tuple<Sp<TyVar>>>,
833 pub blocks: Vec<NodeInstance>,
835 pub deptys: Vec<Sp<var::Local>>,
837 pub stmts: Vec<Sp<stmt::Statement>>,
839 pub registers: Vec<RegisterInstance>,
841 pub flips: Vec<FlipInstance>,
843 }
844
845 #[derive(Debug, Clone)]
847 pub struct Const {
848 pub name: Sp<var::Global>,
850 pub options: options::Const,
852 pub ty: Sp<ty::Base>,
854 pub value: Sp<expr::Expr>,
856 }
857
858 #[derive(Debug, Clone)]
862 pub struct ExtNode {
863 pub name: Sp<NodeName>,
865 pub inputs: Sp<Tuple<Sp<TyVar>>>,
867 pub outputs: Sp<Tuple<Sp<TyVar>>>,
869 pub options: options::ExtNode,
871 }
872
873 #[derive(Debug, Clone)]
877 pub struct ExtConst {
878 pub name: Sp<var::Global>,
880 pub ty: Sp<ty::Base>,
882 pub options: options::ExtConst,
884 }
885
886 #[derive(Debug, Clone)]
888 pub enum Decl {
889 Const(Sp<Const>),
891 Node(Sp<Node>),
893 ExtConst(Sp<ExtConst>),
895 ExtNode(Sp<ExtNode>),
897 }
898
899 #[derive(Debug, Clone)]
901 pub struct Prog {
902 pub decls: Vec<Sp<Decl>>,
904 }
905}