davedelong / DDMathParser

String → Number
MIT License
854 stars 153 forks source link

Nested expressions #136

Closed ffried closed 7 years ago

ffried commented 7 years ago

This essentially only adds one feature over #135 yet needs the commits from #135. However, git should be able to merge this.

This allows to do something like this:

do {
    let exp = try Expression(string: "$a + 5 - $b + $a")
    // -> $a + 5.0 - $b + $a
    let a = try Expression(string: "$b - 2")
    // -> $b - 2.0
    let b = 4.0
    // -> 4.0
    let simplified = exp.simplify(["a": a], evaluator: .default)
    // -> $b - 2.0 + 5.0 - $b + $b - 2.0
    let result = try Evaluator.default.evaluate(exp, substitutions: ["a": a, "b": b])
    // -> 5.0
} catch {
    print("Failed: \(error)")
}
ffried commented 7 years ago

Since this adds new features, I've also increased the version to 3.1.

ffried commented 7 years ago

@davedelong Any comments on this and #135? Thought I'd wait until after WWDC before pinging you... 😉

davedelong commented 7 years ago

I like the use of a Substitution protocol 👍