Allow arbitrary patterns as func params

This commit is contained in:
Brandon Dyck 2023-07-05 10:23:34 -06:00
parent eed952d154
commit 4f606e0e30
2 changed files with 3 additions and 15 deletions

2
AST.cs
View File

@ -54,7 +54,7 @@ public partial record SimplePattern
public abstract record Binding(Expr Value);
public record VarBinding(Pattern Pattern, Expr Value) : Binding(Value);
public record FuncBinding(Name Name, SimplePattern[] Params, Expr Value) : Binding(Value)
public record FuncBinding(Name Name, Pattern[] Params, Expr Value) : Binding(Value)
{
public override string ToString()
{

View File

@ -264,22 +264,10 @@ class Parser
switch (p)
{
case (SimplePattern(var funcName)) when funcName != null && match(TokenType.LParen):
List<SimplePattern> funcParams = new List<SimplePattern>();
List<Pattern> funcParams = new List<Pattern>();
while (!check(TokenType.RParen))
{
Name? paramName;
if (match(TokenType.Blank))
{
funcParams.Add(new(null));
}
else if ((paramName = name()) != null)
{
funcParams.Add(new(paramName));
}
else
{
throw error(peek(), "Expect identifier or '_' as function parameter.");
}
funcParams.Add(pattern());
if (!match(TokenType.Comma))
{
break;