This commit is contained in:
silva guimaraes 2025-08-23 20:02:18 -03:00
parent 0cd6040a1f
commit c1f75bb980
6 changed files with 140 additions and 67 deletions

56
lex.go
View file

@ -22,40 +22,44 @@ func (p *position) Position() position {
return *p
}
type Range [2]position
type (
Range [2]position
type token[T comparable] struct {
position
value T
}
token[T comparable] struct {
position
value T
}
type Lexeme interface {
Position() position
Equals(Lexeme) bool
}
Lexeme interface {
Position() position
Equals(Lexeme) bool
}
type Id string
Id string
type Token token[string]
Token token[string]
type IntToken token[int]
IntToken token[int]
type FloatToken token[float64]
FloatToken token[float64]
type Identifier token[Id]
Identifier token[Id]
)
var (
Lparen = &Token{value: "("}
Rparen = &Token{value: ")"}
Space = &Token{value: " "}
Tab = &Token{value: "\t"}
Newline = &Token{value: "\n"}
SingleQuote = &Token{value: "'"}
Let = &Token{value: "let"}
LambdaTok = &Token{value: "lambda"}
If = &Token{value: "if"}
TrueTok = &Token{value: "t"}
NilTok = &Token{value: "nil"}
Lparen = &Token{value: "("}
Rparen = &Token{value: ")"}
LsquareBracket = &Token{value: "["}
RsquareBracket = &Token{value: "]"}
Space = &Token{value: " "}
Tab = &Token{value: "\t"}
Newline = &Token{value: "\n"}
SingleQuote = &Token{value: "'"}
Let = &Token{value: "let"}
LambdaTok = &Token{value: "lambda"}
If = &Token{value: "if"}
TrueTok = &Token{value: "t"}
NilTok = &Token{value: "nil"}
)
func (t Token) String() string {
@ -107,7 +111,7 @@ func (t *Identifier) Equals(a Lexeme) bool {
var matchInt = regexp.MustCompile(`-?\d+`)
var matchFloat = regexp.MustCompile(`-?\d+\.\d*`)
var matchIdentifier = regexp.MustCompile(`[^\'() ]+`)
var matchIdentifier = regexp.MustCompile(`[^\'() \[\]]+`)
func lex(source string) (*Input, error) {
var (