Dynamic functions

How to create a synamic function?
Why do I need it… you ask? Well… I have a project to create a representation of a mathematical function. The user must enter the function (for ex: in a dynamic text: “xxMath.sin(x)” (without quotas) and if i could call that function in as i could create the graphical representation).
Thanks in advance

EDIT:
In other words… how exactly do i make flash show from “2+2” to “4”??


function mathFunction (x:Number):Number {
  var result = x * x * Math.sin(x);
  return result;
}

yes… but i need it to be DYNAMIC!!! the user must write xxMath.sin(x), not me, the editor… get it?

You want to be parsing your metalanguage, then. The kind of dynamic you mean exists in programming language like LISP, but for flash I would do something like…

This would be much easier in Polish Notation, but you can figure out a way around it.

I’m a function, I have a string, the first thing I do is check to see if there are math symbols in my string, if not, I return the numerical value of my string, otherwise, I figure out which is the LAST math symbol to operate on, I store that in my own private variable, then I recursively call TWO new instances of myself, giving each the two halves of the string, minus my operation. I then apply the results of calling those two functions to my private operation variable, using a switch/case or if/else block to say what value of operation translates to what real math operation.

So say my string I got at input was 44sin(4). I use mad programming skills to decide that I’m going to apply "" first, so I save my local variable operation=“multiply”, then I call two new instances of myself, to one, I give the string “4”, to the other, I give "4sin(4)

The first instance is going to return “4”, the second is going to save local operation=“multiply”, and then call two new versions of itself, one with 4, and one with sin(4).

The first of THOSE, with return 4, the second will store local operation = “sine”, and call ONE instance of itself (using coding skills to reliazize SINE only has one operand, unlike *… That Instance returns the numerical value 4

Now we just chug the numbers back together. The function instance where operation=sine, has gotten its needed numerical variable, and you’ve used if or case statements to tell it that means apply Math.sin(num) to it’s variable, and return that.

The instance that called itself with sine, has now gotten the answer to what sine 4 is, it can now use it’s operator=“multiply” to multiply the results of the two functions it called.

The instance above that, our first instance, does the same thing with its results.

Of course, that’s SOLVING a formula AFTER the numbers have been placed in, but you can have your user create the formula as text, and then define the variables. Your code that just swap the numbers for the variable names before sending the string off the the first function.

nice one Scarybug but… there’s a problem… if i have a*(x+y) which one does flash calculate first??
so… i guess there is no other way to do this… but still… is there any way of calling a function which is already written in actionscript and its name should be dynamic. Like: let’s say i made a function in the first frame of the root called rect. How do I make call it dynamically? as in “rect” should be a string…:smiley: like in delphi… the call procedure call(function_name) -something like this… is there??

Theres a parser out there that does this. I dont have the link on hand, but I think it can be found on flashcomponents.net - or search for actionscript interpreter or something along those lines - Im pretty sure “interpreter” is in the name of the thing.

oooh…:smiley: thanks a lot senocular… i’ll give it a try…:smiley:
still… i can’t believe flash doesn’t have something like this…

Search this site too, I know I saw one on there somewhere: http://www.layer51.com/proto/

Someting like this:

function rect():Void {
 trace("rect called with arguments:
	" + arguments);
}
this["rect"](1, 2, 3);

:slight_smile:

ooo… TheCanadian is right… it works… thnks a hell of a lot
still… i’m still wondering how to calculate “2+2” I mean… if there is this thing then… there must be something for this as well… right??

nice one Scarybug but… there’s a problem… if i have a*(x+y) which one does flash calculate first??

You’d have to parse the string to find the inner-most parenthases first, then treat paranthases as a math function, just pass “x+y” to the next instance, and when we get the answer, just pass it back as-is. How you write the parser is a arder question. I usually do that stuff with a lot of trial and error.

What The Canadian described IS a dynamic function call in flash. You could use these in the above code I described to call your math functions without huge if/else or switch/case blocks.

Instead of

if(myOperation==“multiply”){ return arg1*arg2; }
else if(myOperation==“sin”){ Math.sin(arg1); }
etc. etc.

You could define the multiply and other math functions…

function multiply(arg1:Number, arg2:Number):Number {
return arg1*arg2;
}

//arg2 is ignored, but for simplicity’s sake keep all math functions uniform
function sin(arg1:Number, arg2:Number):Number {
return Math.sin(arg1);
}
etc. etc.

and just call:

thismyOperation;

…at the and of the main recursive parsing function after myOperation, arg1 and arg2 have been defined. That will make your code much shorter than what I had originally suggested :slight_smile:

for anyone interested, this is what I was talking about
http://flashcomponents.net/components/preview/preview.php?id=118
(demo)
to find the main page, go to http://flashcomponents.net and search for “interpreter”