Calling functions

Started by
9 comments, last by Zotoaster 17 years ago
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.

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.

*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 :)
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 :)
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.
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; };
Which language are we talking about?
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:

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

This topic is closed to new replies.

Advertisement