From 6a95720a755d5d2c388a0d3ad369f01685f14895 Mon Sep 17 00:00:00 2001 From: David Eriksson <cryslacks@live.se> Date: Mon, 2 Nov 2020 09:05:17 +0000 Subject: [PATCH] Update HOME_EXAM.md --- HOME_EXAM.md | 135 +++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 86 deletions(-) diff --git a/HOME_EXAM.md b/HOME_EXAM.md index e89d7e1..aba84f2 100644 --- a/HOME_EXAM.md +++ b/HOME_EXAM.md @@ -22,8 +22,7 @@ ### Params ```ebnf : "()" - | "(" [ "," ], Param ")" - ; + | "(" (Param ",")+ ")" ``` ### Param ```ebnf @@ -108,7 +107,7 @@ ### Exprs ```ebnf : "()" - | "(" [ "," ], Expr1 ")" + | "(" (Expr1 ",")+ ")" ``` ### LoCode ```ebnf @@ -177,25 +176,6 @@ ; ``` -### Accepted code -```rust -fn main() { - let mut a = 3; - let b = &mut a; - *b = 1; - - while a <= 3 { - *b = add(*b, 1); - } - - let c = a / 3; -} - -fn add(a:i32, b:i32) -> i32 { - a+b -} -``` - > - Give an example that showcases all rules of your EBNF. The program should "do" something as used in the next exercise. > - For your implementation, show that your compiler successfully accepts the input program. @@ -219,6 +199,8 @@ fn add(a:i32, b:i32) -> i32 { > - Give a set of examples that are syntactically illegal, and rejected by your compiler. + +# TODO MORE ERRORS **Rejected Code** #### Fn misstype ```rust @@ -285,7 +267,7 @@ The Structural Operational Semantics (SOS) that my compiler follows is as descri Symbols used: - σ, state - σ', derived state -- ⇓, evaluates +- →, evaluates - c, command - v, variable - e, expression @@ -293,23 +275,11 @@ Symbols used: Expression can result in a - b, boolean - i, integer -- f, function - - -### Function sequence -```math -<f, σ>⇓σ' -``` -```Rust -fn a(){ - b(); -} -``` ### Command sequence ```math -\frac{<c_1,σ> ⇓ σ' <c_2,σ'> ⇓ σ''}{<c_1;c_2,σ> ⇓ σ''} +\frac{<c_1,σ> → σ' <c_2,σ'> ⇓ σ''}{<c_1;c_2,σ> → σ''} ``` ```rust @@ -320,7 +290,7 @@ let b = 2; ### Operands for integer expressions ```math -\frac{<e_1,σ> ⇓ i_1 <e_2,σ> ⇓ i_2}{<e_1 + e_2,σ> ⇓ i_1 \text{add } i_2} +\frac{<e_1,σ> → i_1 <e_2,σ> → i_2}{<e_1 + e_2,σ> → i_1 \text{add } i_2} ``` Above is addition operand of two expressions described, here are all the integer expressions that can be described in the same way: @@ -342,7 +312,7 @@ Above is addition operand of two expressions described, here are all the integer ``` ### Operands for boolean expressions ```math -\frac{<e_1,σ> ⇓ b_1 <e_2,σ> ⇓ b_2}{<e_1 == e_2,σ> ⇓ b_1 \text{equal } b_2} +\frac{<e_1,σ> → b_1 <e_2,σ> → b_2}{<e_1 == e_2,σ> → b_1 \text{equal } b_2} ``` Above is an equal operation of two expressions described, here are all the boolean expressions that can be described in the same way: - "||", Or @@ -358,7 +328,7 @@ true != false; ### Different references of expression ```math -\frac{<e,σ> ⇓ v}{<\&e, σ> ⇓ \text{ref } v} +\frac{<e,σ> → v}{<\&e, σ> → \text{ref } v} ``` ```rust @@ -368,7 +338,7 @@ true != false; Referencing an expression creates a reference to the variable that expression is evaulated to. ```math -\frac{<e,σ> ⇓ v}{<\&\text{mut } e, σ> ⇓ \text{refmut } v} +\frac{<e,σ> → v}{<\&\text{mut } e, σ> → \text{refmut } v} ``` ```rust @@ -378,7 +348,7 @@ Referencing an expression creates a reference to the variable that expression is Creates a mutable reference to the variable which the expression is evaluated to. ```math -\frac{<e,σ> ⇓ v}{<*e, σ> ⇓ \text{deref } v} +\frac{<e,σ> → v}{<*e, σ> → \text{deref } v} ``` ```rust @@ -389,7 +359,7 @@ Dereferencing works the same way as reference but the other way around. ### Function call expression ```math -<e,σ> ⇓ \text{return of }e +\frac{<f(e_1, ..., e_n),σ> → <x,σ'>; <e_1,σ> → x; ... ;<e_n,σ> → x}{<f,σ> → x} ``` ```rust @@ -407,7 +377,7 @@ A function call evaluates to the return evaluation of the function call e. ### Let expression ```math -\frac{<v, σ> ⇓ <\text{let } v \text{ = } e,σ> ⇓ σ'}{<\text{let } v \text{ = } e, σ> ⇓ σ[v \text{ = } e]} +\frac{<e, σ> → v,σ'}{<\text{let id} \text{ = } e, σ> → σ'[\text{id = } e]} ``` ```rust @@ -417,7 +387,7 @@ let a = 1; The let expression declares a unique variable $`v`$ with a type $`t`$ to the current scope. ```math -\frac{<v, σ> ⇓ <\text{let mut } v \text{ = } e,σ> ⇓ σ'}{<\text{let mut } v \text{ = } e, σ> ⇓ σ[v \text{ = } E]} +\frac{<e, σ> → v,σ'}{<\text{let mut id} \text{ = } e, σ> → σ'[\text{id = } v]} ``` ```rust @@ -428,7 +398,7 @@ The let expression can also declare a unique mutable variable $`v`$ with a type ### Assignment expression ```math -\frac{}{<v \text{ = } e, σ> ⇓ σ[v \text{ = } e]} +\frac{<e, σ> → v,σ'}{<\text{id = } e, σ> → σ'[\text{id = } v]} ``` ```rust @@ -439,7 +409,7 @@ The assignment expression assigns a new value to a mutable variable. ### If expression ```math -\frac{<e,σ> ⇓ b <c_1,σ> ⇓ σ' <c_2,σ> ⇓ σ''}{<\text{if } e \text{ then } c_1 \text{ else } c_2, σ> ⇓ σ'\text{ || }σ''} +\frac{<e,σ> → b <c_1,σ> → σ' <c_2,σ> → σ''}{<\text{if } e \text{ then } c_1 \text{ else } c_2, σ> → σ'\text{ || }σ''} ``` ```rust @@ -454,13 +424,13 @@ The if expression will result in the state to change to $`σ'`$ or $`σ''`$ depe ### While expression ```math -\frac{<e,σ> ⇓ \text{ false}}{<\text{while } e \text{ do } c, σ> ⇓ σ} +\frac{<e,σ> → \text{ false}}{<\text{while } e \text{ do } c, σ> → σ} ``` A while expression that is false will lead to no change of state since the expression will just be skipped over. ```math -\frac{<e,σ> ⇓ \text{ true} <c,σ> ⇓ σ' <\text{while } e \text{ do } c,σ'> ⇓ σ''}{<\text{while } e \text{ do } c, σ'> ⇓ σ''} +\frac{<e,σ> → \text{ true} <c,σ> → σ' <\text{while } e \text{ do } c,σ'> → σ''}{<\text{while } e \text{ do } c, σ> → σ''} ``` ```rust @@ -471,19 +441,6 @@ while a < 5 { ``` A while expression that is true will lead to two changes of state since the command changes the state and the while also changes the state. -### Return expression - -```math -\frac{<f,σ> ⇓ σ}{<\text{return } e, σ> ⇓ σ[\text{return }e]} -``` - -```rust -fn a() -> i32 { - 1+1 -} -``` - -A function returns by adding the return value into the current state when terminating. > - For your implementation, give a program (or set of test programs) that cover all the semantics of your language that you have successfully implemented. (Maybe a subset of the input language accepted by the grammar.) @@ -511,6 +468,7 @@ fn add(a:i32, b:i32) -> i32 { We use the same symbols as the interpreter but with one addition - t, type +- σ, type environment Different types are - i32, integer @@ -524,7 +482,7 @@ Different types are ### Command sequence ```math -\frac{<c_1,σ> ⇓ σ' <c_2,σ'> ⇓ σ''}{<c_1;c_2,σ> ⇓ σ''} +\frac{<c_1,σ> → σ' <c_2,σ'> → σ''}{<c_1;c_2,σ> → σ''} ``` Exactly as the SOS rules of the interpreter. @@ -532,7 +490,7 @@ Exactly as the SOS rules of the interpreter. ### Operands for integer types ```math -\frac{<e_1,σ> ⇓ \text{i32} <e_2,σ> ⇓ \text{i32}}{<e_1 + e_2,σ> ⇓ \text{i32}} +\frac{<e_1,σ> → \text{i32} <e_2,σ> → \text{i32}}{<e_1 + e_2,σ> → \text{i32}} ``` Above is addition operand of two expressions described, here are all the integer expressions that can be described in the same way: @@ -540,12 +498,6 @@ Above is addition operand of two expressions described, here are all the integer - "-", Subtraction - "*", Multiplication - "/", Divison -- ">", Greater than -- "<", Lesser than -- "=>", Greater than or equal to -- "=<", Lesser than or equal to -- "==", Equal -- "!=", Not equal ```rust (1+2)*3; @@ -555,7 +507,7 @@ Above is addition operand of two expressions described, here are all the integer ### Operands for boolean types ```math -\frac{<e_1,σ> ⇓ \text{Bool} <e_2,σ> ⇓ \text{Bool}}{<e_1 == e_2,σ> ⇓ \text{Bool}} +\frac{<e_1,σ> → \text{Bool} <e_2,σ> → \text{Bool}}{<e_1 == e_2,σ> → \text{Bool}} ``` Above is an equal operation of two expressions described, here are all the boolean expressions that can be described in the same way: @@ -564,6 +516,19 @@ Above is an equal operation of two expressions described, here are all the boole - "==", Equal - "!=", Not equal + +```math +\frac{<e_1,σ> → \text{i32} <e_2,σ> → \text{i32}}{<e_1 == e_2,σ> → \text{Bool}} +``` + +Above is an equal operation of two expressions described, here are all the boolean expressions that can be described in the same way: +- ">", Greater than +- "<", Lesser than +- "=>", Greater than or equal to +- "=<", Lesser than or equal to +- "==", Equal +- "!=", Not equal + ```rust true || false; true != 13; // Error type missmatch @@ -572,9 +537,14 @@ true != 13; // Error type missmatch ### Different references ```math -\frac{<e,σ> ⇓ \text{\&} t}{<*e,σ> ⇓ t} +\frac{<e,σ> → \text{\&} t}{<*e,σ> → t} +``` +```math +\frac{<e,σ> → \text{\&mut } t}{<*e,σ> → \text{mut } t} +``` +```math +\frac{<*e,σ> → t}{<e,σ> → \text{\&}t} ``` - ```rust let a = 3; let b = &a; @@ -587,7 +557,7 @@ We do not check references interaction in the type checker we only care about th ### Function call type ```math -<e,σ> ⇓ \text{return type of } e +\frac{<f(e_1, ..., e_n),σ> → <t,σ'>; <e_1,σ> → t; ... ;<e_n,σ> → t}{<f,σ> → t} ``` ```rust @@ -607,7 +577,7 @@ A function call checks the return type of the specified function. ### Let types ```math -\frac{<v,σ> ⇓ t <\text{let } v \text{ : } t,σ> ⇓ σ'}{<\text{let } v \text{ : } t, σ> ⇓ σ[v \text{ = } t]} +\frac{<e, σ> → t,σ'}{<\text{let id} \text{ = } e, σ> → σ'[\text{id = } e]} ``` ```rust @@ -616,8 +586,9 @@ let a: i32 = 1; The let expression declares a unique variable $`v`$ with a type $`t`$ to the current scope. + ```math -\frac{<v,σ> ⇓ t <\text{let mut } v \text{ : } t,σ> ⇓ σ'}{<\text{let } v \text{ : } t, σ> ⇓ σ[v \text{ = } mut t]} +\frac{<e, σ> → \text{mut }t,σ'}{<\text{let id} \text{ = } e, σ> → σ'[\text{id = } e]} ``` ```rust @@ -628,7 +599,7 @@ The let expression can also declare a unique mutable variable $`v`$ with a type ### Assignment expression ```math -\frac{<v,σ> ⇓ \text{mut }t <e,σ> ⇓ t}{<v \text{ = } t,σ> ⇓ σ[v \text{ = } t]} +\frac{<v,σ> → \text{mut }t <e,σ> → t}{<v \text{ = } t,σ>} ``` ```rust @@ -643,7 +614,7 @@ The assignment expression assigns a new value to a mutable variable if the types ### If ```math -\frac{<e,σ> ⇓ \text{ Bool} <c_1,σ> ⇓ σ' <c_2,σ> ⇓ σ''}{<\text{if }e\text{ then }c_1\text{ else }c_2,σ> ⇓ σ} +\frac{<e,σ> → \text{ Bool} <c_1,σ> → t <c_2,σ> → t}{<\text{if }e\text{ then }c_1\text{ else }c_2,σ> → t} ``` ```rust @@ -658,7 +629,7 @@ The difference between interpreter and type checker is that we do not check whic ### While ```math -\frac{<e,σ> ⇓ \text{ Bool} <c,σ> ⇓ σ' <\text{while }e\text{ do }c, σ'> ⇓ σ''}{<\text{while }e\text{ do }c,σ> ⇓ σ''} +\frac{ \langle e,σ \rangle → \text{bool} ; \langle c,σ \rangle → σ';}{ \langle \text{while } e \text{ do } c,σ \rangle → ()} ``` ```rust @@ -668,14 +639,6 @@ while false { ``` Same goes here, the difference between interpreter and type checker is that we do not check if the while statement will run. We only care about that $`e`$ is of boolean type and that no type errors happends in the while path. -### Return -```math -\frac{<f,σ> ⇓ σ}{<\text{return: }t,σ> ⇓ σ[\text{return: }t]} -``` - -Checks if a function returns with the pre-declared type. Otherwise an error for type missmatch is raised. - - > - For your implementation, give a set of programs demonstrating that ill-typed programs are rejected, connect back to the Type Checking Rules to argue why these are illegal and thus should be rejected. #### Return type is wrong -- GitLab