🔒 Locked
Calling functions
Advertisement
Original Post
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 :)
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.
Few questions.
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.
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.
You should look into function pointers, member function pointers, and function objects (and suitable abstractions such as Boost.Function/Boost.Bind). You may also be interested in lambda functions, although they are not part of the language (take a look at expression templates).
jfl.
jfl.
Quote:
Original post by Tjaalie
I think he means the return value?
Hehe, you may be right. Well, it's interesting reading nevertheless.
Quote:
Original post by smr
Which language are we talking about?
QFE
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.
ToohrVyk got it closest :)
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).
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).
1° Separate parsing from evaluation, so that math parsing is in one class, and evaluation (both math and non-math) is in another. Evaluation is not math-specific, so it does not belong in a math-specific class.
2° Store functions in an associative container to find them by name.
3° Use a virtual machine which controls evaluation and owns the function store, which it uses to resolve function calls.
2° Store functions in an associative container to find them by name.
3° Use a virtual machine which controls evaluation and owns the function store, which it uses to resolve function calls.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement