|
8 | 8 | import org.piccode.ast.Arg; |
9 | 9 | import org.piccode.ast.Ast; |
10 | 10 | import org.piccode.ast.ClauseAst; |
| 11 | +import org.piccode.ast.ErrorNodeExpr; |
11 | 12 | import org.piccode.ast.FunctionAst; |
12 | 13 | import org.piccode.ast.TupleAst; |
13 | 14 | import org.piccode.ast.WhenAst; |
14 | 15 | import org.piccode.ast.WhenCase; |
| 16 | +import org.piccode.ast.IdentifierAst; |
15 | 17 |
|
16 | 18 | /** |
17 | 19 | * |
@@ -210,9 +212,20 @@ private void createWhenExpression(Integer frame) { |
210 | 212 | cases.add(when); |
211 | 213 | } |
212 | 214 |
|
| 215 | + |
213 | 216 | var args = clause.getArgs(); |
214 | 217 | var cond = args.size() == 1 ? args.getFirst() : new TupleAst(args); |
215 | | - cases.add(new WhenCase(List.of(cond), clause.body)); |
| 218 | + |
| 219 | + var errNode = new ErrorNodeExpr("Inexhaustive when expression: no pattern matched: when " + cond + " { ... }"); |
| 220 | + errNode.file = file; |
| 221 | + errNode.line = line; |
| 222 | + errNode.column = column; |
| 223 | + |
| 224 | + var generalCaseBody = isMatched(clause.body, cases) |
| 225 | + ? errNode |
| 226 | + : clause.body; |
| 227 | + |
| 228 | + cases.add(new WhenCase(List.of(cond), generalCaseBody)); |
216 | 229 | var match = new WhenAst(cond, cases, null); |
217 | 230 | // System.out.println("Expr: " + match + " " + clauses); |
218 | 231 | match.file = file; |
@@ -255,21 +268,42 @@ private void computeStableClause() { |
255 | 268 | } else if (top instanceof Arg arg) { |
256 | 269 | newParams.add(index, arg); |
257 | 270 | index++; |
| 271 | + } else { |
| 272 | + var arg = new Arg("$_PLACEHOLDER_ID_" + index + "__$"); |
| 273 | + arg.file = file; |
| 274 | + arg.line = line; |
| 275 | + arg.column = column; |
| 276 | + newParams.add(index, arg); |
| 277 | + index++; |
258 | 278 | } |
259 | 279 | } |
260 | 280 | } |
261 | 281 |
|
262 | 282 | if (!clauses.isEmpty()) { |
| 283 | + size = newParams.size(); |
263 | 284 | if (newParams.size() < size) { |
264 | | - var err = new PiccodeException(file, line, column, "Cannot get a parameter list from clause declrations for the declared function. Only able to assemble " + newParams.size() + " args out of " + size); |
265 | | - |
266 | | - var note = new PiccodeException(file, line, column, "Clause list: " + clauses + " only results to expression " + newParams + " vs the parameters " + params); |
267 | | - err.addNote(note); |
268 | | - throw err; |
| 285 | + for (int i = 0; i < size; i++) { |
| 286 | + var arg = new Arg("$_PLACEHOLDER_ID_" + i + "__$"); |
| 287 | + arg.file = file; |
| 288 | + arg.line = line; |
| 289 | + arg.column = column; |
| 290 | + newParams.addLast(arg); |
| 291 | + } |
269 | 292 | } |
270 | | - } |
271 | | - clauses.add(new ClauseAst(newParams, body)); |
| 293 | + } |
| 294 | + |
| 295 | + clauses.add(new ClauseAst(params, body)); |
272 | 296 | params = newParams; |
273 | 297 | } |
274 | 298 |
|
| 299 | + private boolean isMatched(Ast body, ArrayList<WhenCase> cases) { |
| 300 | + for (var case_ : cases) { |
| 301 | + if (case_.value == body || case_.value.equals(body)) { |
| 302 | + return true; |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + return false; |
| 307 | + } |
| 308 | + |
275 | 309 | } |
0 commit comments