tinyexpr
                                
                                 tinyexpr copied to clipboard
                                
                                    tinyexpr copied to clipboard
                            
                            
                            
                        Extraction of tokens
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
Did you look at the code? It would be very easy to modify it to do what you want.
I did, but it wasn't obvious to me where to short-circuit the tokenizing and inject this logic. Any quick pointer?
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 thanks for the notes on a solid approach. I'll take a crack at it and let you know how I get on
You might also want to see here https://github.com/codeplea/tinyexpr/issues/102