mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
d3f8d83c77
* feat: Add support for emitting literal C This commit adds a new Obj type, C, as well as a command for emitting C literals (which are represented by this object). This is necessary for circumstances in which Carp's handling of templates and emission orders prevents interop with C. For example, the c11 macro static_assert must only take a string literal in its second position. However, Carp's memory management will typically assign a string literal (in Carp) to an anonymous variable, which makes them impossible to use with static_assert. The new emit-c command will support this use case: ``` (static-assert 0 (Unsafe.emit-c "\"message!\"")) ``` The literal string "message!" will be emitted in the compiler's C output in the position corresponding to the macro call. We also add a special type for c literals, CTy, to prevent conflating them with Strings. This helps maintainers define clear boundaries and express what interop code requires the use of literal C. Likewise, we need to emit `c_code` to represent this type in C. This wouldn't be necessary except that Carp sometimes auto-generates functions that refer to Carp types in their C equivalents, so we need this for completeness. It is typed as the void pointer. N.B. That the command is not yet exposed in Carp in this commit. Thanks to @TimDeve for recommending the name emit-c! * feat: Add preproc command and add emit-c to Unsafe module This commit adds the preproc command, which enables users to emit arbitrary C in the compilers emitted C output. The C emitted by calls to preproc will be appended to the output after include directives but prior to any other emissions. One can use it to call preprocessor directives in C, define helper functions etc. preproc takes a C type value as an argument and must be used in conjunction with emit-c. Both functions are added to the Unsafe module to signal their dangerousness. One can use `register` in combination with preproc to define code entirely in Carp and obviate the need of additional header files. For example: ``` (Unsafe.preproc (Unsafe.emit-c "#define FOO 0")) (Unsafe.preproc (Unsafe.emit-c "void foo() { printf(\"%d\\n\", 1); }")) (register FOO Int) (register foo (Fn [] ())) (defn main [] (do (foo) (IO.println &(fmt "%d" FOO)))) ``` The prior example emits C that defines a FOO macro and foo function, which are both referenced in the main function emitted by Carps normal processing. Here's what the output looks like: ``` // .. several other includes emitted by the compiler... void foo() { printf("%d\n", 1); } //Types: // Depth 3 typedef struct { union { struct { Long member0; } Just; // Nothing char __dummy; } u; char _tag; } Maybe__Long; ``` The C passed to preproc calls is emitted prior to Carps other emissions, ensuring the user has access to these definitions before any Carp code is called. * docs: Add documentation on emit-c and preproc * feat: add macros for emitting C compiler directives This suite of macros uses the `Unsafe.emit-c` and `Unsafe.preproc` functions to provide macros for emitting common C compiler directives, such as #ifdef, #define, #pragma and others. |
||
---|---|---|
.. | ||
Array.carp | ||
ArrayExt.carp | ||
Bench.carp | ||
Binary.carp | ||
Blitable.carp | ||
Bool.carp | ||
Byte.carp | ||
carp_bench.h | ||
carp_binary.h | ||
carp_bool.h | ||
carp_byte.h | ||
carp_char.h | ||
carp_debug.h | ||
carp_double.h | ||
carp_float.h | ||
carp_int.h | ||
carp_io.h | ||
carp_long.h | ||
carp_memory.h | ||
carp_pattern.h | ||
carp_safe_int.h | ||
carp_stdbool.h | ||
carp_stdint.h | ||
carp_string.h | ||
carp_system.h | ||
carp_utf8.h | ||
Char.carp | ||
Collection.carp | ||
Color.carp | ||
Control.carp | ||
ControlMacros.carp | ||
Core.carp | ||
core.h | ||
Debug.carp | ||
Derive.carp | ||
Double.carp | ||
Dynamic.carp | ||
Filepath.carp | ||
Float.carp | ||
Format.carp | ||
Function.carp | ||
Generics.carp | ||
Gensym.carp | ||
Geometry.carp | ||
GLFW.carp | ||
Heap.carp | ||
Int.carp | ||
Interfaces.carp | ||
Introspect.carp | ||
IO.carp | ||
List.carp | ||
Long.carp | ||
Macros.carp | ||
Map.carp | ||
Maybe.carp | ||
Opaque.carp | ||
OpenGL.carp | ||
Pattern.carp | ||
Phantom.carp | ||
Platform.carp | ||
Pointer.carp | ||
Project.carp | ||
Quasiquote.carp | ||
Random.carp | ||
Result.carp | ||
SafeInt.carp | ||
SDL_gfx.carp | ||
SDL_image.carp | ||
SDL_mixer.carp | ||
SDL_ttf.carp | ||
SDL.carp | ||
SDLHelper.h | ||
Sort.carp | ||
StaticArray.carp | ||
Statistics.carp | ||
StdInt.carp | ||
String.carp | ||
System.carp | ||
Test.carp | ||
Tuples.carp | ||
Unit.carp | ||
Unsafe.carp | ||
Vector.carp |