feat: better error messages

This commit is contained in:
2025-12-27 02:39:56 -05:00
parent 884180de92
commit 1896cd652d
6 changed files with 70 additions and 51 deletions

View File

@@ -5,4 +5,7 @@ it:
@ chmod +x ${BINARY_NAME} @ chmod +x ${BINARY_NAME}
ex: it ex: it
@ ./lambda.exe - < ./samples/simple.txt @ ./lambda.exe - < ./samples/simple.txt
v: it
@ ./lambda.exe -v - < ./samples/simple.txt

View File

@@ -1,12 +1,12 @@
package saccharine package saccharine
import ( import (
"errors"
"fmt" "fmt"
"git.maximhutz.com/max/lambda/pkg/iterator" "git.maximhutz.com/max/lambda/pkg/iterator"
"git.maximhutz.com/max/lambda/pkg/saccharine/ast" "git.maximhutz.com/max/lambda/pkg/saccharine/ast"
"git.maximhutz.com/max/lambda/pkg/saccharine/token" "git.maximhutz.com/max/lambda/pkg/saccharine/token"
"git.maximhutz.com/max/lambda/pkg/trace"
) )
type TokenIterator = iterator.Iterator[token.Token] type TokenIterator = iterator.Iterator[token.Token]
@@ -25,15 +25,22 @@ func parseToken(i *TokenIterator, expected token.Type) (*token.Token, error) {
} }
func parseExpression(i *TokenIterator) (ast.Expression, error) { func parseExpression(i *TokenIterator) (ast.Expression, error) {
if abs, absErr := parseAbstraction(i); absErr == nil { var err error
return abs, nil var exp ast.Expression
} else if atm, atmErr := parseApplication(i); atmErr == nil { peek := i.MustGet()
return atm, nil
} else if app, appErr := parseAtom(i); appErr == nil { switch peek.Type {
return app, nil case token.OpenParen:
} else { exp, err = parseApplication(i)
return nil, errors.Join(absErr, appErr, atmErr) case token.Slash:
exp, err = parseAbstraction(i)
case token.Atom:
exp, err = parseAtom(i)
default:
return nil, fmt.Errorf("expected expression, got '%v' (col %d)", peek.Value, peek.Index)
} }
return exp, err
} }
func parseParameters(i *TokenIterator) ([]string, error) { func parseParameters(i *TokenIterator) ([]string, error) {
@@ -56,11 +63,11 @@ func parseAbstraction(i *TokenIterator) (*ast.Abstraction, error) {
i2 := i.Copy() i2 := i.Copy()
if _, err := parseToken(i2, token.Slash); err != nil { if _, err := parseToken(i2, token.Slash); err != nil {
return nil, fmt.Errorf("no function slash (col %d): %w", i2.MustGet().Index, err) return nil, trace.WrapError(fmt.Errorf("no function slash (col %d)", i2.MustGet().Index), err)
} else if parameters, err := parseParameters(i2); err != nil { } else if parameters, err := parseParameters(i2); err != nil {
return nil, err return nil, err
} else if _, err = parseToken(i2, token.Dot); err != nil { } else if _, err = parseToken(i2, token.Dot); err != nil {
return nil, fmt.Errorf("no function dot (col %d): %w", i2.MustGet().Index, err) return nil, trace.WrapError(fmt.Errorf("no function dot (col %d)", i2.MustGet().Index), err)
} else if body, err := parseExpression(i2); err != nil { } else if body, err := parseExpression(i2); err != nil {
return nil, err return nil, err
} else { } else {
@@ -74,11 +81,14 @@ func parseApplication(i *TokenIterator) (*ast.Application, error) {
expressions := []ast.Expression{} expressions := []ast.Expression{}
if _, err := parseToken(i2, token.OpenParen); err != nil { if _, err := parseToken(i2, token.OpenParen); err != nil {
return nil, fmt.Errorf("no openning brackets (col %d): %w", i2.MustGet().Index, err) return nil, trace.WrapError(fmt.Errorf("no openning brackets (col %d)", i2.MustGet().Index), err)
} }
for { for {
if exp, err := parseExpression(i2); err != nil { if exp, err := parseExpression(i2); err != nil {
if len(expressions) == 0 {
return nil, trace.WrapError(fmt.Errorf("application has no arguments"), err)
}
break break
} else { } else {
expressions = append(expressions, exp) expressions = append(expressions, exp)
@@ -86,11 +96,7 @@ func parseApplication(i *TokenIterator) (*ast.Application, error) {
} }
if _, err := parseToken(i2, token.CloseParen); err != nil { if _, err := parseToken(i2, token.CloseParen); err != nil {
return nil, fmt.Errorf("no closing brackets (col %d): %w", i2.MustGet().Index, err) return nil, trace.WrapError(fmt.Errorf("no closing brackets (col %d)", i2.MustGet().Index), err)
}
if len(expressions) == 0 {
return nil, fmt.Errorf("application has no arguments")
} }
i.Sync(i2) i.Sync(i2)
@@ -99,7 +105,7 @@ func parseApplication(i *TokenIterator) (*ast.Application, error) {
func parseAtom(i *TokenIterator) (*ast.Atom, error) { func parseAtom(i *TokenIterator) (*ast.Atom, error) {
if tok, err := parseToken(i, token.Atom); err != nil { if tok, err := parseToken(i, token.Atom); err != nil {
return nil, fmt.Errorf("no variable (col %d): %w", i.Index(), err) return nil, trace.WrapError(fmt.Errorf("no variable (col %d)", i.Index()), err)
} else { } else {
return ast.NewAtom(tok.Value), nil return ast.NewAtom(tok.Value), nil
} }

View File

@@ -4,26 +4,19 @@ package token
type Type int type Type int
const ( const (
// Denotes the '(' token. OpenParen Type = iota // Denotes the '(' token.
OpenParen Type = iota CloseParen // Denotes the ')' token.
// Denotes the ')' token. Atom // Denotes an alpha-numeric variable.
CloseParen Slash // Denotes the '/' token.
// Denotes an alpha-numeric variable. Dot // Denotes the '.' token.
Atom Newline // Denotes a new-line.
// Denotes the '/' token.
Slash
// Denotes the '.' token.
Dot
) )
// A representation of a token in source code. // A representation of a token in source code.
type Token struct { type Token struct {
// Where the token begins in the source text. Index int // Where the token begins in the source text.
Index int Type Type // What type the token is.
// What type the token is. Value string // The value of the token.
Type Type
// The value of the token.
Value string
} }
func NewOpenParen(index int) *Token { func NewOpenParen(index int) *Token {
@@ -46,6 +39,10 @@ func NewAtom(name string, index int) *Token {
return &Token{Type: Atom, Index: index, Value: name} return &Token{Type: Atom, Index: index, Value: name}
} }
func NewNewline(index int) *Token {
return &Token{Type: Newline, Index: index, Value: "\\n"}
}
func Name(typ Type) string { func Name(typ Type) string {
switch typ { switch typ {
case OpenParen: case OpenParen:
@@ -58,6 +55,8 @@ func Name(typ Type) string {
return "." return "."
case Atom: case Atom:
return "ATOM" return "ATOM"
case Newline:
return "\\n"
default: default:
return "?" return "?"
} }

View File

@@ -7,6 +7,7 @@ import (
"git.maximhutz.com/max/lambda/pkg/iterator" "git.maximhutz.com/max/lambda/pkg/iterator"
"git.maximhutz.com/max/lambda/pkg/saccharine/token" "git.maximhutz.com/max/lambda/pkg/saccharine/token"
"git.maximhutz.com/max/lambda/pkg/trace"
) )
// isVariables determines whether a rune can be a valid variable. // isVariables determines whether a rune can be a valid variable.
@@ -38,7 +39,7 @@ func getToken(i *iterator.Iterator[rune]) (*token.Token, error) {
letter, err := i.Next() letter, err := i.Next()
if err != nil { if err != nil {
return nil, fmt.Errorf("cannot produce next token: %w", err) return nil, trace.WrapError(fmt.Errorf("cannot produce next token"), err)
} }
switch { switch {
@@ -50,6 +51,8 @@ func getToken(i *iterator.Iterator[rune]) (*token.Token, error) {
return token.NewDot(index), nil return token.NewDot(index), nil
case letter == '\\': case letter == '\\':
return token.NewSlash(index), nil return token.NewSlash(index), nil
case letter == '\n':
return token.NewNewline(index), nil
case unicode.IsSpace(letter): case unicode.IsSpace(letter):
return nil, nil return nil, nil
case isVariable(letter): case isVariable(letter):

23
pkg/trace/trace.go Normal file
View File

@@ -0,0 +1,23 @@
package trace
import (
"errors"
"strings"
)
func Indent(s string, size int) string {
lines := strings.Lines(s)
indent := strings.Repeat(" ", size)
indented := ""
for line := range lines {
indented += indent + line
}
return indented
}
func WrapError(parent error, child error) error {
childErrString := Indent(child.Error(), 4)
return errors.New(parent.Error() + "\n" + childErrString)
}

View File

@@ -1,16 +1 @@
(\0. (\0.
(\inc.
(\add.
(\mult.
(\exp.
(exp (inc (inc (inc (inc 0)))) (inc (inc (inc (inc (inc 0))))))
\n m.(m n)
)
\m n f.(m (n f))
))
\n m.(m inc n)
)
\n f x.(f (n f x))
)
\f x.((((((x))))))
)