Added define operator

This commit is contained in:
2024-06-15 11:36:57 +02:00
parent 65791ea5a1
commit cf696a6f10
9 changed files with 51 additions and 51 deletions

View File

@ -25,6 +25,9 @@ const (
// Number represents a series of numerical characters.
Number
// Define represents the assignment operator `:=` for a new variable.
Define
// Operator represents a mathematical operator.
Operator
@ -63,6 +66,7 @@ func (kind Kind) String() string {
"Keyword",
"String",
"Number",
"Define",
"Operator",
"Separator",
"Comment",

View File

@ -13,7 +13,7 @@ func (list List) String() string {
var last Token
for _, t := range list {
if last.Kind == Keyword || last.Kind == Separator || last.Kind == Operator || t.Kind == Operator {
if last.Kind == Keyword || last.Kind == Separator || last.Kind == Define || t.Kind == Define {
builder.WriteByte(' ')
}

View File

@ -1,5 +1,7 @@
package token
import "bytes"
// Pre-allocate these byte buffers so we can re-use them
// instead of allocating a new buffer every time.
var (
@ -10,6 +12,7 @@ var (
arrayStartBytes = []byte{'['}
arrayEndBytes = []byte{']'}
separatorBytes = []byte{','}
defineBytes = []byte{':', '='}
newLineBytes = []byte{'\n'}
)
@ -38,12 +41,7 @@ func Tokenize(buffer []byte) List {
i++
}
tokens = append(tokens, Token{
String,
start,
buffer[start:end],
})
tokens = append(tokens, Token{String, start, buffer[start:end]})
continue
// Parentheses start
@ -88,11 +86,7 @@ func Tokenize(buffer []byte) List {
i++
}
token := Token{
Identifier,
position,
buffer[position:i],
}
token := Token{Identifier, position, buffer[position:i]}
if Keywords[string(token.Bytes)] {
token.Kind = Keyword
@ -111,12 +105,7 @@ func Tokenize(buffer []byte) List {
i++
}
tokens = append(tokens, Token{
Number,
position,
buffer[position:i],
})
tokens = append(tokens, Token{Number, position, buffer[position:i]})
continue
}
@ -129,12 +118,12 @@ func Tokenize(buffer []byte) List {
i++
}
tokens = append(tokens, Token{
Operator,
position,
buffer[position:i],
})
if bytes.Equal(buffer[position:i], defineBytes) {
tokens = append(tokens, Token{Define, position, defineBytes})
continue
}
tokens = append(tokens, Token{Operator, position, buffer[position:i]})
continue
}
}