1use super::{BinaryOp, Constraint, Decl, Expr, FlatPint, Immediate, Solve, Type, UnaryOp, Var};
2use std::fmt::{Display, Formatter, Result};
3impl Display for FlatPint {
4 fn fmt(&self, f: &mut Formatter) -> Result {
5 for decl in &self.decls {
6 writeln!(f, "{decl}")?;
7 }
8 writeln!(f, "{}", self.solve)
9 }
10}
11
12impl Display for Decl {
13 fn fmt(&self, f: &mut Formatter) -> Result {
14 match self {
15 Decl::Var(var) => write!(f, "{var}"),
16 Decl::Constraint(constraint) => write!(f, "{constraint}"),
17 }
18 }
19}
20
21impl Display for Var {
22 fn fmt(&self, f: &mut Formatter) -> Result {
23 write!(f, "var {}: {};", self.name, self.ty)
24 }
25}
26
27impl Display for Constraint {
28 fn fmt(&self, f: &mut Formatter) -> Result {
29 write!(f, "constraint {};", self.0)
30 }
31}
32
33impl Display for Solve {
34 fn fmt(&self, f: &mut Formatter) -> Result {
35 match self {
36 Solve::Satisfy => write!(f, "solve satisfy;"),
37 Solve::Minimize(obj) => write!(f, "solve minimize {obj};"),
38 Solve::Maximize(obj) => write!(f, "solve maximize {obj};"),
39 }
40 }
41}
42
43impl Display for Expr {
44 fn fmt(&self, f: &mut Formatter) -> Result {
45 match self {
46 Expr::Immediate(imm) => write!(f, "{imm}"),
47 Expr::Path(path) => write!(f, "{path}"),
48 Expr::UnaryOp { op, expr } => write!(f, "{op}{expr}"),
49 Expr::BinaryOp { op, lhs, rhs } => write!(f, "({lhs} {op} {rhs})"),
50 }
51 }
52}
53
54impl Display for Immediate {
55 fn fmt(&self, f: &mut Formatter) -> Result {
56 match self {
57 Immediate::Bool(b) => write!(f, "{b}"),
58 Immediate::Int(i) => write!(f, "{i}"),
59 Immediate::Real(r) => write!(f, "{r:e}"),
60 }
61 }
62}
63
64impl Display for BinaryOp {
65 fn fmt(&self, f: &mut Formatter) -> Result {
66 match self {
67 BinaryOp::Add => write!(f, "+"),
68 BinaryOp::Sub => write!(f, "-"),
69 BinaryOp::Mul => write!(f, "*"),
70 BinaryOp::Div => write!(f, "/"),
71 BinaryOp::Mod => write!(f, "%"),
72 BinaryOp::Equal => write!(f, "=="),
73 BinaryOp::NotEqual => write!(f, "!="),
74 BinaryOp::LessThanOrEqual => write!(f, "<="),
75 BinaryOp::LessThan => write!(f, "<"),
76 BinaryOp::GreaterThanOrEqual => write!(f, ">="),
77 BinaryOp::GreaterThan => write!(f, ">"),
78 BinaryOp::LogicalAnd => write!(f, "&&"),
79 BinaryOp::LogicalOr => write!(f, "||"),
80 }
81 }
82}
83
84impl Display for UnaryOp {
85 fn fmt(&self, f: &mut Formatter) -> Result {
86 match self {
87 UnaryOp::Neg => write!(f, "-"),
88 UnaryOp::Not => write!(f, "!"),
89 }
90 }
91}
92
93impl Display for Type {
94 fn fmt(&self, f: &mut Formatter) -> Result {
95 match self {
96 Type::Bool => write!(f, "bool"),
97 Type::Int => write!(f, "int"),
98 Type::Real => write!(f, "real"),
99 }
100 }
101}