ISibboI / evalexpr

A powerful expression evaluation crate 🦀.
GNU Affero General Public License v3.0
325 stars 54 forks source link

Implicit multiplication support #97

Closed Titaniumtown closed 2 years ago

Titaniumtown commented 2 years ago

Just came across this project, but it doesn't seem to have support for something like

let precompiled = build_operator_tree("2x").unwrap();

let context: HashMapContext = context_map! {
    "x" => x,
}.unwrap();

precompiled.eval_with_context(&context).unwrap().as_number().unwrap()

The key here being that 2x is not interpreted as 2*x. Could this functionality be included in the future? Thanks!

ISibboI commented 2 years ago

Hi, thank you for your idea. If I am not mistaken, what you are asking for is unfortunately very hard. Writing two non-operator tokens after each other makes the first one a function that is called with the second as argument at the moment. If the first token is an identifier, and that identifier happens to be both a variable and a function in the given context, then the evaluation would be ambiguous. I would like to avoid having more complicated rules about evaluation, since evalexpr is meant to be simple and its inputs to be readable for people who do not know any details. Additionally, the problem solved does not seem crucial to me, as it is not much work to write 2 * x instead of 2x.

So unfortunately, I need to deny this feature request.