Hello Joey
> I was wondering if there was a way to implement a REPL without having to 
> recompile a long > string of code every time using tcc_compile_string

tcc compile a linux 2.4.26 in 8 seconds:
37897 idents, 6674784 lines, 194464580 bytes, 8.269 s, 807168 lines/s, 23.5 MB/s
mem_max_size= 9587234 bytes

A no so big program will be recompiled in 1 sec. A modified version of
your program is attached.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "libtcc.h"

char* my_program[] = {
"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"int sub(int a, int b){return a-b;}\n"
"int foo(int n)\n"
"{\n"
"    printf(\"Hello World!\\n\");\n"
"    printf(\"sub(%d, %d) = %d\\n\", 2*n, n, sub(2*n, n));\n"
"    return 0;\n"
"}\n",

"#include <tcclib.h>\n" /* include the "Simple libc header for TCC" */
"int sub(int a, int b){return a-b;}\n"
"int foo(int n){\n"
"    printf(\"Hello world!\n\");\n"
"    printf(\"%d\n\", sub(15,10));\n"
"}\n" 
};

int main(int argc, char **argv)
{
    TCCState *s;
    int (*func)(int);
    int i;

    for (i =0; i < sizeof(my_program)/sizeof(my_program[0]); i++) {
        s = tcc_new();
        if (!s) {
            fprintf(stderr, "Could not create tcc state\n");
            exit(1);
        }

        /* 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);

        if (tcc_compile_string(s, my_program[i]) == -1)
            return 1;

        /* relocate the code */
        if (tcc_relocate(s, TCC_RELOCATE_AUTO) < 0)
            return 1;

        /* get entry symbol */
        func = tcc_get_symbol(s, "foo");

        /* run the code */
        func(32);

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

    return 0;
}
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to