Skip to content

Commit

Permalink
Implement instruction sequences; let; var expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Aug 27, 2024
1 parent b501c71 commit 0a156d2
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions rust-semantics/execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
requires "execution/configuration.md"
requires "execution/block.md"
requires "execution/calls.md"
requires "execution/let.md"
requires "execution/stack.md"
requires "execution/statements.md"
module RUST-EXECUTION
imports RUST-BLOCK
imports RUST-CALLS
imports RUST-LET
imports RUST-STACK
imports RUST-STATEMENTS
endmodule
```
30 changes: 30 additions & 0 deletions rust-semantics/execution/let.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
```k
module RUST-LET
imports COMMON-K-CELL
imports RUST-CASTS
imports RUST-EXECUTION-CONFIGURATION
imports RUST-VALUE-SYNTAX
imports RUST-SHARED-SYNTAX
// Not all cases are implemented
rule
<k>
let Variable:Identifier : T:Type = V:Value ; => .K
...
</k>
<next-value-id> NextId:Int => NextId +Int 1 </next-value-id>
<locals> Locals:Map => Locals[Variable <- NextId] </locals>
<values> Values:Map => Values[NextId <- implicitCast(V, T)] </values>
rule
<k>
let Variable:Identifier = V:Value ; => .K
...
</k>
<next-value-id> NextId:Int => NextId +Int 1 </next-value-id>
<locals> Locals:Map => Locals[Variable <- NextId] </locals>
<values> Values:Map => Values[NextId <- V] </values>
requires notBool mayBeDefaultTypedInt(V)
endmodule
```
11 changes: 11 additions & 0 deletions rust-semantics/execution/statements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```k
module RUST-STATEMENTS
imports RUST-SHARED-SYNTAX
rule Nes:NonEmptyStatements E:Expression => Nes ~> E
rule S:Statement Ss:NonEmptyStatements => S ~> Ss
rule .NonEmptyStatements => .K
endmodule
```
2 changes: 2 additions & 0 deletions rust-semantics/expression.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
```k
requires "expression/casts.md"
requires "expression/literals.md"
requires "expression/variables.md"
module RUST-EXPRESSION
imports private RUST-CASTS
imports private RUST-EXPRESSION-LITERALS
imports private RUST-EXPRESSION-VARIABLES
endmodule
```
18 changes: 18 additions & 0 deletions rust-semantics/expression/variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```k
module RUST-EXPRESSION-VARIABLES
imports COMMON-K-CELL
imports RUST-EXECUTION-CONFIGURATION
imports RUST-VALUE-SYNTAX
imports RUST-SHARED-SYNTAX
rule
<k>
Variable:Identifier :: .PathExprSegments => V
...
</k>
<locals> Variable |-> VarId:Int ... </locals>
<values> VarId |-> V:Value ... </values>
endmodule
```
5 changes: 5 additions & 0 deletions rust-semantics/representation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
```k
module RUST-VALUE-SYNTAX
imports BOOL
imports LIST // for filling the second argument of `error`.
imports MAP
imports MINT
Expand Down Expand Up @@ -28,6 +29,10 @@ module RUST-VALUE-SYNTAX
syntax KResult ::= Value
syntax ValueOrError ::= Value | SemanticsError
syntax Bool ::= mayBeDefaultTypedInt(Value) [function, total]
rule mayBeDefaultTypedInt(_V) => false [owise]
rule mayBeDefaultTypedInt(u128(_)) => true
endmodule
module RUST-REPRESENTATION
Expand Down
4 changes: 2 additions & 2 deletions rust-semantics/rust-common-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,9 @@ https://doc.rust-lang.org/reference/items/extern-crates.html

```k
syntax LetStatement ::= OuterAttributes "let" PatternNoTopAlt MaybeColonType MaybeEqualsExpressionMaybeElseBlockExpression ";"
syntax LetStatement ::= OuterAttributes "let" PatternNoTopAlt MaybeColonType ";"
| OuterAttributes "let" PatternNoTopAlt MaybeColonType "=" Expression MaybeElseBlockExpression ";" [strict(4)]
syntax MaybeColonType ::= "" | ":" Type
syntax MaybeEqualsExpressionMaybeElseBlockExpression ::= "" | "=" Expression MaybeElseBlockExpression
// TODO: Not implemented properly to remove ambiguities
syntax MaybeElseBlockExpression ::= "" // | "else" BlockExpression
Expand Down
5 changes: 3 additions & 2 deletions rust-semantics/targets/preprocessing/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
requires "configuration.md"
requires "../../preprocessing.md"
requires "../../representation.md"
requires "../../expression.md"
requires "../../expression/casts.md"
requires "../../expression/literals.md"
requires "../../rust-common-syntax.md"
module RUST-SYNTAX
imports RUST-COMMON-SYNTAX
endmodule
module RUST
imports private RUST-EXPRESSION
imports private RUST-EXPRESSION-LITERALS
imports private RUST-PREPROCESSING
imports private RUST-RUNNING-CONFIGURATION
endmodule
Expand Down
4 changes: 4 additions & 0 deletions tests/execution/let-final-expression.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
new LetFinal;
call LetFinal.let_final;
return_value;
check_eq 100_u64
19 changes: 19 additions & 0 deletions tests/execution/let-final-expression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait LetFinal {
#[init]
fn init(&self) {
}

#[upgrade]
fn upgrade(&self) {}

fn let_final(&self) -> u64 {
let x = 100_u64;
x
}
}

0 comments on commit 0a156d2

Please sign in to comment.