C - How to get a user defined function and turn it into a pointer? - c

I would like to know if there is any way of getting a user defined function (with two variables) from stdin in mathematical form and turn it into a function pointer. In other words, what I want to do is run:
> ./program a*b
Program turns that into a pointer of a function that returns:
return a*b;
So, the output of program is
user_defined_function(int)(int)
which would then be part of a much larger program.
I would post some code if I had any idea of how to tackle this problem, but I don't... I just need help with the step of turning the user defined function into a function pointer, since I know how to turn the user defined function into a C function.

There is no simple solution to that since you would have to generate code.
Simples solution that comes to my mind for this:
generate a C file from within your programm that only has one function, inserting the command line argument as return statement
give the function a known or generated name
exec the compiler and generate a shared library
dynamically load that shared library
call the known function
I fear it doesn't get any simpler than that.
The other solution would be to write/ use an expression parser and parse the math expression and than evaluate at runtime...

Just for fun, here is a link to CINT
CINT is an interpreter for C and C++ code...
... A CINT script can call compiled classes/functions and compiled code can make callbacks to CINT interpreted functions ...
I'm not saying this is a "good" solution (and in fact it may be very "bad" in cases!), but some people have already put a good bit of effort -- "slightly less than 400,000 lines of code" -- into this project ;-)
Happy coding.

This is very hard to do in C because it is a compiled language. You could do what Mario The Spoon is suggesting, or you could switch to a dynamic language like ruby or javascript. These languages have an "eval" method that takes a string and executes the code inside the string, and they have the ability to dynamically define functions.

What you're proposing is entirely possible, you simply write code which transforms user text into machine code. This is called a compiler. gcc would be much like your program, if it ran the code it generated.

Related

When a go program is called from C, is it compiled or interpreted?

I made a C program. And I made a go file with go functions defined.
In the C program, I called go functions. Is go called from C compiled or interpretted?
I made a C program. And I made a go file with go functions defined. In the C program, I called go functions
You made a Go program which calls C functions (the other way around is not yet possible.) Then you're apparently calling Go functions from C again which is a bit weird and doesn't make much sense most of the time. See https://stackoverflow.com/a/6147097/532430.
I'm going to assume you used gccgo to compile your program. Because if you used Go's gc then there wouldn't be any confusion about what language your program is written in.
Is go called from C compiled or interpretted?
It's compiled. gccgo is a Go front-end for GCC. And GCC stands for GNU Compiler Collection.
it is always compiled. C will never run function without compilation.
In your program when you first call the go function,the compiler will generate the necessary codes for function call,space for function arguments,store details about function arguments type etc.
If everything is correct as per the compiler standard,object file is created and further there are other processes like linking and all.
So basically you cannot say it as " Is go called from C compiled or interpretted?",it's a series of processes which works together to make your program run.

Is there any design tools that I can get the exact number of instructions of different functions in C code

I need to analyze some c code with functions and I want to get the exact number of instructions of different function in c code. I have no idea. Thanks.
If you're talking about assembly instructions, this is pretty easy:
Make a small C program with main. Immediately in main, call your desired function. Compile with the gcc -c option (outputs an object file). Open up your object f ile (.o) in a text editor. Look for t he call function (This is the call into the function). Start counting the lines until you hit a return (ret) instruction.
There you go! Of course, your compiler will probably optimize this object code so it might be a little more complex, but this is probably a really easy way to do it.

C Compilation With missing function definitions

I've been following a couple of C tutorials and there is a certain point I'm not sure I understand. Some examples show function prototypes but without function definitions. The tutorials say that the code should compile ok, even if they won't run.
Is this correct? Should C programs with missing function definitions compile ok?
The source code will compile with declarations only, but if any of the functions are called then a linker error will occur if the functions are not defined somewhere.
Yes, this is correct. It is the feature that makes it possible to split a big program into multiple source files.
There's a big difference between function declarations and function definitions. To use a function, you have to declare the function first, but you can only compile the program if the functions you use have been defined.
The C compilation process is a series of steps that feed one into another. In a typical compilation process, first the preprocessor runs, then the compiler generates assembly language for each source file, then the assembler turns that assembly language into machine code, and then the linker puts all the necessary pieces together. The compiler step typically won't finish unless you declare functions, but the compiler doesn't care about where the functions are actually implemented - it just generates assembly language code with holes where calls to the real functions can be placed. The linker fills in those holes with calls to the actual functions.
So you can declare a function but define it in a different file, which is what the tutorial was probably doing. However, you still have to define the function somewhere, or else you won't get a full executable binary.
Yes, there is something called linking. This is a process of resolving references to different symbols, like variables, functions etc. The compiler is happy even if it does not know anything about a function's definition. However, if compiler knows about the function's prototype, it can check if the function is used correctly, so that mistakes get flagged early.
Refer Wikipedia or google to know more about linking.

How to implement standard C function extraction?

I have a "a pain in the a$$" task to extract/parse all standard C functions that were called in the main() function. Ex: printf, fseek, etc...
Currently, my only plan is to read each line inside the main() and search if a standard C functions exists by checking the list of standard C functions that I will also be defining (#define CFUNCTIONS "printf...")
As you know there are so many standard C functions, so defining all of them will be so annoying.
Any idea on how can I check if a string is a standard C functions?
If you have heard of cscope, try looking into the database it generates. There are instructions available at the cscope front end to list out all the functions that a given function has called.
If you look at the list of the calls from main(), you should be able to narrow down your work considerably.
If you have to parse by hand, I suggest starting with the included standard headers. They should give you a decent idea about which functions could you expect to see in main().
Either way, the work sounds non-trivial and interesting.
Parsing C source code seems simple at first blush, but as others have pointed out, the possibility of a programmer getting far off the leash by using #defines and #includes is rather common. Unless it is known that the specific program to be parsed is mild-mannered with respect to text substitution, the complexity of parsing arbitrary C source code is considerable.
Consider the less used, but far more effective tactic of parsing the object module. Compile the source module, but do not link it. To further simplify, reprocess the file containing main to remove all other functions, but leave declarations in their places.
Depending on the requirements, there are two ways to complete the task:
Write a program which opens the object module and iterates through the external reference symbol table. If the symbol matches one of the interesting function names, list it. Many platforms have library functions for parsing an object module.
Write a command file or script which uses the developer tools to examine object modules. For example, on Linux, the command nm lists external references with a U.
The task may look simple at first but in order to be really 100% sure you would need to parse the C-file. It is not sufficient to just look for the name, you need to know the context as well i.e. when to check the id, first when you have determined that the id is a function you can check if it is a standard c-runtime function.
(plus I guess it makes the task more interesting :-)
I don't think there's any way around having to define a list of standard C functions to accomplish your task. But it's even more annoying than that -- consider macros,
for example:
#define OUTPUT(foo) printf("%s\n",foo)
main()
{
OUTPUT("Ha ha!\n");
}
So you'll probably want to run your code through the preprocessor before checking
which functions are called from main(). Then you might have cases like this:
some_func("This might look like a call to fclose(fp), but surprise!\n");
So you'll probably need a full-blown parser to do this rigorously, since string literals
may span multiple lines.
I won't bring up trigraphs...that would just be pointless sadism. :-) Anyway, good luck, and happy coding!

Run a program and return value libreoffice calc

I wrote an algorithm in c that takes input of double precision numbers and returns a double. I would like to use this in libreoffice calc. I tried making a macro function using shell but it only gives back 0. How do I write the macro so it gives back the number that the c program returns. Also how do I make it so I can input the three numbers from a cell into the c program?
Thank you.
Using a shell interface does not seem workable here, because as you mentioned, it will just return 0 for success. Instead, I can think of two ways to do this.
The first is to create a Calc add-in. You can specify the number of arguments (3 in your case) in the IDL file. This would mean that there is no need for any OpenOffice Basic code -- everything can be done in C++. It is a rather clean solution. There are quite a few configuration files needed though, and it may initially be more difficult than what is described below.
The second is to dynamically load the DLL and call the function. I have had success using PyUNO with the ctypes library, but it can be done in OpenOffice Basic as well.
See also:
https://forum.openoffice.org/en/forum/viewtopic.php?f=45&t=45675
http://sheepdogguides.com/fdb/opof6dll.htm

Resources