nicklockwood / Expression

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

String literals/variables #9

Closed RayMckaig closed 6 years ago

RayMckaig commented 6 years ago

Great expression tool.

Do you have plans to support string processing such as concatenation, length, substrings, etc. ?

If not, how would you suggest I handle this as strings are a component of the expressions I'm working with?

nicklockwood commented 6 years ago

This is complicated. The Expression framework only deals with numeric values, so it can't directly manipulate strings, however you can cheat by using a lookup table to map Doubles to strings and manipulate them that way. I'm doing that in another project.

I'll see if I can create a minimal example for you. Do your expressions need to work with any types other than Strings and numbers?

RayMckaig commented 6 years ago

I understand how complicated it can be. Unfortunately the project I'm working on is a dumbed down programming language supporting numbers (works), booleans (works), strings and dates.

The string and date methods I think can be dealt with by just adding functions, even concatenation could just be translated to a function call before calling the expression evaluator although it would be convienient to just be able to use +, but & (vba, vb) could be an alternative. It's the handling of the values in the expression and the return type that seems to be problematic. I need to be able to return strings, dates, etc. from an expression.

nicklockwood commented 6 years ago

I have a class in the Layout Project called AnyExpression which is a wrapper for Expression that handles mapping of arbitrary types. It's fairly self-contained, so perhaps you could use that?

https://github.com/schibsted/layout/blob/master/Layout/AnyExpression.swift

You should just need to copy Optional+Layout.swift and the stringify() utility function from the project, and it should have no other dependencies IIRC.

RayMckaig commented 6 years ago

That sounds ideal.

Could you give me a couple of examples of how I would go about mapping a date type and perhaps doing something like 'This is a test concatenating: ' + variable + ' with some text'?

When I get home, I'll look into using this wrapper.

Thanks for your responses :~)

nicklockwood commented 6 years ago

AnyExpression's version of the SymbolEvaluator function is (Any) throws -> Any instead of (Double) throws -> Double, so mapping data types is pretty simple, you just need to cast the result to the type you expect it to be.

This should work out of the box:

let expression = AnyExpression("'This is a test concatenating: ' + variable + ' with some text'")
let result = try expression.evaluate() as! String

There aren't any built-in operators for dealing with strings, apart from "+" for concatenation, so if you want substring functions, etc you'll need to supply them yourself using the symbols dictionary.

The latest version of Expression supports the subscript operator, but only for integer indexes, and it doesn't really support member functions, so for now you'll have to do something like substring(string, range) rather than string[range] or string.substring(range).

RayMckaig commented 6 years ago

Sounds like a workable plan.

Thank you for your help.

RayMckaig commented 6 years ago

Just wanted to let you know AnyExpression works a treat. Minor tweaks to my code and it now deals with String concatenation.

Nice work and thank you for your help.

nicklockwood commented 6 years ago

@RayMckaig FYI, as of 0.11.0 AnyExpression is now included as part of the Expression library.