Re: [Vala] Fwd: Vala - generates code at runtime



On Thu, 2010-02-11 at 01:22 +1100, Fredderic Unpenstein wrote:
On 10 February 2010 09:29, jideel <jideel free fr> wrote:
Thanks for your response.
You're probably right, perhaps it's not the right tool.

Sounds to me like you might be able to get away with just a few key
functions in a scripting language, and keep the rest static.


If it's just a few functions you might be interested in libtcc from the
tcc project.   
http://bellard.org/tcc/



$ cat a-libtcc-test.c
/*
 * Simple Test program for libtcc
 *
 * libtcc can be useful to use tcc as a "backend" for a code generator.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "libtcc.h"

// argv[1] should be a path to a file containg C code
//
int main(int argc, char **argv)
{
    TCCState *s;
    int (*func)(int);
    void *mem;
    int size;
    int rc = 0;

    s = tcc_new();
    if (!s) {
        fprintf(stderr, "Could not create tcc state\n");
        exit(1);
    }

//    tcc_add_include_path(s, "/usr/include");
//    tcc_add_include_path(s, "/usr/local/lib/tcc/include");
//    tcc_add_include_path(s, ".");
//    tcc_add_include_path(s, "/usr/lib/tcc/include");
//    tcc_add_include_path(s, "/home/jake/src/tcc-0.9.25/tests");

    /* if tcclib.h and libtcc1.a are not installed, where can we find
them */
//    if (argc == 2 && !memcmp(argv[1], "lib_path=",9))
//        tcc_set_lib_path(s, argv[1]+9);

    /* MUST BE CALLED before any compilation */
    tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
    rc = tcc_add_file(s, argv[1]);
//    fprintf(stderr, "rc = %d\n", rc);
    if(rc == -1)
    {
        fprintf(stderr, "Could not add file\n");
        exit(1);
    }
    rc = tcc_run(s, argc, argv);
//    fprintf(stderr, "run rc = %d\n", rc);
    if (rc != 0)
    {
        fprintf(stderr, "Could not run file\n");
        exit(1);
    }


    /* delete the state */
    tcc_delete(s);

    return 0;
}





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]