Skip to content
Snippets Groups Projects
Commit 66e5fbae authored by Aron Strandberg's avatar Aron Strandberg
Browse files

added most SOS rules to describe the interpreter

parent 65f979a7
Branches
No related tags found
No related merge requests found
......@@ -161,6 +161,10 @@ fn main() {
test_bool(true);
}
```
The parser has been implemented using an LR(1) parser generator, [lalrpop](https://github.com/lalrpop/lalrpop). It's a strong tool that offers your code to be DRY and have a readeble grammar from the beginning. It also offers macros which have been used to make the the code less repetetive, more readeble and to implement precedence.
To write the parser the documention has been sufficient but examples from both the documentation and the lalrpop repo has been used.
Above descirbed EBNF and showcase defines a minimal subset of Rust, including
- Function declaration, with excplicit return type either void (NONE) or primitive type
- Primitive types are i32 and boolean
......@@ -186,31 +190,122 @@ For furture development
- Compare your solution to the requirements (as stated in the README.md). What are your contributions to the implementation.
Structural Operational Semantics (SOS)
Symbolx:
Symbols:
- σ, state
- σ', derived state
- ⇓, evaluates
- c, command
- x, variable
- e, expression
- b, boolean expression
An expression can be either a
- b, boolean
- n, number
- f, function
Function sequence
$<f, σ>⇓σ'$
```rust
fn main() {
test();
}
```
The function call moves the current state, $σ$ to the new derived state $σ'$ in the function, test()'s context.
Command sequence
$\frac{<c1,σ> ⇓ σ' <c2,σ'> ⇓ σ''}{<c1;c2,σ> ⇓ σ''}$
Arithmetic
```rust
let x : i32 = 2;
test();
```
A sequence is a composition of commands where the first command is first executed then the second command. Otherwise the intermediate step, $σ'$ would be lost.
Operands for arithmetic expressions
$\frac{<e1,σ> ⇓ n1 <e2, σ> ⇓ n2}{<e1 + e2, σ> ⇓ n1 \text{ } plus \text{ } n2}$
plus is addition as implemented in the computer. Other arithmetic operands imlemented are
- -, minus
- *, multiplication
- /, division
- ">", greather than
- "<", less than
- "==", equal
- "!=", not equal
Operands for boolean expressions
$\frac{<e1,σ> ⇓ b1 <e2, σ> ⇓ b2}{<e1 == e2, σ> ⇓ b1 \text{ } equal \text{ } b2}$
how equals is evaluated is up to the computer. Other boolean operands implemented are
- ">", greather than
- "<", less than
- "==", equal
- "!=", not equal
- "&&", and
- "||", or
$\frac{<e1,σ> ⇓ n1 <e2, σ> ⇓ n2}{<e1 + e2, σ> ⇓ n1 plus n2}$
If true
$\frac{<b, σ> ⇓ true <c1, σ> ⇓ σ'}{<if b then c1> ⇓ σ}$
$\frac{<b, σ> ⇓ true <c1, σ> ⇓ σ'}{<if \text{ } b \text{ } then \text{ } c1> ⇓ σ}$
then the state, $σ$ will change immediately to $σ'$.
If false
$\frac{<b, σ> ⇓ false <c1, σ> ⇓ σ'}{<if b then c1> ⇓ σ}$
$\frac{<b, σ> ⇓ false <c1, σ> ⇓ σ''}{<if \text{ } b \text{ } then \text{ } c1> ⇓ σ}$
the state, $σ'$ will be changed at the second command to $σ''$.
While false
$\frac{<b,σ> ⇓ false}{<while \text{ } b \text{ } do \text{ } c,σ> ⇓ σ}$
While true
$\frac{<b, σ> ⇓ true <c,σ> ⇓ σ' 0 <while \text{ } b \text{ } do \text{ } c, σ'> ⇓ σ''}{<while \text{ } b \text{ } do \text{ } c,σ> ⇓ σ''}$
then $σ'$ is the result of the current state, $σ$ and the last state $σ''$ is the result of $σ'$.
Let assignment expression
$\frac{<x, σ><let \text{ } x:=e, σ>⇓σ'}{<let \text{ } x := e, σ> ⇓ σ[x := e]}$
```rust
let a : i32 = 1;
let b : bool = true;
let c : i32 = test(); // test() explicit return of type i32
```
For let commands, if the variable is already declared the state $σ$ will be changed to the one of the second command, state $σ'$, the variable will be lost. Otherwise the variable will be assigned to the state, $σ$.
Assignment expression
$\frac{}{<x := e, σ> ⇓ σ[x := e]}$
```rust
x = 1;
x = true;
x = test();
```
For assignment, when the variable is assigned to a number, boolean or function the variable and corresponding rhs is moved to the same state.
Return
$\frac{<f, σ>⇓σ}{<return : e, σ> ⇓ σ[return : e]}$
```rust
fn main() -> i32 {
return 1 + 1;
}
```
At explicit return the expression is moved to the state of the function.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment