clever-lang/clever — Issue #98: External function support
Author: gogo40
Created: 2012-02-10 21:10:45+00:00
State: open
Labels: Compiler, Feature Request, Interpreter, Parser, Performance, Tests

Hi!

I am working with external function support. Currently, we have support to Linux Shared Library (.so), i. e., now clever accept C code as extension language! I used libffi (http://sourceware.org/libffi/) for manipulate external function call and libdl for manage shared libraries.

I wrote a test

```
//f_ext.cpp
//C code for example shared libray

#include

extern "C"{

double f1(int a, double c){
printf("<>a=%d\n",a);
printf("<>c=%e\n",c);
return a+c;
}

double f2(const char* f, int a, double c){
printf("<<>>a=%d\n",a);
printf("<<>>c=%e\n",c);
printf("<<>>f=%s\n",f);

//f[0]='S';
return 6.6;
}

double f3(char* f){
printf("________________--\n");
printf("<<>>f=%c\n",f[0]);
//f[0]='S';
return 6.6;
}

int f4(char* f){
if(f==0) printf("casa\n");
else printf("moert\n");

puts(f);

return 2;
}

char* f5(char* f){
f[0]='9';
printf("%s\n",f);
return f;
}

};
```
You can build the shared library using (you must be root to run this script)

```
g++ -fPIC -O3 -shared -c f_ext.cpp
g++ -shared -Wl,-soname,libfext3.so -o libfext3.so f_ext.o -lc -ldl

install libfext3.so /usr/local/lib
ldconfig
```
To use the shared libray you must use the sintax:

```
[library_name, type_return] function_name (args)

where type_return (i need implement support for other clever types) can be

i = Int
d = Double
b = Byte
s = String
v = Void

```

I wait you like it!

Now, i'll work to improve performance and add to support other types. It's necessary test and fix any issue founded.