lib/accy/src/choir/shape/expression.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 pub const SymbolId = u32;
 2 
 3 pub const Term = struct {
 4     symbol: SymbolId,
 5     coefficient: i64 = 1,
 6 };
 7 
 8 pub const Expression = struct {
 9     constant: i64 = 0,
10     terms: []const Term = &.{},
11 
12     pub fn eql(self: Expression, other: Expression) bool {
13         if (self.constant != other.constant or self.terms.len != other.terms.len) return false;
14         for (self.terms, other.terms) |lhs, rhs| {
15             if (lhs.symbol != rhs.symbol or lhs.coefficient != rhs.coefficient) return false;
16         }
17         return true;
18     }
19 
20     pub fn isConstant(self: Expression) bool {
21         return self.terms.len == 0;
22     }
23 
24     pub fn staticValue(self: Expression) ?u64 {
25         if (!self.isConstant() or self.constant < 0) return null;
26         return @intCast(self.constant);
27     }
28 };
29 
30 pub fn constant(value: i64) Expression {
31     return .{ .constant = value };
32 }