-
Notifications
You must be signed in to change notification settings - Fork 2
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
For and While Loops #88
Changes from 8 commits
326883e
184da55
dd9e5ed
3328947
57e4d7f
5bc1f42
89450c6
b81f9d6
4aedc14
1b80f3e
3260cac
a9b7172
0fe1c0d
12844f6
324f966
077e603
a85fc04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
```k | ||
|
||
module RUST-LOOP-EXPRESSIONS | ||
imports RUST-SHARED-SYNTAX | ||
imports RUST-VALUE-SYNTAX | ||
imports INT | ||
imports MINT | ||
|
||
syntax IteratorLoopExpression ::= "for1" Pattern "in" ExpressionExceptStructExpression BlockExpression | ||
| "for2" Pattern "in" ExpressionExceptStructExpression BlockExpression | ||
|
||
rule for Patt:Identifier:PatternNoTopAlt | R:PatternNoTopAlts in First..Last B:BlockExpression => | ||
{ | ||
.InnerAttributes | ||
(for1 Patt:Identifier:PatternNoTopAlt | R:PatternNoTopAlts in First..Last B:BlockExpression):IteratorLoopExpression; // Covers the cases of "for x | x in range" | ||
.NonEmptyStatements | ||
}; | ||
|
||
rule for1 Patt:Identifier:PatternNoTopAlt | .PatternNoTopAlts in First..Last B:BlockExpression => | ||
let Patt = First; ~> | ||
if (Patt :: .PathExprSegments):PathExprSegments < Last { .InnerAttributes B; (for2 Patt:Identifier:PatternNoTopAlt | .PatternNoTopAlts in First..Last B):IteratorLoopExpression; .NonEmptyStatements}; | ||
|
||
rule for2 Patt:Identifier:PatternNoTopAlt | .PatternNoTopAlts in _..Last B:BlockExpression => | ||
let Patt = (Patt :: .PathExprSegments):PathExprSegments + ptrValue(null, u64(Int2MInt(1:Int))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm... This is interesting... Right now, we have defined addition only between ints of the same kind, and I think that Rust does not allow adding ints of different types, e.g.
|
||
~> for Patt:Identifier:PatternNoTopAlt | .PatternNoTopAlts in (Patt :: .PathExprSegments):PathExprSegments..Last B | ||
|
||
rule while (E:ExpressionExceptStructExpression) S:BlockExpression => if E { .InnerAttributes S; while(E)S; .NonEmptyStatements}; | ||
|
||
endmodule | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -663,7 +663,8 @@ https://doc.rust-lang.org/reference/items/extern-crates.html | |
|
||
```k | ||
|
||
syntax IteratorLoopExpression ::= "for" Pattern "in" ExpressionExceptStructExpression BlockExpression | ||
syntax IteratorLoopExpression ::= "for" Pattern "in" ExpressionExceptStructExpression BlockExpression | ||
| "while" ExpressionExceptStructExpression BlockExpression | ||
|
||
``` | ||
|
||
|
@@ -680,6 +681,7 @@ https://doc.rust-lang.org/reference/items/extern-crates.html | |
```k | ||
|
||
syntax IfExpression ::= "if" ExpressionExceptStructExpression BlockExpression MaybeIfElseExpression [strict(1)] | ||
|
||
syntax MaybeIfElseExpression ::= "" | ||
| "else" IfElseExpression | ||
syntax IfElseExpression ::= BlockExpression | ||
|
@@ -769,7 +771,7 @@ https://doc.rust-lang.org/reference/items/extern-crates.html | |
|
||
```k | ||
|
||
syntax RangePattern ::= "TODO: not needed yet, not implementing" | ||
syntax RangePattern ::= Expression ".." Expression // "TODO: not needed yet, not implementing" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the TODO since you just added this to the syntax. |
||
|
||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
new LoopExpressions; | ||
call LoopExpressions.iterator_evaluation |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
new LoopExpressions; | ||
call LoopExpressions.while_evaluation |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
new LoopExpressions; | ||
call LoopExpressions.iterator_with_variables |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use multiversx_sc::imports::*; | ||
|
||
#[multiversx_sc::contract] | ||
pub trait LoopExpressions { | ||
#[init] | ||
fn init(&self) { | ||
} | ||
|
||
#[upgrade] | ||
fn upgrade(&self) {} | ||
|
||
fn iterator_evaluation(&self){ | ||
for i in 1_u64..10_u64 { | ||
let x = i * 2_u64; | ||
}; | ||
} | ||
|
||
fn while_evaluation(&self){ | ||
while 1_u64 < 1_u64 { | ||
let x: u64 = 2_u64; | ||
}; | ||
} | ||
|
||
fn iterator_with_variables(&self) { | ||
let y = 20_u64; | ||
let z = 10_u64; | ||
|
||
for i in z..y { | ||
let x = i * 2_u64; | ||
}; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove spaces?