Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


C++ function ptrs / scripting system ideas

This topic is 5921 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

I want to create a scripting system which allows scripts to use variables and functions in the code. It''s easy to make an identifier table with entries connecting functions and their names. But if you want to be able to use any number of functions from the code, typing in one macro call for every function you''ll want to use may take too long. I hadn''t seen a solution to this problem, and I came up with an idea. I was going to ask a question about my current problem but I thought I might introduce a wider picture of what I am trying to do, because ppl may be interested. So here''s my idea which I''ve been working towards: Have a header file which declares every vbl & fn you may want to use. The easiest way to do this is to #include all the .h files in the project. I use the preprocessor (the first stage of the compiler) which unrolls all the macros and gives me a very long list of declarations. I have a precompiler (a separate program I am making) which runs through the preprocessed header and for ever vbl & function, generates a macro call which makes an entry into the identifier table. The precompiler produces a .cpp file containing a function which fills up the identifier table. The code thus produced is compiled with the regular compiler, along with all the other code, to produce the main program. Since my scripting language uses C-like syntax, I can use some of the same code to break both the header and the scripts into tokens, so the precompiler and main program share some code. Here''s the current problem: As things currently stand, the line void func(int a); becomes ADD_FN(a,ty_int) which expands to something like i.name="a"; i.proc=(int(*)(...))a; [add ident i to table] btw the ty_int is part of an enum produced by the precompiler as it learns all the types defined by the header. This is placed in another .h file which the main program uses. If I have two functions void func(int a); void func(void *p); then no unique function can be found, so it''s a compiler error. What can I do with these sort of errors? I''ve had a look at the library header files with the two functions and it turns out that one is an inline function that calls the other. I thought inline meant the function doesn''t get put anywhere but is copied wherever it is called, meaning that you can''t attach a function pointer to it. Later there may be genuine cases where two functions I might actually want to use have the same name, eg two different constructors of the same object, so I''ll need some solution there anyway.

Share this post

Link to post
Share on other sites
For constructors I can get the precompiler to generate functions like
Thing *new_Thing { return new_Thing(); }
and allowing multiple ones wouldn''t be too hard there.

But in code in libraries I use which are included, I can''t detect clashes so easily.

Share this post

Link to post
Share on other sites

  • 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!