-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Unary operators #36
Comments
This will require some tricky changes in the parser locgic. I will leave this issue open, maybe I will find some time, or there is a clever contributor who wants to earn eternal fame and honour ... ;-) |
I've wrote a parser that support unary operator in Kotlin. I've find that is not so hard to support 1 char unary operator for start. private var previousToken: Token? = null
public var index: Int = 0
override fun next(): Token {
if (index >= input.length()) throw IllegalStateException()
val token = StringBuilder()
var char = input[index]
while (char.isWhitespace() && index < input.length()) {
char = input[++index]
}
if (char.isDigit()) {
while ((Character.isDigit(char) || char == ExpressionParser.DecimalSeparator) && (index < input.length())) {
token.append(input.charAt(index++))
char = if (index == input.length()) ' ' else input[index]
}
previousToken = Token(TokenType.Number, token.toString())
return previousToken!!
} else if (char in unaryOperators.keySet() && !peek().isWhitespace()
&& (previousToken == null || "(" == previousToken!!.text || "," == previousToken!!.text
|| previousToken!!.text in operators.keySet() || previousToken!!.text in unaryOperators.keySet())) {
token.append(char)
index++
previousToken = Token(TokenType.UnaryOperator, token.toString())
return previousToken!!
} else if (char.isLetter() || char == '_') {
while ((char.isLetter() || char.isDigit() || char == '_') && index < input.length()) {
token.append(input[index++])
char = if (index == input.length()) ' ' else input[index]
}
previousToken = Token(TokenType.Name, token.toString())
return previousToken!!
} else if (char == '(') {
index++
previousToken = Token(TokenType.LeftParen, "(")
return previousToken!!
} else if (char == ')') {
index++
previousToken = Token(TokenType.RightParen, ")")
return previousToken!!
} else if (char == ',') {
index++
previousToken = Token(TokenType.Comma, ",")
return previousToken!!
} else {
while (!char.isLetter() && !char.isDigit() && char != '_' && !Character.isWhitespace(char)
&& char != '(' && char != ')' && char != ',' && index < input.length()) {
token.append(input[index])
index++
char = if (index == input.length()) ' ' else input[index]
if (char in unaryOperators) {
break
}
}
if (token.toString() !in operators.keySet()) {
throw IllegalArgumentException("Unknown operator '" + token + "' at indexition " + (index - token.length() + 1))
}
previousToken = Token(TokenType.Operator, token.toString())
return previousToken!!
}
|
Thank you very much for this code snippet! |
'not' or '!' as a unary operator would be nice, as that is pretty common in many expressions. |
I would love to see this added to the project. It appears it hasn't been worked on recently. |
So we could write
+5
or-SQRT(9)
Currently it throws exception.
The text was updated successfully, but these errors were encountered: