Conversation
Y-Nak
requested changes
May 8, 2026
| (_, initStmt') <- evalLoopStmt loopEnv initStmt | ||
| cond' <- evalExp loopEnv cond | ||
| (_, post') <- evalLoopStmt loopEnv post | ||
| bodies' <- mapM (fmap snd . evalLoopStmt loopEnv) body |
Member
There was a problem hiding this comment.
This could cause a bug when shadowing happens inside for body.
e.g.,
code:
import std.{Num,Add,Sub,Eq,Ord,Bounded,Typedef,le};
contract C {
function main() -> word {
let x = 100;
let i = 0;
let s = 0;
for(i=0;i<=0;i=i+1) { let x = 1; s = x; }
return s;
}
}
hull:
object C {
code {
function main () -> word {
let x : word
x := 100
let i : word
i := 0
let s : word
s := 0
for ({ i := 0
}; le$word(i, 0); { i := Add_add$word(i, 1)
}) { let x : word
x := 1
s := 100
}
return s
}
| | AsmBlock {Asm $1} | ||
| | 'if' '(' Expr ')' Body %shift {If $3 $5 []} | ||
| | 'if' '(' Expr ')' Body 'else' Body {If $3 $5 $7} | ||
| | 'for' '(' ForClauseStmt ';' Expr ';' ForClauseStmt ')' Body {For $3 $5 $7 $9} |
Member
There was a problem hiding this comment.
We probably need to disallow let in the for post clause in either the parser or the type checker.
Currently, the below code can be compiled without an error.
import std.{Num,Add,Sub,Eq,Ord,Bounded,Typedef,le};
contract C {
function main() -> word {
let i = 0;
let s = 99;
for(i=0;i<=0;let j=1) { s = j; i = i + 1; }
return s;
}
}
| (condExp, condStmts) <- emitExp cond | ||
| postStmts <- emitStmt post | ||
| bodyStmts <- concat <$> mapM emitStmt body | ||
| -- Hoist allocs (from init/cond/post) into an outer block so variables |
Member
There was a problem hiding this comment.
This causes a duplicated let problem in yul.
e.g.,
code:
import std.{Num,Add,Sub,Eq,Ord,Bounded,Typedef,le};
contract Prefor {
function main() -> word {
let i = 100;
let s = 0;
for(let i=1;i<=10;i=i+1) { s = s + i; }
return s;
}
}
yul:
function usr$main () -> _result {
let i
i := 100
let s
s := 0
let i
for {i := 1 let _v5 let _v6 _v5 := usr$le$word(i, 10)} _v5 {_v6 := usr$Add_add$word(i, 1) i := _v6 _v5 := usr$le$word(i, 10)}
{let _v7
_v7 := usr$Add_add$word(s, i)
s := _v7}
_result := s
leave
}
Collaborator
Author
|
@Y-Nak thanks for the thorough review. I tried to fix the issues you found, please look again. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds for loops of the form
Declaring variables in the initialisation block is allowed
TBD: multiple statements in the init/post blocks