Skip to content

Commit

Permalink
add exp
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Feb 20, 2024
1 parent 40d7274 commit 2e0b60e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion spreadsheet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ const (
TokenSubtract
TokenMultiply
TokenDivide
TokenExponent
TokenExclamation
TokenLeftParenthesis
TokenRightParenthesis
Expand Down Expand Up @@ -482,6 +483,8 @@ func tokenize(input string) ([]Token, error) {
tokens = append(tokens, Token{Index: i, Type: TokenMultiply, Value: "*"})
} else if c == '/' {
tokens = append(tokens, Token{Index: i, Type: TokenDivide, Value: "/"})
} else if c == '^' {
tokens = append(tokens, Token{Index: i, Type: TokenExponent, Value: "^"})
} else if c == '(' {
tokens = append(tokens, Token{Index: i, Type: TokenLeftParenthesis, Value: "("})
} else if c == ')' {
Expand Down Expand Up @@ -670,7 +673,7 @@ func parseNodes(stack []ExpressionNode, tokens []Token, i, maxColumn, maxRow int
Expression: top,
})
return stack, 1, nil
case TokenAdd, TokenSubtract, TokenMultiply, TokenDivide:
case TokenAdd, TokenSubtract, TokenMultiply, TokenDivide, TokenExponent:
node := BinaryExpressionNode{
Op: token,
}
Expand Down Expand Up @@ -801,6 +804,12 @@ func evaluate(table *Table, cell *Cell, visited visitSet, expressionNode Express
return leftResult - rightResult, nil
case TokenMultiply:
return leftResult * rightResult, nil
case TokenExponent:
res := 1
for i := 0; i < rightResult; i++ {
res *= leftResult
}
return res, nil
case TokenDivide:
if rightResult == 0 {
return 0, fmt.Errorf("could not divide by zero")
Expand Down

0 comments on commit 2e0b60e

Please sign in to comment.