Skip to content

Commit

Permalink
fmt + clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-Besancon committed Aug 13, 2024
1 parent 566fd7a commit 20e2ea4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 63 deletions.
15 changes: 7 additions & 8 deletions parser/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl TryFrom<RangeExpr> for Range {
#[inline]
fn try_from(expr: RangeExpr) -> Result<Self, &'static str> {
match (expr.start, expr.end) {
(RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) => Ok(start as usize..end as usize),
(RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) => {
Ok(start as usize..end as usize)
}
_ => Err("invalid range expression"),
}
}
Expand Down Expand Up @@ -97,7 +99,7 @@ impl fmt::Debug for RangeExpr {

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

Expand Down Expand Up @@ -789,10 +791,7 @@ impl fmt::Display for AccessType {
"slice of elements at indices {}..{}",
range.start, range.end
),
Self::UnresolvedSlice(_) => write!(
f,
"slice of elements at currently unknown indices"
),
Self::UnresolvedSlice(_) => write!(f, "slice of elements at currently unknown indices"),
Self::Index(idx) => write!(f, "reference to element at index {idx}"),
Self::Matrix(row, col) => write!(f, "reference to value in matrix at [{row}][{col}]"),
}
Expand Down Expand Up @@ -920,7 +919,7 @@ impl SymbolAccess {
..self.clone()
}),
}
},
}
AccessType::UnresolvedSlice(_) => unreachable!(), // Should be resolved by now
AccessType::Matrix(row, col) => match ty {
Type::Felt | Type::Vector(_) => Err(InvalidAccessError::IndexIntoScalar),
Expand Down Expand Up @@ -984,7 +983,7 @@ impl SymbolAccess {
..self.clone()
}),
}
},
}
AccessType::UnresolvedSlice(_) => unreachable!(), // Should be resolved by now
AccessType::Matrix(row, col) => match ty {
Type::Felt | Type::Vector(_) => Err(InvalidAccessError::IndexIntoScalar),
Expand Down
2 changes: 1 addition & 1 deletion parser/src/ast/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ impl TraceBinding {
})
}
AccessType::Matrix(_, _) => Err(InvalidAccessError::IndexIntoScalar),
AccessType::UnresolvedSlice(_range) => todo!()
AccessType::UnresolvedSlice(_range) => todo!(),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions parser/src/parser/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ macro_rules! slice {
ScalarExpr::SymbolAccess(SymbolAccess {
span: miden_diagnostics::SourceSpan::UNKNOWN,
name: ResolvableIdentifier::Unresolved(NamespacedIdentifier::Binding(ident!($name))),
access_type: AccessType::UnresolvedSlice(Box::new(RangeExpr::from_range_unsized($range))),
access_type: AccessType::UnresolvedSlice(Box::new(RangeExpr::from_range_unsized(
$range,
))),
offset: 0,
ty: None,
})
Expand Down Expand Up @@ -522,7 +524,10 @@ macro_rules! lc {

macro_rules! range {
($range:expr) => {
Expr::RangeExpr(miden_diagnostics::Span::new(SourceSpan::UNKNOWN, $range.into()))
Expr::RangeExpr(miden_diagnostics::Span::new(
SourceSpan::UNKNOWN,
$range.into(),
))
};
}

Expand Down
97 changes: 50 additions & 47 deletions parser/src/sema/semantic_analysis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{borrow::BorrowMut, collections::{HashMap, HashSet}, fmt, mem, ops::ControlFlow};
use std::{
borrow::BorrowMut,
collections::{HashMap, HashSet},
fmt, mem,
ops::ControlFlow,
};

use miden_diagnostics::{DiagnosticsHandler, Severity, SourceSpan, Span, Spanned};

Expand Down Expand Up @@ -221,9 +226,8 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {

// First, constants
for constant in module.constants.values() {

let namespaced_name = NamespacedIdentifier::Binding(constant.name);

// See if a constant with the same name was previously imported
if let Some((prev, _)) = self.imported.get_key_value(&namespaced_name) {
self.declaration_import_conflict(constant.span(), prev.span())?;
Expand All @@ -236,7 +240,10 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {
);

// Store the values in order to resolve them early in case of use in ranges
self.constants.insert(ResolvableIdentifier::Unresolved(namespaced_name), constant.value.clone());
self.constants.insert(
ResolvableIdentifier::Unresolved(namespaced_name),
constant.value.clone(),
);
}

// Next, functions.
Expand Down Expand Up @@ -525,49 +532,46 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {
ControlFlow::Continue(())
}

fn visit_mut_expr(&mut self,
expr: &mut ast::Expr,
) -> ControlFlow<SemanticAnalysisError>
{
fn visit_mut_expr(&mut self, expr: &mut ast::Expr) -> ControlFlow<SemanticAnalysisError> {
match expr {
ast::Expr::Const(_) => ControlFlow::Continue(()),
ast::Expr::RangeExpr(range) => {
// Goal: Resolve the constants in the range expression
match range.start.clone() {
RangeBoundExpr::Const(_) => {},
RangeBoundExpr::Const(_) => {}
RangeBoundExpr::SymbolAccess(sym) => {
let ri = sym.name;
let value = self.constants.get(&ri).unwrap().clone();
match value {
ConstantExpr::Scalar(value) => {
range.start = RangeBoundExpr::Const(value);
},
_ => unreachable!()
}
_ => unreachable!(),
}
},
}
}
match range.end.clone() {
RangeBoundExpr::Const(_) => {},
RangeBoundExpr::Const(_) => {}
RangeBoundExpr::SymbolAccess(sym) => {
let ri = sym.name;
let value = self.constants.get(&ri).unwrap().clone();
match value {
ConstantExpr::Scalar(value) => {
range.end = RangeBoundExpr::Const(value);
},
_ => unreachable!()
}
_ => unreachable!(),
}
},
}
}

match (range.start.clone(), range.end.clone()) {
(RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) => {
range.ty = Some(Type::Vector((end - start) as usize));
},
_ => {},

if let (RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) =
(range.start.clone(), range.end.clone())
{
range.ty = Some(Type::Vector((end - start) as usize));
}

ControlFlow::Continue(())
},
}
ast::Expr::Vector(ref mut exprs) => {
for expr in exprs.iter_mut() {
self.visit_mut_expr(expr)?;
Expand All @@ -594,7 +598,6 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {
&mut self,
expr: &mut ListComprehension,
) -> ControlFlow<SemanticAnalysisError> {

// Visit the iterables first, and resolve their identifiers
for iterable in expr.iterables.iter_mut() {
self.visit_mut_expr(iterable)?;
Expand Down Expand Up @@ -886,42 +889,41 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {
let derived_from = resolved_binding_ty.span();
let resolved_binding_ty = resolved_binding_ty.item;

#[allow(clippy::single_match)]
match expr.access_type.borrow_mut() {
AccessType::UnresolvedSlice(range) => {

match range.start.clone() {
RangeBoundExpr::Const(_) => {},
RangeBoundExpr::Const(_) => {}
RangeBoundExpr::SymbolAccess(sym) => {
let ri = sym.name;
let value = self.constants.get(&ri).unwrap().clone();
match value {
ConstantExpr::Scalar(value) => {
range.start = RangeBoundExpr::Const(value);
},
_ => unreachable!()
}
_ => unreachable!(),
}
},
}
}
match range.end.clone() {
RangeBoundExpr::Const(_) => {},
RangeBoundExpr::Const(_) => {}
RangeBoundExpr::SymbolAccess(sym) => {
let ri = sym.name;
let value = self.constants.get(&ri).unwrap().clone();
match value {
ConstantExpr::Scalar(value) => {
range.end = RangeBoundExpr::Const(value);
},
_ => unreachable!()
}
_ => unreachable!(),
}
},
}
}

match (range.start.clone(), range.end.clone()) {
(RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) => {
range.ty = Some(Type::Vector((end - start) as usize));
expr.access_type = AccessType::Slice(start as usize..end as usize);
},
_ => {},

if let (RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) =
(range.start.clone(), range.end.clone())
{
range.ty = Some(Type::Vector((end - start) as usize));
expr.access_type = AccessType::Slice(start as usize..end as usize);
}

/*let mut sub_expr = Expr::RangeExpr(Span::new(derived_from, *range.clone()));
Expand All @@ -938,7 +940,7 @@ impl<'a> VisitMut<SemanticAnalysisError> for SemanticAnalysis<'a> {
},
_ => unreachable!(),
};*/
},
}
_ => {}
}

Expand Down Expand Up @@ -1661,13 +1663,14 @@ impl<'a> SemanticAnalysis<'a> {
match expr {
Expr::Const(constant) => Ok(BindingType::Local(constant.ty())),
Expr::RangeExpr(range) => {
match (range.start.clone(), range.end.clone()) {
(RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) => {
Ok(BindingType::Local(Type::Vector((end - start) as usize)))
}
_ => todo!(), // I think it should be unreachable if we resolved correctly the constant
if let (RangeBoundExpr::Const(start), RangeBoundExpr::Const(end)) =
(range.start.clone(), range.end.clone())
{
Ok(BindingType::Local(Type::Vector((end - start) as usize)))
} else {
todo!() // I think it should be unreachable if we resolved correctly the constant
}
},
}
Expr::Vector(ref elems) => {
let mut binding_tys = Vec::with_capacity(elems.len());
for elem in elems.iter() {
Expand Down
6 changes: 2 additions & 4 deletions parser/src/transforms/constant_propagation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ impl<'a> VisitMut<SemanticAnalysisError> for ConstantPropagation<'a> {
Err(err) => ControlFlow::Break(err),
},
// Ranges are not constant
Expr::RangeExpr(_) => {
ControlFlow::Continue(())
},
Expr::RangeExpr(_) => ControlFlow::Continue(()),
// Visit vector elements, and promote the vector to `Expr::Const` if possible
Expr::Vector(ref mut vector) => {
if vector.is_empty() {
Expand Down Expand Up @@ -505,7 +503,7 @@ impl<'a> VisitMut<SemanticAnalysisError> for ConstantPropagation<'a> {
Expr::RangeExpr(_range) => {
todo!()
//range.end - range.start
},
}
_ => unreachable!(),
};

Expand Down
2 changes: 1 addition & 1 deletion parser/src/transforms/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ fn eval_expr_binding_type(
Expr::RangeExpr(_range) => {
todo!()
//Ok(BindingType::Local(Type::Vector(range.end - range.start)))
},
}
Expr::Vector(ref elems) => match elems[0].ty() {
None | Some(Type::Felt) => {
let mut binding_tys = Vec::with_capacity(elems.len());
Expand Down

0 comments on commit 20e2ea4

Please sign in to comment.