Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor IntegrityStmt #216

Merged
merged 5 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ir/src/constraint_builder/integrity_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use super::{
ListFoldingValueType, NamedTraceAccess, SemanticError, Symbol, SymbolType, Variable,
VariableType, VectorAccess, CURRENT_ROW,
};
// TODO: Move this to air-script-core
use parser::ast::ConstraintType;
grjte marked this conversation as resolved.
Show resolved Hide resolved

mod list_comprehension;

mod list_folding;

impl ConstraintBuilder {
Expand All @@ -23,7 +24,7 @@ impl ConstraintBuilder {
stmt: IntegrityStmt,
) -> Result<(), SemanticError> {
match stmt {
IntegrityStmt::Constraint(constraint) => {
IntegrityStmt::Constraint(ConstraintType::Inline(constraint), _) => {
// add the left hand side expression to the graph.
let lhs = self.insert_expr(constraint.lhs())?;

Expand Down Expand Up @@ -53,7 +54,7 @@ impl ConstraintBuilder {
self.symbol_table.insert_variable(variable)?
}
}
IntegrityStmt::EvaluatorFunctionCall(_) => todo!(),
IntegrityStmt::Constraint(ConstraintType::Evaluator(_), _) => todo!(),
}

Ok(())
Expand Down
9 changes: 7 additions & 2 deletions parser/src/ast/integrity_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ use super::{EvaluatorFunctionCall, Expression, Variable};

#[derive(Debug, Eq, PartialEq)]
pub enum IntegrityStmt {
Constraint(IntegrityConstraint),
EvaluatorFunctionCall(EvaluatorFunctionCall),
Constraint(ConstraintType, Option<Expression>),
Variable(Variable),
}

#[derive(Debug, Eq, PartialEq)]
pub enum ConstraintType {
Inline(IntegrityConstraint),
Evaluator(EvaluatorFunctionCall),
}

/// Stores the expression corresponding to the integrity constraint.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct IntegrityConstraint {
Expand Down
8 changes: 4 additions & 4 deletions parser/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
ast::{
boundary_constraints::{Boundary, BoundaryConstraint, BoundaryStmt},
integrity_constraints::{IntegrityConstraint, IntegrityStmt},
integrity_constraints::{ConstraintType, IntegrityConstraint, IntegrityStmt},
Constant, ConstantType, Expression, EvaluatorFunction, EvaluatorFunctionCall, Identifier,
IndexedTraceAccess, Iterable, ListComprehension, ListFoldingType, ListFoldingValueType,
MatrixAccess, NamedTraceAccess, PeriodicColumn, PublicInput, RandBinding, RandomValues,
Expand Down Expand Up @@ -270,7 +270,7 @@ IntegrityStmts: Vec<IntegrityStmt> = {
<integrity_stmts: IntegrityStmt+> =>? {
// check if at least one integrity constraint is defined
let integrity_constraints_exist = integrity_stmts.iter().any(|stmt| match stmt {
IntegrityStmt::Constraint(_) | IntegrityStmt::EvaluatorFunctionCall(_) => true,
IntegrityStmt::Constraint(_, _) => true,
_ => false,
});
if !integrity_constraints_exist {
Expand All @@ -290,9 +290,9 @@ IntegrityStmt: IntegrityStmt = {
"let" <name: Identifier> "=" <integrity_variable_type: IntegrityVariableType> =>
IntegrityStmt::Variable(Variable::new(name, integrity_variable_type)),
"enf" <lhs: IntegrityExpr> "=" <rhs: IntegrityExpr> =>
IntegrityStmt::Constraint(IntegrityConstraint::new(lhs, rhs)),
IntegrityStmt::Constraint(ConstraintType::Inline(IntegrityConstraint::new(lhs, rhs)), None),
"enf" <evaluator_fn_call: EvaluatorFunctionCall> =>
IntegrityStmt::EvaluatorFunctionCall(evaluator_fn_call),
IntegrityStmt::Constraint(ConstraintType::Evaluator(evaluator_fn_call), None),
}

IntegrityVariableType: VariableType = {
Expand Down
68 changes: 42 additions & 26 deletions parser/src/parser/tests/arithmetic_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
build_parse_test, Expression::*, Identifier, IntegrityConstraint, IntegrityStmt::*, Source,
SourceSection::*,
};
use crate::ast::NamedTraceAccess;
use crate::ast::{ConstraintType, NamedTraceAccess};

// EXPRESSIONS
// ================================================================================================
Expand All @@ -14,7 +14,7 @@ fn single_addition() {
integrity_constraints:
enf clk' + clk = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Add(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -24,7 +24,8 @@ fn single_addition() {
Box::new(Elem(Identifier("clk".to_string()))),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -36,7 +37,7 @@ fn multi_addition() {
integrity_constraints:
enf clk' + clk + 2 = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Add(
Box::new(Add(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Expand All @@ -49,7 +50,8 @@ fn multi_addition() {
Box::new(Const(2)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -61,7 +63,7 @@ fn single_subtraction() {
integrity_constraints:
enf clk' - clk = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Sub(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -71,7 +73,8 @@ fn single_subtraction() {
Box::new(Elem(Identifier("clk".to_string()))),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -83,7 +86,7 @@ fn multi_subtraction() {
integrity_constraints:
enf clk' - clk - 1 = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Sub(
Box::new(Sub(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Expand All @@ -96,7 +99,8 @@ fn multi_subtraction() {
Box::new(Const(1)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -108,7 +112,7 @@ fn single_multiplication() {
integrity_constraints:
enf clk' * clk = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Mul(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -118,7 +122,8 @@ fn single_multiplication() {
Box::new(Elem(Identifier("clk".to_string()))),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -130,7 +135,7 @@ fn multi_multiplication() {
integrity_constraints:
enf clk' * clk * 2 = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Mul(
Box::new(Mul(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Expand All @@ -143,7 +148,8 @@ fn multi_multiplication() {
Box::new(Const(2)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -155,7 +161,11 @@ fn unit_with_parens() {
integrity_constraints:
enf (2) + 1 = 3";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(Add(Box::new(Const(2)), Box::new(Const(1))), Const(3)),
ConstraintType::Inline(IntegrityConstraint::new(
Add(Box::new(Const(2)), Box::new(Const(1))),
Const(3),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -167,7 +177,7 @@ fn ops_with_parens() {
integrity_constraints:
enf (clk' + clk) * 2 = 4";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Mul(
Box::new(Add(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Expand All @@ -180,7 +190,8 @@ fn ops_with_parens() {
Box::new(Const(2)),
),
Const(4),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -192,7 +203,7 @@ fn const_exponentiation() {
integrity_constraints:
enf clk'^2 = 1";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Exp(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -202,7 +213,8 @@ fn const_exponentiation() {
Box::new(Const(2)),
),
Const(1),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -214,7 +226,7 @@ fn non_const_exponentiation() {
integrity_constraints:
enf clk'^(clk + 2) = 1";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Exp(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -227,7 +239,8 @@ fn non_const_exponentiation() {
)),
),
Const(1),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand Down Expand Up @@ -257,7 +270,7 @@ fn multi_arithmetic_ops_same_precedence() {
integrity_constraints:
enf clk' - clk - 2 + 1 = 0";
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Add(
Box::new(Sub(
Box::new(Sub(
Expand All @@ -273,7 +286,8 @@ fn multi_arithmetic_ops_same_precedence() {
Box::new(Const(1)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -290,7 +304,7 @@ fn multi_arithmetic_ops_different_precedence() {
// 3. Addition/Subtraction
// These operations are evaluated in the order of decreasing precedence.
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Sub(
Box::new(Sub(
Box::new(Exp(
Expand All @@ -309,7 +323,8 @@ fn multi_arithmetic_ops_different_precedence() {
Box::new(Const(1)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Expand All @@ -327,7 +342,7 @@ fn multi_arithmetic_ops_different_precedence_w_parens() {
// 4. Addition/Subtraction
// These operations are evaluated in the order of decreasing precedence.
let expected = Source(vec![IntegrityConstraints(vec![Constraint(
IntegrityConstraint::new(
ConstraintType::Inline(IntegrityConstraint::new(
Sub(
Box::new(NamedTraceAccess(NamedTraceAccess::new(
Identifier("clk".to_string()),
Expand All @@ -343,7 +358,8 @@ fn multi_arithmetic_ops_different_precedence_w_parens() {
)),
),
Const(0),
),
)),
None,
)])]);
build_parse_test!(source).expect_ast(expected);
}
Loading