fix: unbound substitutions, explanation tag

This commit is contained in:
2025-12-25 01:55:46 -05:00
parent a56ec808ec
commit 99703c2587
6 changed files with 82 additions and 23 deletions

View File

@@ -20,18 +20,23 @@ func ParseExpression(i *iterator.Iterator[tokenizer.Token]) (lambda.Expression,
case tokenizer.TokenDot:
return nil, fmt.Errorf("Token '.' found without a corresponding slash (column %d).", token.Index)
case tokenizer.TokenSlash:
atom, atom_err := i.Next()
if atom_err != nil {
return nil, fmt.Errorf("Could not find parameter of function: %w", atom_err)
} else if atom.Type != tokenizer.TokenVariable {
return nil, fmt.Errorf("Expected function parameter, got '%v' (column %d).", atom.Value, atom.Index)
atoms := []string{}
for {
atom, atom_err := i.Next()
if atom_err != nil {
return nil, fmt.Errorf("Could not find parameter or terminator of function: %w", atom_err)
} else if atom.Type == tokenizer.TokenVariable {
atoms = append(atoms, atom.Value)
} else if atom.Type == tokenizer.TokenDot {
break
} else {
return nil, fmt.Errorf("Expected function parameter or terminator, got '%v' (column %d).", atom.Value, atom.Index)
}
}
dot, dot_err := i.Next()
if dot_err != nil {
return nil, fmt.Errorf("Could not find function parameter terminator: %w", dot_err)
} else if dot.Type != tokenizer.TokenDot {
return nil, fmt.Errorf("Expected function parameter terminator, got '%v' (column %v).", dot.Value, dot.Index)
if len(atoms) == 0 {
return nil, fmt.Errorf("Every function must have atleast one parameter (column %d)", token.Index)
}
body, body_err := ParseExpression(i)
@@ -39,16 +44,32 @@ func ParseExpression(i *iterator.Iterator[tokenizer.Token]) (lambda.Expression,
return nil, fmt.Errorf("Could not parse function body: %w", body_err)
}
return lambda.NewAbstraction(atom.Value, body), nil
// Construction.
result := body
for i := len(atoms) - 1; i >= 0; i-- {
result = lambda.NewAbstraction(atoms[i], result)
}
return result, nil
case tokenizer.TokenOpenParen:
fn, fn_err := ParseExpression(i)
if fn_err != nil {
return nil, fmt.Errorf("Could not parse call function: %w", fn_err)
}
arg, arg_err := ParseExpression(i)
if arg_err != nil {
return nil, fmt.Errorf("Could not parse call argument: %w", arg_err)
args := []lambda.Expression{}
for {
if next, next_err := i.Peek(); next_err == nil && next.Type == tokenizer.TokenCloseParen {
break
}
arg, arg_err := ParseExpression(i)
if arg_err != nil {
return nil, fmt.Errorf("Could not parse call argument: %w", arg_err)
}
args = append(args, arg)
}
close, close_err := i.Next()
@@ -58,7 +79,13 @@ func ParseExpression(i *iterator.Iterator[tokenizer.Token]) (lambda.Expression,
return nil, fmt.Errorf("Expected call terminating parenthesis, got '%v' (column %v).", close.Value, close.Index)
}
return lambda.NewApplication(fn, arg), nil
// Construction.
result := fn
for _, arg := range args {
result = lambda.NewApplication(result, arg)
}
return result, nil
case tokenizer.TokenCloseParen:
return nil, fmt.Errorf("Token ')' found without a corresponding openning parenthesis (column %d).", token.Index)
default: