Jump to content
  • Advertisement
Sign in to follow this  
Zotoaster

Calling functions

This topic is 4255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
*double post*

Someone posted and I can't see it unless I'm on this reply screen thing, and I cant see my last reply, so sorry for this annoying post :)

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
I think he means the return value?

You mean like this?


//Your function
int CreateInt()
{
return 15;
}

//In main
int SomeInt = CreateInt();



Tjaalie,

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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:

  1. Find the definition of the function with that name.
  2. Count the number of arguments in the definition, and check that the calling code has the correct number as well.
  3. For each argument, pop a value from the stack and associate it with the variable name.
  4. Execute the code associated to that function. until you hit a return statement or the end of the function.
  5. Pop everything your function has added to the stack, then push the returned value onto the stack, if any.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!