iden3 / circom

zkSnark circuit compiler
GNU General Public License v3.0
1.28k stars 244 forks source link

Prefix increment/decrement operator #207

Closed 000wan closed 11 months ago

000wan commented 1 year ago

Hi, I've been working with circom 2.0.0, compiling circuits with circom circuit.circom --r1cs --wasm --sym -o . I was struggling with the following error for a long time, which indicates the last character of the circuit file.

error[P1012]: UnrecognizedToken { token: (1222, Token(78, "}"), 1223), expected: ["\"!\"", "\"(\"", "\"-\"", "\"[\"", "\"_\"", "\"parallel\"", "\"~\"", "r#\"0x[0-9A-Fa-f]*\"#", "r#\"[$_]*[a-zA-Z][a-zA-Z$_0-9]*\"#", "r#\"[0-9]+\"#"] }
   │
42 │ }
   │ ^ here

It turns out that the problem was the for loop using the prefix increment operator.

for (var i = 0; i < n; ++i) {
    // Error
}
for (var i = 0; i < n; i++) {
    // Everything fine
}

I had to change it to the postfix increment, and noticed that the prefix op is not implemented in circom: https://docs.circom.io/circom-language/basic-operators/#arithmetic-operators Moreover, for the single-line prefix op

error[P1012]: illegal expression
   │
35 │     ++i;
   │     ^^ here

the compiler shows an appropriate error message.

So we should either improve the error message for the op inside the loop declaration, or implement prefix op (If this is hard, I think allowing prefix op only for the for loop is totally ok).

clararod9 commented 11 months ago

Hi!

We have improved the error message for the prefix operator in our last commit, now we return the following error message when we detect it:

error[P1012]: illegal expression: circom language does not admit the ++i operator, use i++ instead
  ┌─ "aux.circom":6:35
  │
6 │     for(var i = 0; i < arg_count; ++i){
  │                                   ^^^ here

previous errors were found

Thank you for reporting this issue

000wan commented 11 months ago

Great to hear that! Thanks for your work, and hope this would help somebody from debugging lol