C++ function ptrs / scripting system ideas

Started by
1 comment, last by AndyMan 21 years, 11 months ago
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.
Advertisement
If it''s a constructor, then you''re not gonna be able to call it directly using that system anyway. I think you may just have to place the unique name restriction on your code. You could write some simple wrappers to do this if necessary.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions ]
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.

This topic is closed to new replies.

Advertisement