Allow arbitrary patterns as func params
This commit is contained in:
parent
eed952d154
commit
4f606e0e30
2
AST.cs
2
AST.cs
@ -54,7 +54,7 @@ public partial record SimplePattern
|
|||||||
|
|
||||||
public abstract record Binding(Expr Value);
|
public abstract record Binding(Expr Value);
|
||||||
public record VarBinding(Pattern Pattern, Expr Value) : Binding(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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
16
Parser.cs
16
Parser.cs
@ -264,22 +264,10 @@ class Parser
|
|||||||
switch (p)
|
switch (p)
|
||||||
{
|
{
|
||||||
case (SimplePattern(var funcName)) when funcName != null && match(TokenType.LParen):
|
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))
|
while (!check(TokenType.RParen))
|
||||||
{
|
{
|
||||||
Name? paramName;
|
funcParams.Add(pattern());
|
||||||
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.");
|
|
||||||
}
|
|
||||||
if (!match(TokenType.Comma))
|
if (!match(TokenType.Comma))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user