code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(38 lines)

C Interoperability

Tomo is intended to be used as a complete, standalone programming language, but it's also meant to be easy to integrate with existing C libraries. In order to make this possible, there are a few tools available.

Using C Libraries

In order to link against a compiled shared library, you can use use -lfoo to cause Tomo to add -lfoo to the linker flags when compiling your final executable. You can also use use <foo.h> or use ./foo.h to cause Tomo to insert a corresponding #include when compiling your code.

You can also use ./foo.c or use ./foo.S to use C or assembly source files from inside a Tomo source file.

Inline C Code

As a final escape hatch, you can use C_code to add code that will be put, verbatim in the transpiled C code generated by Tomo. There are two forms: one that creates an expression value and one that creates a block that is executed without evaluating to anything:

# Inline C block:
C_code `
    printf("This is just a block that is executed without a return value\n");
`

# Inline C expression (you must specify a type)
val := C_code : Int32 `int x = 1; x + 1`

Inline C expressions must specify a type and they can be compound statement expressions. In other words, if an inline C expression has a type, it will be enclosed with ({ ...; }) so that you can put semicolon-terminated statements before the final expression in their own scope if you want.

1 # C Interoperability
3 Tomo is intended to be used as a complete, standalone programming language, but
4 it's also meant to be easy to integrate with existing C libraries. In order to
5 make this possible, there are a few tools available.
7 ## Using C Libraries
9 In order to link against a compiled shared library, you can use `use -lfoo` to
10 cause Tomo to add `-lfoo` to the linker flags when compiling your final
11 executable. You can also use `use <foo.h>` or `use ./foo.h` to cause Tomo to
12 insert a corresponding `#include` when compiling your code.
14 You can also `use ./foo.c` or `use ./foo.S` to use C or assembly source files
15 from inside a Tomo source file.
17 ## Inline C Code
19 As a final escape hatch, you can use `C_code` to add code that will be put,
20 verbatim in the transpiled C code generated by Tomo. There are two forms: one
21 that creates an expression value and one that creates a block that is executed
22 without evaluating to anything:
24 ```tomo
25 # Inline C block:
26 C_code `
27 printf("This is just a block that is executed without a return value\n");
30 # Inline C expression (you must specify a type)
31 val := C_code : Int32 `int x = 1; x + 1`
32 ```
34 Inline C expressions must specify a type and they can be [compound statement
35 expressions](https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html). In
36 other words, if an inline C expression has a type, it will be enclosed
37 with `({ ...; })` so that you can put semicolon-terminated statements before
38 the final expression in their own scope if you want.