ocaml-containers/3.15/containers/Containers_codegen/index.html
Sebastian Provenzano 6214d6bd10 add 3.15 docs
2024-11-18 14:28:31 -06:00

13 lines
4.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Containers_codegen (containers.Containers_codegen)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../odoc.support/odoc.css"/><meta name="generator" content="odoc 2.4.3"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../odoc.support/highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../index.html">containers</a> &#x00BB; Containers_codegen</nav><header class="odoc-preamble"><h1>Module <code><span>Containers_codegen</span></code></h1></header><nav class="odoc-toc"><ul><li><a href="#code-generators">Code generators</a></li></ul></nav><div class="odoc-content"><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 class="language-ocaml"><code>module CG = Containers_codegen
let () =
let module B = CG.Bitfield in
let b = B.make ~name:&quot;t&quot; () in
B.field_bit b &quot;x&quot;;
B.field_bit b &quot;y&quot;;
B.field_bit b &quot;z&quot;;
B.field_int b ~width:5 &quot;foo&quot;;
CG.emit_file &quot;foo.mli&quot; [B.gen_mli b];
CG.emit_file &quot;foo.ml&quot; [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><div class="odoc-spec"><div class="spec module anchored" id="module-Fmt"><a href="#module-Fmt" class="anchor"></a><code><span><span class="keyword">module</span> Fmt</span><span> = <a href="../CCFormat/index.html">CCFormat</a></span></code></div></div><div class="odoc-spec"><div class="spec type anchored" id="type-code"><a href="#type-code" class="anchor"></a><code><span><span class="keyword">type</span> code</span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Code"><a href="#module-Code" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Code/index.html">Code</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-Bitfield"><a href="#module-Bitfield" class="anchor"></a><code><span><span class="keyword">module</span> <a href="Bitfield/index.html">Bitfield</a></span><span> : <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-emit_file"><a href="#val-emit_file" class="anchor"></a><code><span><span class="keyword">val</span> emit_file : <span>string <span class="arrow">&#45;&gt;</span></span> <span><span><a href="#type-code">code</a> list</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p><code>emit_file file cs</code> emits code fragments <code>cs</code> into the given file at path <code>file</code></p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-emit_chan"><a href="#val-emit_chan" class="anchor"></a><code><span><span class="keyword">val</span> emit_chan : <span><span class="xref-unresolved">Stdlib</span>.out_channel <span class="arrow">&#45;&gt;</span></span> <span><span><a href="#type-code">code</a> list</span> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-emit_string"><a href="#val-emit_string" class="anchor"></a><code><span><span class="keyword">val</span> emit_string : <span><span><a href="#type-code">code</a> list</span> <span class="arrow">&#45;&gt;</span></span> string</span></code></div></div></div></body></html>