Quote:Original post by ToohrVyk 3° Use a virtual machine which controls evaluation and owns the function store, which it uses to resolve function calls.
In my personal experience, writing a virtual machine and compiling my script to some sort of intermediate language is far easier to implement than directly executing source code.
When you say "parsing" do you mean tokenizing? Because yes, that's in a class of it's own. Or do you mean converting it to RPN? I think that one makes most sense. Other types of evaluation? Would that be like: print "hello " & "world"; ? If so, good idea :)
I'm not really clear on what an associative container is. I googled it and it didn't help too much.
Yet another newbie question from the infamous 'toaster. This time it concerns functions. I have thought of lots of techniques, and tried to implements them, but they all end up falling apart.
I don't want to just call functions as procedures, or subroutines, but also so they can be used in expressions, such as sqrt().
What is the standard method to get this working? Is there anything that I should know before I continue? Thank you :)
Just to get over the possible confusion — are you asking how to implement function calls in your own scripting language (as mentioned in your other threads)? If that's the case, then in your RPN implementation the approach is simple:
Find the definition of the function with that name.
Count the number of arguments in the definition, and check that the calling code has the correct number as well.
For each argument, pop a value from the stack and associate it with the variable name.
Execute the code associated to that function. until you hit a return statement or the end of the function.
Pop everything your function has added to the stack, then push the returned value onto the stack, if any.
Yes, I want to implement functions in my own scripting language. I'm not that newbie that I don't know how to work functions in C++ lol :)
I think I should maybe have explained my problem in more detail. Yes, I have implemented RPN, and getting functions to work with that is fine. But what botheres me is how I should go about actually creating the functions. Should I make a struct with the code, parameters, identifier, then just make a global std::vector<> to store all the functions? It sounds workable enough, but it would cause problems in some cases, i.e. Loading multiple scripts (and before anyone says "put the functions and vectors and variables in a class", I could, but my maths parser is already in a class, so accessing the functions from another class would be difficult).