Skip to content

Commit 207ac16

Browse files
committed
rt: Fixed the clause general case and throw an error if no pattern matches
1 parent 0e526fc commit 207ac16

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/main/java/org/piccode/rt/PiccodeClosure.java

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import org.piccode.ast.Arg;
99
import org.piccode.ast.Ast;
1010
import org.piccode.ast.ClauseAst;
11+
import org.piccode.ast.ErrorNodeExpr;
1112
import org.piccode.ast.FunctionAst;
1213
import org.piccode.ast.TupleAst;
1314
import org.piccode.ast.WhenAst;
1415
import org.piccode.ast.WhenCase;
16+
import org.piccode.ast.IdentifierAst;
1517

1618
/**
1719
*
@@ -210,9 +212,20 @@ private void createWhenExpression(Integer frame) {
210212
cases.add(when);
211213
}
212214

215+
213216
var args = clause.getArgs();
214217
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));
216229
var match = new WhenAst(cond, cases, null);
217230
// System.out.println("Expr: " + match + " " + clauses);
218231
match.file = file;
@@ -255,21 +268,42 @@ private void computeStableClause() {
255268
} else if (top instanceof Arg arg) {
256269
newParams.add(index, arg);
257270
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++;
258278
}
259279
}
260280
}
261281

262282
if (!clauses.isEmpty()) {
283+
size = newParams.size();
263284
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+
}
269292
}
270-
}
271-
clauses.add(new ClauseAst(newParams, body));
293+
}
294+
295+
clauses.add(new ClauseAst(params, body));
272296
params = newParams;
273297
}
274298

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+
275309
}

0 commit comments

Comments
 (0)