Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java instructions #10

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down
2 changes: 1 addition & 1 deletion notes/Makefile
Original file line number Diff line number Diff line change
@@ -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 > $@
Expand Down
52 changes: 52 additions & 0 deletions notes/java.md
Original file line number Diff line number Diff line change
@@ -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 typedExpression = 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``` with a method which returns the type of an expression:

```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
}
}
```