feat: cleaner parsing functions

This commit is contained in:
2025-12-27 20:46:10 -05:00
parent c37e96770f
commit 14fc4b30da
5 changed files with 121 additions and 85 deletions

View File

@@ -1,5 +1,7 @@
package token
import "fmt"
// All tokens in the pseudo-lambda language.
type Type int
@@ -8,12 +10,12 @@ const (
CloseParen // Denotes the ')' token.
OpenBrace // Denotes the '{' token.
CloseBrace // Denotes the '}' token.
End // Denotes the ';' token.
HardBreak // Denotes the ';' token.
Assign // Denotes the ':=' token.
Atom // Denotes an alpha-numeric variable.
Slash // Denotes the '/' token.
Dot // Denotes the '.' token.
Newline // Denotes a new-line.
SoftBreak // Denotes a new-line.
)
// A representation of a token in source code.
@@ -43,8 +45,8 @@ func NewDot(column int) *Token {
return &Token{Type: Dot, Column: column, Value: "."}
}
func NewEnd(column int) *Token {
return &Token{Type: End, Column: column, Value: ";"}
func NewHardBreak(column int) *Token {
return &Token{Type: HardBreak, Column: column, Value: ";"}
}
func NewAssign(column int) *Token {
@@ -59,8 +61,8 @@ func NewAtom(name string, column int) *Token {
return &Token{Type: Atom, Column: column, Value: name}
}
func NewNewline(column int) *Token {
return &Token{Type: Newline, Column: column, Value: "\\n"}
func NewSoftBreak(column int) *Token {
return &Token{Type: SoftBreak, Column: column, Value: "\\n"}
}
func Name(typ Type) string {
@@ -75,10 +77,12 @@ func Name(typ Type) string {
return "."
case Atom:
return "ATOM"
case Newline:
case SoftBreak:
return "\\n"
case HardBreak:
return ";"
default:
return "?"
panic(fmt.Errorf("unknown token type %v", typ))
}
}