Closed vanniktech closed 1 year ago
This works for me. It seems like you were originally trying with infix (which wouldn't have worked), but postfix should (and does, when I tried it).
(Also, it's not related to the question, but your implementation of fact() will never terminate if you give it a negative number)
Oh yes. It does indeed work. Sorry for the noise. And thanks for catching the implementation error :)
However it seems like plus operators afterwards aren't tolerated after a postfix:
let expression = Expression(
optimized,
constants: [
"π": Double.pi,
"ℇ": Darwin.M_E,
],
symbols: [
.function("log10", arity: 1): { log10($0[0]) },
.infix("()"): { $0[0] * $0[1] },
.infix("^"): { pow($0[0], $0[1]) },
.postfix("!"): { Double(fact(Int($0[0]))) },
.postfix("π"): { $0[0] * Double.pi },
.postfix("ℇ"): { $0[0] * Darwin.M_E },
.prefix("√"): { sqrt($0[0]) },
]
)
9!+1
should yield 362881
but it throws an error: Expression.Expression.Error error 3.
This is a limitation of the parser. Because it supports arbitrary operators it can't tell that !+
isn't meant to be an infix operator in its own right.
I'll see if there's a way to make it more flexible, but in the meantime adding spaces should solve it:
9! + 1
Yes adding spaces does the trick.
I would expect 120, but the infix operator isn't called.