Skip to content
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

Closed
yoavst opened this issue Jul 21, 2015 · 5 comments · Fixed by #110
Closed

Support for Unary operators #36

yoavst opened this issue Jul 21, 2015 · 5 comments · Fixed by #110

Comments

@yoavst
Copy link

yoavst commented Jul 21, 2015

So we could write +5 or -SQRT(9)
Currently it throws exception.

@uklimaschewski
Copy link
Collaborator

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 ... ;-)

@yoavst
Copy link
Author

yoavst commented Aug 15, 2015

I've wrote a parser that support unary operator in Kotlin.
Here is my project - https://gitlab.com/yoav-sternberg/algebraEval (it was done for learning how to parse expressions, but yes it is based on EvalEx.), if someone want to port the unary part, you are welcome.

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!!
        }

@uklimaschewski
Copy link
Collaborator

Thank you very much for this code snippet!

@micabytes
Copy link

'not' or '!' as a unary operator would be nice, as that is pretty common in many expressions.

@Sub6Resources
Copy link

I would love to see this added to the project. It appears it hasn't been worked on recently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants