Implemented negative numbers

This commit is contained in:
Eduard Urbach 2024-07-27 17:48:03 +02:00
parent 944bacf4e1
commit 6861ae9a90
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 28 additions and 1 deletions

View File

@ -131,7 +131,15 @@ func Tokenize(buffer []byte) List {
i++
}
tokens = append(tokens, Token{Kind: Number, Position: position, Length: Length(i - position)})
last := len(tokens) - 1
if len(tokens) > 0 && tokens[last].Kind == Negate {
tokens[last].Kind = Number
tokens[last].Length = Length(i-position) + 1
} else {
tokens = append(tokens, Token{Kind: Number, Position: position, Length: Length(i - position)})
}
continue
}

View File

@ -166,6 +166,19 @@ func TestNegateAfterOperator(t *testing.T) {
}
}
func TestNegateNumber(t *testing.T) {
tokens := token.Tokenize([]byte(`-1`))
expected := []token.Kind{
token.Number,
token.EOF,
}
for i, kind := range expected {
assert.Equal(t, tokens[i].Kind, kind)
}
}
func TestOperatorAssign(t *testing.T) {
tokens := token.Tokenize([]byte(`a += b -= c *= d /= e &= f |= g ^= h <<= i >>= j`))

View File

@ -0,0 +1,5 @@
main() {
a := -32
b := 64
syscall(60, a + b)
}

View File

@ -42,6 +42,7 @@ var programs = []struct {
{"loop", "", "", 0},
{"loop-lifetime", "", "", 0},
{"assert", "", "", 1},
{"negative", "", "", 32},
}
func TestPrograms(t *testing.T) {