Re: string_exec()
- Posted by dcuny Mar 05, 2015
- 2193 views
As a follow up, I'm not sure I understand why you need to see Euphoria's variables. For example:
a = string_exec("a+b")
The code that builds the string "a+b" obviously thinks there are variables called a and b, because it built a string expression containing those variables. But how does the code know about them?
Put a different way, if string_exec() were able to used named variables, would that be enough? For example:
sequence context = {{"a", 100}, {"b", 200}} string_exec(context, "myRoutine(a+b)+sin(a)")
Would something like that satisfy your needs? That would be even simpler to write, and it could be written in pure Euphoria. I could cannibalize the parser I've written for Py to do this.
The question then becomes what's the best way to pass values of variables to string_exec. For something like a spreadsheet, the context would be pretty clunky. Passing the routine_id of a user-defined function that would resolve variables would be a more flexible approach, along the lines of:
sequence context = {{"a", 100}, {"b", 200}} -- returns {value, success_flag} function lookup_variable(sequence varName) for i = 1 to length(context) if equal(context[i][1], varName) then -- return value and success flag return {context[i][2], 1} end if end for -- return bogus value and failure flag return {0, 0} end function string_exec(routine_id("lookup_variable"), "myRoutine(a+b)+sin(a)")
Or the function lookup_variable() could be supplied, and you'd just recode it to your needs.
Would that work?
- David