nicklockwood / Expression

A cross-platform Swift library for evaluating mathematical expressions at runtime
MIT License
824 stars 50 forks source link

variables with index #8

Closed wildthink closed 6 years ago

wildthink commented 6 years ago

It would be great if we could support "myarray[1]" as a symbol. If you don't think its a good idea for the main branch could you offer a pointer to the best way to implement it anyway? :-) thanks.

nicklockwood commented 6 years ago

It’s definitely something I want to support. The main issue I’m wrestling with is whether to make the [ ] part of the identifier (as I have with dots and quotes) or to make it a separate operator.

wildthink commented 6 years ago

Umm, making it an operator would be more swifty, corresponding to the subscript() method. Also, wouldn't being an operator be easier when the key (e.g. items[key]) might also be a symbol?

nicklockwood commented 6 years ago

@wildthink yes, the problem is that you can't subscript a number, which is all that Expression currently knows how to deal with.

nicklockwood commented 6 years ago

@wildthink I guess for your use case, you'd want the subscript to be part of the symbol, in the same way that functions currently work. So you'd have something like

.subscript(name, arity:)

That could work. It would mean you couldn't subscript the result of a function though, so you couldn't write an expression like:

foo(5)[6]
wildthink commented 6 years ago

On a separate but related note, I'm interested in supporting identifiers like x' (being a variant of x). Its something inspired by TLA+ (https://en.wikipedia.org/wiki/TLA%2B). Maybe adding a new enum type? Any pointers into the code to effect this would be welcome :-).

nicklockwood commented 6 years ago

@wildthink I don't think it's necessary to introduce a new enum type for that - it should just be possible for me to support symbols that include a trailing ', as it wouldn't be ambiguous with any other symbol. Would that work for you?

wildthink commented 6 years ago

That would be great. I'm just trying to be sensitive to your overall goals but if I don't have to maintain my own fork, all the better. :-) thanks.

nicklockwood commented 6 years ago

@wildthink OK, both your feature requests are implemented in version 0.9.0 😊

wildthink commented 6 years ago

Wow! Thanks a bunch. How much do I owe you? :-)

nicklockwood commented 6 years ago

This one’s on the house ;-)