Implemented negative numbers
This commit is contained in:
parent
944bacf4e1
commit
6861ae9a90
@ -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
|
||||
}
|
||||
|
||||
|
@ -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`))
|
||||
|
||||
|
5
tests/programs/negative.q
Normal file
5
tests/programs/negative.q
Normal file
@ -0,0 +1,5 @@
|
||||
main() {
|
||||
a := -32
|
||||
b := 64
|
||||
syscall(60, a + b)
|
||||
}
|
@ -42,6 +42,7 @@ var programs = []struct {
|
||||
{"loop", "", "", 0},
|
||||
{"loop-lifetime", "", "", 0},
|
||||
{"assert", "", "", 1},
|
||||
{"negative", "", "", 32},
|
||||
}
|
||||
|
||||
func TestPrograms(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user