wip: new folder structure, overhaul language
This commit is contained in:
41
pkg/saccharine/ast.go
Normal file
41
pkg/saccharine/ast.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package saccharine
|
||||
|
||||
type Node interface {
|
||||
IsNode()
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
type Abstraction struct {
|
||||
Parameters []string
|
||||
Body Node
|
||||
}
|
||||
|
||||
type Application struct {
|
||||
Abstraction Node
|
||||
Arguments []Node
|
||||
}
|
||||
|
||||
type Variable struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
func NewAbstraction(parameter []string, body Node) *Abstraction {
|
||||
return &Abstraction{Parameters: parameter, Body: body}
|
||||
}
|
||||
|
||||
func NewApplication(abstraction Node, arguments []Node) *Application {
|
||||
return &Application{Abstraction: abstraction, Arguments: arguments}
|
||||
}
|
||||
|
||||
func NewVariable(name string) *Variable {
|
||||
return &Variable{Name: name}
|
||||
}
|
||||
|
||||
/** ------------------------------------------------------------------------- */
|
||||
|
||||
func (_ Abstraction) IsNode() {}
|
||||
func (_ Application) IsNode() {}
|
||||
func (_ Variable) IsNode() {}
|
||||
80
pkg/saccharine/parser.go
Normal file
80
pkg/saccharine/parser.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package saccharine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
)
|
||||
|
||||
func isVariableToken(t Token) bool {
|
||||
return t.Type == TokenVariable
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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{}
|
||||
|
||||
for _, token := range tokens {
|
||||
variables = append(variables, token.Value)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
body, bodyErr := ParseExpression(i)
|
||||
if bodyErr != nil {
|
||||
return nil, fmt.Errorf("could not parse function body: %w", bodyErr)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unexpected token '%v' (column %d)", token.Value, token.Index)
|
||||
}
|
||||
|
||||
func GetTree(tokens []Token) (Node, error) {
|
||||
return ParseExpression(iterator.New(tokens))
|
||||
}
|
||||
27
pkg/saccharine/token.go
Normal file
27
pkg/saccharine/token.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package saccharine
|
||||
|
||||
// All tokens in the pseudo-lambda language.
|
||||
type TokenType int
|
||||
|
||||
const (
|
||||
// Denotes the '(' token.
|
||||
TokenOpenParen TokenType = iota
|
||||
// Denotes the ')' token.
|
||||
TokenCloseParen
|
||||
// Denotes an alpha-numeric variable.
|
||||
TokenVariable
|
||||
// Denotes the '/' token.
|
||||
TokenSlash
|
||||
// Denotes the '.' token.
|
||||
TokenDot
|
||||
)
|
||||
|
||||
// A representation of a token in source code.
|
||||
type Token struct {
|
||||
// Where the token begins in the source text.
|
||||
Index int
|
||||
// What type the token is.
|
||||
Type TokenType
|
||||
// The value of the token.
|
||||
Value string
|
||||
}
|
||||
72
pkg/saccharine/tokenizer.go
Normal file
72
pkg/saccharine/tokenizer.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package saccharine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"unicode"
|
||||
|
||||
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||
)
|
||||
|
||||
// isVariables determines whether a rune can be a valid variable.
|
||||
func isVariable(r rune) bool {
|
||||
return unicode.IsLetter(r) || unicode.IsNumber(r)
|
||||
}
|
||||
|
||||
// Pulls the next token from an iterator over runes. If it cannot, it will
|
||||
// return nil. If an error occurs, it will return that.
|
||||
func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
||||
index := i.Index()
|
||||
|
||||
if i.IsDone() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
letter, err := i.Pop()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot produce next token: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case letter == '(':
|
||||
// The opening deliminator of an application.
|
||||
return &Token{Type: TokenOpenParen, Index: index, Value: string(letter)}, nil
|
||||
case letter == ')':
|
||||
// The terminator of an application.
|
||||
return &Token{Type: TokenCloseParen, Index: index, Value: string(letter)}, nil
|
||||
case letter == '.':
|
||||
// The terminator of the parameters in an abstraction.
|
||||
return &Token{Type: TokenDot, Index: index, Value: string(letter)}, nil
|
||||
case letter == '\\':
|
||||
// The opening deliminator of an abstraction.
|
||||
return &Token{Type: TokenSlash, Index: index, Value: string(letter)}, nil
|
||||
case unicode.IsSpace(letter):
|
||||
// If there is a space character, ignore it.
|
||||
return nil, nil
|
||||
case isVariable(letter):
|
||||
rest := i.PopWhile(isVariable)
|
||||
atom := string(append([]rune{letter}, rest...))
|
||||
|
||||
return &Token{Index: index, Type: TokenVariable, Value: atom}, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("unknown character '%v'", letter)
|
||||
}
|
||||
|
||||
// Parses a list of runes into tokens. All error encountered are returned, as well.
|
||||
func GetTokens(input []rune) ([]Token, error) {
|
||||
i := iterator.New(input)
|
||||
tokens := []Token{}
|
||||
errorList := []error{}
|
||||
|
||||
for !i.IsDone() {
|
||||
token, err := getToken(i)
|
||||
if err != nil {
|
||||
errorList = append(errorList, err)
|
||||
} else if token != nil {
|
||||
tokens = append(tokens, *token)
|
||||
}
|
||||
}
|
||||
|
||||
return tokens, errors.Join(errorList...)
|
||||
}
|
||||
Reference in New Issue
Block a user