feat: parser
This commit is contained in:
@@ -6,19 +6,24 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/pkg/cli"
|
"git.maximhutz.com/max/lambda/pkg/cli"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/parser"
|
||||||
"git.maximhutz.com/max/lambda/pkg/tokenizer"
|
"git.maximhutz.com/max/lambda/pkg/tokenizer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
slog.Info("Using program arguments.", "args", os.Args)
|
slog.Info("Using program arguments.", "args", os.Args)
|
||||||
|
|
||||||
options, err := cli.ParseOptions(os.Args[1:])
|
options, err := cli.ParseOptions(os.Args[1:])
|
||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
|
|
||||||
slog.Info("Parsed CLI options.", "options", options)
|
slog.Info("Parsed CLI options.", "options", options)
|
||||||
|
|
||||||
tokens, fails := tokenizer.GetTokens([]rune(options.Input))
|
tokens, fails := tokenizer.GetTokens([]rune(options.Input))
|
||||||
if len(fails) > 0 {
|
if len(fails) > 0 {
|
||||||
cli.HandleError(errors.Join(fails...))
|
cli.HandleError(errors.Join(fails...))
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("Parsed tokens.", "tokens", tokens)
|
slog.Info("Parsed tokens.", "tokens", tokens)
|
||||||
|
|
||||||
|
ast, err := parser.GetTree(tokens)
|
||||||
|
cli.HandleError(err)
|
||||||
|
slog.Info("Parsed syntax tree.", "tree", ast)
|
||||||
}
|
}
|
||||||
|
|||||||
23
pkg/lambda/expression.go
Normal file
23
pkg/lambda/expression.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package lambda
|
||||||
|
|
||||||
|
type Expression interface {
|
||||||
|
isExpression()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Function struct {
|
||||||
|
Parameter string
|
||||||
|
Body Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
type Call struct {
|
||||||
|
Function Expression
|
||||||
|
Argument Expression
|
||||||
|
}
|
||||||
|
|
||||||
|
type Atom struct {
|
||||||
|
Value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_ Function) isExpression() {}
|
||||||
|
func (_ Call) isExpression() {}
|
||||||
|
func (_ Atom) isExpression() {}
|
||||||
72
pkg/parser/parser.go
Normal file
72
pkg/parser/parser.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/iterator"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/tokenizer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseExpression(i *iterator.Iterator[tokenizer.Token]) (lambda.Expression, error) {
|
||||||
|
token, err := i.Next()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Could not get next token: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch token.Type {
|
||||||
|
case tokenizer.TokenAtom:
|
||||||
|
return lambda.Atom{Value: token.Value}, nil
|
||||||
|
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.TokenAtom {
|
||||||
|
return nil, fmt.Errorf("Expected function parameter, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, body_err := ParseExpression(i)
|
||||||
|
if body_err != nil {
|
||||||
|
return nil, fmt.Errorf("Could not parse function body: %w", body_err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lambda.Function{Parameter: atom.Value, Body: body}, 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
close, close_err := i.Next()
|
||||||
|
if close_err != nil {
|
||||||
|
return nil, fmt.Errorf("Could not parse call terminating parenthesis: %w", close_err)
|
||||||
|
} else if close.Type != tokenizer.TokenCloseParen {
|
||||||
|
return nil, fmt.Errorf("Expected call terminating parenthesis, got '%v' (column %v).", close.Value, close.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lambda.Call{Function: fn, Argument: arg}, nil
|
||||||
|
case tokenizer.TokenCloseParen:
|
||||||
|
return nil, fmt.Errorf("Token ')' found without a corresponding openning parenthesis (column %d).", token.Index)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("Unknown token '%v' (column %d).", token.Value, token.Index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTree(tokens []tokenizer.Token) (lambda.Expression, error) {
|
||||||
|
i := iterator.New(tokens)
|
||||||
|
return ParseExpression(&i)
|
||||||
|
}
|
||||||
@@ -21,25 +21,25 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
|||||||
switch letter {
|
switch letter {
|
||||||
case '(':
|
case '(':
|
||||||
return &Token{
|
return &Token{
|
||||||
Type: TokenOpenParen,
|
Type: TokenOpenParen,
|
||||||
Index: i.Index(),
|
Index: i.Index(),
|
||||||
Value: string(letter),
|
Value: string(letter),
|
||||||
}, nil
|
}, nil
|
||||||
case ')':
|
case ')':
|
||||||
return &Token{
|
return &Token{
|
||||||
Type: TokenCloseParen,
|
Type: TokenCloseParen,
|
||||||
Index: i.Index(),
|
Index: i.Index(),
|
||||||
Value: string(letter),
|
Value: string(letter),
|
||||||
}, nil
|
}, nil
|
||||||
case '.':
|
case '.':
|
||||||
return &Token{
|
return &Token{
|
||||||
Type: TokenDot,
|
Type: TokenDot,
|
||||||
Index: i.Index(),
|
Index: i.Index(),
|
||||||
Value: string(letter),
|
Value: string(letter),
|
||||||
}, nil
|
}, nil
|
||||||
case '\\':
|
case '\\':
|
||||||
return &Token{
|
return &Token{
|
||||||
Type: TokenSlash,
|
Type: TokenSlash,
|
||||||
Index: i.Index(),
|
Index: i.Index(),
|
||||||
Value: string(letter),
|
Value: string(letter),
|
||||||
}, nil
|
}, nil
|
||||||
@@ -58,19 +58,16 @@ func getToken(i *iterator.Iterator[rune]) (*Token, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pop, err := i.Next()
|
pop, err := i.Peek()
|
||||||
if err != nil {
|
if err != nil || unicode.IsSpace(pop) || unicode.IsPunct(pop) {
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if unicode.IsSpace(pop) {
|
|
||||||
return &Token{
|
return &Token{
|
||||||
Index: index,
|
Index: index,
|
||||||
Type: TokenAtom,
|
Type: TokenAtom,
|
||||||
Value: atom,
|
Value: atom,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i.Next()
|
||||||
atom += string(pop)
|
atom += string(pop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,4 +87,4 @@ func GetTokens(input []rune) ([]Token, []error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return tokens, errors
|
return tokens, errors
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user