SkillAgentSearch skills...

Cdecl

Nim helper for using C Macros

Install / Use

/learn @elcritch/Cdecl
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<h2><a class="toc-backref" id="cdotddotedotcdotldotcolon-commonly-desired-edge-case-library" href="#cdotddotedotcdotldotcolon-commonly-desired-edge-case-library">C.D.E.C.L.: Commonly Desired Edge Case Library</a></h2><p>See full docs at <a class="reference external" href="https://elcritch.github.io/cdecl/">docs</a> or source on github at <a class="reference external" href="https://github.com/elcritch/cdecl">elcritch/cdecl</a>.</p> <p>Small library for macros to handle various edge cases for Nim syntax. These are mostly edge case syntax handlers or tricky C Macro interfacings. The goal is to implement them as generically and well unit tested as possible.</p> <p>Current macros includes:</p> <ul class="simple"><li><a class="reference external" href="https://elcritch.github.io/cdecl/cdecl/cdecls.html">cdecls</a>: Macros to help using C macros that declare variables<ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">cdeclmacro</span></span></tt></li> </ul> </li> <li><a class="reference external" href="https://elcritch.github.io/cdecl/cdecl/applies.html">applies</a>: Macros that unpack arguments from various forms and calls functions<ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">unpackObjectArgs</span></span></tt>: macro to <em>splat</em> an object to position arguments</li> <li><tt class="docutils literal"><span class="pre"><span class="Identifier">unpackObjectArgFields</span></span></tt>: macro to <em>splat</em> an object to keyword arguments</li> <li><tt class="docutils literal"><span class="pre"><span class="Identifier">unpackLabelsAsArgs</span></span></tt>: turn <em>labels</em> to named arguments</li> </ul> </li> <li><a class="reference external" href="https://elcritch.github.io/cdecl/cdecl/bitfields.html">bitfields</a>: Macros for making bitfield style accessor<ul class="simple"><li><tt class="docutils literal"><span class="pre"><span class="Identifier">bitfields</span></span></tt>: create <em>bitfield</em> accessors for hardware registers using any int type</li> </ul> </li> </ul> <p>You can see various usages in the <a class="reference external" href="https://github.com/elcritch/cdecl/tree/main/tests">tests </a> folder.</p> <h2><a class="toc-backref" id="macros" href="#macros">Macros</a></h2> <h3><a class="toc-backref" id="macros-nimunpackobjectargs" href="#macros-nimunpackobjectargs"><tt class="docutils literal"><span class="pre"><span class="Identifier">unpackObjectArgs</span></span></tt></a></h3><p>Helper to apply all fields of an object as named paramters.</p> <p><pre class="listing"><span class="Keyword">type</span> <span class="Identifier">AddObj</span> <span class="Operator">=</span> <span class="Keyword">object</span> <span class="Identifier">a</span><span class="Operator">*:</span> <span class="Identifier">int</span> <span class="Identifier">b</span><span class="Operator">*:</span> <span class="Identifier">int</span>

<span class="Keyword">proc</span> <span class="Identifier">add</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">int</span> <span class="Operator">=</span> <span class="Identifier">result</span> <span class="Operator">=</span> <span class="Identifier">a</span> <span class="Operator">+</span> <span class="Identifier">b</span>

<span class="Keyword">let</span> <span class="Identifier">args</span> <span class="Operator">=</span> <span class="Identifier">AddObj</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">:</span> <span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="DecNumber">2</span><span class="Punctuation">)</span> <span class="Keyword">let</span> <span class="Identifier">res</span> <span class="Operator">=</span> <span class="Identifier">unpackObjectArgs</span><span class="Punctuation">(</span><span class="Identifier">add</span><span class="Punctuation">,</span> <span class="Identifier">args</span><span class="Punctuation">)</span> <span class="Identifier">assert</span> <span class="Identifier">res</span> <span class="Operator">==</span> <span class="DecNumber">3</span> </pre></p>

<h3><a class="toc-backref" id="macros-nimunpacklabelsasargs" href="#macros-nimunpacklabelsasargs"><tt class="docutils literal"><span class="pre"><span class="Identifier">unpackLabelsAsArgs</span></span></tt></a></h3><p>Helper to transform <tt class="docutils literal"><span class="pre"><span class="Identifier">labels</span></span></tt> as named arguments to a function. <em>Labels</em> are regular Nim syntax for calling procs but are transformed to parameter names:</p> <p><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">string</span> <span class="Operator">=</span> <span class="StringLit">&quot;buzz&quot;</span><span class="Punctuation">,</span> <span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Identifier">echo</span> <span class="Identifier">name</span><span class="Punctuation">,</span> <span class="StringLit">&quot;:&quot;</span><span class="Punctuation">,</span> <span class="StringLit">&quot; a: &quot;</span><span class="Punctuation">,</span> <span class="Operator">$</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="StringLit">&quot; b: &quot;</span><span class="Punctuation">,</span> <span class="Operator">$</span><span class="Identifier">b</span>

<span class="Keyword">template</span> <span class="Identifier">Foo</span><span class="Punctuation">(</span><span class="Identifier">blk</span><span class="Punctuation">:</span> <span class="Identifier">varargs</span><span class="Punctuation">[</span><span class="Identifier">untyped</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Operator">=</span> <span class="Comment">## create a new template to act YAML like API</span> <span class="Identifier">unpackLabelsAsArgs</span><span class="Punctuation">(</span><span class="Identifier">foo</span><span class="Punctuation">,</span> <span class="Identifier">blk</span><span class="Punctuation">)</span>

<span class="Identifier">Foo</span><span class="Punctuation">:</span> <span class="Identifier">name</span><span class="Punctuation">:</span> <span class="StringLit">"buzz"</span> <span class="Identifier">a</span><span class="Punctuation">:</span> <span class="DecNumber">11</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="DecNumber">22</span>

</pre></p>

<p>Will call <tt class="docutils literal"><span class="pre"><span class="Identifier">foo</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Operator">=</span><span class="StringLit">&quot;buzz&quot;</span><span class="Punctuation">,</span><span class="Identifier">a</span><span class="Operator">=</span><span class="DecNumber">11</span><span class="Punctuation">,</span><span class="Identifier">b</span><span class="Operator">=</span><span class="DecNumber">22</span><span class="Punctuation">)</span></span></tt> and print:</p> <p><pre class="listing"> buzz: a: 11 b: 22 </pre></p> <h3><a class="toc-backref" id="macros-nimcdeclmacro" href="#macros-nimcdeclmacro"><tt class="docutils literal"><span class="pre"><span class="Identifier">cdeclmacro</span></span></tt></a></h3><p>Macro helper for wrapping a C macro that declares a new C variable.</p> <p>It handles emitting the appropriate C code for calling the macro. Additionally it defines a new Nim variable using importc which imports the declared variable.</p> <h4><a class="toc-backref" id="nimcdeclmacro-basic-example" href="#nimcdeclmacro-basic-example">Basic Example</a></h4><p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">cdecl</span><span class="Operator">/</span><span class="Identifier">cdecls</span> <span class="Keyword">import</span> <span class="Identifier">cdecl</span><span class="Operator">/</span><span class="Identifier">cdeclapi</span> <span class="Keyword">export</span> <span class="Identifier">cdeclapi</span> <span class="Comment"># this is needed clients to use the declared apis</span>

<span class="Keyword">proc</span> <span class="Identifier">CDefineVar</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Identifier">name</span><span class="Punctuation">:</span> <span class="Identifier">CToken</span><span class="Punctuation">,</span> <span class="Identifier">size</span><span class="Punctuation">:</span> <span class="Keyword">static</span><span class="Punctuation">[</span><span class="Identifier">int</span><span class="Punctuation">]</span><span class="Punctuation">)</span> <span class="Punctuation">{</span><span class="Operator">.</span> <span class="Identifier">cdeclmacro</span><span class="Punctuation">:</span> <span class="StringLit">"C_MACRO_VARIABLE_DECLARER"</span><span class="Punctuation">,</span> <span class="Identifier">cdeclsVar</span><span class="Punctuation">(</span><span class="Identifier">name</span> <span class="Operator">-></span> <span class="Identifier">array</span><span class="Punctuation">[</span><span class="Identifier">size</span><span class="Punctuation">,</span> <span class="Identifier">int32</span><span class="Punctuation">]</span><span class="Punctuation">)</span><span class="O

View on GitHub
GitHub Stars10
CategoryDevelopment
Updated1y ago
Forks0

Languages

Nim

Security Score

60/100

Audited on Aug 29, 2024

No findings