From 82ce286f208cefac69b3559259c6545cef54afab Mon Sep 17 00:00:00 2001 From: schaeff Date: Wed, 29 Mar 2023 10:43:09 +0200 Subject: [PATCH] remove parentheses in parser, rename to ArrayLiteralExpression --- src/asm_compiler/mod.rs | 28 ++++++++++++------------ src/constant_evaluator/mod.rs | 6 ++--- src/parser/display.rs | 2 +- src/parser/powdr.lalrpop | 18 +++++++++------ src/riscv/compiler.rs | 2 +- tests/asm_data/mem_read_write.asm | 2 +- tests/asm_data/palindrome.asm | 2 +- tests/pil_data/global.pil | 2 +- tests/pil_data/sum_via_witness_query.pil | 2 +- 9 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/asm_compiler/mod.rs b/src/asm_compiler/mod.rs index f2801d693..bc2c92183 100644 --- a/src/asm_compiler/mod.rs +++ b/src/asm_compiler/mod.rs @@ -826,7 +826,7 @@ mod test { pub fn compile_simple_sum() { let expectation = r#" namespace Assembly(1024); -pol constant first_step = ([1] + [0]*); +pol constant first_step = [1] + [0]*; (first_step * pc) = 0; pol commit pc; pol commit X; @@ -859,19 +859,19 @@ CNT' = ((((first_step' * 0) + (reg_write_X_CNT * X)) + (instr_dec_CNT * (CNT - 1 pc' = ((((first_step' * 0) + (instr_jmpz * ((XIsZero * instr_jmpz_param_l) + ((1 - XIsZero) * (pc + 1))))) + (instr_jmp * instr_jmp_param_l)) + ((1 - ((first_step' + instr_jmpz) + instr_jmp)) * (pc + 1))); pol constant line(i) { i }; pol commit X_free_value(i) query (i, pc, (0, ("input", 1)), (3, ("input", (CNT + 1))), (7, ("input", 0))); -pol constant p_X_const = ([0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*); -pol constant p_X_read_free = ([1, 0, 0, 1, 0, 0, 0, -1, 0] + [0]*); -pol constant p_instr_assert_zero = ([0, 0, 0, 0, 0, 0, 0, 0, 1] + [0]*); -pol constant p_instr_dec_CNT = ([0, 0, 0, 0, 1, 0, 0, 0, 0] + [0]*); -pol constant p_instr_jmp = ([0, 0, 0, 0, 0, 1, 0, 0, 0] + [0]*); -pol constant p_instr_jmp_param_l = ([0, 0, 0, 0, 0, 1, 0, 0, 0] + [0]*); -pol constant p_instr_jmpz = ([0, 0, 1, 0, 0, 0, 0, 0, 0] + [0]*); -pol constant p_instr_jmpz_param_l = ([0, 0, 6, 0, 0, 0, 0, 0, 0] + [0]*); -pol constant p_read_X_A = ([0, 0, 0, 1, 0, 0, 0, 1, 1] + [0]*); -pol constant p_read_X_CNT = ([0, 0, 1, 0, 0, 0, 0, 0, 0] + [0]*); -pol constant p_read_X_pc = ([0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*); -pol constant p_reg_write_X_A = ([0, 0, 0, 1, 0, 0, 0, 1, 0] + [0]*); -pol constant p_reg_write_X_CNT = ([1, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*); +pol constant p_X_const = [0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*; +pol constant p_X_read_free = [1, 0, 0, 1, 0, 0, 0, -1, 0] + [0]*; +pol constant p_instr_assert_zero = [0, 0, 0, 0, 0, 0, 0, 0, 1] + [0]*; +pol constant p_instr_dec_CNT = [0, 0, 0, 0, 1, 0, 0, 0, 0] + [0]*; +pol constant p_instr_jmp = [0, 0, 0, 0, 0, 1, 0, 0, 0] + [0]*; +pol constant p_instr_jmp_param_l = [0, 0, 0, 0, 0, 1, 0, 0, 0] + [0]*; +pol constant p_instr_jmpz = [0, 0, 1, 0, 0, 0, 0, 0, 0] + [0]*; +pol constant p_instr_jmpz_param_l = [0, 0, 6, 0, 0, 0, 0, 0, 0] + [0]*; +pol constant p_read_X_A = [0, 0, 0, 1, 0, 0, 0, 1, 1] + [0]*; +pol constant p_read_X_CNT = [0, 0, 1, 0, 0, 0, 0, 0, 0] + [0]*; +pol constant p_read_X_pc = [0, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*; +pol constant p_reg_write_X_A = [0, 0, 0, 1, 0, 0, 0, 1, 0] + [0]*; +pol constant p_reg_write_X_CNT = [1, 0, 0, 0, 0, 0, 0, 0, 0] + [0]*; { pc, reg_write_X_A, reg_write_X_CNT, instr_jmpz, instr_jmpz_param_l, instr_jmp, instr_jmp_param_l, instr_dec_CNT, instr_assert_zero, X_const, X_read_free, read_X_A, read_X_CNT, read_X_pc } in { line, p_reg_write_X_A, p_reg_write_X_CNT, p_instr_jmpz, p_instr_jmpz_param_l, p_instr_jmp, p_instr_jmp_param_l, p_instr_dec_CNT, p_instr_assert_zero, p_X_const, p_X_read_free, p_read_X_A, p_read_X_CNT, p_read_X_pc }; "#; diff --git a/src/constant_evaluator/mod.rs b/src/constant_evaluator/mod.rs index 413933099..2512a51f7 100644 --- a/src/constant_evaluator/mod.rs +++ b/src/constant_evaluator/mod.rs @@ -264,9 +264,9 @@ mod test { let src = r#" constant %N = 10; namespace F(%N); - col fixed alt = ([0, 1, 0, 1, 0, 1] + [0]*); - col fixed empty = ([] + [0]*); - col fixed ref_other = ([%N-1, alt(1), 8] + [0]*); + col fixed alt = [0, 1, 0, 1, 0, 1] + [0]*; + col fixed empty = [] + [0]*; + col fixed ref_other = [%N-1, alt(1), 8] + [0]*; "#; let analyzed = analyze_string(src); let (constants, degree) = generate(&analyzed); diff --git a/src/parser/display.rs b/src/parser/display.rs index 343d744d8..586780a03 100644 --- a/src/parser/display.rs +++ b/src/parser/display.rs @@ -97,7 +97,7 @@ impl Display for ArrayExpression { ArrayExpression::RepeatedValue(expressions) => { write!(f, "[{}]*", format_expressions(expressions)) } - ArrayExpression::Concat(left, right) => write!(f, "({} + {})", left, right), + ArrayExpression::Concat(left, right) => write!(f, "{} + {}", left, right), } } } diff --git a/src/parser/powdr.lalrpop b/src/parser/powdr.lalrpop index 01b692540..c327d5dfa 100644 --- a/src/parser/powdr.lalrpop +++ b/src/parser/powdr.lalrpop @@ -71,13 +71,23 @@ PolynomialConstantDefinition: Statement = { FunctionDefinition: FunctionDefinition = { "(" ")" "{" "}" => FunctionDefinition::Mapping(<>), - "=" => FunctionDefinition::Array(<>), + "=" => FunctionDefinition::Array(<>), } ParameterList: Vec = { "," )*> => { list.push(end); list } } +ArrayLiteralExpression: ArrayExpression = { + "+" => ArrayExpression::concat(<>), + ArrayLiteralTerm, +} + +ArrayLiteralTerm: ArrayExpression = { + "[" "]" => ArrayExpression::value(<>), + "[" "]" "*" => ArrayExpression::repeated_value(<>), +} + PolynomialCommitDeclaration: Statement = { <@L> PolCol CommitWitness => Statement::PolynomialCommitDeclaration(<>, None), PolCol CommitWitness "(" ")" "query" @@ -329,12 +339,6 @@ PublicReference: String = { ":" } -ArrayExpression: ArrayExpression = { - "[" "]" => ArrayExpression::value(<>), - "[" "]" "*" => ArrayExpression::repeated_value(<>), - "(" "+" ")" => ArrayExpression::concat(<>), -} - // ---------------------------- Terminals ----------------------------- diff --git a/src/riscv/compiler.rs b/src/riscv/compiler.rs index 3008bbc5e..2d60d681f 100644 --- a/src/riscv/compiler.rs +++ b/src/riscv/compiler.rs @@ -78,7 +78,7 @@ pil{ // positive numbers (assumed to be much smaller than the field order) col fixed POSITIVE(i) { i + 1 }; - col fixed FIRST = ([1] + [0]*); + col fixed FIRST = [1] + [0]*; col fixed LAST(i) { FIRST(i + 1) }; col fixed STEP(i) { i }; diff --git a/tests/asm_data/mem_read_write.asm b/tests/asm_data/mem_read_write.asm index bdcdf10c8..95d6893bd 100644 --- a/tests/asm_data/mem_read_write.asm +++ b/tests/asm_data/mem_read_write.asm @@ -28,7 +28,7 @@ pil{ // positive numbers (assumed to be much smaller than the field order) col fixed POSITIVE(i) { i + 1 }; - col fixed FIRST = ([1] + [0]*); + col fixed FIRST = [1] + [0]*; col fixed LAST(i) { FIRST(i + 1) }; col fixed STEP(i) { i }; diff --git a/tests/asm_data/palindrome.asm b/tests/asm_data/palindrome.asm index 7d37290a6..aefd60e81 100644 --- a/tests/asm_data/palindrome.asm +++ b/tests/asm_data/palindrome.asm @@ -24,7 +24,7 @@ pil{ // positive numbers (assumed to be less than half the field order) col fixed POSITIVE(i) { i + 1 }; - col fixed FIRST = ([1] + [0]*); + col fixed FIRST = [1] + [0]*; col fixed NOTLAST(i) { 1 - FIRST(i + 1) }; // This enforces that addresses are stored in an ascending order diff --git a/tests/pil_data/global.pil b/tests/pil_data/global.pil index 43ced05a9..4b7fad74b 100644 --- a/tests/pil_data/global.pil +++ b/tests/pil_data/global.pil @@ -16,7 +16,7 @@ namespace Global(%N); macro ite(C, A, B) { is_nonzero(C) * A + is_zero(C) * B}; macro one_hot(i, index) { ite(is_equal(i, index), 1, 0) }; - col fixed L1 = ([1] + [0]*); // will be filled with zeros + col fixed L1 = [1] + [0]*; col fixed LLAST(i) { one_hot(i, %N - 1) }; col fixed BYTE(i) { i & 0xff }; col fixed BYTE2(i) { i & 0xffff }; diff --git a/tests/pil_data/sum_via_witness_query.pil b/tests/pil_data/sum_via_witness_query.pil index 876229ee4..c7c5694ca 100644 --- a/tests/pil_data/sum_via_witness_query.pil +++ b/tests/pil_data/sum_via_witness_query.pil @@ -12,7 +12,7 @@ namespace Sum(%N); pol fixed ISLAST(i) { one_hot(i, %last_row) }; pol fixed ISALMOSTLAST(i) { one_hot(i, %last_row - 1) }; - pol fixed ISFIRST = ([ 1, 0 ] + [0]*); + pol fixed ISFIRST = [ 1, 0 ] + [0]*; col witness input(i) query ("in", i); col witness sum;