Skip to content
Snippets Groups Projects
Commit 9ecee400 authored by Wilma Krutrök's avatar Wilma Krutrök
Browse files

Fixed incorrect examples and corrected ebnf

parent 20009529
No related branches found
No related tags found
No related merge requests found
...@@ -81,37 +81,34 @@ Comment on additional things that you have experienced and learned throughout th ...@@ -81,37 +81,34 @@ Comment on additional things that you have experienced and learned throughout th
In the following sections will the bullet points above be covered. The answers will be based on the code attached. In the following sections will the bullet points above be covered. The answers will be based on the code attached.
## Syntax ## Syntax
The implemented langugage is desciribed usign EBNF grammar below. The language is implemented using lalrpop. Note that "?" means optional and when a part ends with "s" for example "programs" is it a vector containing one or more program (same as program+). The implemented langugage is desciribed usign EBNF grammar below. The language is implemented using lalrpop.
#### EBNF #### EBNF
```ebnf ```ebnf
Program Program
:Program* :Function*
|Function
|Decl
|Expr
; ;
``` ```
```ebnf ```ebnf
Stmt Function
:"if" Expr BlockExpr ("else" BlockExpr)? :"fn" Id "(" (Arg ",")* Arg? ")" "->" Type BlockExpr
|"while" Expr BlockExpr |"fn" Id "()" "->" "()" BlockExpr
|Decl
|Expr
; ;
``` ```
```ebnf ```ebnf
Function BlockExpr
:"fn" Id "(" Arg* ")" "->" Type BlockExpr :"{" (Stmt ";")* Stmt? "}"
|"fn" Id "()" "->" "()" BlockExpr
; ;
``` ```
```ebnf ```ebnf
BlockExpr Stmt
:"{" Stmt* "}" :"if" Expr BlockExpr ("else" BlockExpr)?
|"while" Expr BlockExpr
|Decl
|Expr
; ;
``` ```
...@@ -133,7 +130,7 @@ Arg ...@@ -133,7 +130,7 @@ Arg
Expr Expr
:Expr ExprOp Factor :Expr ExprOp Factor
|Expr LogicOp Factor |Expr LogicOp Factor
|Id "(" Expr* ")" |Id "(" (Expr ",")* Expr? ")"
|Factor |Factor
; ;
``` ```
...@@ -261,14 +258,14 @@ Three examples that will not go through the parser and be rejected by the compil ...@@ -261,14 +258,14 @@ Three examples that will not go through the parser and be rejected by the compil
No return type. No return type.
```rust ```rust
fn a(x: bool) -> bool { fn a(x) -> bool {
x x
}; };
fn main() -> () { fn main() -> () {
a(); a();
} }
``` ```
No argument given in functioncall. No type given for argument in function a.
```rust ```rust
fn a(x: i32, y: i32) -> bool { fn a(x: i32, y: i32) -> bool {
...@@ -304,13 +301,9 @@ In the future could the following things be implemented to allow the parser to a ...@@ -304,13 +301,9 @@ In the future could the following things be implemented to allow the parser to a
- Other loops than while - Other loops than while
- More types - More types
- Nested functions - Nested functions
- Functions not needed to be decleared in specific order
- Global assignments
- Shadowing
- Pretty printing - Pretty printing
- Mutable arguments
Currently it is needed to seperate statements with ";" (except for the last one) for the parser to interpret it as a vector. This would be nice to rewrite in the future. Currently it is needed to seperate functions with ";" (except for the last one) for the parser to interpret it as a vector. This would be nice to rewrite in the future.
## Semantics ## Semantics
Below is the Structural Operational Semantics (SOS) described using small-step semantics for the implemented language. Below is the Structural Operational Semantics (SOS) described using small-step semantics for the implemented language.
...@@ -359,7 +352,7 @@ Can also be combined in a derivation tree. ...@@ -359,7 +352,7 @@ Can also be combined in a derivation tree.
##### Assignment ##### Assignment
```math ```math
\frac{<e,σ> → e'}{<x:=e,σ> → <x:=e',σ>} \frac{<e,σ> → n}{<x:=e,σ> → σ[x:=n]}
``` ```
Let statements works in a similiar way. Let statements works in a similiar way.
...@@ -436,7 +429,6 @@ Supported types: ...@@ -436,7 +429,6 @@ Supported types:
- i32 - i32
- bool - bool
- unit - unit
- unknown
##### Arithmetic, boolean & comparison operations ##### Arithmetic, boolean & comparison operations
```math ```math
...@@ -461,8 +453,6 @@ Simliar SOS goes for the following operands: ...@@ -461,8 +453,6 @@ Simliar SOS goes for the following operands:
- "==", equals - "==", equals
- "!=", not equals - "!=", not equals
For the two last operands can the types for e0 and e1 be both i32 and boolean, concluding in a boolean.
```math ```math
\frac{<e0,σ> → bool <e1,σ> → bool}{<e0 || e1,σ> → bool} \frac{<e0,σ> → bool <e1,σ> → bool}{<e0 || e1,σ> → bool}
``` ```
...@@ -470,6 +460,8 @@ SOS for the or operand between two boolean types returning a boolean. ...@@ -470,6 +460,8 @@ SOS for the or operand between two boolean types returning a boolean.
Similar SOS goes for the following operand: Similar SOS goes for the following operand:
- "&&", and - "&&", and
- "==", equals
- "!=", not equals
```rust ```rust
5 * 3; 5 * 3;
...@@ -481,7 +473,7 @@ Similar SOS goes for the following operand: ...@@ -481,7 +473,7 @@ Similar SOS goes for the following operand:
##### Assignment ##### Assignment
```math ```math
\frac{<x,σ> → bool <e,σ> → bool}{<x:=e,σ>} \frac{<x,σ> → bool <e,σ> → bool}{<x:=e,σ> -> ()}
``` ```
Let statements works in a similiar way. For an assignment to be valid needs the variable to be initialized as mutable together with a type. An assignment will only be accepted by the type checker if the new type matches the current. Let statements works in a similiar way. For an assignment to be valid needs the variable to be initialized as mutable together with a type. An assignment will only be accepted by the type checker if the new type matches the current.
...@@ -500,7 +492,7 @@ fn b() -> bool { ...@@ -500,7 +492,7 @@ fn b() -> bool {
##### Conditional command ##### Conditional command
```math ```math
\frac{<e,σ> → \text{ bool } <c0,σ> → σ'}{<\text{ if bool then } c0 \text{ else } c1,σ> → σ'}; \frac{<e,σ> → \text{ bool } <c0,σ> → t}{<\text{ if e then } c0 \text{ else } c1,σ> → t}, where t = type of last statement
``` ```
For the if statement to go through the type checker need the expression, e, be a boolean. For the if statement to go through the type checker need the expression, e, be a boolean.
...@@ -516,7 +508,7 @@ For the if statement to go through the type checker need the expression, e, be a ...@@ -516,7 +508,7 @@ For the if statement to go through the type checker need the expression, e, be a
##### While command ##### While command
```math ```math
\frac{<e,σ> → \text{ bool } <c,σ> → σ'' <\text{ while bool do } c,σ''> → σ'}{<\text{ while bool do } c,σ> → σ'} \frac{<e,σ> → \text{ bool } <c,σ> → () <\text{ while e do } c,σ''> → ()}{<\text{ while e do } c,σ> → ()}
``` ```
```rust ```rust
...@@ -551,17 +543,17 @@ It would be nice do add the following in the future: ...@@ -551,17 +543,17 @@ It would be nice do add the following in the future:
#### Invalid examples #### Invalid examples
```rust ```rust
fn a(x: &i32) -> () { fn a(x: &mut i32) -> () {
*x += 1; *x += 1;
} }
fn main() -> () { fn main() -> () {
let y: i32 = 5; let mut x = 0;
let y = &x;
a(&y); a(&mut x);
println!("{}", y);
} }
``` ```
This example is invalid due to trying to mutate an immutable reference. Variable x is borrowed both as mutable and immutable which is not accepted.
```rust ```rust
fn b(x: &mut i32, y: &mut i32) -> () { fn b(x: &mut i32, y: &mut i32) -> () {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment