Implemented negation

This commit is contained in:
2024-07-28 15:42:51 +02:00
parent 6861ae9a90
commit bb74c0cf50
11 changed files with 136 additions and 21 deletions

View File

@ -38,8 +38,6 @@ const (
Shr // >>
LogicalAnd // &&
LogicalOr // ||
Not // ! (unary)
Negate // - (unary)
Equal // ==
Less // <
Greater // >
@ -51,6 +49,10 @@ const (
Call // x()
Array // [x]
Separator // ,
_unary // <unary>
Not // ! (unary)
Negate // - (unary)
_unaryEnd // </unary>
_assignments // <assignments>
Assign // =
AddAssign // +=

View File

@ -43,6 +43,11 @@ func (t Token) IsOperator() bool {
return t.Kind > _operators && t.Kind < _operatorsEnd
}
// IsUnaryOperator returns true if the token is a unary operator.
func (t Token) IsUnaryOperator() bool {
return t.Kind > _unary && t.Kind < _unaryEnd
}
// Reset resets the token to default values.
func (t *Token) Reset() {
t.Position = 0

View File

@ -27,7 +27,7 @@ func Tokenize(buffer []byte) List {
case '\n':
tokens = append(tokens, Token{Kind: NewLine, Position: i, Length: 1})
case '-':
if len(tokens) == 0 || tokens[len(tokens)-1].IsOperator() || tokens[len(tokens)-1].IsExpressionStart() {
if len(tokens) == 0 || tokens[len(tokens)-1].IsOperator() || tokens[len(tokens)-1].IsExpressionStart() || tokens[len(tokens)-1].IsKeyword() {
tokens = append(tokens, Token{Kind: Negate, Position: i, Length: 1})
} else {
if i+1 < Position(len(buffer)) && buffer[i+1] == '=' {