Comments (8)
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.
from expression.
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?
from expression.
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.
from expression.
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.
from expression.
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 :~)
from expression.
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)
.
from expression.
Sounds like a workable plan.
Thank you for your help.
from expression.
@RayMckaig FYI, as of 0.11.0 AnyExpression is now included as part of the Expression library.
from expression.
Related Issues (20)
- Question: Overloading existing operators HOT 2
- Usage for macOS Apps (Swift 5.1/Xcode 11.2.1) HOT 3
- Equality of strings, substrings and characters HOT 1
- Order of operations question HOT 3
- Square brackets HOT 1
- when using the sample code it won't execute HOT 4
- Expression.parse("'\\n'") fails HOT 2
- AnyExpression using array variables can't be evaluated with error 0 HOT 1
- Expose AST HOT 1
- Test suite crashes on Xcode 13.3 / Swift 5.6 HOT 7
- Ability to use ^ instead of pow(x, y) HOT 4
- Question: Can multiplication be done with parentheses? HOT 1
- Function expression failed with string paramater which contains \ HOT 1
- Crash in Swift 5.7 when creating stringBounds error HOT 4
- Getting sub expressions during evaluation HOT 1
- Lazy evaluation of ternary operator's true/false cases HOT 1
- Question: Is implicit multiplication possible with Expression? HOT 5
- Support ^ as pow operator HOT 2
- Fact `!` postfix operator does not work HOT 6
- Complex expressions are not working HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expression.