tinyexpr icon indicating copy to clipboard operation
tinyexpr copied to clipboard

Extraction of tokens

Open ianhbell opened this issue 1 year ago • 4 comments

I wonder how one could extract the tokens from the expression needing to be defined without providing them upfront. For me that would be very useful in the dynamic programming context, where I might have an expression like:

a*sin(3.0) + foo(y)

and I would like the function to return a, foo, y as the tokens needing to be provided

ianhbell avatar Jan 12 '24 19:01 ianhbell

Did you look at the code? It would be very easy to modify it to do what you want.

codeplea avatar Jan 12 '24 20:01 codeplea

I did, but it wasn't obvious to me where to short-circuit the tokenizing and inject this logic. Any quick pointer?

ianhbell avatar Jan 12 '24 22:01 ianhbell

Start here: https://github.com/codeplea/tinyexpr/blob/74804b8c5d296aad0866bbde6c27e2bc1d85e5f2/tinyexpr.c#L261

You could add something after that such as:

if (!var) var = my_function_to_save_token_names(start, s->next - start);

You'd define that function as:

static const te_variable *my_function_to_save_token_names(const char *name, int len) {
...
}

Whenever an unknown token is found, it'll call that function. That function could callback to your code directly, if you already know what the token should be.

Otherwise, you'll need some way of deciding what is a variable vs a function, as they have different syntax. A work-around is that you have an iterative process where it tries it as one, and then another, and sees where the parsing error happens. This could repeat in a simple loop until it parses correctly, and viola, you have your tokens and their types.

If you don't like that, there's probably 20 other ways you can hack it up around that area to accomplish what you're looking for.

Hopefully that helps.

codeplea avatar Jan 13 '24 03:01 codeplea

@codeplea thanks for the notes on a solid approach. I'll take a crack at it and let you know how I get on

ianhbell avatar Jan 15 '24 14:01 ianhbell

You might also want to see here https://github.com/codeplea/tinyexpr/issues/102

codeplea avatar Jan 30 '24 15:01 codeplea