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

String literals/variables #9

Closed
RayMckaig opened this issue Dec 5, 2017 · 8 comments
Closed

String literals/variables #9

RayMckaig opened this issue Dec 5, 2017 · 8 comments

Comments

@RayMckaig
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

nicklockwood commented Dec 6, 2017

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
Copy link
Author

Sounds like a workable plan.

Thank you for your help.

@RayMckaig
Copy link
Author

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
Copy link
Owner

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

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

No branches or pull requests

2 participants