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

Test re-parsing of all test files after optimization #1476

Closed
wants to merge 12 commits into from
39 changes: 14 additions & 25 deletions ast/src/analyzed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,11 @@ impl<T: Display> Display for Analyzed<T> {
// These are printed as part of the enum.
continue;
}
let (name, is_local) = update_namespace(name, f)?;
let (name, _) = update_namespace(name, f)?;
match symbol.kind {
SymbolKind::Poly(_) => {
writeln_indented(f, format_poly(&name, symbol, definition))?;
}
SymbolKind::Constant() => {
assert!(symbol.stage.is_none());
let Some(FunctionValueDefinition::Expression(TypedExpression {
e,
type_scheme,
})) = &definition
else {
panic!(
"Invalid constant value: {}",
definition.as_ref().unwrap()
);
};
assert!(
type_scheme.is_none()
|| type_scheme == &Some((Type::Fe).into())
);
writeln_indented_by(
f,
format!("constant {name} = {e};"),
is_local.into(),
)?;
}
SymbolKind::Other() => {
assert!(symbol.stage.is_none());
match definition {
Expand Down Expand Up @@ -428,12 +406,23 @@ impl Display for AlgebraicReference {

impl Display for PolynomialReference {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self.name)?;
if let Some(type_args) = &self.type_args {
if !type_args.is_empty() {
write!(f, "::<{}>", type_args.iter().join(", "))?;
// We need to add a `::`-component, so the name should not contain a `.`.
// NOTE: This special handling can be removed once we remove
// the `to_dotted_string` function.
let name = if self.name.contains('.') {
// Re-format the name with ``::`-separators.
SymbolPath::from_str(&self.name).unwrap().to_string()
} else {
self.name.clone()
};
write!(f, "{name}::<{}>", type_args.iter().join(", "))?;
return Ok(());
}
}
write!(f, "{}", self.name)?;

Ok(())
}
}
Expand Down
2 changes: 0 additions & 2 deletions ast/src/analyzed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,6 @@ impl Symbol {
pub enum SymbolKind {
/// Fixed, witness or intermediate polynomial
Poly(PolynomialType),
/// A constant value.
Constant(),
/// Other symbol, depends on the type.
/// Examples include functions not of the type "int -> fe".
Other(),
Expand Down
24 changes: 13 additions & 11 deletions ast/src/parsed/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,19 @@ impl Display for PilStatement {
match self {
PilStatement::Include(_, path) => write!(f, "include {};", quote(path)),
PilStatement::Namespace(_, name, poly_length) => {
write!(
f,
"namespace {name}{};",
poly_length
.as_ref()
.map(|l| format!("({l})"))
.unwrap_or_default()
)
write!(f, "namespace")?;
let name = name.to_string();
match poly_length {
None if name.is_empty() => {
write!(f, ";")
}
None => {
write!(f, " {name};")
}
Some(poly_length) => {
write!(f, " {name}({poly_length});")
}
}
}
PilStatement::LetStatement(_, pattern, type_scheme, value) => write_indented_by(
f,
Expand Down Expand Up @@ -521,9 +526,6 @@ impl Display for PilStatement {
),
1,
),
PilStatement::ConstantDefinition(_, name, value) => {
write_indented_by(f, format!("constant {name} = {value};"), 1)
}
PilStatement::Expression(_, e) => write_indented_by(f, format!("{e};"), 1),
PilStatement::EnumDeclaration(_, enum_decl) => write_indented_by(f, enum_decl, 1),
}
Expand Down
8 changes: 2 additions & 6 deletions ast/src/parsed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ pub enum PilStatement {
SelectedExpressions<Expression>,
),
ConnectIdentity(SourceRef, Vec<Expression>, Vec<Expression>),
ConstantDefinition(SourceRef, String, Expression),
EnumDeclaration(SourceRef, EnumDeclaration<Expression>),
Expression(SourceRef, Expression),
}
Expand All @@ -126,7 +125,6 @@ impl PilStatement {
match self {
PilStatement::PolynomialDefinition(_, name, _)
| PilStatement::PolynomialConstantDefinition(_, name, _)
| PilStatement::ConstantDefinition(_, name, _)
| PilStatement::PublicDeclaration(_, name, _, _, _)
| PilStatement::LetStatement(_, name, _, _) => {
Box::new(once((name, None, SymbolCategory::Value)))
Expand Down Expand Up @@ -168,8 +166,7 @@ impl Children<Expression> for PilStatement {
}
PilStatement::Expression(_, e)
| PilStatement::Namespace(_, _, Some(e))
| PilStatement::PolynomialDefinition(_, _, e)
| PilStatement::ConstantDefinition(_, _, e) => Box::new(once(e)),
| PilStatement::PolynomialDefinition(_, _, e) => Box::new(once(e)),

PilStatement::EnumDeclaration(_, enum_decl) => enum_decl.children(),

Expand Down Expand Up @@ -203,8 +200,7 @@ impl Children<Expression> for PilStatement {
}
PilStatement::Expression(_, e)
| PilStatement::Namespace(_, _, Some(e))
| PilStatement::PolynomialDefinition(_, _, e)
| PilStatement::ConstantDefinition(_, _, e) => Box::new(once(e)),
| PilStatement::PolynomialDefinition(_, _, e) => Box::new(once(e)),

PilStatement::EnumDeclaration(_, enum_decl) => enum_decl.children_mut(),

Expand Down
3 changes: 1 addition & 2 deletions backend/src/estark/json_exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ fn symbol_kind_to_json_string(k: SymbolKind) -> &'static str {
match k {
SymbolKind::Poly(poly_type) => polynomial_type_to_json_string(poly_type),
SymbolKind::Other() => panic!("Cannot translate \"other\" symbol to json."),
SymbolKind::Constant() => unreachable!(),
}
}

Expand Down Expand Up @@ -225,7 +224,7 @@ impl<'a, T: FieldElement> Exporter<'a, T> {
panic!("Should be in intermediates")
}
SymbolKind::Poly(_) => Some(symbol.id),
SymbolKind::Other() | SymbolKind::Constant() => None,
SymbolKind::Other() => None,
}?;

let out = Reference {
Expand Down
52 changes: 26 additions & 26 deletions executor/src/constant_evaluator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ mod test {
#[test]
fn counter() {
let src = r#"
constant %N = 8;
namespace F(%N);
let N: int = 8;
namespace F(N);
pol constant EVEN(i) { 2 * (i - 1) + 4 };
"#;
let analyzed = analyze_string(src);
Expand All @@ -216,8 +216,8 @@ mod test {
#[test]
fn xor() {
let src = r#"
constant %N = 8;
namespace F(%N);
let N: int = 8;
namespace F(N);
pol constant X(i) { i ^ (i + 17) | 3 };
"#;
let analyzed = analyze_string(src);
Expand All @@ -235,8 +235,8 @@ mod test {
#[test]
fn match_expression() {
let src = r#"
constant %N = 8;
namespace F(%N);
let N: int = 8;
namespace F(N);
pol constant X(i) { match i {
0 => 7,
3 => 9,
Expand All @@ -256,8 +256,8 @@ mod test {
#[test]
fn if_expression() {
let src = r#"
constant %N = 8;
namespace F(%N);
let N: int = 8;
namespace F(N);
let X: col = |i| if i < 3 { 7 } else { 9 };
"#;
let analyzed = analyze_string(src);
Expand All @@ -272,8 +272,8 @@ mod test {
#[test]
fn macro_directive() {
let src = r#"
constant %N = 8;
namespace F(%N);
let N: int = 8;
namespace F(N);
let minus_one: int -> int = |x| x - 1;
pol constant EVEN(i) { 2 * minus_one(i) + 2 };
"#;
Expand Down Expand Up @@ -372,8 +372,8 @@ mod test {
#[test]
fn repetition_front() {
let src = r#"
constant %N = 10;
namespace F(%N);
let N: int = 10;
namespace F(N);
col fixed arr = [0, 1, 2]* + [7];
"#;
let analyzed = analyze_string(src);
Expand Down Expand Up @@ -465,8 +465,8 @@ mod test {
#[should_panic = "got `expr` when calling function F.w"]
fn calling_witness() {
let src = r#"
constant %N = 10;
namespace F(%N);
let N: int = 10;
namespace F(N);
let w;
let x: col = |i| w(i) + 1;
"#;
Expand All @@ -479,8 +479,8 @@ mod test {
#[should_panic = "Value symbol not found: w"]
fn symbol_not_found() {
let src = r#"
constant %N = 10;
namespace F(%N);
let N: int = 10;
namespace F(N);
let x = |i| w(i) + 1;
"#;
let analyzed = analyze_string::<GoldilocksField>(src);
Expand All @@ -492,8 +492,8 @@ mod test {
#[should_panic = "got `expr` when calling function F.y"]
fn forward_reference_to_array() {
let src = r#"
constant %N = 10;
namespace F(%N);
let N: int = 10;
namespace F(N);
let x: col = |i| y(i) + 1;
col fixed y = [1, 2, 3]*;
"#;
Expand All @@ -505,8 +505,8 @@ mod test {
#[test]
fn forward_reference_to_function() {
let src = r#"
constant %N = 4;
namespace F(%N);
let N: int = 4;
namespace F(N);
let x = |i| y(i) + 1;
let y = |i| i + 20;
let X: col = x;
Expand All @@ -528,11 +528,11 @@ mod test {
#[test]
fn bigint_arith() {
let src = r#"
constant %N = 4;
namespace std::convert(%N);
let N: int = 4;
namespace std::convert(N);
let int = [];
let fe = [];
namespace F(%N);
namespace F(N);
let x: col = |i| (1 << (2000 + i)) >> 2000;
"#;
let analyzed = analyze_string::<GoldilocksField>(src);
Expand All @@ -547,11 +547,11 @@ mod test {
#[test]
fn modulo_negative() {
let src = r#"
constant %N = 4;
namespace std::convert(%N);
let N: int = 4;
namespace std::convert(N);
let int = [];
let fe = [];
namespace F(%N);
namespace F(N);
let x_arr = [ 3 % 4, (-3) % 4, 3 % (-4), (-3) % (-4)];
let x: col = |i| 100 + x_arr[i];
"#;
Expand Down
4 changes: 2 additions & 2 deletions executor/src/witgen/block_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ mod tests {
#[test]
fn fibonacci() {
let src = r#"
constant %N = 8;
let N: int = 8;

namespace Fibonacci(%N);
namespace Fibonacci(N);
col fixed ISFIRST = [1] + [0]*;
col fixed ISLAST = [0]* + [1];
col witness x, y;
Expand Down
30 changes: 26 additions & 4 deletions parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ mod test {
#[test]
fn reparse() {
let input = r#"
constant %N = 16;
namespace Fibonacci(%N);
constant %last_row = %N - 1;
let N: int = 16;
namespace Fibonacci(N);
let last_row = N - 1;
let bool: expr -> expr = (|X| X * (1 - X));
let one_hot = (|i, which| match i {
which => 1,
Expand Down Expand Up @@ -444,7 +444,7 @@ namespace Fibonacci(%N);

#[test]
fn reparse_strings_and_tuples() {
let input = r#"constant %N = ("abc", 3);"#;
let input = r#"let N = ("abc", 3);"#;
let printed = format!("{}", parse(Some("input"), input).unwrap());
assert_eq!(input.trim(), printed.trim());
}
Expand Down Expand Up @@ -540,6 +540,28 @@ namespace N(2);
namespace N(2);
let<T: Ord> max: T, T -> T = (|a, b| if a < b { b } else { a });
let seven = max::<int>(3, 7);
"#;
let printed = format!("{}", parse(Some("input"), input).unwrap_err_to_stderr());
assert_eq!(expected.trim(), printed.trim());
}

#[test]
fn empty_namespace() {
let input = r#"
namespace(2);
let x = 2;
namespace;
let y = 4;
namespace N(8);
let z = 8;
"#;
let expected = r#"
namespace (2);
let x = 2;
namespace;
let y = 4;
namespace N(8);
let z = 8;
"#;
let printed = format!("{}", parse(Some("input"), input).unwrap_err_to_stderr());
assert_eq!(expected.trim(), printed.trim());
Expand Down
8 changes: 2 additions & 6 deletions parser/src/powdr.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub PilStatement = {
Include,
Namespace,
LetStatement,
ConstantDefinition,
PolynomialDefinition,
PublicDeclaration,
PolynomialConstantDeclaration,
Expand All @@ -126,18 +125,15 @@ Include: PilStatement = {
};

Namespace: PilStatement = {
<start:@L> "namespace" <name:SymbolPath> <pol_degree:("(" <Expression> ")")?> <end:@R> ";" => PilStatement::Namespace(ctx.source_ref(start, end), name, pol_degree)
<start:@L> "namespace" <name:(SymbolPath)?> <pol_degree:("(" <Expression> ")")?> <end:@R> ";"
=> PilStatement::Namespace(ctx.source_ref(start, end), name.unwrap_or_default(), pol_degree)
}

LetStatement: PilStatement = {
<start:@L> "let" <name:GenericTypedName> <expr:( "=" <Expression> )?> <end:@R> ";" =>
PilStatement::LetStatement(ctx.source_ref(start, end), name.0, name.1, expr)
}

ConstantDefinition: PilStatement = {
<start:@L> "constant" <id:ConstantIdentifier> "=" <expr:Expression> <end:@R> ";" => PilStatement::ConstantDefinition(ctx.source_ref(start, end), id, expr)
}

PolynomialDefinition: PilStatement = {
<start:@L> PolCol <id:Identifier> "=" <expr:Expression> <end:@R> ";" => PilStatement::PolynomialDefinition(ctx.source_ref(start, end), id, expr)
}
Expand Down
Loading
Loading