diff --git a/HOME_EXAM.md b/HOME_EXAM.md index b0a11c2b39cecd00e6bede5d66534f68a4d2ce64..0aea725dde3656d87f7afc9a3effae907ab2c131 100644 --- a/HOME_EXAM.md +++ b/HOME_EXAM.md @@ -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. ## 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 Program - :Program* - |Function - |Decl - |Expr + :Function* ; ``` ```ebnf -Stmt - :"if" Expr BlockExpr ("else" BlockExpr)? - |"while" Expr BlockExpr - |Decl - |Expr +Function + :"fn" Id "(" (Arg ",")* Arg? ")" "->" Type BlockExpr + |"fn" Id "()" "->" "()" BlockExpr ; ``` ```ebnf -Function - :"fn" Id "(" Arg* ")" "->" Type BlockExpr - |"fn" Id "()" "->" "()" BlockExpr +BlockExpr + :"{" (Stmt ";")* Stmt? "}" ; ``` ```ebnf -BlockExpr - :"{" Stmt* "}" +Stmt + :"if" Expr BlockExpr ("else" BlockExpr)? + |"while" Expr BlockExpr + |Decl + |Expr ; ``` @@ -133,7 +130,7 @@ Arg Expr :Expr ExprOp Factor |Expr LogicOp Factor - |Id "(" Expr* ")" + |Id "(" (Expr ",")* Expr? ")" |Factor ; ``` @@ -261,14 +258,14 @@ Three examples that will not go through the parser and be rejected by the compil No return type. ```rust - fn a(x: bool) -> bool { + fn a(x) -> bool { x }; fn main() -> () { a(); } ``` -No argument given in functioncall. +No type given for argument in function a. ```rust 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 - Other loops than while - More types - Nested functions -- Functions not needed to be decleared in specific order -- Global assignments -- Shadowing - 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 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. ##### Assignment ```math -\frac{<e,σ> → e'}{<x:=e,σ> → <x:=e',σ>} +\frac{<e,σ> → n}{<x:=e,σ> → σ[x:=n]} ``` Let statements works in a similiar way. @@ -436,7 +429,6 @@ Supported types: - i32 - bool - unit -- unknown ##### Arithmetic, boolean & comparison operations ```math @@ -461,8 +453,6 @@ Simliar SOS goes for the following operands: - "==", 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 \frac{<e0,σ> → bool <e1,σ> → bool}{<e0 || e1,σ> → bool} ``` @@ -470,6 +460,8 @@ SOS for the or operand between two boolean types returning a boolean. Similar SOS goes for the following operand: - "&&", and +- "==", equals +- "!=", not equals ```rust 5 * 3; @@ -481,7 +473,7 @@ Similar SOS goes for the following operand: ##### Assignment ```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. @@ -500,7 +492,7 @@ fn b() -> bool { ##### Conditional command ```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. @@ -516,7 +508,7 @@ For the if statement to go through the type checker need the expression, e, be a ##### While command ```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 @@ -551,17 +543,17 @@ It would be nice do add the following in the future: #### Invalid examples ```rust -fn a(x: &i32) -> () { +fn a(x: &mut i32) -> () { *x += 1; } - fn main() -> () { - let y: i32 = 5; - - a(&y); + let mut x = 0; + let y = &x; + 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 fn b(x: &mut i32, y: &mut i32) -> () {