feat: better recursive descent
This commit is contained in:
@@ -1,80 +1,132 @@
|
||||
package saccharine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
"git.maximhutz.com/max/lambda/pkg/saccharine/ast"
|
||||
"git.maximhutz.com/max/lambda/pkg/saccharine/token"
|
||||
)
|
||||
|
||||
func isVariableToken(t Token) bool {
|
||||
return t.Type == TokenVariable
|
||||
type TokenIterator = iterator.Iterator[token.Token]
|
||||
|
||||
func parseToken(i *TokenIterator, expected token.Type) (*token.Token, error) {
|
||||
i2 := i.Copy()
|
||||
|
||||
if tok, err := i2.Next(); err != nil {
|
||||
return nil, err
|
||||
} else if tok.Type != expected {
|
||||
return nil, fmt.Errorf("expected token, got %v'", tok.Value)
|
||||
} else {
|
||||
i.Sync(i2)
|
||||
return &tok, nil
|
||||
}
|
||||
}
|
||||
|
||||
func ParseExpression(i *iterator.Iterator[Token]) (Node, error) {
|
||||
token, err := i.Pop()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get next token: %w", err)
|
||||
func parseExpression(i *TokenIterator) (ast.Expression, error) {
|
||||
slog.Info("attempt exp", "index", i.Index())
|
||||
if abs, absErr := parseAbstraction(i); absErr == nil {
|
||||
slog.Info("got exp")
|
||||
return abs, nil
|
||||
} else if atm, atmErr := parseApplication(i); atmErr == nil {
|
||||
slog.Info("got exp")
|
||||
return atm, nil
|
||||
} else if app, appErr := parseAtom(i); appErr == nil {
|
||||
slog.Info("got exp")
|
||||
return app, nil
|
||||
} else {
|
||||
slog.Info("fail exp")
|
||||
return nil, errors.Join(absErr, appErr, atmErr)
|
||||
}
|
||||
}
|
||||
|
||||
func parseParameters(i *TokenIterator) ([]string, error) {
|
||||
slog.Info("parse param")
|
||||
i2 := i.Copy()
|
||||
variables := []string{}
|
||||
|
||||
for {
|
||||
if tok, err := parseToken(i2, token.Atom); err != nil {
|
||||
break
|
||||
} else {
|
||||
variables = append(variables, tok.Value)
|
||||
}
|
||||
}
|
||||
|
||||
switch token.Type {
|
||||
case TokenVariable:
|
||||
return NewVariable(token.Value), nil
|
||||
case TokenDot:
|
||||
return nil, fmt.Errorf("token '.' found without a corresponding slash (column %d)", token.Index)
|
||||
case TokenSlash:
|
||||
tokens := i.PopWhile(isVariableToken)
|
||||
variables := []string{}
|
||||
slog.Info("got exp")
|
||||
i.Sync(i2)
|
||||
return variables, nil
|
||||
}
|
||||
|
||||
for _, token := range tokens {
|
||||
variables = append(variables, token.Value)
|
||||
}
|
||||
func parseAbstraction(i *TokenIterator) (*ast.Abstraction, error) {
|
||||
slog.Info("attempt abs")
|
||||
i2 := i.Copy()
|
||||
|
||||
if dot, dotErr := i.Pop(); dotErr != nil {
|
||||
return nil, fmt.Errorf("could not find parameter terminator: %w", dotErr)
|
||||
} else if dot.Type != TokenDot {
|
||||
return nil, fmt.Errorf("expected '.', got '%v' (column %d)", dot.Value, dot.Index)
|
||||
}
|
||||
if _, err := parseToken(i2, token.Slash); err != nil {
|
||||
slog.Info("fail abs")
|
||||
return nil, err
|
||||
} else if parameters, err := parseParameters(i2); err != nil {
|
||||
slog.Info("fail abs")
|
||||
return nil, err
|
||||
} else if _, err = parseToken(i2, token.Dot); err != nil {
|
||||
slog.Info("fail abs")
|
||||
return nil, err
|
||||
} else if body, err := parseExpression(i2); err != nil {
|
||||
slog.Info("fail abs")
|
||||
return nil, err
|
||||
} else {
|
||||
slog.Info("got abs")
|
||||
i.Sync(i2)
|
||||
return ast.NewAbstraction(parameters, body), nil
|
||||
}
|
||||
}
|
||||
|
||||
body, bodyErr := ParseExpression(i)
|
||||
if bodyErr != nil {
|
||||
return nil, fmt.Errorf("could not parse function body: %w", bodyErr)
|
||||
}
|
||||
func parseApplication(i *TokenIterator) (*ast.Application, error) {
|
||||
slog.Info("attempt app")
|
||||
i2 := i.Copy()
|
||||
expressions := []ast.Expression{}
|
||||
|
||||
return NewAbstraction(variables, body), nil
|
||||
case TokenOpenParen:
|
||||
fn, fnErr := ParseExpression(i)
|
||||
if fnErr != nil {
|
||||
return nil, fmt.Errorf("could not parse call function: %w", fnErr)
|
||||
}
|
||||
|
||||
args := []Node{}
|
||||
|
||||
for {
|
||||
if next, nextErr := i.Peek(); nextErr == nil && next.Type == TokenCloseParen {
|
||||
break
|
||||
}
|
||||
|
||||
arg, argErr := ParseExpression(i)
|
||||
if argErr != nil {
|
||||
return nil, fmt.Errorf("could not parse call argument: %w", argErr)
|
||||
}
|
||||
|
||||
args = append(args, arg)
|
||||
}
|
||||
|
||||
closing, closingErr := i.Pop()
|
||||
if closingErr != nil {
|
||||
return nil, fmt.Errorf("could not parse call terminating parenthesis: %w", closingErr)
|
||||
} else if closing.Type != TokenCloseParen {
|
||||
return nil, fmt.Errorf("expected call terminating parenthesis, got '%v' (column %v)", closing.Value, closing.Index)
|
||||
}
|
||||
|
||||
return NewApplication(fn, args), nil
|
||||
if _, err := parseToken(i2, token.OpenParen); err != nil {
|
||||
slog.Info("fail app")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unexpected token '%v' (column %d)", token.Value, token.Index)
|
||||
for {
|
||||
if exp, err := parseExpression(i2); err != nil {
|
||||
break
|
||||
} else {
|
||||
expressions = append(expressions, exp)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := parseToken(i2, token.CloseParen); err != nil {
|
||||
slog.Info("fail app")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(expressions) == 0 {
|
||||
slog.Info("fail app")
|
||||
return nil, fmt.Errorf("application has no arguments")
|
||||
}
|
||||
|
||||
slog.Info("got app")
|
||||
i.Sync(i2)
|
||||
return ast.NewApplication(expressions[0], expressions[1:]), nil
|
||||
}
|
||||
|
||||
func GetTree(tokens []Token) (Node, error) {
|
||||
return ParseExpression(iterator.New(tokens))
|
||||
func parseAtom(i *TokenIterator) (*ast.Atom, error) {
|
||||
slog.Info("attempt atm")
|
||||
if tok, err := parseToken(i, token.Atom); err != nil {
|
||||
slog.Info("fail atm")
|
||||
return nil, err
|
||||
} else {
|
||||
slog.Info("got atm")
|
||||
return ast.NewAtom(tok.Value), nil
|
||||
}
|
||||
}
|
||||
|
||||
func Parse(i *TokenIterator) (ast.Expression, error) {
|
||||
return parseExpression(i)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user