Hello there.

I'm reading about Nim to start a research project but couldn't find how to do 
exactly what I need. Yes, I'm new to Nim and have only tested the **Hello 
world** code online but am looking forward to use it more seriously.

Basically, I wanna pass a string containing the source code of a C function to 
some api and be able to call the compiled code. I found that compileAPI and hcr 
exist, but couldn't find any examples using C. If they exist, please accept my 
apologies. There's also dynlib but (if I'm not mistaken), that requires me to 
save the code to a file first and I was hopping everything could be done 
in-memory.

I'm focusing on using tcc first because it's fast and I need the code 
compilation to happen as fast as possible, hence why I'm using C for the 
function to avoid any extra step translating Nim to C. However, as the problem 
I'm trying to solve gets larger, I'll need a compiler that can do optimization; 
that's why something like nlvm could be another option and then writing the 
code in Nim could work.

As an example of a source code, suppose a very simple function like this 
(ignore any typos or mistakes for now, just focus on the idea :-) ). Both mat, 
G, and result would be created in Nim:
    
    
    void calc(float ** mat, int N, int M, struct graph *G, float * result) {
            
            int i, j;
            
            for (i=0; i<N; i++)
                    for (j=0; j<M; j++)
                            result += mat[i][j] * G[i*j + j]->weight;
    
    }
    
    
    Run

Thanks.

Reply via email to