This commit is contained in:
silva guimaraes 2025-05-25 21:56:22 -03:00
parent 7c40723537
commit 8f5b16878f
4 changed files with 316 additions and 144 deletions

30
lex.go
View file

@ -39,6 +39,8 @@ type Token token[string]
type Int token[int]
type Float token[float64]
type Identifier token[Id]
var (
@ -84,9 +86,8 @@ func (t *Token) Equals(a Lexeme) bool {
}
}
func (t *Int) Equals(a Lexeme) bool {
return false
}
func (t *Int) Equals(a Lexeme) bool { return false }
func (t *Float) Equals(a Lexeme) bool { return false }
func (t *Identifier) Equals(a Lexeme) bool {
switch x := a.(type) {
@ -97,17 +98,18 @@ func (t *Identifier) Equals(a Lexeme) bool {
}
}
var matchNumbers = regexp.MustCompile(`-?\d+`)
var matchInt = regexp.MustCompile(`-?\d+`)
var matchFloat = regexp.MustCompile(`-?\d+\.\d*`)
var matchIdentifier = regexp.MustCompile(`[^\'() ]+`)
func lex(source string) (*Input, error) {
var (
pos, line, column int
pos, line, column int = 1, 1, 1
tokens []Lexeme
)
outer:
for len(source) > 0 {
p := position{pos: pos, line: line, column: column, filepath: "sourceinput"}
p := position{pos: pos, line: line, column: column, filepath: "stdin"}
for _, try := range knownTokens {
if !strings.HasPrefix(source, try.value) {
continue
@ -130,7 +132,21 @@ outer:
}
continue outer
}
index := matchNumbers.FindStringSubmatchIndex(source)
index := matchFloat.FindStringSubmatchIndex(source)
if index != nil && index[0] == 0 {
a := source[index[0]:index[1]]
conv, err := strconv.ParseFloat(a, 64)
if err == nil {
var i = &Float{position: p, value: conv}
tokens = append(tokens, i)
l := len(a)
source = source[l:]
column += l
pos += l
continue outer
}
}
index = matchInt.FindStringSubmatchIndex(source)
if index != nil && index[0] == 0 {
a := source[index[0]:index[1]]
conv, err := strconv.Atoi(a)