You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: notes/java.md
+50-3
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,7 @@ sealed interface TypedExpr {
78
78
79
79
# How to start
80
80
81
-
This is just one way to start and the ideas given are not complete - be prepared to change everything along the way to a solution that passes all tests. To get started, here is a possible first try at an implementaion of the `typecheck` function in the `TypeChecker`:
81
+
This is just one way to start and the ideas given are not complete - be prepared to change everything along the way to a solution that passes all tests. To get started, here is a possible first try at an implementation of the `typecheck` function in the `TypeChecker`:
82
82
83
83
```java
84
84
publicAnnotatedProgram typecheck(Program p) {
@@ -91,7 +91,7 @@ This is just one way to start and the ideas given are not complete - be prepared
91
91
}
92
92
```
93
93
94
-
The first statement extracts a list of function definitions (from the parser) from `p` and stores it in `funDefs`.The `var` means that the type of `funDefs` is automatically infered. Then the signatures, which are stored in a field in the `TypeChecker` class, are initialized. To make the last line of this functions work, the type `AnnotatedProgram)` was modified to an inner `record` of the `TypeChecker` in the following way (the functions `extractSignatures` and `checkFunDefs` will be explained below):
94
+
The first statement extracts a list of function definitions from (the parse-tree) `p` and stores it in `funDefs`.The `var` means that the type of `funDefs` is automatically infered. Then the signatures, which are stored in a field in the `TypeChecker` class, are initialized. To make the last line of this functions work, the type `AnnotatedProgram)` was modified to an inner `record` of the `TypeChecker` in the following way (the functions `extractSignatures` and `checkFunDefs` will be explained below):
95
95
96
96
```java
97
97
record AnnotatedProgram(Map<String, TypedFunDef> defs) { }
@@ -109,12 +109,59 @@ The type `TypedFunDef` is also defined as an inner `record` of `TypeChecker`:
109
109
```java
110
110
sealed interfaceStatement {
111
111
record Decl(StringvarName, CTypetype) implements Statement {}
112
-
// TODO: add more cases
112
+
// TODO: add more records for all the statements we need in the typed syntax
113
113
}
114
114
```
115
115
116
116
```java
117
117
enumCType {
118
118
Int, Double, Bool, Void
119
119
}
120
+
```
121
+
122
+
A `Type` from the parser syntax can then be translated to a `CType` like this (where this function definition could for example be put into `TypeChecker`):
Now, we'll get back to the functions we left unexplained above, `extractSignatures` and `checkFunDefs`. As the name suggests, `extractSignatures` should pass through all function definitions and extract their signatures, which we will need to check the actual function defintions. This is a possible implementaion:
135
+
136
+
```java
137
+
void extractSignatures(List<DFun> funDefs) {
138
+
for(var def : funDefs) {
139
+
List<CType> argTypes = def.listarg_.stream()
140
+
.map(arg -> typeFrom(((ADecl) arg).type_))
141
+
.toList();
142
+
Signature signature = new Signature(typeFrom(def.type_), argTypes);
143
+
if(signatures.put(def.id_, signature) != null) {
144
+
throw new TypeException("Signature already defined for " + def.id_);
145
+
};
146
+
}
147
+
}
148
+
```
149
+
150
+
One thing that could be added here (or somewhere else) is, that there should be exactly one function called "main" which returns `int` and has no arguments. We will only give a sketch of an implementation of `checkFunDefs`. One thing to take care of is dealing with contexts in the right way, which needs some data structure like a stack or list. This is not explained in detail here:
0 commit comments