mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
13 lines
No EOL
3.5 KiB
HTML
13 lines
No EOL
3.5 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Containers_codegen (containers.Containers_codegen)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="generator" content="odoc 1.5.2"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div class="content"><header><nav><a href="../index.html">Up</a> – <a href="../index.html">containers</a> » Containers_codegen</nav><h1>Module <code>Containers_codegen</code></h1><h2 id="code-generators"><a href="#code-generators" class="anchor"></a>Code generators</h2><p>The code generator library is designed to be used from a build system (for example, from <code>dune</code>) to generate efficient code for features that are harder to provide at runtime.</p><p>The idea is that the build system should invoke some OCaml script that depends on <code>containers.codegen</code>; the script uses the DSL below to describe what code to generate (e.g. a description of a bitfield type) and emits a <code>.ml</code> file (and possibly a <code>.mli</code> file).</p><p>For example, the build script might contain:</p><pre><code class="ml">module CG = Containers_codegen
|
||
let () =
|
||
let module B = CG.Bitfield in
|
||
let b = B.make ~name:"t" () in
|
||
B.field_bit b "x";
|
||
B.field_bit b "y";
|
||
B.field_bit b "z";
|
||
B.field_int b ~width:5 "foo";
|
||
|
||
CG.emit_file "foo.mli" (B.gen_mli b);
|
||
CG.emit_file "foo.ml" (B.gen_ml b);
|
||
()</code></pre><p>and this will produce <code>foo.ml</code> and <code>foo.mli</code> with a bitfield containing <code>x</code>, <code>y</code>, and <code>z</code>.</p></header><div class="spec module" id="module-Fmt"><a href="#module-Fmt" class="anchor"></a><code><span class="keyword">module</span> Fmt = <a href="../CCFormat/index.html">CCFormat</a></code></div><dl><dt class="spec type" id="type-code"><a href="#type-code" class="anchor"></a><code><span class="keyword">type</span> code</code></dt></dl><dl><dt class="spec module" id="module-Code"><a href="#module-Code" class="anchor"></a><code><span class="keyword">module</span> <a href="Code/index.html">Code</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code></dt><dd></dd></dl><dl><dt class="spec module" id="module-Bitfield"><a href="#module-Bitfield" class="anchor"></a><code><span class="keyword">module</span> <a href="Bitfield/index.html">Bitfield</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code></dt><dd></dd></dl><dl><dt class="spec value" id="val-emit_file"><a href="#val-emit_file" class="anchor"></a><code><span class="keyword">val</span> emit_file : string <span>-></span> <span><a href="index.html#type-code">code</a> list</span> <span>-></span> unit</code></dt><dd><p><code>emit_file file cs</code> emits code fragments <code>cs</code> into the given file at path <code>file</code></p></dd></dl><dl><dt class="spec value" id="val-emit_chan"><a href="#val-emit_chan" class="anchor"></a><code><span class="keyword">val</span> emit_chan : Stdlib.out_channel <span>-></span> <span><a href="index.html#type-code">code</a> list</span> <span>-></span> unit</code></dt><dt class="spec value" id="val-emit_string"><a href="#val-emit_string" class="anchor"></a><code><span class="keyword">val</span> emit_string : <span><a href="index.html#type-code">code</a> list</span> <span>-></span> string</code></dt></dl></div></body></html> |