Skip to main content
gamedev.net
🔒 Locked

Calling functions

Started by Zotoaster 9 replies 1.3k views
Original Post
Zotoaster
Zotoaster
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 :)
smr
smr
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.

Zotoaster
Zotoaster
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.

Zotoaster
Zotoaster
*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 :)
Tjaalie
Tjaalie
I think he means the return value?

You mean like this?

//Your functionint CreateInt(){  return 15;}//In mainint SomeInt = CreateInt();


Tjaalie,
if (*pYou == ASSHOLE) { pYou->Die(); delete pYou; };
smr
smr
Which language are we talking about?
jflanglois
jflanglois
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
ToohrVyk
ToohrVyk
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.
Zotoaster
Zotoaster
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).
ToohrVyk
ToohrVyk
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.