From 247453ccc93f757e52b4a6e8997a183ad3268940 Mon Sep 17 00:00:00 2001 From: Felix Cherubini Date: Wed, 20 Nov 2024 12:18:47 +0100 Subject: [PATCH 1/3] Rough draft for Java instructions --- notes/java.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 notes/java.md diff --git a/notes/java.md b/notes/java.md new file mode 100644 index 0000000..284199a --- /dev/null +++ b/notes/java.md @@ -0,0 +1,52 @@ +# Recommendations for solving lab 2 with Java + +The stub for lab 2 uses Java 21, +which admits a more readable alternative to the visitor pattern suggested in the book. +The following example code shows how you can process an object ```exp``` of the type type ```Exp``` +generated by the parser defined in the stub. + +```java +switch(exp) { + case EInt e -> ... ; + case EDouble e -> ... ; + case EAnd e -> ... ; + case EOr e -> ... ; + ... + default -> throw new IllegalStateException("Case for " + e + " is not yet implemented."); +``` + +On the right hand side of the ```->``` you can extract further information from ```e```, +for example, in the case ```EAnd e```, you can use the two operands ```e.exp_1``` and ```e.exp_2```. + +```switch```, if used like above produces a Java-expression, which means that you can use it in assignments: + +```java +var typeExpression = switch(exp) { ... } +``` + +To define the neccessary datatypes, ```record```s are very useful. +You can define a record like this: + +```java +public record TypedAnd(TypedExpr e1, TypedExpr e2) { +} +``` + +Objects of this type can be constructed with ```new TypedAnd(e1,e2)``` where ```e1``` and ```e2``` are ```TypedExpr```s. For solving the lab, it is reasonable to define an ```interface``` ```TypedExpr``` and with a method which returns the type of an expression and make the datatypes for specific expressions implement it: + +```java +public interface TypedExpr { + Type type(); +} +``` + +Where ```Type``` is a datatype for types that needs to be defined. With this interface, the ```TypedAnd``` from above can be turned into an implementation like this: + +```java +public record TypedAnd(TypedExpr e1, TypedExpr e2) { + Type type() { + return new Type.Bool; // this is what it could look like if you implement basic types as an enum + } +} +``` + From d8b5178a5bbd6f9ab5024288354cf21c8c5bc2ef Mon Sep 17 00:00:00 2001 From: Felix Cherubini Date: Wed, 20 Nov 2024 12:23:23 +0100 Subject: [PATCH 2/3] Small improvements --- notes/java.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/notes/java.md b/notes/java.md index 284199a..b6d4f3e 100644 --- a/notes/java.md +++ b/notes/java.md @@ -13,6 +13,7 @@ switch(exp) { case EOr e -> ... ; ... default -> throw new IllegalStateException("Case for " + e + " is not yet implemented."); +} ``` On the right hand side of the ```->``` you can extract further information from ```e```, @@ -21,7 +22,7 @@ for example, in the case ```EAnd e```, you can use the two operands ```e.exp_1`` ```switch```, if used like above produces a Java-expression, which means that you can use it in assignments: ```java -var typeExpression = switch(exp) { ... } +var typedExpression = switch(exp) { ... } ``` To define the neccessary datatypes, ```record```s are very useful. @@ -32,7 +33,7 @@ public record TypedAnd(TypedExpr e1, TypedExpr e2) { } ``` -Objects of this type can be constructed with ```new TypedAnd(e1,e2)``` where ```e1``` and ```e2``` are ```TypedExpr```s. For solving the lab, it is reasonable to define an ```interface``` ```TypedExpr``` and with a method which returns the type of an expression and make the datatypes for specific expressions implement it: +Objects of this type can be constructed with ```new TypedAnd(e1,e2)``` where ```e1``` and ```e2``` are ```TypedExpr```s. For solving the lab, it is reasonable to define an ```interface``` ```TypedExpr``` with a method which returns the type of an expression: ```java public interface TypedExpr { From 15c19b1b35a7cb84caea447eda7724a200b2ee42 Mon Sep 17 00:00:00 2001 From: Andreas Abel Date: Thu, 21 Nov 2024 15:23:27 +0100 Subject: [PATCH 3/3] Publish notes/java.md --- README.md | 2 +- notes/Makefile | 2 +- notes/java.md | 13 ++++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 553dff4..d3a41f2 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Material: plt = course book, dragon = Dragon book. Slides follow closely the plt | Tue 19/11 | 13-15 | Type checking | [slides](plt-book/ipl-book/slides/4-slides-ipl-book.pdf), plt 4, dragon 5,6, [script](notes/type-checking.html), [prime.c](notes/prime.c), [prime-stms.c](notes/prime-stms.c), [division.c](notes/division.c), [division-annotated.c](notes/division-annotated.c) | | Thu 21/11 | 13-15 | Interpreting | [slides](plt-book/ipl-book/slides/5-slides-ipl-book.pdf), plt 5, [script](notes/interpreter.html) | | Tue 26/11 | 13-14 | Hands-on with Lab 2 (Haskell) | [script](notes/monads.html) | -| Tue 26/11 | 14-15 | Hands-on with Lab 2 (Java) | | +| Tue 26/11 | 14-15 | Hands-on with Lab 2 (Java) | [script](notes/java.html) | | Thu 28/11 | 13-15 **SB-H5** | Code generation | [slides](plt-book/ipl-book/slides/6-slides-ipl-book.pdf), plt 6, dragon 6,7, [notes](notes/compilation.html), [prime.c](notes/prime.c), [prime.j](notes/prime.j) | | Tue 03/12 | 13-14 | Hands-on with Lab 3 (Haskell) | | | Tue 03/12 | 14-15 | Hands-on with Lab 3 (Java) | | diff --git a/notes/Makefile b/notes/Makefile index 9a3943b..8b332aa 100644 --- a/notes/Makefile +++ b/notes/Makefile @@ -1,7 +1,7 @@ .PHONY: all all: index.html -files = type-checking.html interpreter.html monads.html compilation.html cbn-cbv.html typing.html LR-table.html git-primer.html +files = type-checking.html interpreter.html monads.html compilation.html cbn-cbv.html typing.html LR-table.html git-primer.html java.html index.html : $(files) tree -H '.' -L 2 --noreport --charset utf-8 > $@ diff --git a/notes/java.md b/notes/java.md index b6d4f3e..15be6a6 100644 --- a/notes/java.md +++ b/notes/java.md @@ -7,12 +7,12 @@ generated by the parser defined in the stub. ```java switch(exp) { - case EInt e -> ... ; - case EDouble e -> ... ; - case EAnd e -> ... ; - case EOr e -> ... ; - ... - default -> throw new IllegalStateException("Case for " + e + " is not yet implemented."); + case EInt e -> ... ; + case EDouble e -> ... ; + case EAnd e -> ... ; + case EOr e -> ... ; + ... + default -> throw new IllegalStateException("Case for " + e + " is not yet implemented."); } ``` @@ -50,4 +50,3 @@ public record TypedAnd(TypedExpr e1, TypedExpr e2) { } } ``` -