Skip to content

Commit

Permalink
Make index access a proper operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Oct 31, 2023
1 parent d36b461 commit ea7951d
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 270 deletions.
5 changes: 2 additions & 3 deletions analysis/src/macro_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ where

fn process_expression(&mut self, e: &mut Expression<T>) {
if let Expression::Reference(poly) = e {
if poly.namespace().is_none() && self.parameter_names.contains_key(poly.name()) {
assert!(poly.index().is_none());
*e = self.arguments[self.parameter_names[poly.name()]].clone()
if poly.namespace.is_none() && self.parameter_names.contains_key(&poly.name) {
*e = self.arguments[self.parameter_names[&poly.name]].clone()
}
} else if let Expression::FunctionCall(call) = e {
if self.macros.contains_key(call.id.as_str()) {
Expand Down
4 changes: 2 additions & 2 deletions asm_to_pil/src/romgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn substitute_name_in_statement_expressions<T>(
) {
fn substitute<T>(e: &mut Expression<T>, substitution: &HashMap<String, String>) {
if let Expression::Reference(r) = e {
if let Some(v) = substitution.get(r.name()).cloned() {
*r.name_mut() = v;
if let Some(v) = substitution.get(&r.name).cloned() {
r.name = v;
}
};
}
Expand Down
20 changes: 7 additions & 13 deletions asm_to_pil/src/vm_to_constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ impl<T: FieldElement> ASMPILConverter<T> {
body.iter_mut().for_each(|s| {
s.post_visit_expressions_mut(&mut |e| {
if let Expression::Reference(r) = e {
if let Some(sub) = substitutions.get(r.name()) {
*r.name_mut() = sub.to_string();
if let Some(sub) = substitutions.get(&r.name) {
r.name = sub.to_string();
}
}
});
Expand Down Expand Up @@ -497,7 +497,7 @@ impl<T: FieldElement> ASMPILConverter<T> {
Input::Literal(_, LiteralKind::Label) => {
if let Expression::Reference(r) = a {
instruction_literal_arg
.push(InstructionLiteralArg::LabelRef(r.name().into()));
.push(InstructionLiteralArg::LabelRef(r.name));
} else {
panic!();
}
Expand Down Expand Up @@ -538,8 +538,7 @@ impl<T: FieldElement> ASMPILConverter<T> {
.map(|(reg, a)| {
// Output a value trough assignment register "reg"
if let Expression::Reference(r) = a {
assert!(r.index().is_none());
(reg.clone(), vec![r.name().into()])
(reg.clone(), vec![r.name])
} else {
panic!("Expected direct register to assign to in instruction call.");
}
Expand All @@ -562,14 +561,13 @@ impl<T: FieldElement> ASMPILConverter<T> {
) -> Vec<(T, AffineExpressionComponent<T>)> {
match value {
Expression::PublicReference(_) => panic!(),
Expression::IndexAccess(_) => panic!(),
Expression::FunctionCall(_) => panic!(),
Expression::Reference(reference) => {
assert!(reference.namespace().is_none());
assert!(reference.index().is_none());
// TODO check it actually is a register
vec![(
1.into(),
AffineExpressionComponent::Register(reference.name().into()),
AffineExpressionComponent::Register(reference.name),
)]
}
Expression::Number(value) => vec![(value, AffineExpressionComponent::Constant)],
Expand Down Expand Up @@ -1028,11 +1026,7 @@ fn extract_update<T: FieldElement>(expr: Expression<T>) -> (Option<String>, Expr
if let Expression::BinaryOperation(left, BinaryOperator::Sub, right) = expr {
match *left {
Expression::UnaryOperation(UnaryOperator::Next, column) => match *column {
Expression::Reference(column) => {
assert!(column.namespace().is_none());
assert!(column.index().is_none());
return (Some(column.name().into()), *right);
}
Expression::Reference(column) => (Some(column.name), *right),
_ => (
None,
Expression::UnaryOperation(UnaryOperator::Next, column) - *right,
Expand Down
10 changes: 1 addition & 9 deletions ast/src/analyzed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,6 @@ impl Display for AlgebraicReference {

impl Display for PolynomialReference {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{}{}",
self.name,
self.index
.as_ref()
.map(|s| format!("[{s}]"))
.unwrap_or_default(),
)
write!(f, "{}", self.name,)
}
}
5 changes: 2 additions & 3 deletions ast/src/analyzed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub enum SymbolKind {
Other(),
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum FunctionValueDefinition<T> {
Mapping(Expression<T>),
Array(Vec<RepeatedArray<T>>),
Expand All @@ -324,7 +324,7 @@ pub enum FunctionValueDefinition<T> {
}

/// An array of elements that might be repeated.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RepeatedArray<T> {
/// The pattern to be repeated
pattern: Vec<Expression<T>>,
Expand Down Expand Up @@ -563,7 +563,6 @@ pub struct PolynomialReference {
/// Optional because it is filled in in a second stage of analysis.
/// TODO make this non-optional
pub poly_id: Option<PolyID>,
pub index: Option<u64>,
}

#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
Expand Down
8 changes: 3 additions & 5 deletions ast/src/asm_analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,10 @@ pub enum FunctionStatement<T> {
Return(Return<T>),
}

impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference<T>>>
for FunctionStatement<T>
{
impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference>> for FunctionStatement<T> {
fn visit_expressions_mut<F, B>(&mut self, f: &mut F, o: VisitOrder) -> std::ops::ControlFlow<B>
where
F: FnMut(&mut Expression<T, NamespacedPolynomialReference<T>>) -> std::ops::ControlFlow<B>,
F: FnMut(&mut Expression<T, NamespacedPolynomialReference>) -> std::ops::ControlFlow<B>,
{
match self {
FunctionStatement::Assignment(assignment) => {
Expand All @@ -567,7 +565,7 @@ impl<T> ExpressionVisitable<Expression<T, NamespacedPolynomialReference<T>>>

fn visit_expressions<F, B>(&self, f: &mut F, o: VisitOrder) -> std::ops::ControlFlow<B>
where
F: FnMut(&Expression<T, NamespacedPolynomialReference<T>>) -> std::ops::ControlFlow<B>,
F: FnMut(&Expression<T, NamespacedPolynomialReference>) -> std::ops::ControlFlow<B>,
{
match self {
FunctionStatement::Assignment(assignment) => {
Expand Down
17 changes: 11 additions & 6 deletions ast/src/parsed/build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use crate::parsed::Expression;

use super::{PolynomialReference, UnaryOperator};
use super::{NamespacedPolynomialReference, UnaryOperator};

pub fn direct_reference<S: Into<String>, T>(name: S) -> Expression<T> {
PolynomialReference::new(name).single().local().into()
NamespacedPolynomialReference {
namespace: None,
name: name.into(),
}
.into()
}

pub fn namespaced_reference<S: Into<String>, T>(namespace: String, name: S) -> Expression<T> {
PolynomialReference::new(name)
.single()
.namespaced(namespace)
.into()
NamespacedPolynomialReference {
namespace: Some(namespace),
name: name.into(),
}
.into()
}

pub fn next_reference<T>(name: &str) -> Expression<T> {
Expand Down
31 changes: 9 additions & 22 deletions ast/src/parsed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ impl Display for ParamList {
}
}

impl<T: Display, Ref: Display> Display for IndexAccess<T, Ref> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}[{}]", self.array, self.index)
}
}

impl<T: Display, Ref: Display> Display for FunctionCall<T, Ref> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}({})", self.id, format_expressions(&self.arguments))
Expand Down Expand Up @@ -441,6 +447,7 @@ impl<T: Display, Ref: Display> Display for Expression<T, Ref> {
write!(f, "{exp}{op}")
}
}
Expression::IndexAccess(index_access) => write!(f, "{index_access}"),
Expression::FunctionCall(fun_call) => write!(f, "{fun_call}"),
Expression::FreeInput(input) => write!(f, "${{ {input} }}"),
Expression::MatchExpression(scrutinee, arms) => {
Expand All @@ -464,7 +471,7 @@ impl<T: Display> Display for PolynomialName<T> {
}
}

impl<T: Display> Display for NamespacedPolynomialReference<T> {
impl Display for NamespacedPolynomialReference {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
Expand All @@ -473,31 +480,11 @@ impl<T: Display> Display for NamespacedPolynomialReference<T> {
.as_ref()
.map(|n| format!("{n}."))
.unwrap_or_default(),
self.pol
self.name
)
}
}

impl<T: Display> Display for IndexedPolynomialReference<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{}{}",
self.pol,
self.index
.as_ref()
.map(|s| format!("[{s}]"))
.unwrap_or_default(),
)
}
}

impl Display for PolynomialReference {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.name)
}
}

impl<T: Display, Ref: Display> Display for LambdaExpression<T, Ref> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "|{}| {}", self.params.iter().format(", "), self.body)
Expand Down
Loading

0 comments on commit ea7951d

Please sign in to comment.