Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 41 lines (31 sloc) 1.148 kB
098d4b2 @tsion Initial commit. Pretty much fully working.
authored
1 # libtcc bindings for Clay
2
3 ## Instructions
4 Link with `-ldl` and `-ltcc`.
5
6 ## Example
c2ad04a @tsion Attempt to fix formatting of README
authored
7 import tcc.*;
098d4b2 @tsion Initial commit. Pretty much fully working.
authored
8
c2ad04a @tsion Attempt to fix formatting of README
authored
9 main() {
10 // A string holding the C program we will Just-In-Time compile with TCC.
11 var program = "
12 int fib(int n) {
13 if(n <= 2)
14 return 1;
15 else
16 return fib(n-1) + fib(n-2);
17 }
18
19 int foo(int n) {
20 printf(\"Hello world!\\n\");
21 printf(\"fib(%d) = %d\\n\", n, fib(n));
22 printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));
23 return 0;
24 }";
25
26 // Create a new TCC compilation context and compile the above C program.
27 var tcc = TCC();
28 compileString(tcc, program);
29
30 // Add Clay's "+" operator visible to the C code as "add".
31 addFunction(tcc, "add", add, Int, Int);
32
33 // You can't get functions and symbols from the compiled code until you relocate.
34 relocate(tcc);
35
36 // Get and call the "foo" function from the C program.
37 var func = getFunction(tcc, "foo", (Int), (Int));
38 func(32);
39 }
098d4b2 @tsion Initial commit. Pretty much fully working.
authored
40
Something went wrong with that request. Please try again.