Skip to content

Commit aa257b9

Browse files
committed
Attempting to add bus constraints parsing, may need rework
1 parent 181ae63 commit aa257b9

File tree

12 files changed

+191
-21
lines changed

12 files changed

+191
-21
lines changed

air/src/passes/translate_from_ast.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ impl<'a> AirBuilder<'a> {
297297
}
298298
ast::Statement::Enforce(_)
299299
| ast::Statement::EnforceIf(_, _)
300-
| ast::Statement::EnforceAll(_) => {
300+
| ast::Statement::EnforceAll(_)
301+
| ast::Statement::BusEnforce(_) => {
301302
unreachable!()
302303
}
303304
}
@@ -447,7 +448,9 @@ impl<'a> AirBuilder<'a> {
447448
panic!("expected scalar expression to produce scalar value, got: {invalid:?}")
448449
}
449450
},
450-
ast::ScalarExpr::Call(_) | ast::ScalarExpr::BoundedSymbolAccess(_) => unreachable!(),
451+
ast::ScalarExpr::Call(_)
452+
| ast::ScalarExpr::BoundedSymbolAccess(_)
453+
| ast::ScalarExpr::BusOperation(_) => unreachable!(),
451454
}
452455
}
453456

mir/derive-ir/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use syn::{parse_macro_input, DeriveInput};
2929
/// f: Link<Vec<Link<Op>>>,
3030
/// }
3131
/// ```
32-
///
32+
///
3333
/// We now isolate the optional fields:
3434
/// - `a: BackLink<Owner>`
3535
/// - `b: Vec<BackLink<Owner>>`
@@ -38,13 +38,13 @@ use syn::{parse_macro_input, DeriveInput};
3838
/// and the required fields:
3939
/// - `c: i32`
4040
/// - `d: Link<Op>`
41-
///
41+
///
4242
/// We generate an initial state `FooBuilder0` with no fields set,
4343
/// except for the optional fields which are set to their default values.
4444
/// ```ignore
4545
/// let initial_state = [true, true, false, false, true, true];
4646
/// ```
47-
///
47+
///
4848
/// We then generate a state with each of the required fields set,
4949
/// and every possible combination of those field states.
5050
/// We sort the states by the number of fields set, then by leftmost true bits.
@@ -58,7 +58,7 @@ use syn::{parse_macro_input, DeriveInput};
5858
/// [true, true, true, true, true, true], // 3
5959
/// ];
6060
/// ```
61-
///
61+
///
6262
/// We generate a transition table for each state,
6363
/// which maps from the current state to the next state.
6464
/// rows are indexed by the current state,
@@ -71,7 +71,7 @@ use syn::{parse_macro_input, DeriveInput};
7171
/// /* 3: */ [3, 3, 3, 3, 3, 3],
7272
/// ];
7373
/// ```
74-
///
74+
///
7575
/// We then generate an implementation for each state,
7676
/// with a method for each field., which transitions to the next state.
7777
///
@@ -107,7 +107,7 @@ use syn::{parse_macro_input, DeriveInput};
107107
/// f: Link::new(vec![f0, f1]),
108108
/// });
109109
/// ```
110-
///
110+
///
111111
#[proc_macro_derive(Builder, attributes(enum_wrapper))]
112112
pub fn derive_builder(input: TokenStream) -> TokenStream {
113113
let ast = parse_macro_input!(input as DeriveInput);

mir/src/passes/translate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ impl<'a> MirBuilder<'a> {
374374
ast::Statement::Enforce(enf) => self.translate_enforce(enf),
375375
ast::Statement::EnforceIf(enf, cond) => self.translate_enforce_if(enf, cond),
376376
ast::Statement::EnforceAll(list_comp) => self.translate_enforce_all(list_comp),
377+
ast::Statement::BusEnforce(_) => todo!(),
377378
}
378379
}
379380
fn translate_let(&mut self, let_stmt: &'a ast::Let) -> Result<Link<Op>, CompileError> {
@@ -808,6 +809,7 @@ impl<'a> MirBuilder<'a> {
808809
ast::ScalarExpr::Binary(b) => self.translate_binary_op(b),
809810
ast::ScalarExpr::Call(c) => self.translate_call(c),
810811
ast::ScalarExpr::Let(l) => self.translate_let(l),
812+
ast::ScalarExpr::BusOperation(_) => todo!(),
811813
}
812814
}
813815

parser/src/ast/declarations.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ pub enum BusType {
104104
Mult,
105105
}
106106

107+
#[derive(Debug, Clone, PartialEq, Eq)]
108+
pub enum BusOperator {
109+
/// Add a tuple to the bus
110+
Add,
111+
/// Remove a tuple from the bus
112+
Rem,
113+
}
114+
impl std::fmt::Display for BusOperator {
115+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
116+
match self {
117+
Self::Add => write!(f, "add"),
118+
Self::Rem => write!(f, "rem"),
119+
}
120+
}
121+
}
122+
107123
impl Eq for Bus {}
108124
impl PartialEq for Bus {
109125
fn eq(&self, other: &Self) -> bool {

parser/src/ast/display.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ impl<'a> fmt::Display for DisplayStatement<'a> {
111111
write!(f, "enf {}", expr)
112112
}
113113
Statement::Expr(ref expr) => write!(f, "return {}", expr),
114+
Statement::BusEnforce(ref expr) => write!(f, "bus_enf {}", expr),
114115
}
115116
}
116117
}

parser/src/ast/expression.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ impl TryFrom<ScalarExpr> for Expr {
441441
Err(InvalidExprError::BoundedSymbolAccess(expr.span()))
442442
}
443443
ScalarExpr::Let(expr) => Ok(Self::Let(expr)),
444+
ScalarExpr::BusOperation(_) => todo!(),
444445
}
445446
}
446447
}
@@ -489,6 +490,8 @@ pub enum ScalarExpr {
489490
/// binary expressions or function calls to a block of statements, and only when the result
490491
/// of evaluating the `let` produces a valid scalar expression.
491492
Let(Box<Let>),
493+
/// A bus operation
494+
BusOperation(BusOperation),
492495
}
493496
impl ScalarExpr {
494497
/// Returns true if this is a constant value
@@ -523,6 +526,7 @@ impl ScalarExpr {
523526
},
524527
Self::Call(ref expr) => Ok(expr.ty),
525528
Self::Let(ref expr) => Ok(expr.ty()),
529+
Self::BusOperation(_) => todo!(),
526530
}
527531
}
528532
}
@@ -579,6 +583,7 @@ impl fmt::Debug for ScalarExpr {
579583
Self::Binary(ref expr) => f.debug_tuple("Binary").field(expr).finish(),
580584
Self::Call(ref expr) => f.debug_tuple("Call").field(expr).finish(),
581585
Self::Let(ref expr) => write!(f, "{:#?}", expr),
586+
Self::BusOperation(ref expr) => f.debug_tuple("BusOp").field(expr).finish(),
582587
}
583588
}
584589
}
@@ -598,6 +603,7 @@ impl fmt::Display for ScalarExpr {
598603
};
599604
write!(f, "{display}")
600605
}
606+
Self::BusOperation(ref expr) => write!(f, "{}", expr),
601607
}
602608
}
603609
}
@@ -1256,6 +1262,53 @@ impl fmt::Display for ListComprehension {
12561262
}
12571263
}
12581264

1265+
#[derive(Clone, Spanned)]
1266+
pub struct BusOperation {
1267+
#[span]
1268+
pub span: SourceSpan,
1269+
pub bus: ResolvableIdentifier,
1270+
pub op: BusOperator,
1271+
pub args: Vec<Expr>,
1272+
}
1273+
1274+
impl BusOperation {
1275+
pub fn new(span: SourceSpan, bus: Identifier, op: BusOperator, args: Vec<Expr>) -> Self {
1276+
Self {
1277+
span,
1278+
bus: ResolvableIdentifier::Unresolved(NamespacedIdentifier::Bus(bus)),
1279+
op,
1280+
args,
1281+
}
1282+
}
1283+
}
1284+
1285+
impl Eq for BusOperation {}
1286+
impl PartialEq for BusOperation {
1287+
fn eq(&self, other: &Self) -> bool {
1288+
self.bus == other.bus && self.args == other.args && self.op == other.op
1289+
}
1290+
}
1291+
impl fmt::Debug for BusOperation {
1292+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1293+
f.debug_struct("BusOperation")
1294+
.field("bus", &self.bus)
1295+
.field("op", &self.op)
1296+
.field("args", &self.args)
1297+
.finish()
1298+
}
1299+
}
1300+
impl fmt::Display for BusOperation {
1301+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1302+
write!(
1303+
f,
1304+
"{}{}{}",
1305+
self.bus,
1306+
self.op,
1307+
DisplayTuple(self.args.as_slice())
1308+
)
1309+
}
1310+
}
1311+
12591312
/// Represents a function call (either a pure function or an evaluator).
12601313
///
12611314
/// Calls are permitted in a scalar expression context, but arguments to the

parser/src/ast/statement.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub enum Statement {
6060
/// Just like `Enforce`, except the constraint is contained in the body of a list comprehension,
6161
/// and must be enforced on every value produced by that comprehension.
6262
EnforceAll(ListComprehension),
63+
/// Declares a bus related constraint
64+
BusEnforce(ListComprehension),
6365
}
6466
impl Statement {
6567
/// Checks this statement to see if it contains any constraints
@@ -69,7 +71,10 @@ impl Statement {
6971
/// one or more constraints in its body.
7072
pub fn has_constraints(&self) -> bool {
7173
match self {
72-
Self::Enforce(_) | Self::EnforceIf(_, _) | Self::EnforceAll(_) => true,
74+
Self::Enforce(_)
75+
| Self::EnforceIf(_, _)
76+
| Self::EnforceAll(_)
77+
| Self::BusEnforce(_) => true,
7378
Self::Let(Let { body, .. }) => body.iter().any(|s| s.has_constraints()),
7479
Self::Expr(_) => false,
7580
}
@@ -161,9 +166,10 @@ impl Let {
161166
last = let_expr.body.last();
162167
}
163168
Statement::Expr(ref expr) => return expr.ty(),
164-
Statement::Enforce(_) | Statement::EnforceIf(_, _) | Statement::EnforceAll(_) => {
165-
break
166-
}
169+
Statement::Enforce(_)
170+
| Statement::EnforceIf(_, _)
171+
| Statement::EnforceAll(_)
172+
| Statement::BusEnforce(_) => break,
167173
}
168174
}
169175

parser/src/ast/visit.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ where
586586
}
587587
ast::Statement::EnforceAll(ref mut expr) => visitor.visit_mut_enforce_all(expr),
588588
ast::Statement::Expr(ref mut expr) => visitor.visit_mut_expr(expr),
589+
ast::Statement::BusEnforce(ref mut _expr) => todo!(),
589590
}
590591
}
591592

@@ -647,6 +648,7 @@ where
647648
ast::ScalarExpr::Binary(ref mut expr) => visitor.visit_mut_binary_expr(expr),
648649
ast::ScalarExpr::Call(ref mut expr) => visitor.visit_mut_call(expr),
649650
ast::ScalarExpr::Let(ref mut expr) => visitor.visit_mut_let(expr),
651+
ast::ScalarExpr::BusOperation(ref mut _expr) => todo!(),
650652
}
651653
}
652654

parser/src/lexer/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ pub enum Token {
8484
DeclIdentRef(Symbol),
8585
/// A function identifier
8686
FunctionIdent(Symbol),
87+
/// A bus identifier
88+
BusIdent(Symbol),
8789
/// Integers should only contain numeric characters.
8890
Num(u64),
8991

@@ -122,8 +124,14 @@ pub enum Token {
122124
Buses,
123125
/// Used to represent a multiset bus declaration.
124126
Unit,
125-
/// Used to represent a logup bus declaration.
127+
/// Used to represent a logup bus declaration.
126128
Mult,
129+
/// Used to represent an empty bus
130+
Null,
131+
/// Used to represent the addition of a given tuple to a bus
132+
Add,
133+
/// Used to represent the removal of a given tuple to a bus
134+
Rem,
127135

128136
// BOUNDARY CONSTRAINT KEYWORDS
129137
// --------------------------------------------------------------------------------------------
@@ -199,6 +207,9 @@ impl Token {
199207
"buses" => Self::Buses,
200208
"unit" => Self::Unit,
201209
"mult" => Self::Mult,
210+
"null" => Self::Null,
211+
"add" => Self::Add,
212+
"rem" => Self::Rem,
202213
"boundary_constraints" => Self::BoundaryConstraints,
203214
"integrity_constraints" => Self::IntegrityConstraints,
204215
"first" => Self::First,
@@ -257,6 +268,7 @@ impl fmt::Display for Token {
257268
Self::Ident(ref id) => write!(f, "{}", id),
258269
Self::DeclIdentRef(ref id) => write!(f, "{}", id),
259270
Self::FunctionIdent(ref id) => write!(f, "{}", id),
271+
Self::BusIdent(ref id) => write!(f, "{}", id),
260272
Self::Num(ref i) => write!(f, "{}", i),
261273
Self::Def => write!(f, "def"),
262274
Self::Mod => write!(f, "mod"),
@@ -275,6 +287,9 @@ impl fmt::Display for Token {
275287
Self::Buses => write!(f, "buses"),
276288
Self::Unit => write!(f, "unit"),
277289
Self::Mult => write!(f, "mult"),
290+
Self::Null => write!(f, "null"),
291+
Self::Add => write!(f, "add"),
292+
Self::Rem => write!(f, "rem"),
278293
Self::BoundaryConstraints => write!(f, "boundary_constraints"),
279294
Self::First => write!(f, "first"),
280295
Self::Last => write!(f, "last"),

0 commit comments

Comments
 (0)