@@ -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
fna(x:bool)->bool{
fna(x)->bool{
x
x
};
};
fnmain()->(){
fnmain()->(){
a();
a();
}
}
```
```
No argument given in functioncall.
No type given for argument in function a.
```rust
```rust
fna(x:i32,y:i32)->bool{
fna(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.
@@ -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
fna(x:&i32)->(){
fna(x:&muti32)->(){
*x+=1;
*x+=1;
}
}
fnmain()->(){
fnmain()->(){
lety:i32=5;
letmutx=0;
lety=&x;
a(&y);
a(&mutx);
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.