<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divyanshu Sinha</title>
    <description>The latest articles on DEV Community by Divyanshu Sinha (@divyanshusinha136).</description>
    <link>https://dev.to/divyanshusinha136</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2444956%2F061cd6da-2d10-4599-b948-898f0f254032.jpg</url>
      <title>DEV Community: Divyanshu Sinha</title>
      <link>https://dev.to/divyanshusinha136</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divyanshusinha136"/>
    <language>en</language>
    <item>
      <title>Self-driving cars that learn by deep Q-learning, with drift and reverse, in pure KingCobra</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Tue, 24 Nov 2026 06:36:27 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/self-driving-cars-that-learn-by-deep-q-learning-with-drift-and-reverse-in-pure-kingcobra-25j3</link>
      <guid>https://dev.to/divyanshusinha136/self-driving-cars-that-learn-by-deep-q-learning-with-drift-and-reverse-in-pure-kingcobra-25j3</guid>
      <description>&lt;p&gt;I built a self-driving-car simulator where &lt;strong&gt;every car on the road is driven by the same neural network&lt;/strong&gt;, trained with Double DQN. The cars have a proper dynamic tyre model, so they can understeer, &lt;strong&gt;drift with a handbrake&lt;/strong&gt; and &lt;strong&gt;drive in reverse&lt;/strong&gt;. Everything is written in &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;KingCobra&lt;/a&gt;, my programming language, using only its standard library (&lt;code&gt;std.nn&lt;/code&gt;, &lt;code&gt;std.http&lt;/code&gt;, &lt;code&gt;std.json&lt;/code&gt; and &lt;code&gt;std.system&lt;/code&gt;). There is no Python and there are no external dependencies.&lt;/p&gt;

&lt;p&gt;Repo: &lt;strong&gt;&lt;a href="https://github.com/DivyanshuSinha136/KingCobra-Self-Driving-Car-DQN" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra-Self-Driving-Car-DQN&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuiwgvkeiggcemuuzyakx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuiwgvkeiggcemuuzyakx.png" alt="The dashboard: five cars on an unseen track, telemetry, the shared-network panel, Q-values, inputs, training curves and model card" width="800" height="1511"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The dashboard, captured from the real app: the AI and four NPC cars on a track the network never saw, all driven by one network.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Laps&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;100 %&lt;/strong&gt; completed on 3 tracks the network never saw, at about 14 m/s, with no wrecks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Training&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1.7 minutes on one CPU core, for a network of about 21,000 parameters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Agent&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;one shared Double DQN, 3-step returns, Huber loss, gradient clipping, soft target updates, 12 actions (steering × pedal)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Physics&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;dynamic bicycle model, tyre friction circle, handbrake drift, reverse gear, soft walls, car-to-car collisions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reproducible&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;one seed controls everything, and two runs with the same seed give bit-identical models&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fevqrsqlx9krogeokdye1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fevqrsqlx9krogeokdye1.gif" alt="47-second gameplay recording: AI cars racing in traffic, the chase camera, the drifter model sliding, a human adding a handbrake drift and reversing, then the AI taking over again" width="600" height="347"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;47 seconds recorded from the real dashboard: AI traffic on an unseen track, the chase camera, the drifter model, a human adding a handbrake drift and reversing, then the AI taking back over.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;You need KingCobra 1.0 (&lt;code&gt;kingcobra&lt;/code&gt; on your &lt;code&gt;PATH&lt;/code&gt;), and &lt;code&gt;node&lt;/code&gt; only for the front-end tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/DivyanshuSinha136/KingCobra-Self-Driving-Car-DQN
&lt;span class="nb"&gt;cd&lt;/span&gt; KingCobra-Self-Driving-Car-DQN

make serve    &lt;span class="c"&gt;# open http://127.0.0.1:8766, two pre-trained models are included&lt;/span&gt;
make &lt;span class="nb"&gt;eval&lt;/span&gt;     &lt;span class="c"&gt;# laps on seen and unseen tracks, with traffic&lt;/span&gt;
make train    &lt;span class="c"&gt;# retrain from scratch (about 2 minutes on one CPU core)&lt;/span&gt;
make &lt;span class="nb"&gt;test&lt;/span&gt;     &lt;span class="c"&gt;# 67 KingCobra tests plus front-end tests&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;make&lt;/code&gt;? Every target is one command: &lt;code&gt;kingcobra run src/main.kc &amp;lt;command&amp;gt; [options]&lt;/code&gt;, with the commands &lt;code&gt;train&lt;/code&gt;, &lt;code&gt;eval&lt;/code&gt;, &lt;code&gt;serve&lt;/code&gt; and &lt;code&gt;info&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  One network, many drivers
&lt;/h2&gt;

&lt;p&gt;This is &lt;strong&gt;parameter sharing&lt;/strong&gt;, the standard way to train many identical agents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    Shared KingCobra NN      (one set of weights)
                           |
                           +--- Observation 01 -&amp;gt; Action 01      car 1
                           +--- Observation 02 -&amp;gt; Action 02      car 2
                           +--- Observation 03 -&amp;gt; Action 03      car 3
                           +--- Observation N  -&amp;gt; Action N       car N
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One brain, N bodies.&lt;/strong&gt; Each car senses its own world (its lidar, its neighbours, its own slip) and gets its own action, but the weights are shared, so everything any car learns helps all of them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One batched forward pass per tick.&lt;/strong&gt; The observations of every running car are stacked and sent through the network together, which is much cheaper than one pass per car. A test checks that the batch gives exactly the per-car answers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One replay buffer.&lt;/strong&gt; Every car's transitions go into the same buffer, so a 4-car pack produces 4× the experience per tick.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At serving time too.&lt;/strong&gt; In the dashboard the NPCs and (in AI mode) the ego car use the same network. When you take the wheel, &lt;em&gt;you&lt;/em&gt; replace only the ego car's action, and the NPCs keep using the network.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flnn1j3rzmfhg3xxpcy1o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flnn1j3rzmfhg3xxpcy1o.png" alt="The shared-network panel: one row per car, with its observation going in and its action coming out" width="800" height="411"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The panel in the dashboard shows this live: one row per car, the observation going in and the action coming out.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What the network sees and does
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Observation (22 numbers per car).&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;7 lidar rays to the road edge.&lt;/li&gt;
&lt;li&gt;A 7-sector radar that gives the distance to the nearest &lt;em&gt;other car&lt;/em&gt; in each 30° sector. Thin rays would miss a neighbour a few metres to the side.&lt;/li&gt;
&lt;li&gt;Forward speed, &lt;strong&gt;sideways speed and yaw rate&lt;/strong&gt; (so the network can feel a slide), and steering angle.&lt;/li&gt;
&lt;li&gt;Lateral offset, heading error, and the road's curvature 10 m and 25 m ahead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Actions (12).&lt;/strong&gt; Steering {left, straight, right} × pedal {gas, coast, brake/reverse, handbrake}. Steering is rate-limited, so a car can't snap between full locks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reward.&lt;/strong&gt; Progress along the centre line, a small time cost so speed matters, a small lateral-offset cost, penalties for wall scrapes (−0.4) and car contacts (−1), a +20 bonus for finishing a lap, and an optional drift bonus. A run ends on a lap, on being stuck, going the wrong way, wrecking (14 scrapes) or a time limit.&lt;/p&gt;
&lt;h2&gt;
  
  
  Car physics: drift and reverse
&lt;/h2&gt;

&lt;p&gt;Each car is a &lt;strong&gt;dynamic bicycle model&lt;/strong&gt;, with mass, inertia, two axles and four 25 ms sub-steps per 0.1 s tick, not a point on rails. Tyre lateral force is linear in the slip angle until it saturates at &lt;code&gt;μ·load&lt;/code&gt;, and it shares that grip budget with the longitudinal force (a friction circle). That gives the behaviour you'd expect from a real car:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Understeer:&lt;/strong&gt; brake or accelerate hard in a bend and the front tyres run out of grip.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drift:&lt;/strong&gt; the &lt;strong&gt;handbrake&lt;/strong&gt; locks the rear wheels (a small friction budget), the rear lets go, and the car rotates and slides. The dashboard flags it and shows tyre marks and smoke.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse:&lt;/strong&gt; the "brake / reverse" pedal brakes while moving forward and, once stopped, drives the car backwards (up to 6 m/s), so the cars can back out of trouble.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Soft walls:&lt;/strong&gt; touching the road edge pushes the car back and scrubs speed (a scrape) instead of ending the run. Too many scrapes wreck it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd60wxxli1nl8k3dw4j50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd60wxxli1nl8k3dw4j50.png" alt="A human driver in a handbrake drift at 51 km/h, with a DRIFT badge, tyre marks and smoke" width="800" height="788"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;A human driver in a handbrake drift.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6enc8hi526yu75hi7kw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6enc8hi526yu75hi7kw9.png" alt="The same car reversing, with the R gear and REVERSE badge" width="800" height="788"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same car reversing.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Racer and drifter
&lt;/h2&gt;

&lt;p&gt;Two networks ship, trained with the same code. Only the reward differs (&lt;code&gt;reward.drift&lt;/code&gt;):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;driver_ddqn&lt;/code&gt; (racing)&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;drifter_ddqn&lt;/code&gt; (drift)&lt;/th&gt;
&lt;th&gt;rule-based driver&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;laps completed, unseen tracks&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100 %&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100 %&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;92 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;laps completed, training tracks&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;100 %&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;98 %&lt;/td&gt;
&lt;td&gt;88 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mean speed, unseen tracks&lt;/td&gt;
&lt;td&gt;13.9 m/s&lt;/td&gt;
&lt;td&gt;7.5 m/s&lt;/td&gt;
&lt;td&gt;13.2 m/s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;mean lap time, unseen tracks&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;28.4 s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;52.9 s&lt;/td&gt;
&lt;td&gt;29.9 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;time spent drifting&lt;/td&gt;
&lt;td&gt;0.4 %&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;53.7 %&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;2.2 %&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;car contacts per run (unseen)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.00&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.00&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1.11&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wall scrapes per run (unseen)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.4&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;0.3&lt;/td&gt;
&lt;td&gt;1.2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The numbers come from &lt;code&gt;make eval&lt;/code&gt; (3 start positions per track, 4 cars on the road). The rule-based driver is blind to the handbrake and reverse. An honest reading: the racer is the fastest &lt;em&gt;and&lt;/em&gt; the most reliable. The drifter is a style model that trades a lot of speed for sliding. It learns to drift because the bonus pays, so treat it as a demonstration, not as a better driver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu80dvyhyt4tnoyf65e6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fu80dvyhyt4tnoyf65e6s.png" alt="The drifter model sliding through a bend, leaving tyre marks and smoke, with an NPC drifting ahead" width="800" height="793"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The drifter model sliding through a bend.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The dashboard
&lt;/h2&gt;

&lt;p&gt;The dashboard is a dependency-free front-end served by KingCobra's own &lt;code&gt;std.http&lt;/code&gt;. You can drive the cyan car yourself: &lt;code&gt;↑&lt;/code&gt;/&lt;code&gt;W&lt;/code&gt; gas, &lt;code&gt;↓&lt;/code&gt;/&lt;code&gt;S&lt;/code&gt; brake and then reverse, &lt;code&gt;←&lt;/code&gt; &lt;code&gt;→&lt;/code&gt; or &lt;code&gt;A&lt;/code&gt; &lt;code&gt;D&lt;/code&gt; to steer, and &lt;code&gt;Space&lt;/code&gt; for the handbrake. Pressing a driving key takes the wheel. Other keys: &lt;code&gt;Space&lt;/code&gt; play/pause, &lt;code&gt;N&lt;/code&gt; step, &lt;code&gt;R&lt;/code&gt; reset, &lt;code&gt;M&lt;/code&gt; AI/You, &lt;code&gt;C&lt;/code&gt; camera, &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; for speed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faz4yz8ejgvg10uyxwasi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faz4yz8ejgvg10uyxwasi.png" alt="Chase camera following the ego car, with its lidar rays and an NPC ahead" width="800" height="793"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The chase camera, with the ego car's lidar (cyan, amber or red by distance) and the NPCs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkghuim4nnpyp5jdktddi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkghuim4nnpyp5jdktddi.png" alt="The grid of the 12 Q-values: three steering rows by four pedal columns" width="800" height="395"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What the network wants: Q(s, a) for all 12 actions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd67xzjgw3guwqr15sg99.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fd67xzjgw3guwqr15sg99.png" alt="Telemetry: speed, slip angle, yaw rate, lap progress, scrapes, contacts and drift time" width="800" height="407"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Telemetry: speed, slip angle, yaw rate, scrapes, contacts and drift time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm0ms736bz6yneafob4ef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm0ms736bz6yneafob4ef.png" alt="The benchmark card comparing the AI with the rule-based driver" width="800" height="740"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The built-in benchmark races the network against the rule-based driver on the same roads and starts.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It also works on a phone.&lt;/p&gt;
&lt;h2&gt;
  
  
  Training
&lt;/h2&gt;

&lt;p&gt;An &lt;em&gt;episode&lt;/em&gt; is a pack of 4 cars driving together on a random training track from random start positions. The &lt;strong&gt;checkpoint is chosen on separate validation tracks&lt;/strong&gt;, and the final numbers are measured on &lt;strong&gt;test tracks that were never used for training or for any decision&lt;/strong&gt;. That separation matters, because it means the 100 % figure is not about tracks the agent memorised.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flgodcch2ws5otnu90wpk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flgodcch2ws5otnu90wpk.png" alt="Training curves: validation laps completed, validation progress, training progress and epsilon over 90 episodes" width="799" height="301"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Training curves over 90 episodes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Every tunable lives in &lt;code&gt;config/default.json&lt;/code&gt; and is documented in &lt;code&gt;docs/TRAINING.md&lt;/code&gt;. Unknown keys and nonsensical values are rejected with a clear message. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra run src/main.kc &lt;span class="nb"&gt;eval&lt;/span&gt; --starts 3                  &lt;span class="c"&gt;# the AI, 3 start positions per track&lt;/span&gt;
kingcobra run src/main.kc &lt;span class="nb"&gt;eval&lt;/span&gt; --baseline --starts 3       &lt;span class="c"&gt;# the hand-written driver, same roads&lt;/span&gt;
kingcobra run src/main.kc train --name drifter_ddqn --set reward.drift=0.4 --set train.select=last
kingcobra run src/main.kc train --set sim.cars=6 --set sim.spacing=15 --episodes 120   &lt;span class="c"&gt;# busier traffic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The code layout
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  main.kc        CLI (train · eval · serve · info)
  config.kc      defaults, JSON overlay, --set overrides, validation
  track.kc       procedural roads + occupancy grid for fast lidar
  car.kc         dynamic car: tyres, drift, reverse, soft walls, road lidar
  world.kc       several cars: collisions, car radar, rewards, respawn
  features.kc    the 22-number observation of one car
  replay.kc      one circular buffer for all cars (optional prioritised sampling)
  agent.kc       the shared Double DQN: batched inference, vectorised learning step
  trainer.kc     training loop, evaluation on seen / unseen tracks, checkpointing
  baseline.kc    the hand-written driver to compare against
  server.kc      routing, sessions, models, security headers, static assets
  util.kc        RNG, statistics, helpers
web/             index.html · style.css · app.js
config/          default.json
models/          pre-trained driver_ddqn and drifter_ddqn (+ model cards and curves)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server has a small JSON API (&lt;code&gt;/api/state&lt;/code&gt;, &lt;code&gt;/api/reset&lt;/code&gt;, &lt;code&gt;/api/ai-step&lt;/code&gt;, &lt;code&gt;/api/drive&lt;/code&gt;, &lt;code&gt;/api/models&lt;/code&gt;, &lt;code&gt;/api/model&lt;/code&gt;, &lt;code&gt;/api/metrics&lt;/code&gt;, &lt;code&gt;/api/tracks&lt;/code&gt;, &lt;code&gt;/api/track&lt;/code&gt;, &lt;code&gt;/api/benchmark&lt;/code&gt; and &lt;code&gt;/api/health&lt;/code&gt;). Games are per session, and each session has its own cars and chosen model.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tested like a real project
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;make test&lt;/code&gt; runs &lt;strong&gt;67 KingCobra tests&lt;/strong&gt; covering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;track determinism and drivable bends,&lt;/li&gt;
&lt;li&gt;the physics: acceleration, braking distance, reverse, the grip limit, the handbrake breaking the rear loose, and soft walls,&lt;/li&gt;
&lt;li&gt;the multi-car world: collisions, car radar, end conditions and respawn,&lt;/li&gt;
&lt;li&gt;the shared network: a batched pass equals per-car passes, and observation &lt;em&gt;i&lt;/em&gt; maps to action &lt;em&gt;i&lt;/em&gt;,&lt;/li&gt;
&lt;li&gt;the Double-DQN target against a reference,&lt;/li&gt;
&lt;li&gt;exact save and load round-trips,&lt;/li&gt;
&lt;li&gt;determinism of whole training runs,&lt;/li&gt;
&lt;li&gt;and the real server booted as a child process and driven over HTTP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also unit tests for the front-end's pure functions, and a smoke test that runs the real &lt;code&gt;app.js&lt;/code&gt; against a live server with a stub DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The simulator is 2-D&lt;/strong&gt; and the roads are procedural loops, with no junctions, traffic rules or pedestrians.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The racer rarely drifts or reverses on its own,&lt;/strong&gt; because a racing reward gives it no reason to. Those abilities show up in the drifter and in human drivers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The NPCs are the same network as the ego car,&lt;/strong&gt; not independent policies, so they cooperate by construction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The server is single-threaded,&lt;/strong&gt; and a benchmark request blocks other requests while it runs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training runs on the interpreter, on one CPU core,&lt;/strong&gt; at about 1,500 transitions per second including learning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The results come from one seed,&lt;/strong&gt; so there are no confidence intervals across seeds.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where this comes from
&lt;/h2&gt;

&lt;p&gt;This is the same approach as my &lt;a href="https://dev.to/divyanshusinha136/a-double-dqn-agent-that-plays-snake-written-entirely-in-kingcobra-493h"&gt;Snake DQN agent&lt;/a&gt;, pushed further: continuous physics, several agents sharing one network and a harder evaluation protocol. The background on DQN and Double DQN is in my &lt;code&gt;std.nn&lt;/code&gt; guide: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Get it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Self-driving car DQN:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra-Self-Driving-Car-DQN" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra-Self-Driving-Car-DQN&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KingCobra:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MIT licensed. If you retrain it with other settings, or add a feature like junctions or traffic lights, tell me and I'll link it. A star helps other people find it.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>machinelearning</category>
      <category>gamedev</category>
      <category>opensource</category>
    </item>
    <item>
      <title>A Double DQN agent that plays Snake, written entirely in KingCobra</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Sat, 21 Nov 2026 11:28:31 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/a-double-dqn-agent-that-plays-snake-written-entirely-in-kingcobra-493h</link>
      <guid>https://dev.to/divyanshusinha136/a-double-dqn-agent-that-plays-snake-written-entirely-in-kingcobra-493h</guid>
      <description>&lt;p&gt;I built a &lt;strong&gt;Double DQN agent that learns to play Snake&lt;/strong&gt;, and every part of it is written in &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;KingCobra&lt;/a&gt;, my programming language: the game, the replay buffer, the neural-network training, the HTTP server and a live web dashboard. It uses only KingCobra's standard library (&lt;code&gt;std.nn&lt;/code&gt;, &lt;code&gt;std.http&lt;/code&gt;, &lt;code&gt;std.json&lt;/code&gt; and &lt;code&gt;std.system&lt;/code&gt;). There is no Python and there are no external dependencies.&lt;/p&gt;

&lt;p&gt;Repo: &lt;strong&gt;&lt;a href="https://github.com/DivyanshuSinha136/KingCobra-Snake-DQN" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra-Snake-DQN&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjfzlu59tzs13u8fnpe19.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjfzlu59tzs13u8fnpe19.png" alt="The dashboard: the AI playing Snake, its Q-values and inputs, training curves and a benchmark" width="800" height="1234"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The dashboard, captured from the real app: the trained agent playing, what it sees and wants, and how it learned.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Score&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;mean &lt;strong&gt;33.1&lt;/strong&gt; (best game &lt;strong&gt;50&lt;/strong&gt;) over 100 greedy games on a 10×10 board&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Training&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;about 2 minutes on one CPU core&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Agent&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Double DQN, 3-step returns, Huber loss, gradient clipping, soft target updates, optional prioritized replay&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Network&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;19 → 96 → 96 → 3, 11,523 parameters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reproducible&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;one seed controls everything, and two runs with the same seed give bit-identical models&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The model is small on purpose. The interesting part is that a training loop of this kind runs end to end in a language I wrote, on its own interpreter.&lt;/p&gt;
&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;You need KingCobra 1.0 (&lt;code&gt;kingcobra&lt;/code&gt; on your &lt;code&gt;PATH&lt;/code&gt;), and &lt;code&gt;node&lt;/code&gt; only for the front-end tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/DivyanshuSinha136/KingCobra-Snake-DQN
&lt;span class="nb"&gt;cd&lt;/span&gt; KingCobra-Snake-DQN

make serve    &lt;span class="c"&gt;# open http://127.0.0.1:8765, a pre-trained model is included&lt;/span&gt;
make watch    &lt;span class="c"&gt;# or watch it play in the terminal&lt;/span&gt;
make train    &lt;span class="c"&gt;# retrain from scratch (about 2 minutes on one CPU core)&lt;/span&gt;
make &lt;span class="nb"&gt;test&lt;/span&gt;     &lt;span class="c"&gt;# 48 KingCobra tests plus front-end tests&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;make&lt;/code&gt;? Every target is one command: &lt;code&gt;kingcobra run src/main.kc &amp;lt;command&amp;gt; [options]&lt;/code&gt;, where the commands are &lt;code&gt;train&lt;/code&gt;, &lt;code&gt;eval&lt;/code&gt;, &lt;code&gt;watch&lt;/code&gt;, &lt;code&gt;serve&lt;/code&gt; and &lt;code&gt;info&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffmjam8619gtl5f7e0eol.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ffmjam8619gtl5f7e0eol.gif" alt="Gameplay recording: the trained agent plays, a human takes over, then the AI resumes" width="720" height="428"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;28 seconds recorded from the real dashboard: the AI plays, a human takes over with the arrow keys, and the AI resumes.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The dashboard
&lt;/h2&gt;

&lt;p&gt;The front-end is a dependency-free canvas page, served by KingCobra's own &lt;code&gt;std.http&lt;/code&gt; under a strict Content-Security-Policy. It shows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the snake with smooth animation,&lt;/li&gt;
&lt;li&gt;the network's &lt;strong&gt;Q-values&lt;/strong&gt; for the three possible moves, with the greedy choice highlighted,&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;19 inputs&lt;/strong&gt; it is looking at,&lt;/li&gt;
&lt;li&gt;training curves, a model card, and a server-side &lt;strong&gt;benchmark&lt;/strong&gt; with a score histogram and a breakdown of how games ended.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can take over at any time with the arrow keys, and the network keeps showing what it would have done. It also works on a phone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08ff5j5x0dkyb7c2vrnq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F08ff5j5x0dkyb7c2vrnq.png" alt="Taking over from the AI: the input inspector shows danger straight lit up, and the network recommends turning right" width="799" height="512"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Human mode: steer with the arrow keys while the network shows what it would do.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr1wy67q11jaxm8fe2fpj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr1wy67q11jaxm8fe2fpj.png" alt="Q-value bars for the three moves" width="800" height="293"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;What the network wants: one bar per move, with the greedy choice highlighted.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;State.&lt;/strong&gt; The network doesn't see pixels. It gets 19 numbers expressed &lt;em&gt;relative to the snake's heading&lt;/em&gt;: danger on the left, straight ahead and on the right, where the food is (ahead, behind, left or right), how many free cells there are before an obstacle on each side, the body length, and a &lt;strong&gt;flood-fill estimate of the free space reachable after each possible move&lt;/strong&gt;. The flood-fill inputs are what teach the agent not to box itself into a pocket. Removing them drops the mean score from 33 to 21.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learning.&lt;/strong&gt; For a minibatch of transitions, the target for the action that was taken is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;y = r_n + γⁿ · (1 − done) · Q_target(s', argmax_a Q_online(s', a))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The online network &lt;em&gt;picks&lt;/em&gt; the next action and the target network &lt;em&gt;scores&lt;/em&gt; it. That's Double DQN, and it removes the over-estimation bias of plain DQN. The whole update is a handful of tensor operations with no per-sample loops, so a batch of 64 costs a fraction of a millisecond. The vectorised target is unit-tested against a one-sample-at-a-time reference to 1e-9.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8pnnhssz8ojal3dv56mq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8pnnhssz8ojal3dv56mq.png" alt="Training curves: greedy evaluation score, training score and epsilon over 800 episodes" width="800" height="302"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The greedy evaluation score climbs as epsilon decays. The circled point is the best checkpoint (episode 650), and that's the model that ships.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the 800-episode run, the evaluation score rises from 3.7 to 36.6. Hyper-parameters and an ablation are in &lt;code&gt;docs/TRAINING.md&lt;/code&gt;, and every tunable lives in &lt;code&gt;config/default.json&lt;/code&gt;. Unknown keys and nonsensical values are rejected with a clear message instead of silently falling back to a default.&lt;/p&gt;

&lt;h2&gt;
  
  
  The code layout
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  main.kc        CLI (train · eval · watch · serve · info)
  config.kc      defaults, JSON overlay, --set overrides, validation
  env.kc         Snake rules, rewards, ASCII renderer
  features.kc    state encoder (+ flood fill)
  replay.kc      circular buffer, optional prioritised sampling (sum tree)
  agent.kc       Double DQN: networks, vectorised learning step, save/load
  trainer.kc     training loop, evaluation, checkpointing, metrics
  server.kc      routing, sessions, security headers, static assets
  util.kc        RNG, statistics, helpers
web/             index.html · style.css · app.js
config/          default.json
models/          pre-trained model + model card + curves
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server exposes a small JSON API (&lt;code&gt;/api/state&lt;/code&gt;, &lt;code&gt;/api/reset&lt;/code&gt;, &lt;code&gt;/api/ai-step&lt;/code&gt;, &lt;code&gt;/api/human&lt;/code&gt;, &lt;code&gt;/api/model&lt;/code&gt;, &lt;code&gt;/api/metrics&lt;/code&gt;, &lt;code&gt;/api/benchmark&lt;/code&gt; and &lt;code&gt;/api/health&lt;/code&gt;). Games are per-session, so several browser tabs never share a board.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tested like a real project
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;make test&lt;/code&gt; runs &lt;strong&gt;48 KingCobra tests&lt;/strong&gt; covering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the game rules, including the tail-cell rule and a perfect-game win,&lt;/li&gt;
&lt;li&gt;feature semantics and rotation-equivariance,&lt;/li&gt;
&lt;li&gt;the sum tree and importance weights,&lt;/li&gt;
&lt;li&gt;the Double-DQN target against a reference,&lt;/li&gt;
&lt;li&gt;exact save and load round-trips,&lt;/li&gt;
&lt;li&gt;determinism of whole training runs,&lt;/li&gt;
&lt;li&gt;and the real server booted as a child process and driven over HTTP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are also unit tests for the front-end's pure functions, and a smoke test that runs the real &lt;code&gt;app.js&lt;/code&gt; against a live server with a stub DOM.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The server is single-threaded.&lt;/strong&gt; A benchmark request blocks other requests while it runs, and it's capped at 50 games.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training runs on the interpreter, on the CPU,&lt;/strong&gt; at about 700 environment steps per second including learning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The numbers come from one seed.&lt;/strong&gt; Differences of a few points between algorithm variants are within run-to-run noise.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only 10×10 boards ship trained.&lt;/strong&gt; Other sizes work through &lt;code&gt;env.size&lt;/code&gt; but need their own training run.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;I wanted a project that stresses &lt;code&gt;std.nn&lt;/code&gt; the way real work would: a vectorised learning step, a replay buffer, a checkpointed training loop, determinism and a live app on top. If you want the background, my guide to &lt;code&gt;std.nn&lt;/code&gt; covers reinforcement learning with DQN and Double DQN on a grid world: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Get it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Snake DQN:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra-Snake-DQN" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra-Snake-DQN&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KingCobra:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MIT licensed. If you retrain it with other settings, or beat 33.1, tell me the config and I'll link it. A star helps other people find it.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>machinelearning</category>
      <category>gamedev</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Speech in KingCobra: the complete guide to std.tts</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Fri, 20 Nov 2026 20:10:38 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/speech-in-kingcobra-the-complete-guide-to-stdtts-36</link>
      <guid>https://dev.to/divyanshusinha136/speech-in-kingcobra-the-complete-guide-to-stdtts-36</guid>
      <description>&lt;p&gt;KingCobra's standard library includes &lt;strong&gt;&lt;code&gt;std.tts&lt;/code&gt;&lt;/strong&gt;, a native, dependency-free English speech synthesizer. There is no network, no downloaded voices and no operating-system speech service: it's C code inside the interpreter, it runs offline, and the same text, options and seed always produce the same samples, byte for byte. I've written a book-length guide to it, &lt;strong&gt;"Speech in KingCobra: The Complete Guide to std.tts"&lt;/strong&gt; (first edition, November 2026, for KingCobra 1.0.0), and this post is the short version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read the guide (PDF, 45 pages):&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_tts_Complete_Guide.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_tts_Complete_Guide.pdf&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Speech to a WAV file in one line
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight d"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_wav&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello! KingCobra can read $5.50, 10/28/2026 and 3:30 pm."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"hello.wav"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"voice=narrator"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frames&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;to_wav&lt;/code&gt; writes a 16-bit mono WAV at 22,050 Hz and returns the path, the length in seconds, the frame count and the file size. The first thing the engine does is turn that text into words, and you can see exactly what it will say before you listen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Dr. Smith paid $5.50 on 10/28/2026 at 3:30 pm for 4.5GB."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;doctor Smith paid five dollars and fifty cents on October twenty eighth twenty twenty six at three thirty P M for four point five gigabytes.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The idea: look before you listen
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std.tts&lt;/code&gt; is a pipeline, and each stage can be inspected on its own:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;text → clean-up → normalise → tokenise → pronounce → plan prosody → formant synthesizer → audio
        (normalize)            (words)     (phonemes)               (align)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the main design rule of the guide. A sentence that sounds wrong can almost always be diagnosed in text: &lt;code&gt;normalize&lt;/code&gt; shows the words, &lt;code&gt;phonemes&lt;/code&gt; and &lt;code&gt;explain&lt;/code&gt; show the pronunciation and which rule produced it, &lt;code&gt;words&lt;/code&gt; shows how tags were understood and &lt;code&gt;align&lt;/code&gt; shows the timeline. You listen last.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture: a classic pipeline, written from scratch in C
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std.tts&lt;/code&gt; isn't a wrapper around someone else's speech engine. It's a small, classic text-to-speech pipeline written from scratch in about 3,600 lines of C and linked into the interpreter. Each stage lives in its own source file and hands a plain data structure to the next one, which is why every stage can be inspected from KingCobra.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Stage&lt;/th&gt;
&lt;th&gt;Source file (&lt;code&gt;std/tts/&lt;/code&gt;)&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;Inspect it with&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Front end&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tts_text.c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unicode clean-up, normalisation (numbers, years, dates, times, currency, units, abbreviations, URLs, camelCase), the SSML subset, inline &lt;code&gt;{phonemes}&lt;/code&gt;, and the tokeniser&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;normalize&lt;/code&gt;, &lt;code&gt;words&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pronunciation&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tts_g2p.c&lt;/code&gt;, &lt;code&gt;tts_dict.c&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The seven-step word path (homographs, lexicon, dictionary, morphology, compounds, acronyms, letter-to-sound rules) plus stress assignment&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;phonemes&lt;/code&gt;, &lt;code&gt;explain&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Phonemes and voices&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tts_phon.c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The 41-symbol inventory, articulatory parameters, ARPAbet and IPA conversion, and the built-in voices&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;phoneme_table&lt;/code&gt;, &lt;code&gt;voice_info&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Styles&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tts_style.c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The 20-parameter styles, tags, sound events and interjections, with typo correction&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;styles&lt;/code&gt;, &lt;code&gt;style_info&lt;/code&gt;, &lt;code&gt;resolve_tag&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Planning&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tts_plan.c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Phone durations, articulatory segments and the pitch contour&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;duration&lt;/code&gt;, &lt;code&gt;align&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Synthesis&lt;/td&gt;
&lt;td&gt;&lt;code&gt;tts_synth.c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The formant synthesizer that turns the plan into samples&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;speak&lt;/code&gt;, &lt;code&gt;to_wav&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Glue&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;tts.c&lt;/code&gt;, &lt;code&gt;tts_internal.h&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;The entry points, option parsing, and the structures shared between stages&lt;/td&gt;
&lt;td&gt;&lt;code&gt;info&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Here is one sentence followed through those stages, with the real output at each step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;text      Dr. Smith paid $5.50 [sigh] today.
normalize doctor Smith paid five dollars and fifty cents today.
phonemes  D AA1 K T ER0 | S M IH1 TH | P EY1 D | F AY1 V | D AA1 L ER0 Z | AE1 N D | F IH1 F T IY0 | S EH1 N T S | T AH0 D EY1 .
align     doctor 0.04-0.414 · smith 0.414-0.731 · paid 0.731-1.063 · five 1.063-1.417 · dollars 1.417-1.851
          and 1.851-2.006 · fifty 2.006-2.413 · cents 2.413-2.854 · [sigh] 2.854-3.864 · today 3.864-4.308
audio     100,073 frames, 4.538 s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;normalize&lt;/code&gt; and &lt;code&gt;phonemes&lt;/code&gt; lines were run on the sentence without its tag, because &lt;code&gt;normalize&lt;/code&gt; shows only the text stage and doesn't interpret &lt;code&gt;[tags]&lt;/code&gt;. &lt;code&gt;align&lt;/code&gt; is where the &lt;code&gt;[sigh]&lt;/code&gt; shows up, as a one-second event in the timeline.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the planner decides what it sounds like
&lt;/h3&gt;

&lt;p&gt;The planner (&lt;code&gt;tts_plan.c&lt;/code&gt;) turns phonemes into timed articulatory segments and a pitch contour. Grammatical words such as &lt;code&gt;the&lt;/code&gt;, &lt;code&gt;of&lt;/code&gt; and &lt;code&gt;can&lt;/code&gt; are reduced and shortened, and &lt;strong&gt;content words carry the pitch accents&lt;/strong&gt; on their stressed syllable. Durations follow Klatt-style rules: stress, consonant clusters, shortening before voiceless consonants and phrase-final lengthening. Pitch follows a declining baseline with accent peaks, so statements fall, yes/no questions rise, and commas give a continuation rise. Stops are built as closure, burst and aspiration, and US flapping or UK r-dropping is applied here, which is why the phoneme string doesn't depend on &lt;code&gt;accent&lt;/code&gt; and the accent only appears at synthesis time.&lt;/p&gt;

&lt;h3&gt;
  
  
  How the synthesizer makes the sound
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;tts_synth.c&lt;/code&gt; is a &lt;strong&gt;Klatt-style formant synthesizer&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Voicing:&lt;/strong&gt; a Rosenberg glottal pulse, shaped by open quotient, spectral tilt, jitter, shimmer and breathiness.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vocal tract:&lt;/strong&gt; a cascade of five formant resonators (F1 to F5) plus a nasal pole and zero pair.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Noise:&lt;/strong&gt; aspiration goes through the same cascade, while frication and stop bursts go through a parallel band-pass filter. Voiced fricatives are modulated by the glottal cycle, and a whisper replaces voicing with aspiration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control:&lt;/strong&gt; the planner's segments become piecewise-linear parameter tracks that are evaluated every 2 ms. Formant tracks coarticulate across phone boundaries, and stops glide from their place into the next vowel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The formant values come from the classic Peterson and Barney / Klatt (1980) averages for an adult male, and the other voices scale them. That's what the &lt;code&gt;tract&lt;/code&gt; parameter in &lt;code&gt;define_voice&lt;/code&gt; does: it multiplies the formants, so a longer tract gives a larger, lower-sounding speaker.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it's built this way
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every boundary is observable.&lt;/strong&gt; A stage's output is a plain structure (words, phones, segments), so there is a function to look at it. You debug in text and listen last.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's deterministic.&lt;/strong&gt; The only randomness is the noise and jitter, which come from one seeded generator, so the same text, options and seed give the same samples.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing is outside the module.&lt;/strong&gt; No network, no voice files and no operating-system speech service, which makes the output the same on every platform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The output is just audio.&lt;/strong&gt; The result is a &lt;code&gt;std.music_note&lt;/code&gt; buffer, so filters, reverb and mixing come for free.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the guide covers
&lt;/h2&gt;

&lt;p&gt;The guide has three parts and three appendices:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part I: Meet &lt;code&gt;std.tts&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Design and conventions:&lt;/strong&gt; the pipeline, determinism, the rules for options, errors and audio buffers, and a short list of what &lt;code&gt;std.tts&lt;/code&gt; is &lt;em&gt;not&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speaking:&lt;/strong&gt; &lt;code&gt;speak&lt;/code&gt;, &lt;code&gt;to_wav&lt;/code&gt;, &lt;code&gt;say&lt;/code&gt;, &lt;code&gt;save_wav&lt;/code&gt;, file sizes and sample rates, and the seed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Part II: Controlling the voice&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Options reference:&lt;/strong&gt; every option with its default and valid range (voice, rate, wpm, pitch, range, sr, pause, accent, reverb and more), plus measurements of what each really does to the timing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text normalisation:&lt;/strong&gt; numbers, money, dates, units, times, abbreviations and identifiers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pronunciation:&lt;/strong&gt; how a word's phonemes are chosen, and how to fix one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Vocal styles and expressive tags.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Voices.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSML.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Part III: Timing, mixing and practice&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Timing, subtitles and lip-sync.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mixing with &lt;code&gt;std.music_note&lt;/code&gt;.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A complete program&lt;/strong&gt; that builds an SRT subtitle file.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance and limits.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Common mistakes.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A verification checklist&lt;/strong&gt; for speech code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Work problems,&lt;/strong&gt; with answers only.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Appendices:&lt;/strong&gt; a cheat sheet, reference tables (functions grouped by purpose, the built-in voices, the 41-symbol phoneme set and style parameter ranges), and a map of the pipeline in the source tree. Boxes marked &lt;em&gt;Gotcha&lt;/em&gt;, &lt;em&gt;Measured&lt;/em&gt;, &lt;em&gt;Expert tip&lt;/em&gt; and &lt;em&gt;Note&lt;/em&gt; carry the non-obvious material.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pronunciation you can debug
&lt;/h2&gt;

&lt;p&gt;For each word the engine tries, in order, homograph rules, your lexicon, the built-in 1,215-word dictionary, morphology, compound splitting, acronym spelling, and about 330 letter-to-sound rules. &lt;code&gt;explain&lt;/code&gt; tells you which one fired:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_word&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kobold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"K OW1 B OW0 L D"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kobold"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// lexicon&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kobolds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// morphology&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;explain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"kobolds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phonemes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// K OW1 B OW0 L D Z&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One &lt;code&gt;add_word&lt;/code&gt; covers every inflection, because morphology builds on the lexicon. Homographs are chosen from context, so &lt;code&gt;read&lt;/code&gt; is pronounced differently in "I read it" and "I have read it". You can also load a full CMUdict file for dictionary-grade coverage, or override one place inline with &lt;code&gt;"say {K IH1 NG K OW1 B R AH0} now"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Styles, tags and SSML
&lt;/h2&gt;

&lt;p&gt;Put a tag in square brackets anywhere in the text, and everything after it is spoken in that style:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_wav&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Oh no [sigh] I really thought we had it. [sad] It's over. [laugh] Just kidding! [excited] We won!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"out.wav"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 54 built-in styles (35 moods and 19 manners), 17 sound events such as &lt;code&gt;[laugh]&lt;/code&gt;, &lt;code&gt;[sigh]&lt;/code&gt; and &lt;code&gt;[cough]&lt;/code&gt;, and 24 interjections such as &lt;code&gt;[wow]&lt;/code&gt; and &lt;code&gt;[ugh]&lt;/code&gt;. Tags are forgiving about case and typos, so &lt;code&gt;[wisper]&lt;/code&gt; resolves to &lt;code&gt;whisper&lt;/code&gt;. Intensity words, numeric intensity and combinations work too: &lt;code&gt;[very sad]&lt;/code&gt;, &lt;code&gt;[sad:0.5]&lt;/code&gt;, &lt;code&gt;[sad+whisper]&lt;/code&gt;. Each style is a bundle of 20 parameters, and you can define your own:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define_style&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"grumpy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"angry"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;pitch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;creak&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aliases&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"cranky"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define_voice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hero"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"male"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;f0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;135&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tract&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.08&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;breath&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.08&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_wav&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"[grumpy] Get off my lawn."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"lawn.wav"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;voice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"hero"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pitch&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are 10 built-in voices (&lt;code&gt;male&lt;/code&gt;, &lt;code&gt;female&lt;/code&gt;, &lt;code&gt;child&lt;/code&gt;, &lt;code&gt;teen&lt;/code&gt;, &lt;code&gt;deep&lt;/code&gt;, &lt;code&gt;elder&lt;/code&gt;, &lt;code&gt;narrator&lt;/code&gt;, &lt;code&gt;whisper&lt;/code&gt;, &lt;code&gt;robot&lt;/code&gt; and &lt;code&gt;alien&lt;/code&gt;), and you can define more. A subset of SSML is supported as well: &lt;code&gt;break&lt;/code&gt;, &lt;code&gt;prosody&lt;/code&gt;, &lt;code&gt;emphasis&lt;/code&gt;, &lt;code&gt;phoneme&lt;/code&gt;, &lt;code&gt;sub&lt;/code&gt;, &lt;code&gt;say-as&lt;/code&gt;, &lt;code&gt;voice&lt;/code&gt; and &lt;code&gt;mark&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Timing for subtitles and lip-sync
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;align&lt;/code&gt; returns the whole timeline: each word with start and end times, its phonemes, and each phoneme with a &lt;strong&gt;viseme&lt;/strong&gt;, a mouth-shape id from 0 to 15.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;align&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Good morning, world."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;phonemes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;good 0.04 0.2558 G UH1 D
morning 0.2558 0.732889 M AO1 R N IH0 NG
world 0.902889 1.27509 W ER1 L D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guide's worked example uses this to build an SRT subtitle file: it groups words into cues wherever a silence of 0.2 s or more appears. Here is what it produces for a short script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
00:00:00,040 --&amp;gt; 00:00:01,044
the kobold said

2
00:00:01,299 --&amp;gt; 00:00:02,574
king cobra is ready

3
00:00:02,954 --&amp;gt; 00:00:03,199
is it

4
00:00:03,598 --&amp;gt; 00:00:04,200
yes it is
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because synthesis is deterministic, the times from &lt;code&gt;align&lt;/code&gt; line up with the audio from &lt;code&gt;to_wav&lt;/code&gt; for the same text and options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speech is audio
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;speak&lt;/code&gt; returns a regular &lt;code&gt;std.music_note&lt;/code&gt; buffer, so everything in that module applies to speech: filters, reverb, mixing and saving. The synthesizer runs at whatever sample rate you ask for, so render speech at &lt;code&gt;sr=44100&lt;/code&gt; or &lt;code&gt;sr=48000&lt;/code&gt; to match the music it will join, with no resampling step. A band-pass filter on the &lt;code&gt;robot&lt;/code&gt; voice gives a radio effect in two lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Robots do not dream."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"voice=robot"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;radio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;music_note&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bandpass"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1500.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Things the guide warns about
&lt;/h2&gt;

&lt;p&gt;The "common mistakes" section is the most practical page:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A typo in an option name&lt;/strong&gt; (&lt;code&gt;"rat=2"&lt;/code&gt;) is silently ignored. Out-of-range values raise an error, but unknown names don't, so check with &lt;code&gt;duration&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rate=2&lt;/code&gt; doesn't halve the duration.&lt;/strong&gt; The sentence goes from 2.858 s to 1.752 s, not 1.429 s, because consonants compress less than vowels.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;normalize&lt;/code&gt; doesn't show tags or substitutions.&lt;/strong&gt; Use &lt;code&gt;words&lt;/code&gt; for tags and &lt;code&gt;phonemes&lt;/code&gt; for the final result.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Free buffers from &lt;code&gt;speak&lt;/code&gt;&lt;/strong&gt; with &lt;code&gt;tts.free&lt;/code&gt;, or use &lt;code&gt;to_wav&lt;/code&gt;, which frees its own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lexicon, substitutions, voices and styles are global&lt;/strong&gt; to the running program, so define them once at start-up.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Speed and honest limits
&lt;/h2&gt;

&lt;p&gt;The guide reports 30 copies of a sentence (88.9 s of speech) synthesized and written in 137 ms, about 650 times real time on its machine. Treat that as an order of magnitude, since your hardware will differ.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It's a formant synthesizer.&lt;/strong&gt; It's clear and fully controllable, but unmistakably synthetic. It isn't a neural voice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;English only.&lt;/strong&gt; Other scripts are skipped, and foreign words go through English rules unless you add them with &lt;code&gt;add_word&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The dictionary is deliberately compact.&lt;/strong&gt; The rule engine is about 85 to 90 percent accurate on regular vocabulary, so load a pronouncing dictionary for a specific domain.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Styles and events are cues, not recordings.&lt;/strong&gt; A laugh or a sigh is convincing as a signal, not as a human sound.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Text up to 400,000 characters per call,&lt;/strong&gt; and Roman numerals, chemical formulas and tables aren't interpreted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Every example was run
&lt;/h2&gt;

&lt;p&gt;The guide was written for KingCobra 1.0.0, and every program in it was run on the real interpreter, with the output shown as printed. The numbers that depend on the synthesizer (durations, frame counts, file sizes) are exact for the default settings and repeatable, because the synthesizer is deterministic. The work problems in Chapter 15 give five groups of problems with only the final answer, plus six open challenges. Chapter 14 is a 9-step verification checklist for speech code, and Appendix A is a cheat sheet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Guide (PDF):&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_tts_Complete_Guide.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_tts_Complete_Guide.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repo:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;, with the compact reference in &lt;code&gt;docs/STD_TTS.md&lt;/code&gt; and a runnable tour in &lt;code&gt;examples/std_tts.kc&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The pipeline in the source tree&lt;/strong&gt; (&lt;code&gt;std/tts/&lt;/code&gt;): &lt;code&gt;tts_text.c&lt;/code&gt; (normalisation, tags and SSML), &lt;code&gt;tts_g2p.c&lt;/code&gt; and &lt;code&gt;tts_dict.c&lt;/code&gt; (pronunciation), &lt;code&gt;tts_plan.c&lt;/code&gt; (prosody and durations), &lt;code&gt;tts_style.c&lt;/code&gt; (styles) and &lt;code&gt;tts_synth.c&lt;/code&gt; (the formant synthesizer)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New to the language?&lt;/strong&gt; Start with the &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_Complete_Expert_Guide.pdf" rel="noopener noreferrer"&gt;Complete Expert Guide&lt;/a&gt; or the &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;tutorial folder&lt;/a&gt;. The guide assumes you know the language.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a word comes out wrong, or you find a pronunciation or a style that behaves differently from the guide, please open an issue. A star on the repo helps other people find it.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>audio</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Cobra Music Studio: a music player, a DAW and a graphics engine, all written in KingCobra</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Fri, 20 Nov 2026 03:41:12 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/cobra-music-studio-a-music-player-a-daw-and-a-graphics-engine-all-written-in-kingcobra-4f0i</link>
      <guid>https://dev.to/divyanshusinha136/cobra-music-studio-a-music-player-a-daw-and-a-graphics-engine-all-written-in-kingcobra-4f0i</guid>
      <description>&lt;p&gt;&lt;strong&gt;Cobra Music Studio&lt;/strong&gt; is three programs in one window: a music player, a music production studio, and a graphics stack built from scratch. It is written entirely in &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;KingCobra&lt;/a&gt;, my programming language, in about 7,000 lines with no native extensions.&lt;/p&gt;

&lt;p&gt;Repo: &lt;strong&gt;&lt;a href="https://github.com/DivyanshuSinha136/Cobra-Music" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Cobra-Music&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgsbxe79eb90inw7dy22q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgsbxe79eb90inw7dy22q.png" alt="Cobra Music Studio: library and player" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The library and player: a sortable library, a waveform seek bar and a mini spectrum.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What it is
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A music player&lt;/strong&gt; with a library, playlists, a queue, an equalizer and live visualizers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A production studio&lt;/strong&gt; with a step sequencer, a piano-roll editor and a multi-track mixer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A graphics stack&lt;/strong&gt; with vector icons, TrueType text and anti-aliased shapes, built on top of the plain 2D canvas of &lt;code&gt;std.game&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The app doesn't use &lt;code&gt;std.gui&lt;/code&gt;. The widget toolkit, the SVG renderer and the font renderer are all KingCobra code. Everything else comes from the standard library: &lt;code&gt;std.game&lt;/code&gt; for the canvas and input, &lt;code&gt;std.music_note&lt;/code&gt; for synthesis and DSP, &lt;code&gt;std.system&lt;/code&gt; for processes and files, and &lt;code&gt;std.json&lt;/code&gt; for saved state.&lt;/p&gt;
&lt;h2&gt;
  
  
  The player
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Library:&lt;/strong&gt; built-in generated tracks, notation scores (&lt;code&gt;.kcm&lt;/code&gt;) and any &lt;code&gt;.wav&lt;/code&gt; in your folders. With &lt;code&gt;ffmpeg&lt;/code&gt; installed, it also imports mp3, flac, ogg, m4a and opus. It has search across title, artist, album and genre, sortable columns, favorites, play counts, and recently and most played lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Playback:&lt;/strong&gt; click or drag the &lt;strong&gt;waveform&lt;/strong&gt; to seek. Speed runs from 0.5x to 1.5x with pitch preserved, and there's shuffle, repeat, a sleep timer and &lt;strong&gt;loudness matching&lt;/strong&gt; that evens out track levels without pushing peaks into clipping.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never freezes:&lt;/strong&gt; heavy renders run in a &lt;strong&gt;background KingCobra process&lt;/strong&gt;, so the window stays responsive, and the next queued track is prefetched.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No audio device?&lt;/strong&gt; It plays through the system player (&lt;code&gt;pw-play&lt;/code&gt;, &lt;code&gt;paplay&lt;/code&gt;, &lt;code&gt;aplay&lt;/code&gt;, &lt;code&gt;ffplay&lt;/code&gt;, &lt;code&gt;afplay&lt;/code&gt; or the Windows SoundPlayer), and with none available it falls back to a simulated clock, so everything stays usable, even on a server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2x4apffqyvatqvo5lcfy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2x4apffqyvatqvo5lcfy.png" alt="Equalizer and effects" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The equalizer, with the Bass Boost preset and a live response curve.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Equalizer and effects.&lt;/strong&gt; Ten bands from 32 Hz to 16 kHz, presets, and a live frequency-response curve with the spectrum behind it. There are also a pre-amp, a bass shelf, stereo width, reverb, a compressor and a limiter, and you can export the processed track to WAV.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Visualizers.&lt;/strong&gt; Six modes (bars, mirror, scope, radial, waterfall and rings) are driven by a real 2048-point FFT of the audio that's playing. There are five palettes, stereo VU meters with peak hold, and beat detection that pulses the logo and cover art.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fue1su4kyx5urhy95h9kk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fue1su4kyx5urhy95h9kk.png" alt="Radial visualizer in the Neon theme" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The radial visualizer with the rainbow palette in the Neon theme.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The production studio
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftedehl7euae20q7xwkrn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftedehl7euae20q7xwkrn.png" alt="Studio: step sequencer" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The step sequencer: drum lanes with accents and ghost notes, next to the mixer.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step sequencer:&lt;/strong&gt; 8 drum lanes, up to 8 bars, drag-paint, with accents and ghost notes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Piano roll:&lt;/strong&gt; scale highlighting, note preview, drag to move and resize, mouse wheel for velocity, a chord stamp (triad, 7th, power, sus4) and 808 slide.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mixer:&lt;/strong&gt; up to 8 tracks, 20 synth instruments or your own &lt;strong&gt;WAV sampler&lt;/strong&gt;, per-track volume, pan, reverb, mute and solo, &lt;strong&gt;sidechain ducking&lt;/strong&gt; from the kick, humanize, swing and mastering presets.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generators:&lt;/strong&gt; House, Trap, Funk and Lo-fi templates, plus random patterns, arpeggios, chord progressions, basslines and drum fills.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workflow:&lt;/strong&gt; 40-step undo, a &lt;strong&gt;Live&lt;/strong&gt; mode that re-renders while you edit, project save and open, and crash-recovery autosave.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Export:&lt;/strong&gt; WAV, MIDI, one WAV per track (stems), or add the score to your library as &lt;code&gt;.kcm&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3uyq6zjv75tpxrqjl2dr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3uyq6zjv75tpxrqjl2dr.png" alt="Studio: piano roll" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The piano roll, with velocity, chord stamp, and drag to move and resize notes.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The graphics stack, from scratch
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std.game&lt;/code&gt; draws rectangles, bitmaps and an 8x8 pixel font. Everything beyond that is KingCobra:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;src/vec.kc&lt;/code&gt;&lt;/strong&gt; parses SVG (paths with &lt;code&gt;M L H V C S Q T A Z&lt;/code&gt;, shapes, groups, transforms, fill, stroke, opacity and &lt;code&gt;style&lt;/code&gt;) and rasterises it with exact-area anti-aliasing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;src/ttf.kc&lt;/code&gt;&lt;/strong&gt; reads TrueType fonts: &lt;code&gt;head&lt;/code&gt;, &lt;code&gt;maxp&lt;/code&gt;, &lt;code&gt;hhea&lt;/code&gt;, &lt;code&gt;hmtx&lt;/code&gt;, &lt;code&gt;cmap&lt;/code&gt; (formats 4 and 12), &lt;code&gt;loca&lt;/code&gt; and &lt;code&gt;glyf&lt;/code&gt;, including composite glyphs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;src/gfx.kc&lt;/code&gt;&lt;/strong&gt; keeps glyphs, icons, discs and rounded corners in one texture atlas and draws them as tinted sprites, with proportional UTF-8 text in regular, bold and display weights.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first frame takes about 0.25 s to warm up, and after that frames take 2 to 7 ms. Icons are plain &lt;code&gt;.svg&lt;/code&gt; files that the theme tints at draw time, so adding one takes a few lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ui.icon("bell", cx, cy, 20, T.accent);   // any theme colour
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcd22larfjlekrqeks7eb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcd22larfjlekrqeks7eb.png" alt="Score view" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The score view: a piano roll of the playing arrangement.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Five themes
&lt;/h2&gt;

&lt;p&gt;Midnight is the default. The others are Neon, Sunset, Forest and Paper.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2o41zny3bez3c0ocr542.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2o41zny3bez3c0ocr542.png" alt="Neon theme" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Neon&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi2ol3zungcbc7brwjaw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi2ol3zungcbc7brwjaw8.png" alt="Paper theme" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Paper&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  A design lesson from building it
&lt;/h2&gt;

&lt;p&gt;KingCobra creates a separate module instance for every &lt;code&gt;import&lt;/code&gt;, so module-level variables aren't shared between files. The app handles that by keeping &lt;strong&gt;all application state in one object that is passed around explicitly&lt;/strong&gt;. The UI is an immediate-mode widget toolkit, and a single guarded frame loop runs everything.&lt;/p&gt;

&lt;p&gt;That guard matters. Every frame is wrapped, so a bug in one screen is written to &lt;code&gt;error.log&lt;/code&gt; and the music keeps playing. Repeated faults fall back to the library screen.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tested by driving the real app
&lt;/h2&gt;

&lt;p&gt;The tests run the real application headlessly: they inject mouse and keyboard events, render frames and assert on the result. There are four suites with &lt;strong&gt;123 checks&lt;/strong&gt; in all:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Suite&lt;/th&gt;
&lt;th&gt;Checks&lt;/th&gt;
&lt;th&gt;Covers&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tests/ui_test.kc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;td&gt;search, sorting, menus, playback, seeking, volume, EQ, playlists, persistence&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tests/studio_test.kc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;18&lt;/td&gt;
&lt;td&gt;sequencer and piano-roll clicks, undo, render, save and open, exports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tests/mature_test.kc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;38&lt;/td&gt;
&lt;td&gt;velocity, sidechain, sampler, stems, drag editing, crash guard, cache limit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;tests/gfx_test.kc&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;29&lt;/td&gt;
&lt;td&gt;SVG rasteriser accuracy, TrueType parsing, text measurement, atlas&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Even the README banner, badges, architecture diagram and screenshots are produced by KingCobra programs (&lt;code&gt;tools/readme_assets.kc&lt;/code&gt; and &lt;code&gt;tools/screenshots.kc&lt;/code&gt;).&lt;/p&gt;
&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;You need KingCobra 1.0 or newer. An audio player and &lt;code&gt;ffmpeg&lt;/code&gt; are optional.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra run main.kc                       &lt;span class="c"&gt;# open the window (1280x720)&lt;/span&gt;
kingcobra run main.kc --size 1600x900       &lt;span class="c"&gt;# larger window&lt;/span&gt;
&lt;span class="nv"&gt;KC_MUSIC_SILENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 kingcobra run main.kc     &lt;span class="c"&gt;# no audio device: simulated playback&lt;/span&gt;
kingcobra run main.kc --headless            &lt;span class="c"&gt;# no display: render frames headlessly&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add your music in &lt;strong&gt;Settings&lt;/strong&gt;, or drop files into the &lt;code&gt;music/&lt;/code&gt; folder. The Studio is on key &lt;code&gt;8&lt;/code&gt;, and &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;8&lt;/code&gt; switch screens. &lt;code&gt;Space&lt;/code&gt; plays and pauses, &lt;code&gt;Left&lt;/code&gt; and &lt;code&gt;Right&lt;/code&gt; seek, and &lt;code&gt;/&lt;/code&gt; searches.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Changing volume, EQ or speed re-renders the rest of the track, so there's a short gap (about 0.1 to 0.4 s) when you release a control.&lt;/li&gt;
&lt;li&gt;The window size is fixed at start-up (&lt;code&gt;--size WxH&lt;/code&gt;), because &lt;code&gt;std.game&lt;/code&gt; has no live-resize call.&lt;/li&gt;
&lt;li&gt;Fonts have no kerning or hinting, only grayscale anti-aliasing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cobra Music Studio:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/Cobra-Music" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Cobra-Music&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KingCobra:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MIT licensed code, with fonts under the SIL Open Font License. If you try it, tell me what breaks, especially audio setups I haven't seen. A star helps other people find it.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>music</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Peacock OS 1.9: a desktop operating system written entirely in KingCobra</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Fri, 20 Nov 2026 03:30:43 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/peacock-os-19-a-desktop-operating-system-written-entirely-in-kingcobra-35kg</link>
      <guid>https://dev.to/divyanshusinha136/peacock-os-19-a-desktop-operating-system-written-entirely-in-kingcobra-35kg</guid>
      <description>&lt;p&gt;I wrote an operating system, and every line of it, from the boot sector to the web browser, is written in one language: &lt;strong&gt;&lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;KingCobra&lt;/a&gt;&lt;/strong&gt;, a language I also wrote. It's called &lt;strong&gt;Peacock OS&lt;/strong&gt;, version 1.9 is out, and it boots in QEMU on a laptop and on an Android phone.&lt;/p&gt;

&lt;p&gt;That includes the &lt;strong&gt;bootloader&lt;/strong&gt;. The MBR, the stage-2 loader that switches the CPU into 64-bit mode, and the kernel are all KingCobra, not just the kernel.&lt;/p&gt;

&lt;p&gt;Repo: &lt;strong&gt;&lt;a href="https://github.com/DivyanshuSinha136/Peacock" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Peacock&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk2h8if9vfs2plx6bzihn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk2h8if9vfs2plx6bzihn.png" alt="Peacock OS desktop" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why build it
&lt;/h2&gt;

&lt;p&gt;A language is only credible once it has carried something large. KingCobra is meant to feel like a scripting language while being able to do systems work, and an OS is the harshest test I could think of: raw memory, interrupts, device drivers, a network stack and a GUI, all at once. Peacock is about 17,000 lines of KingCobra in &lt;code&gt;kernel/&lt;/code&gt;, and the bootloader in &lt;code&gt;boot/&lt;/code&gt; is KingCobra too, with its own small x86 instruction emitter instead of an assembler. All of it is compiled by &lt;code&gt;kcc&lt;/code&gt;, KingCobra's own compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;You need &lt;code&gt;qemu-system-x86_64&lt;/code&gt; and the KingCobra toolchain (&lt;code&gt;kingcobra&lt;/code&gt; and &lt;code&gt;kcc&lt;/code&gt;) checked out next to the Peacock folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make         &lt;span class="c"&gt;# builds peacock.img, a 17 MiB bootable raw disk image&lt;/span&gt;
make run     &lt;span class="c"&gt;# boots it in QEMU&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or run an image directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;qemu-system-x86_64 -m 128 -vga std \
  -nic user,model=rtl8139 \
  -usb -device usb-tablet -device usb-kbd \
  -drive &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;peacock.img,format=raw
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-m 128&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the kernel heap is 120 MiB (use &lt;code&gt;-m 256&lt;/code&gt; for 2560x1440 and above)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-vga std&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Bochs/DISPI adapter, so the resolution can be switched live up to 4096x2400&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-nic user,model=rtl8139&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;the supported NIC: DHCP, DNS and TCP through QEMU's user network&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-usb -device usb-tablet -device usb-kbd&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;a UHCI controller with an absolute pointer and a keyboard&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What's inside
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Boots from nothing, in KingCobra
&lt;/h3&gt;

&lt;p&gt;The bootloader is written in KingCobra, not just the kernel. Stage 1 is the 512-byte MBR at LBA 0. Stage 2 (LBA 1-4) enables A20, reads the memory map, switches the CPU into long mode and loads the kernel, and it's KingCobra too. A tiny x86 instruction emitter (&lt;code&gt;boot/x86.kc&lt;/code&gt;) takes the place of an assembler, so even the code that runs before any operating system exists is KingCobra.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;make boot-selftest&lt;/code&gt; rebuilds the original NASM bootloader and checks that the KingCobra one is &lt;strong&gt;byte-for-byte identical&lt;/strong&gt;. The BIOS path handles multiple A20 methods, E820/E801/88h memory-map fallbacks and a checksummed handover block.&lt;/p&gt;

&lt;h3&gt;
  
  
  A 64-bit rendering pipeline
&lt;/h3&gt;

&lt;p&gt;The back buffer is RGBA16, with 16 bits per channel. Blending and the glass blur happen in 16-bit, and a 4x4 ordered-dither flush writes the result to the 24/32 bpp screen. That means the internal pipeline is 64 bpp even though the output is limited by the display.&lt;/p&gt;

&lt;h3&gt;
  
  
  A glass desktop
&lt;/h3&gt;

&lt;p&gt;A dock and top bar, translucent panels, damage-rectangle redraw, 9 themes, SVG icons and scalable fonts. The desktop stays uncluttered: Files, Browser, Terminal, Notes and Settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxkkk8qpbhm6r5ctxdoin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxkkk8qpbhm6r5ctxdoin.png" alt="Peacock OS launcher" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Firb5rv364gjjx02sjym9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Firb5rv364gjjx02sjym9.png" alt="Nine themes in Settings" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Nine wallpaper themes, button depth and live resolution switching in Settings.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  14 applications
&lt;/h3&gt;

&lt;p&gt;Terminal, Notes 2 (find/replace, selection, undo), Paint, Calculator, System Info, Files, Calendar, Clock, Snake, Minesweeper, System Monitor, Web Browser, App Builder 2 and 3D Studio, plus Settings.&lt;/p&gt;
&lt;h3&gt;
  
  
  KCFS 2, a crash-safe file system
&lt;/h3&gt;

&lt;p&gt;A two-slot store with CRC32 on every block, atomic two-slot commits, &lt;code&gt;fsck&lt;/code&gt;, attributes and a background autosave thread.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwb1skjlhq1rn21yih16s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwb1skjlhq1rn21yih16s.png" alt="Files, the KCFS 2 file manager" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Files, the file manager for KCFS 2.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Kernel threads
&lt;/h3&gt;

&lt;p&gt;A cooperative scheduler with mutexes, semaphores and queues. In the terminal, &lt;code&gt;ps&lt;/code&gt;, &lt;code&gt;kill&lt;/code&gt; and &lt;code&gt;spawn&lt;/code&gt; work on them.&lt;/p&gt;
&lt;h3&gt;
  
  
  A network stack and a browser
&lt;/h3&gt;

&lt;p&gt;An RTL8139 driver, ARP, IPv4, ICMP, UDP, DHCP, DNS, TCP and HTTP. The browser has tabs, bookmarks, find-in-page and an HTML renderer, and I've browsed real HTTP sites with it, including &lt;code&gt;example.com&lt;/code&gt;, &lt;code&gt;info.cern.ch&lt;/code&gt;, &lt;code&gt;neverssl.com&lt;/code&gt; and &lt;code&gt;textfiles.com&lt;/code&gt;. The terminal has &lt;code&gt;wget&lt;/code&gt;, &lt;code&gt;ping&lt;/code&gt; and &lt;code&gt;nslookup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fecfswvoelo3pokg7qa3i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fecfswvoelo3pokg7qa3i.png" alt="Peacock OS web browser" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvskmbgriyk1o4ki7yc0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvskmbgriyk1o4ki7yc0h.png" alt="Network settings: NIC, DHCP lease and DNS" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Settings, Network: the RTL8139 NIC, its DHCP lease and DNS.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  USB
&lt;/h3&gt;

&lt;p&gt;A polled UHCI driver with HID boot keyboard and mouse, an absolute tablet, read-only bulk mass storage and a hot-plug scan. There are no hubs yet.&lt;/p&gt;
&lt;h3&gt;
  
  
  PCI, ACPI and hardware control
&lt;/h3&gt;

&lt;p&gt;PCI enumeration goes through bridges, with BAR sizing and capability walking (PM, MSI, MSI-X, PCIe), device naming and driver binding. There is an ACPI table parser with S5 power-off, a reset register, a CPUID report and a memory map.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjoqkldemxqn51emaiifi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjoqkldemxqn51emaiifi.png" alt="Settings, Devices: PCI functions, bound drivers, ACPI and CPU" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Settings, Devices: PCI functions, bound drivers, ACPI and CPU.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  3D and your own executables
&lt;/h3&gt;

&lt;p&gt;A software 3D rasteriser powers 3D Studio. App Builder 2 compiles a window design into a &lt;code&gt;.pea&lt;/code&gt; executable, a native binary format with its own loader, &lt;code&gt;peadump&lt;/code&gt; and a command-line compiler, &lt;code&gt;peac&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1bcj2e5k7zgy6o44uwd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm1bcj2e5k7zgy6o44uwd.png" alt="3D Studio" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;3D Studio, a software rasteriser.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flnr9nkzo54efza5k4n3k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Flnr9nkzo54efza5k4n3k.png" alt="App Builder 2" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;App Builder 2: design a window, run it, save it as a &lt;code&gt;.pea&lt;/code&gt; executable.&lt;/em&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  A terminal that does real work
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzjc314y26hfqj7f7a7ov.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzjc314y26hfqj7f7a7ov.png" alt="Terminal over a glass backdrop" width="800" height="500"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The terminal over a glass backdrop, with &lt;code&gt;neofetch&lt;/code&gt;-style info.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;files     ls [-la] cd pwd cat mkdir touch rm [-r] cp [-r] mv echo [&amp;gt; file] stat attr fsck sync
network   ifconfig dhcp ping nslookup wget &amp;lt;url&amp;gt; [file]  browse [url]
system    ver uname date uptime mem cpu neofetch  ps kill &amp;lt;id&amp;gt;  spawn counter|spin|hog [n]
hardware  lspci [-v]  lscpu  lsmem  acpi  usb  usb read &amp;lt;sector&amp;gt;
programs  name | ./name.pea   peac &amp;lt;src&amp;gt; [out]   peadump &amp;lt;prog&amp;gt;   run &amp;lt;name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How I test an OS without hardware
&lt;/h2&gt;

&lt;p&gt;QEMU boots are slow and awkward to automate, so most of the testing runs on the host. There are &lt;strong&gt;17 host-side suites&lt;/strong&gt; that run the real kernel code against simulated hardware: disks, PCI buses, UHCI controllers and NICs. That includes about 60 USB checks, crash-recovery tests for the file system, ARP/IP/DHCP/DNS/TCP/HTTP against a simulated NIC, and a &lt;strong&gt;boot simulator&lt;/strong&gt; that boots the real image through a simulated BIOS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make host-test     &lt;span class="c"&gt;# all host suites&lt;/span&gt;
make boot-sim      &lt;span class="c"&gt;# simulated BIOS boot of the real image&lt;/span&gt;
make &lt;span class="nb"&gt;test&lt;/span&gt;          &lt;span class="c"&gt;# headless QEMU boot + screendump check (needs QEMU)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The screenshots in the repo are rendered by those test harnesses from the real compositor code, not captured from QEMU.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tested on QEMU only&lt;/strong&gt;, on a laptop and on Android. It has never booted on real hardware, so real PCI, ACPI, USB and NIC behaviour is unverified.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network:&lt;/strong&gt; RTL8139 only, and there's no TLS, so &lt;code&gt;https://&lt;/code&gt; sites don't load. The browser has no CSS, JavaScript or images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;USB:&lt;/strong&gt; UHCI only (no OHCI, EHCI or xHCI), no hubs, and mass storage is read-only.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boot:&lt;/strong&gt; BIOS only (no UEFI), a single CPU and cooperative threads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output depth:&lt;/strong&gt; the screen is 24/32 bpp, and the 64-bit pipeline is internal.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd love feedback on
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Boot results on &lt;strong&gt;other VMs and real machines&lt;/strong&gt;, with the exact setup, whether it works or not.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Driver bugs:&lt;/strong&gt; PCI, UHCI and the NIC driver have only met simulated hardware and QEMU's models.&lt;/li&gt;
&lt;li&gt;Anything in the code that reads badly, since the point of writing it in KingCobra is that it should be readable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/DivyanshuSinha136/Peacock" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Peacock&lt;/a&gt; · The language: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;MIT licensed. A star helps other people find it.&lt;/p&gt;

</description>
      <category>osdev</category>
      <category>showdev</category>
      <category>opensource</category>
      <category>programming</category>
    </item>
    <item>
      <title>The KingCobra REPL: a tour, from calculator to scratchpad</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Thu, 19 Nov 2026 19:40:43 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/the-kingcobra-repl-a-tour-from-calculator-to-scratchpad-28mg</link>
      <guid>https://dev.to/divyanshusinha136/the-kingcobra-repl-a-tour-from-calculator-to-scratchpad-28mg</guid>
      <description>&lt;p&gt;The fastest way to learn a language is to type at it. KingCobra's REPL is a read-eval-print loop that keeps your definitions, type-checks every line, and &lt;strong&gt;never dies on an error&lt;/strong&gt;. This is a tour of what it does. &lt;strong&gt;Every transcript below was run on KingCobra 1.0.0.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Start it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra repl              &lt;span class="c"&gt;# start a session&lt;/span&gt;
kingcobra                   &lt;span class="c"&gt;# no arguments in a terminal also starts it&lt;/span&gt;
kingcobra repl file.kc      &lt;span class="c"&gt;# run file.kc into the session first, then prompt&lt;/span&gt;
kingcobra -i file.kc        &lt;span class="c"&gt;# same thing&lt;/span&gt;
kingcobra --no-typecheck repl   &lt;span class="c"&gt;# start with the static type checker off&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Leave with &lt;code&gt;Ctrl-D&lt;/code&gt;, or with &lt;code&gt;exit&lt;/code&gt;, &lt;code&gt;quit&lt;/code&gt; or &lt;code&gt;:quit&lt;/code&gt;. Inside the REPL, &lt;code&gt;:help&lt;/code&gt; lists every command.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip for Windows:&lt;/strong&gt; if the banner shows garbled characters in &lt;code&gt;cmd.exe&lt;/code&gt;, run &lt;code&gt;chcp 65001&lt;/code&gt; first or use Windows Terminal, which uses UTF-8 by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The basics
&lt;/h2&gt;

&lt;p&gt;Type an expression and its value is echoed. Strings are shown quoted, and assignments and &lt;code&gt;null&lt;/code&gt; results print nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; 1 + 2
3
&amp;gt;&amp;gt;&amp;gt; "hi" + " there"
"hi there"
&amp;gt;&amp;gt;&amp;gt; let xs = [1, "a", 2.5]
&amp;gt;&amp;gt;&amp;gt; xs
[1, "a", 2.5]
&amp;gt;&amp;gt;&amp;gt; _
[1, "a", 2.5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;_&lt;/code&gt;&lt;/strong&gt; is the value of the last expression that was echoed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The trailing &lt;code&gt;;&lt;/code&gt; is optional&lt;/strong&gt; for a single statement. &lt;code&gt;count = 5&lt;/code&gt; and &lt;code&gt;count = 5;&lt;/code&gt; are the same. Several statements on one line still need separators: &lt;code&gt;let a = 1; let b = 2;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assignment needs a declaration first.&lt;/strong&gt; &lt;code&gt;x = 5&lt;/code&gt; on a name you never declared is an error, so use &lt;code&gt;var x = 5&lt;/code&gt; and then &lt;code&gt;x = 6&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Definitions persist
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;let&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;fn&lt;/code&gt;, &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;extern&lt;/code&gt; and &lt;code&gt;import&lt;/code&gt; all land in one global scope that lives for the whole session. Redefining a name, even a function or a struct, replaces it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fn sq&lt;span class="o"&gt;(&lt;/span&gt;n: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; n * n&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; sq&lt;span class="o"&gt;(&lt;/span&gt;12&lt;span class="o"&gt;)&lt;/span&gt;
144
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fn sq&lt;span class="o"&gt;(&lt;/span&gt;n: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; n * n * n&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; sq&lt;span class="o"&gt;(&lt;/span&gt;3&lt;span class="o"&gt;)&lt;/span&gt;
27
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; struct P &lt;span class="o"&gt;{&lt;/span&gt; x: int, y: int &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;let&lt;/span&gt; p &lt;span class="o"&gt;=&lt;/span&gt; P &lt;span class="o"&gt;{&lt;/span&gt; x: 1, y: 2 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; p
P &lt;span class="o"&gt;{&lt;/span&gt; x: 1, y: 2 &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Multi-line input
&lt;/h2&gt;

&lt;p&gt;If a &lt;code&gt;(&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, string or &lt;code&gt;/* */&lt;/code&gt; comment is still open, the prompt changes to &lt;code&gt;...&lt;/code&gt; and input continues until it balances. It also keeps reading when a statement is obviously unfinished: a &lt;code&gt;try&lt;/code&gt; waits for its &lt;code&gt;catch&lt;/code&gt;, and an input that ends in an operator (&lt;code&gt;1 +&lt;/code&gt;, &lt;code&gt;a &amp;amp;&amp;amp;&lt;/code&gt;) waits for its operand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; fn fact(n: int) -&amp;gt; int {
...     if (n &amp;lt; 2) { return 1; }
...     return n * fact(n - 1);
... }
&amp;gt;&amp;gt;&amp;gt; fact(10)
3628800
&amp;gt;&amp;gt;&amp;gt; try {
...     print([1, 2, 3][9]);
... } catch (e) {
...     print("caught:", e.message);
... }
caught: index 9 out of bounds (len 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pasting a block? Use &lt;code&gt;:paste&lt;/code&gt;, enter the lines, and finish with &lt;code&gt;:end&lt;/code&gt; or &lt;code&gt;Ctrl-D&lt;/code&gt;. &lt;code&gt;Ctrl-C&lt;/code&gt; abandons the input you were typing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Every line is type-checked
&lt;/h2&gt;

&lt;p&gt;The static type checker runs on each input against everything declared so far in the session. A rejected input leaves no trace:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fn sq&lt;span class="o"&gt;(&lt;/span&gt;n: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; n * n&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; sq&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"oops"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;repl:2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;:1:4: error: &lt;span class="s1"&gt;'sq'&lt;/span&gt; argument 1: expected int, got string
    1 &lt;span class="o"&gt;|&lt;/span&gt; sq&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"oops"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;    ^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Turn the checker off with &lt;code&gt;:typecheck off&lt;/code&gt; (and back on with &lt;code&gt;:typecheck on&lt;/code&gt;), or start with &lt;code&gt;--no-typecheck&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Errors never end the session
&lt;/h2&gt;

&lt;p&gt;Parse errors, type errors and runtime errors, including ones raised inside &lt;code&gt;std.*&lt;/code&gt; modules, print a normal diagnostic and return you to the prompt. The label &lt;code&gt;&amp;lt;repl:N&amp;gt;&lt;/code&gt; numbers your inputs, and you get the source line and a caret, even for a function defined many inputs ago:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; [1, 2, 3][10]
&amp;lt;repl:3&amp;gt;:1:8: error: index 10 out of bounds (len 3)
    1 | [1,2,3][10]
      |        ^
&amp;gt;&amp;gt;&amp;gt; let ok = 41 + 1
&amp;gt;&amp;gt;&amp;gt; ok
42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Runaway recursion is reported instead of crashing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; fn r(n: int) -&amp;gt; int { return r(n + 1) + 1; }
&amp;gt;&amp;gt;&amp;gt; r(0)
error: stack overflow (recursion too deep)
&amp;gt;&amp;gt;&amp;gt; 1 + 1
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Look around: variables, types and signatures
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; var count = 15
&amp;gt;&amp;gt;&amp;gt; let name = "Ada"
&amp;gt;&amp;gt;&amp;gt; :vars
  sq               fn         (n)
  count            int        15
  name             string     "Ada"
&amp;gt;&amp;gt;&amp;gt; :type 3.5
float
&amp;gt;&amp;gt;&amp;gt; :type [1, 2]
array
&amp;gt;&amp;gt;&amp;gt; :info sq
fn sq(n: int) -&amp;gt; int
&amp;gt;&amp;gt;&amp;gt; :info P
struct P { x: int, y: int }
&amp;gt;&amp;gt;&amp;gt; import std.json;
&amp;gt;&amp;gt;&amp;gt; :info json
module json: parse try_parse is_valid error parse_file stringify pretty minify ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;:info&lt;/code&gt; is the quickest way to see what a module offers, with no documentation lookup, and &lt;code&gt;:del name&lt;/code&gt; forgets a variable or function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Help without leaving
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;help(...)&lt;/code&gt;, &lt;code&gt;?name&lt;/code&gt; and &lt;code&gt;:help name&lt;/code&gt; show the language's own documentation for builtins, keywords, methods and modules. A &lt;code&gt;///&lt;/code&gt; doc comment typed at the prompt waits for the declaration after it, so your own functions get documented too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; ?len
len&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int  —  core
    Length of a string &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; BYTES&lt;span class="o"&gt;)&lt;/span&gt;, array, buffer or object. Arrays also have
    the property a.len.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; /// Add two numbers.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fn add&lt;span class="o"&gt;(&lt;/span&gt;a: int, b: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; a + b&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;help&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;add&lt;span class="o"&gt;)&lt;/span&gt;
fn add&lt;span class="o"&gt;(&lt;/span&gt;a: int, b: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int

    Add two numbers.

    defined at &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;repl:1&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;:2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  History, redo and saving your work
&lt;/h2&gt;

&lt;p&gt;The REPL numbers the inputs that ran. &lt;code&gt;:redo n&lt;/code&gt; runs one again, and &lt;code&gt;:save&lt;/code&gt; writes the whole session to a file, so something you explored can become a real program:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;let&lt;/span&gt; a &lt;span class="o"&gt;=&lt;/span&gt; 10
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;let&lt;/span&gt; b &lt;span class="o"&gt;=&lt;/span&gt; 20
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; a * b
200
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :history
   1  &lt;span class="nb"&gt;let&lt;/span&gt; a &lt;span class="o"&gt;=&lt;/span&gt; 10
   2  &lt;span class="nb"&gt;let&lt;/span&gt; b &lt;span class="o"&gt;=&lt;/span&gt; 20
   3  a * b
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :redo 2
&lt;span class="nb"&gt;let&lt;/span&gt; b &lt;span class="o"&gt;=&lt;/span&gt; 20
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :save session.kc
saved 4 input&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt; to session.kc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To bring code in, &lt;code&gt;:load file.kc&lt;/code&gt; runs a file into the session (&lt;code&gt;main()&lt;/code&gt; is not called), &lt;code&gt;:reload&lt;/code&gt; runs the last &lt;code&gt;:load&lt;/code&gt; again, and &lt;code&gt;:reset&lt;/code&gt; throws everything away and starts fresh.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :load lib.kc
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; greet&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"REPL"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="s2"&gt;"Hello, REPL!"&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :reset
session reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Timing and memory
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;:time&lt;/code&gt; runs one input and reports how long it took. &lt;code&gt;:timing on&lt;/code&gt; times every input. Your number will differ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; fn fib(n: int) -&amp;gt; int { if (n &amp;lt; 2) { return n; } return fib(n - 1) + fib(n - 2); }
&amp;gt;&amp;gt;&amp;gt; :time fib(25)
75025
(986.7 us)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Memory is explicit in a session, just like in a program. &lt;code&gt;free(x)&lt;/code&gt; works on strings, arrays, structs and &lt;code&gt;alloc()&lt;/code&gt; blocks, echoing a freed value is a recoverable use-after-free error, and &lt;code&gt;:mem&lt;/code&gt; prints the live allocations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;let&lt;/span&gt; s &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"id-"&lt;/span&gt; + 42
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; free&lt;span class="o"&gt;(&lt;/span&gt;s&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; s
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;repl&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;:1:1: error: use after free &lt;span class="o"&gt;(&lt;/span&gt;this string was freed&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; :vars
  s                string     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;freed string&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The preprocessor works here too
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; #define SQ(x) ((x)*(x))
&amp;gt;&amp;gt;&amp;gt; SQ(7)
49
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Macros persist for the session, &lt;code&gt;:macros&lt;/code&gt; lists them and &lt;code&gt;#undef&lt;/code&gt; removes one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Display options
&lt;/h2&gt;

&lt;p&gt;Long arrays, structs and objects wrap one element or field per line. Set the width with &lt;code&gt;:set width n&lt;/code&gt; (default 80, minimum 20). On a terminal, values are coloured; &lt;code&gt;:color off&lt;/code&gt; or the &lt;code&gt;NO_COLOR=1&lt;/code&gt; environment variable turns that off:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight irb"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:set&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;200000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;300000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="mi"&gt;400000&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Line editing, history and Tab completion
&lt;/h2&gt;

&lt;p&gt;If GNU readline is installed (&lt;code&gt;libreadline-dev&lt;/code&gt; on Debian and Ubuntu, &lt;code&gt;readline-devel&lt;/code&gt; on Fedora), the build detects it and you get arrow keys, Emacs and vi editing, and a history file in &lt;code&gt;~/.kingcobra_history&lt;/code&gt; that keeps the last 1,000 lines. &lt;strong&gt;Tab completes&lt;/strong&gt; keywords, REPL commands, macros, and every name you've defined. After a dot it completes members, so &lt;code&gt;system.&amp;lt;Tab&amp;gt;&lt;/code&gt; lists the module's functions and &lt;code&gt;p.&amp;lt;Tab&amp;gt;&lt;/code&gt; lists a struct's fields. After &lt;code&gt;:load&lt;/code&gt; or &lt;code&gt;:save&lt;/code&gt; it completes file names. Without readline the REPL still works with plain line input.&lt;/p&gt;

&lt;h2&gt;
  
  
  A startup file
&lt;/h2&gt;

&lt;p&gt;On a terminal, the REPL loads &lt;code&gt;~/.kingcobrarc&lt;/code&gt; if it exists, which is a good place for imports and helpers you always want. The environment variable &lt;code&gt;KC_REPL_INIT&lt;/code&gt; names a file to load in any mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'let motd = "welcome back";'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; init.kc
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'motd'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nv"&gt;KC_REPL_INIT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;init.kc kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"welcome back"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;./.kingcobrarc&lt;/code&gt; in the current directory is deliberately &lt;strong&gt;not&lt;/strong&gt; run, so opening a project folder can't execute code on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use it from scripts
&lt;/h2&gt;

&lt;p&gt;When stdin isn't a terminal, there is no banner and no prompt, but values are still echoed. That makes the REPL handy for one-line checks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'2 * 10 == 20'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'fn sq(n: int) -&amp;gt; int { return n*n; }\nsq(12)\n'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;144
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exit status is &lt;code&gt;1&lt;/code&gt; if any input failed and &lt;code&gt;0&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three ways I use it
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Explore a module.&lt;/strong&gt; &lt;code&gt;import std.json;&lt;/code&gt; then &lt;code&gt;:info json&lt;/code&gt; shows every function, and &lt;code&gt;?name&lt;/code&gt; documents one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Try before you write.&lt;/strong&gt; Test an expression, a string method or a regex in the REPL, then &lt;code&gt;:save&lt;/code&gt; the good lines.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poke at your own code.&lt;/strong&gt; &lt;code&gt;kingcobra repl myprog.kc&lt;/code&gt; loads a file into the session, so you can call its functions with different arguments.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Limits worth knowing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory grows slowly.&lt;/strong&gt; Parsed code is never freed, because closures defined in the session point into it, and &lt;code&gt;:reset&lt;/code&gt; doesn't release it either. That's fine for interactive use, but it's not meant for a REPL embedded in a long-running server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A recovered error skips pending &lt;code&gt;defer&lt;/code&gt; statements.&lt;/strong&gt; Put any cleanup you need on every path in code that can't raise an error, or free explicitly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Ctrl-C&lt;/code&gt; and stack-overflow recovery are POSIX-only.&lt;/strong&gt; On Windows, &lt;code&gt;Ctrl-C&lt;/code&gt; ends the process, and runaway recursion is stopped by the 4,000-call depth limit instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;After an interrupt inside heavy native code&lt;/strong&gt; (for example &lt;code&gt;std.http&lt;/code&gt; or &lt;code&gt;std.nn&lt;/code&gt; training), treat the session as disposable.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A script can catch a stack overflow with &lt;code&gt;try&lt;/code&gt;; the REPL just returns you to the prompt.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;An &lt;code&gt;if&lt;/code&gt; waits for one more line&lt;/strong&gt; on a terminal, to catch an &lt;code&gt;else&lt;/code&gt;. Press Enter to run it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;import "file.kc"&lt;/code&gt; in the REPL&lt;/strong&gt; resolves relative to the current directory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Language tutorial: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Full REPL reference: &lt;code&gt;docs/REPL.md&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If a REPL behaviour surprises you, open an issue. A star on the repo helps other people find it.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Neural networks in KingCobra: a 168-page guide to std.nn</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Thu, 19 Nov 2026 19:33:29 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/neural-networks-in-kingcobra-a-168-page-guide-to-stdnn-1mnh</link>
      <guid>https://dev.to/divyanshusinha136/neural-networks-in-kingcobra-a-168-page-guide-to-stdnn-1mnh</guid>
      <description>&lt;p&gt;KingCobra's standard library includes &lt;strong&gt;&lt;code&gt;std.nn&lt;/code&gt;&lt;/strong&gt;, a native, dependency-free deep-learning library. There is no Python, no GPU and no downloaded weights: it's C code inside the interpreter, and you call it from KingCobra. I've written a guide to it, &lt;strong&gt;"Neural Networks in KingCobra: The Complete Guide to std.nn"&lt;/strong&gt;, and this post is the short version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read the guide (PDF, 168 pages):&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A whole training program in a dozen lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight d"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="no"&gt;Y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;net&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mlp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"activation"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"tanh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"out_activation"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sigmoid"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"bce"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"adam"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"epochs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"batch_size"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"lr"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"verbose"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"epochs run:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loss&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;" final loss below 0.05:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loss&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;399&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"predicted classes:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predict_class&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;X&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;summary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;net&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;epochs run: 400  final loss below 0.05: true
predicted classes: [0, 1, 1, 0]
Model: sequential (4 layers)
  #  Layer                                Output shape           Params
  0  dense 2-&amp;gt;8                           [8]                        24
  1  tanh                                 [8]                         0
  2  dense 8-&amp;gt;1                           [1]                         9
  3  sigmoid                              [1]                         0
Total params: 33  (trainable: 33, frozen: 0, buffers: 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain nested arrays go straight in, &lt;code&gt;nn.fit&lt;/code&gt; runs the whole loop, and &lt;code&gt;nn.seed&lt;/code&gt; makes the run repeatable.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the guide covers
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Part I: the toolkit.&lt;/strong&gt; Tensors, broadcasting, reductions, &lt;code&gt;einsum&lt;/code&gt;, statistics, linear algebra (LU, Cholesky, QR, Jacobi eigen and SVD, matrix exponential) and data preparation (one-hot, scaling, splitting, padding, time-series windows).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part II: the mathematics.&lt;/strong&gt; From a single neuron to back-propagation, the softmax and cross-entropy gradient, initialization, vanishing and exploding gradients, and checking gradients numerically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part III: building and training.&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Layers:&lt;/strong&gt; dense, &lt;code&gt;conv1d&lt;/code&gt;/&lt;code&gt;conv2d&lt;/code&gt;, pooling, &lt;code&gt;rnn&lt;/code&gt;/&lt;code&gt;lstm&lt;/code&gt;/&lt;code&gt;gru&lt;/code&gt; with full BPTT, attention, &lt;code&gt;pos_encoding&lt;/code&gt;, batch and layer norm, embedding, residual blocks and &lt;code&gt;transformer_block&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Losses:&lt;/strong&gt; &lt;code&gt;mse&lt;/code&gt;, &lt;code&gt;mae&lt;/code&gt;, &lt;code&gt;huber&lt;/code&gt;, &lt;code&gt;bce&lt;/code&gt;, &lt;code&gt;ce&lt;/code&gt;, &lt;code&gt;kl&lt;/code&gt;, &lt;code&gt;hinge&lt;/code&gt; and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimizers:&lt;/strong&gt; &lt;code&gt;sgd&lt;/code&gt;, &lt;code&gt;adam&lt;/code&gt;, &lt;code&gt;adamw&lt;/code&gt;, &lt;code&gt;rmsprop&lt;/code&gt;, &lt;code&gt;adagrad&lt;/code&gt;, with gradient clipping and seven learning-rate schedules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training:&lt;/strong&gt; &lt;code&gt;fit&lt;/code&gt;, early stopping and metrics.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part IV: practice.&lt;/strong&gt; Which architecture for which data, model saving and loading (a CRC-checked file format), freezing layers for transfer learning, &lt;strong&gt;reinforcement learning with DQN and Double DQN on a grid world&lt;/strong&gt;, performance, common mistakes and a verification checklist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Appendices.&lt;/strong&gt; A cheat sheet, reference tables, a formula sheet, a glossary and a function index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each layer, activation, loss and optimizer is derived from first principles, with a small example worked by hand and then checked against the real interpreter. There are more than forty worked problems, with full solutions in Chapter 16.&lt;/p&gt;

&lt;h2&gt;
  
  
  Design in four ideas
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;One small interface.&lt;/strong&gt; Every layer implements &lt;code&gt;forward&lt;/code&gt;, &lt;code&gt;backward&lt;/code&gt; and &lt;code&gt;save&lt;/code&gt;, so a convolution, an LSTM and a ReLU look identical to the training engine, and anything that fits together shape-wise can be trained.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verified gradients.&lt;/strong&gt; The guide describes &lt;code&gt;nn.gradcheck&lt;/code&gt; and a repository test that checks about sixty architectures against numerical gradients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch-first shapes.&lt;/strong&gt; &lt;code&gt;[N, features]&lt;/code&gt; for tables, &lt;code&gt;[N, C, H, W]&lt;/code&gt; for images and &lt;code&gt;[N, T, features]&lt;/code&gt; for sequences.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Native handles.&lt;/strong&gt; Tensors live in C memory, not in KingCobra arrays, so you release them explicitly with &lt;code&gt;nn.free&lt;/code&gt;, in line with KingCobra's no-garbage-collector design.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std.nn&lt;/code&gt; is deliberately small and exact:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Speed.&lt;/strong&gt; It uses float64 on one CPU thread, with no GPU. The guide reports about 6,000 samples per second for a 784-256-128-10 MLP, about 3,500 for a small CNN, about 1,300 for an LSTM and about 1,000 for a transformer block. Moderate models train in seconds to minutes, and large image or language models are out of reach.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No pretrained models,&lt;/strong&gt; so you train, save and load your own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not every architecture.&lt;/strong&gt; There's no cross-attention and no bidirectional RNN wrapper.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not an RL framework.&lt;/strong&gt; You write the environment and replay buffer yourself in plain KingCobra, and the guide shows how.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not a full linear-algebra suite.&lt;/strong&gt; There are no complex numbers or FFT.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resuming.&lt;/strong&gt; Optimizer state isn't saved, so resuming training restarts Adam's moments.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Every example was run
&lt;/h2&gt;

&lt;p&gt;Every program in the guide was run on KingCobra 1.0.0, and the output under each program was captured automatically while the book was typeset, so the text and the numbers can't drift apart. Random-dependent results use a fixed &lt;code&gt;nn.seed&lt;/code&gt;, so you get the same numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get started
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Guide (PDF):&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_std_nn_Complete_Guide_Expanded.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repo:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;New to the language?&lt;/strong&gt; Start with the &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;language tutorial&lt;/a&gt;, then read Chapter 1 of the guide.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you train something with &lt;code&gt;std.nn&lt;/code&gt;, or find a place where the guide is unclear or a result differs from yours, please open an issue. A star on the repo helps other people find it.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Calling C from KingCobra: a hands-on FFI tutorial</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Thu, 19 Nov 2026 19:19:18 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/calling-c-from-kingcobra-a-hands-on-ffi-tutorial-2hnf</link>
      <guid>https://dev.to/divyanshusinha136/calling-c-from-kingcobra-a-hands-on-ffi-tutorial-2hnf</guid>
      <description>&lt;p&gt;KingCobra can call C code directly, with no binding generator and no glue files. This tutorial covers the two ways to do it, how values cross the boundary, how to write and load your own C library, and where the language's safety checks stop. &lt;strong&gt;Every example was run on KingCobra 1.0.0, and the output shown is the real output.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You should know the basics of KingCobra (see the &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;language tutorial&lt;/a&gt;) and be able to read simple C.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;You need KingCobra, and from section 6 onwards a C compiler. KingCobra runs on Linux, macOS and Windows, and every program in this tutorial is the same source on all three, apart from the library file names covered in section 6.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;KingCobra&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Windows (PowerShell as Administrator)&lt;/span&gt;
&lt;span class="n"&gt;choco&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;kingcobra&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Linux and macOS&lt;/span&gt;
curl -fsSL https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.sh &lt;span class="o"&gt;|&lt;/span&gt; sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scoop, the no-admin PowerShell installer and building from source are covered in the &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;language tutorial&lt;/a&gt;. After installing, open a new terminal and run &lt;code&gt;kingcobra version&lt;/code&gt;. Run any example in this tutorial with &lt;code&gt;kingcobra run file.kc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A C compiler&lt;/strong&gt; (for the libraries you build yourself)&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Platform&lt;/th&gt;
&lt;th&gt;Compiler&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Linux&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;cc&lt;/code&gt; from gcc or clang (on Debian and Ubuntu: &lt;code&gt;sudo apt install build-essential&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;macOS&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;cc&lt;/code&gt; from the Xcode command line tools (&lt;code&gt;xcode-select --install&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows&lt;/td&gt;
&lt;td&gt;MinGW-w64 &lt;code&gt;gcc&lt;/code&gt; (for example from MSYS2), or MSVC &lt;code&gt;cl&lt;/code&gt; from an "x64 Native Tools Command Prompt"&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip for Windows:&lt;/strong&gt; if non-ASCII text looks garbled in &lt;code&gt;cmd.exe&lt;/code&gt; (for example an em dash printing as &lt;code&gt;ΓÇö&lt;/code&gt;), the console is using an old code page. Run &lt;code&gt;chcp 65001&lt;/code&gt; first, or use Windows Terminal, which uses UTF-8 by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Two ways to call C
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;extern fn&lt;/code&gt; (declared)&lt;/th&gt;
&lt;th&gt;native import (inferred)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Syntax&lt;/td&gt;
&lt;td&gt;&lt;code&gt;extern fn abs(x: int) -&amp;gt; int from "libc.so.6";&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;import "./libmylib.so" as lib;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;You write&lt;/td&gt;
&lt;td&gt;a signature for each function&lt;/td&gt;
&lt;td&gt;nothing, the library is opened as a whole&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Types&lt;/td&gt;
&lt;td&gt;taken from your declaration&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;guessed from the arguments you pass&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Works with&lt;/td&gt;
&lt;td&gt;any shared library&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;.so&lt;/code&gt;, &lt;code&gt;.dll&lt;/code&gt;, &lt;code&gt;.dylib&lt;/code&gt;, or a &lt;code&gt;.o&lt;/code&gt; compiled with &lt;code&gt;-fPIC&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best for&lt;/td&gt;
&lt;td&gt;libc, libm and other system libraries, pointer arguments&lt;/td&gt;
&lt;td&gt;your own small C libraries with simple signatures&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The rule of thumb: &lt;strong&gt;declare what you can, import what's convenient.&lt;/strong&gt; Declared externs say exactly what you mean, and inferred imports can quietly get a signature wrong (section 7).&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Your first foreign call
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42
1.41421
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;extern fn&lt;/code&gt; declaration names the C symbol, gives its parameter and return types, and says which library to find it in. After that you call it like any KingCobra function. A function declared &lt;code&gt;-&amp;gt; void&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Library names.&lt;/strong&gt; On Linux, the C library is &lt;code&gt;libc.so.6&lt;/code&gt; and the math library is &lt;code&gt;libm.so.6&lt;/code&gt;. On Windows, KingCobra maps those two names to the C runtime DLLs (&lt;code&gt;ucrtbase.dll&lt;/code&gt; or &lt;code&gt;msvcrt.dll&lt;/code&gt;), so the same source runs on both. On macOS the interpreter loads names as written, so use the macOS library names there (for example &lt;code&gt;libSystem.B.dylib&lt;/code&gt; for the C library).&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Passing strings
&lt;/h2&gt;

&lt;p&gt;A KingCobra string argument is passed to C as a &lt;code&gt;char *&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;atoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;toupper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello, FFI"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;atoi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"1234"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;toupper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ord&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"k"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
1235
K
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Characters are ints in KingCobra, so &lt;code&gt;toupper&lt;/code&gt; takes and returns an &lt;code&gt;int&lt;/code&gt;. &lt;code&gt;ord&lt;/code&gt; and &lt;code&gt;chr&lt;/code&gt; convert between a one-character string and its code point.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Floats, and the "same kind" rule
&lt;/h2&gt;

&lt;p&gt;Floats work too, as long as the declaration is consistent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;hypot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;hypot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1024 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the limitation. The interpreter's FFI passes arguments in fixed-arity trampolines, so &lt;strong&gt;all parameters of one extern must be the same kind: all int-like, or all float-like.&lt;/strong&gt; C's &lt;code&gt;ldexp(double x, int e)&lt;/code&gt; mixes the two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;ldexp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ldexp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f3b.kc:1:1: error: extern 'ldexp': v0.1 FFI requires all parameters to be the same kind (all int-like or all float-like); mixed signatures need libffi (see ROADMAP)
    1 | extern fn ldexp(x: float, e: int) -&amp;gt; float from "libm.so.6";
      | ^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error appears when the declaration is read, before anything runs. Two ways around it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Wrap it in C.&lt;/strong&gt; Write a small C function with a uniform signature (for example &lt;code&gt;double ldexp_f(double x, double e)&lt;/code&gt;) and call that.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compile with &lt;code&gt;kcc&lt;/code&gt;.&lt;/strong&gt; The compiler (section 10) does full calling-convention handling, and &lt;code&gt;ldexp(1.0, 10)&lt;/code&gt; works there.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other limits of declared externs: &lt;strong&gt;at most 6 parameters&lt;/strong&gt;, and arguments can be ints, bools, strings, floats, pointers or buffers.&lt;/p&gt;

&lt;h3&gt;
  
  
  A C &lt;code&gt;int&lt;/code&gt; is 32 bits, and KingCobra's is 64
&lt;/h3&gt;

&lt;p&gt;KingCobra reads an &lt;code&gt;-&amp;gt; int&lt;/code&gt; result as a full 64-bit value. A C function that returns a 32-bit &lt;code&gt;int&lt;/code&gt; only fills the lower half of the register, so a &lt;strong&gt;negative&lt;/strong&gt; result can come back as a huge positive number. &lt;code&gt;strcmp&lt;/code&gt; returns a negative &lt;code&gt;int&lt;/code&gt; when the first string sorts first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"banana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"apple"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"banana"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4294967295
-1
1 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cast &lt;code&gt;as i32&lt;/code&gt; reinterprets the lower 32 bits as a signed value, which gives you the answer C meant. Use it whenever a C function returns an &lt;code&gt;int&lt;/code&gt; that can be negative, and &lt;code&gt;as u32&lt;/code&gt;, &lt;code&gt;as i16&lt;/code&gt; and so on for the other widths. Functions that return &lt;code&gt;long&lt;/code&gt; or &lt;code&gt;int64_t&lt;/code&gt;, as in your own libraries from section 6, don't need it. Exact values for results like &lt;code&gt;strcmp&lt;/code&gt;'s can differ between C libraries, but the cast gives you the right sign on every platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Pointers and buffers: where &lt;code&gt;unsafe&lt;/code&gt; begins
&lt;/h2&gt;

&lt;p&gt;KingCobra checks every pointer you make itself: bounds, lifetime, null. &lt;strong&gt;C does none of that.&lt;/strong&gt; So the moment a pointer or buffer crosses into native code, you have to say so with an &lt;code&gt;unsafe { }&lt;/code&gt; block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"unreachable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f4b.kc:4:5: error: passing a pointer or buffer to native code requires an `unsafe { }` block (native code is not bounds-checked)
    4 |     memset(buf.ptr, 7, 8);
      |     ^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the block and it works. Here C allocates memory and KingCobra reads it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;void&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// C hands us a raw pointer, so every dereference needs unsafe&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;memset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x41&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_cstr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nf"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;AAAAAAAAAAAAAAA
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An extern declared &lt;code&gt;-&amp;gt; *u8&lt;/code&gt; returns a &lt;strong&gt;raw pointer&lt;/strong&gt;: dereferencing it needs &lt;code&gt;unsafe&lt;/code&gt;, and KingCobra can't know how big it is. &lt;code&gt;read_cstr()&lt;/code&gt; reads a NUL-terminated string from it.&lt;/p&gt;

&lt;p&gt;The safer direction is to give C one of &lt;strong&gt;your&lt;/strong&gt; buffers to fill:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;extern fn strncpy(dst: *u8, src: string, n: int) -&amp;gt; *u8 from "libc.so.6";
extern fn strlen(p: *u8) -&amp;gt; int from "libc.so.6";

fn main() {
    let buf = buffer(32);
    unsafe {
        strncpy(buf.ptr, "copied by libc", 31);
        print(strlen(buf.ptr));
    }
    print(buf.to_string(0, 14));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;14
copied by libc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The buffer is allocated and freed by KingCobra, so you don't have to match a &lt;code&gt;malloc&lt;/code&gt; with a &lt;code&gt;free&lt;/code&gt;. The buffer also remembers its size, so everything you do with it &lt;strong&gt;outside&lt;/strong&gt; &lt;code&gt;unsafe&lt;/code&gt; stays bounds-checked.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Known issue in 1.0.0:&lt;/strong&gt; a &lt;code&gt;return&lt;/code&gt; inside an &lt;code&gt;unsafe { }&lt;/code&gt; block doesn't leave the function, and the function returns &lt;code&gt;null&lt;/code&gt;. Assign the result to a variable inside the block and return it after the block, as in section 9. That pattern is correct on every version.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  6. Writing your own C library
&lt;/h2&gt;

&lt;p&gt;Here is a small library for the rest of the tutorial. Save it as &lt;code&gt;mylib.c&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* mylib.c: a small native library for the FFI tutorial.
 * Uses fixed-width integer types so the same file works on Linux, macOS and
 * Windows (where `long` is only 32 bits). */&lt;/span&gt;
&lt;span class="cp"&gt;#include &amp;lt;stdint.h&amp;gt;&lt;/span&gt;

&lt;span class="cp"&gt;#if defined(_WIN32)&lt;/span&gt;
  &lt;span class="cp"&gt;#define EXPORT __declspec(dllexport)&lt;/span&gt;
&lt;span class="cp"&gt;#else&lt;/span&gt;
  &lt;span class="cp"&gt;#define EXPORT&lt;/span&gt;
&lt;span class="cp"&gt;#endif&lt;/span&gt;

&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;  &lt;span class="nf"&gt;half&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;  &lt;span class="nf"&gt;hypot2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* takes ints, returns a double: the inferred-signature trap (section 7) */&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;  &lt;span class="nf"&gt;mean2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* the fix: take doubles when you return a double */&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;  &lt;span class="nf"&gt;mean2f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* sums n 64-bit integers behind a pointer */&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="nf"&gt;sum_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* fills a caller-provided struct { int32 id; int32 score; int64 total; } (16 bytes) */&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="nf"&gt;fill_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Record&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int64_t&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cm"&gt;/* returns a static C string */&lt;/span&gt;
&lt;span class="no"&gt;EXPORT&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"hello from C"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two portability details in that file matter for FFI in general:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;int64_t&lt;/code&gt;, not &lt;code&gt;long&lt;/code&gt;.&lt;/strong&gt; KingCobra integers are 64-bit. &lt;code&gt;long&lt;/code&gt; is 64 bits on Linux and macOS but only 32 bits on Windows, so a &lt;code&gt;long&lt;/code&gt; parameter would read the wrong half of the value there.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Export your functions on Windows.&lt;/strong&gt; A DLL only exposes functions marked &lt;code&gt;__declspec(dllexport)&lt;/code&gt;. The &lt;code&gt;EXPORT&lt;/code&gt; macro does that on Windows and does nothing elsewhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Build it for your platform
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Platform&lt;/th&gt;
&lt;th&gt;Library file&lt;/th&gt;
&lt;th&gt;Build command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Linux&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libmylib.so&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cc -shared -fPIC -o libmylib.so mylib.c&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;macOS&lt;/td&gt;
&lt;td&gt;&lt;code&gt;libmylib.dylib&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cc -dynamiclib -o libmylib.dylib mylib.c&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows (MinGW-w64)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;mylib.dll&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;gcc -shared -o mylib.dll mylib.c&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows (MSVC)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;mylib.dll&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cl /LD mylib.c /Fe:mylib.dll&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The examples below use the Linux name, &lt;code&gt;./libmylib.so&lt;/code&gt;. &lt;strong&gt;On macOS, write &lt;code&gt;./libmylib.dylib&lt;/code&gt;; on Windows, write &lt;code&gt;./mylib.dll&lt;/code&gt;.&lt;/strong&gt; Nothing else changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Import it
&lt;/h3&gt;

&lt;p&gt;No declarations are needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;half&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;7.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hypot2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;42
3628800
3.5
25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is identical on all three platforms, because only the file name differs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./libmylib.dylib" as lib;     // macOS
import "./mylib.dll" as lib;          // Windows
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How a native import decides the signature: every call looks the symbol up and &lt;strong&gt;infers the calling convention from the arguments you actually pass.&lt;/strong&gt; If all arguments are int-like, everything is treated as 64-bit ints, including the result. If the first argument is a float, everything is treated as doubles.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Path rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Use a path with a directory in it (&lt;code&gt;./libmylib.so&lt;/code&gt;), not a bare name (&lt;code&gt;libmylib.so&lt;/code&gt;).&lt;/strong&gt; A bare name goes to the system loader, which doesn't search your folder, and fails with &lt;code&gt;cannot open shared object file&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A native import's path is resolved &lt;strong&gt;relative to the importing file&lt;/strong&gt;, so the program above also runs when you start it from another directory.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;.o&lt;/code&gt; object file also works, if it was compiled with &lt;code&gt;-fPIC&lt;/code&gt; and you have a C compiler on your &lt;code&gt;PATH&lt;/code&gt; (&lt;code&gt;cc -fPIC -c mylib.c -o mylib.o&lt;/code&gt;, then &lt;code&gt;import "./mylib.o" as lib;&lt;/code&gt;). KingCobra links it into a &lt;code&gt;mylib.kcnative.so&lt;/code&gt; next to it on first use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  One program, three platforms
&lt;/h3&gt;

&lt;p&gt;Library paths are string literals, so a program can't switch between them on its own. The simplest portable layout is one tiny wrapper module per platform that differs only in the file name, with the rest of your code importing whichever one belongs to the OS it runs on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// native_linux.kc
import "./libmylib.so" as lib;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// native_macos.kc
import "./libmylib.dylib" as lib;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// native_windows.kc
import "./mylib.dll" as lib;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The wrapper module in section 9 names the library in two places (the &lt;code&gt;import&lt;/code&gt; and the &lt;code&gt;extern&lt;/code&gt;). Keep one copy per platform with just that file name changed, and everything above it stays the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. The inferred-signature trap
&lt;/h2&gt;

&lt;p&gt;This is the most important gotcha in the whole FFI. Look at &lt;code&gt;mean2&lt;/code&gt; in the C file: it takes two &lt;code&gt;int64_t&lt;/code&gt;s and returns a &lt;code&gt;double&lt;/code&gt;. Import it and call it with ints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight zig"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// C says 3.5&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// C says 15.0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7
30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Both results are wrong, and nothing warned you.&lt;/strong&gt; All-int arguments told KingCobra to read an integer result, but a &lt;code&gt;double&lt;/code&gt; comes back in a different register, so it picked up leftover integer garbage (here the sum of the arguments).&lt;/p&gt;

&lt;p&gt;The fix is on the C side: &lt;strong&gt;if a function returns a double, make it take doubles.&lt;/strong&gt; &lt;code&gt;mean2f&lt;/code&gt; does exactly that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean2f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;4.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean2f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;20.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3.5 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;General rules to keep inferred imports safe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions that take and return ints, or take and return doubles, are safe.&lt;/li&gt;
&lt;li&gt;A function that takes ints and returns a float, or takes floats and returns an int, is &lt;strong&gt;not&lt;/strong&gt;. Change the C signature, or wrap it.&lt;/li&gt;
&lt;li&gt;Functions that return a 32-bit &lt;code&gt;int&lt;/code&gt; which can be negative need &lt;code&gt;as i32&lt;/code&gt; on the result (section 4).&lt;/li&gt;
&lt;li&gt;Pointer arguments belong to declared externs (next section).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Pointers, structs and strings from your own library
&lt;/h2&gt;

&lt;p&gt;For anything beyond plain numbers, declare the externs. This program sums an array in C, has C fill a struct, and reads a C string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;sum_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;fill_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"./libmylib.so"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// an array of five 64-bit integers in a checked buffer&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put_i64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;          &lt;span class="c1"&gt;// 10+20+30+40+50&lt;/span&gt;

    &lt;span class="c1"&gt;// C writes a struct { int32 id; int32 score; int64 total; } into our buffer&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;fill_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_i32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_i32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_i64&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// C returns a pointer to a static string&lt;/span&gt;
    &lt;span class="k"&gt;unsafe&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read_cstr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;150
7 70 7000
hello from C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How it fits together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrays.&lt;/strong&gt; Lay out the elements yourself with &lt;code&gt;put_i64(offset, value)&lt;/code&gt;. The element size and offset are your responsibility, because C won't check them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structs.&lt;/strong&gt; C's &lt;code&gt;struct { int32_t id; int32_t score; int64_t total; }&lt;/code&gt; has &lt;code&gt;id&lt;/code&gt; at offset 0, &lt;code&gt;score&lt;/code&gt; at offset 4 and &lt;code&gt;total&lt;/code&gt; at offset 8, 16 bytes in all. Allocate a buffer of that size, pass &lt;code&gt;buf.ptr&lt;/code&gt;, and read the fields back with &lt;code&gt;get_i32&lt;/code&gt; and &lt;code&gt;get_i64&lt;/code&gt;. Keep the offsets in sync with your C header. The language reference (&lt;code&gt;docs/POINTERS.md&lt;/code&gt;) shows how to describe C-layout structs with &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;sizeof&lt;/code&gt; and &lt;code&gt;offsetof&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Returned strings.&lt;/strong&gt; A &lt;code&gt;-&amp;gt; *u8&lt;/code&gt; result is a raw pointer. &lt;code&gt;read_cstr()&lt;/code&gt; reads up to the NUL terminator, and it needs &lt;code&gt;unsafe&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One path difference.&lt;/strong&gt; A declared extern's &lt;code&gt;from "./libmylib.so"&lt;/code&gt; is looked up relative to the &lt;strong&gt;current working directory&lt;/strong&gt;, not to the source file. Run the program from the folder that holds the library, use an absolute path, or install the library where the system loader finds it. (A native &lt;code&gt;import&lt;/code&gt;, by contrast, resolves relative to the importing file.) On macOS and Windows the file name changes the same way as in section 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Wrap it: a safe KingCobra module around C
&lt;/h2&gt;

&lt;p&gt;The best habit in FFI work is to keep &lt;code&gt;unsafe&lt;/code&gt; small and hide it behind a normal function that checks its inputs. Save this as &lt;code&gt;cmath.kc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// cmath.kc: a safe KingCobra wrapper around the C functions in libmylib.so
import "./libmylib.so" as c;

extern fn sum_array(p: *u8, n: int) -&amp;gt; int from "./libmylib.so";

/// Sum an array of ints using the C implementation.
fn sum(xs: int[]) -&amp;gt; int {
    if (len(xs) == 0) { return 0; }
    let b = buffer(len(xs) * 8);
    defer b.free();                       // freed on every exit path
    for (i, x in xs) { b.put_i64(i * 8, x); }
    var total = 0;
    unsafe { total = sum_array(b.ptr, len(xs)); }
    return total;
}

/// Mean of two numbers (the C side takes doubles).
fn mean(a: float, b: float) -&amp;gt; float { return c.mean2f(a, b); }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On macOS or Windows, change the library file name in the two places it appears (&lt;code&gt;./libmylib.dylib&lt;/code&gt; or &lt;code&gt;./mylib.dll&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "cmath.kc" as cm;

fn main() {
    print(cm.sum([1, 2, 3, 4, 5]));
    print(cm.sum([]));
    print(cm.mean(3.0, 4.0));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;15
0
3.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What the wrapper buys you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callers pass a normal array and never see a pointer.&lt;/li&gt;
&lt;li&gt;The buffer size comes from &lt;code&gt;len(xs)&lt;/code&gt;, so it can't drift out of sync with the element count.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;defer b.free()&lt;/code&gt; releases the buffer on every exit path, including an error.&lt;/li&gt;
&lt;li&gt;The empty-array case is handled before C ever sees it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;unsafe&lt;/code&gt; block is one line, and the result is assigned to a variable and returned afterwards (see the known issue in section 5).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;kingcobra check cmath.kc&lt;/code&gt; still type-checks the module.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10. Calling C from compiled programs with &lt;code&gt;kcc&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Everything above runs in the interpreter. &lt;code&gt;kcc&lt;/code&gt;, the KingCobra compiler written in KingCobra, turns a program into a native executable, and its &lt;code&gt;extern fn&lt;/code&gt; support is different: calls go through the loader's normal import table, and the calling convention follows the platform ABI. That includes mixed int and float signatures, which the interpreter rejects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;ldexp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libm.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello from a compiled KingCobra program"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ldexp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kcc app.kc -o app
./app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kcc: wrote app (5208 bytes, linux executable)
hello from a compiled KingCobra program
7
1024
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The executable is 5,208 bytes. &lt;code&gt;ldd ./app&lt;/code&gt; shows that it links directly against &lt;code&gt;libc.so.6&lt;/code&gt; and &lt;code&gt;libm.so.6&lt;/code&gt;, and the mixed-signature &lt;code&gt;ldexp(1.0, 10)&lt;/code&gt; returns 1024 as it should. For Windows and macOS targets, &lt;code&gt;kcc&lt;/code&gt; translates &lt;code&gt;libc.so.6&lt;/code&gt; to &lt;code&gt;msvcrt.dll&lt;/code&gt; and &lt;code&gt;libSystem.B.dylib&lt;/code&gt;, so one source file builds for all three (&lt;code&gt;--target linux|windows|macos&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  11. What the FFI can't do (yet)
&lt;/h2&gt;

&lt;p&gt;In 1.0.0:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Callbacks.&lt;/strong&gt; You can't pass a KingCobra function to C. For example, &lt;code&gt;qsort&lt;/code&gt; can't take a comparator:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  f10.kc:5:14: error: extern 'qsort': argument 4 must be int, bool, or string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;More than 6 parameters&lt;/strong&gt; (interpreter):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  error: extern 'f': KingCobra's native FFI supports at most 6 parameters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mixed int/float parameters&lt;/strong&gt; (interpreter, section 4). &lt;code&gt;kcc&lt;/code&gt; handles them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structs by value.&lt;/strong&gt; Pass a pointer to a buffer, as in section 8. The roadmap lists full ABI marshalling for the interpreter as planned work.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  12. Errors you will see
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Message&lt;/th&gt;
&lt;th&gt;Cause and fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;symbol 'nosuch_function' not found: ... undefined symbol: nosuch_function&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The library loaded but doesn't contain that name. Check spelling, and that you didn't compile C++ without &lt;code&gt;extern "C"&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;could not load library 'libnope.so': ... cannot open shared object file&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The library isn't where the loader looks. Use &lt;code&gt;./&lt;/code&gt; for a native import, run from the right folder for a declared extern, or use an absolute path.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;'abs' expects 1 argument(s), got 2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The call doesn't match your declaration. This is checked before C is called.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;passing a pointer or buffer to native code requires an unsafe { } block&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Wrap the call in &lt;code&gt;unsafe { }&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;v0.1 FFI requires all parameters to be the same kind&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Mixed int and float parameters (section 4).&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  13. The safety boundary, plainly
&lt;/h2&gt;

&lt;p&gt;KingCobra's memory checks end where C begins. Native code can read or write anywhere, and &lt;strong&gt;a crash inside C can't be caught.&lt;/strong&gt; This program tries to catch it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"libc.so.6"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"caught"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"after"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passing a null pointer to &lt;code&gt;strlen&lt;/code&gt; makes C crash, and the whole process dies with &lt;code&gt;Segmentation fault&lt;/code&gt; (exit code 139). Neither &lt;code&gt;caught&lt;/code&gt; nor &lt;code&gt;after&lt;/code&gt; is printed.&lt;/p&gt;

&lt;p&gt;So the habits that keep FFI code safe are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep &lt;code&gt;unsafe&lt;/code&gt; blocks to a line or two&lt;/strong&gt;, and put them inside wrapper functions that validate their inputs (section 9).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prefer KingCobra buffers over &lt;code&gt;malloc&lt;/code&gt;&lt;/strong&gt;, so memory is freed by the runtime, and use &lt;code&gt;defer&lt;/code&gt; for anything C allocates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check sizes in KingCobra before calling C&lt;/strong&gt;, since C won't check them for you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the C library separately.&lt;/strong&gt; A bug in your C code will look like a mysterious KingCobra crash.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Run &lt;code&gt;kingcobra --leaks&lt;/code&gt;&lt;/strong&gt; to catch KingCobra-side buffers you forgot to free.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;docs/POINTERS.md&lt;/code&gt;: the pointer, buffer and struct-layout reference&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docs/KCC.md&lt;/code&gt;: compiling to native executables, and the freestanding target&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;examples/std_ffi.kc&lt;/code&gt; and &lt;code&gt;examples/native_import_demo.kc&lt;/code&gt;: the shortest runnable FFI examples in the repo&lt;/li&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you hit a wall the FFI can't get past, or find a signature that behaves differently from what's described here, open an issue.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>c</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Learn KingCobra in 30 minutes: a hands-on tutorial</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Thu, 19 Nov 2026 19:13:50 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/learn-kingcobra-in-30-minutes-a-hands-on-tutorial-53eh</link>
      <guid>https://dev.to/divyanshusinha136/learn-kingcobra-in-30-minutes-a-hands-on-tutorial-53eh</guid>
      <description>&lt;p&gt;This tutorial takes you from "nothing installed" to writing, testing and organising real KingCobra programs. &lt;strong&gt;Every example below was run on KingCobra 1.0.0, and the output shown is the real output.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You don't need to know C or systems programming. If you've used Python or JavaScript, most of it will feel familiar. The new parts are explicit memory and pointers, and the language checks those for you, so mistakes become clear errors instead of crashes.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Install and say hello
&lt;/h2&gt;

&lt;p&gt;KingCobra runs on Linux, macOS and Windows, and the same source file runs unchanged on all three. Pick the install that matches your machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Windows&lt;/strong&gt; (x64; ARM machines run it through the built-in emulation)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Chocolatey (PowerShell as Administrator)&lt;/span&gt;
&lt;span class="n"&gt;choco&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;kingcobra&lt;/span&gt;

&lt;span class="c1"&gt;# Scoop&lt;/span&gt;
&lt;span class="n"&gt;scoop&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;DivyanshuSinha136&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;kingcobra&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;releases&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;kingcobra&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="c1"&gt;# or PowerShell with no administrator rights&lt;/span&gt;
&lt;span class="n"&gt;irm&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;DivyanshuSinha136&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;kingcobra&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;releases&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;latest&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;download&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;install&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ps1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;iex&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Linux and macOS&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -fsSL https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.sh &lt;span class="o"&gt;|&lt;/span&gt; sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The installer picks the right package for your machine and verifies its SHA-256 checksum: a &lt;code&gt;.deb&lt;/code&gt; on Debian and Ubuntu, an &lt;code&gt;.rpm&lt;/code&gt; on Fedora and RHEL-family systems, and a tarball into &lt;code&gt;~/.local&lt;/code&gt; on macOS and on Alpine and other musl systems. If there is no prebuilt package for your CPU (a Raspberry Pi Zero, for example), it compiles KingCobra on your machine, which needs a C compiler and &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From source&lt;/strong&gt; (any platform with &lt;code&gt;make&lt;/code&gt; and a C compiler; on Windows use MSYS2 / MinGW-w64)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/DivyanshuSinha136/KingCobra
&lt;span class="nb"&gt;cd&lt;/span&gt; KingCobra
make
./kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After an installer or package manager, open a &lt;strong&gt;new terminal&lt;/strong&gt; (so your &lt;code&gt;PATH&lt;/code&gt; is refreshed) and check it. If you built from source, run &lt;code&gt;./kingcobra&lt;/code&gt; from the build folder instead, or &lt;code&gt;sudo make install&lt;/code&gt; to put it on your &lt;code&gt;PATH&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra version
kingcobra repl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To update, run &lt;code&gt;choco upgrade kingcobra&lt;/code&gt; or &lt;code&gt;scoop update kingcobra&lt;/code&gt; on Windows, or run the install script again on Linux and macOS.&lt;/p&gt;

&lt;p&gt;The REPL keeps your definitions, continues unclosed brackets on the next line, and &lt;code&gt;_&lt;/code&gt; is the last result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight irb"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;31&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tip for Windows:&lt;/strong&gt; if non-ASCII text looks garbled in &lt;code&gt;cmd.exe&lt;/code&gt; (for example an em dash printing as &lt;code&gt;ΓÇö&lt;/code&gt;), the console is using an old code page. Run &lt;code&gt;chcp 65001&lt;/code&gt; first, or use Windows Terminal, which uses UTF-8 by default.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now a real file. Save this as &lt;code&gt;hello.kc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, KingCobra!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra run hello.kc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, KingCobra!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Programs start at &lt;code&gt;fn main&lt;/code&gt;. Files use the &lt;code&gt;.kc&lt;/code&gt; extension. The command is the same in PowerShell, Terminal, bash or zsh: &lt;code&gt;kingcobra run hello.kc&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Values and variables
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Ada"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// immutable&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// mutable&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="no"&gt;LIMIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// same as let, but reads as intent&lt;/span&gt;
    &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{name} has {score} of {LIMIT}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;big&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9007199254740993&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// exact 64-bit integers&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;big&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;idiv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"h\u{e9}llo"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0xFF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mb"&gt;0b1010&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ada has 15 of 100
9007199254740994
3.5 4 3 1
97 6 float null
255 10 1000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;let&lt;/code&gt; is immutable, &lt;code&gt;var&lt;/code&gt; is mutable.&lt;/strong&gt; Type annotations (&lt;code&gt;let x: int = 5&lt;/code&gt;) are optional.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integers are exact 64-bit.&lt;/strong&gt; &lt;code&gt;9007199254740993 + 1&lt;/code&gt; is correct, where a double-based language would round it. &lt;code&gt;_&lt;/code&gt; separates digits and &lt;code&gt;0x&lt;/code&gt; / &lt;code&gt;0b&lt;/code&gt; literals work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/&lt;/code&gt; returns an int when the division is exact&lt;/strong&gt; (&lt;code&gt;8 / 2&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt;) &lt;strong&gt;and a float otherwise&lt;/strong&gt; (&lt;code&gt;7 / 2&lt;/code&gt; is &lt;code&gt;3.5&lt;/code&gt;). &lt;code&gt;idiv&lt;/code&gt; is floor division.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A character literal like &lt;code&gt;'a'&lt;/code&gt; is an int&lt;/strong&gt; holding the Unicode code point (97).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strings are UTF-8 and &lt;code&gt;len&lt;/code&gt; counts bytes.&lt;/strong&gt; &lt;code&gt;"h\u{e9}llo"&lt;/code&gt; is 6 bytes because &lt;code&gt;é&lt;/code&gt; (written here as the escape &lt;code&gt;\u{e9}&lt;/code&gt;) takes two. Save your &lt;code&gt;.kc&lt;/code&gt; files as UTF-8 so text typed directly into a string gets the same result on every platform.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;f-strings&lt;/strong&gt; evaluate any expression inside &lt;code&gt;{...}&lt;/code&gt;. A plain string never interpolates, so JSON text with braces is safe.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Control flow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Level { Low, Mid = 5, High }

fn describe(n: int) -&amp;gt; string {
    return match (n) {
        0 =&amp;gt; "zero",
        1 | 2 | 3 =&amp;gt; "few",
        _ =&amp;gt; "many",
    };
}

fn main() {
    for (i in 0..4) { print(i, describe(i)); }

    var total = 0;
    var i = 0;
    while (i &amp;lt; 5) { total += i; i += 1; }
    print("total", total);

    outer: for (a in 0..3) {
        for (b in 0..3) {
            if (b == 2) { continue outer; }
            if (a == 2) { break outer; }
            print("pair", a, b);
        }
    }
    print(Level.High, describe(Level.Mid));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 zero
1 few
2 few
3 few
total 10
pair 0 0
pair 0 1
pair 1 0
pair 1 1
6 many
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0..4&lt;/code&gt; is a &lt;strong&gt;half-open range&lt;/strong&gt; (0, 1, 2, 3). You can also loop over arrays, strings and objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;match&lt;/code&gt; is an expression.&lt;/strong&gt; Arms are checked top to bottom, &lt;code&gt;|&lt;/code&gt; lets several patterns share an arm, and &lt;code&gt;_&lt;/code&gt; is the catch-all. With no match and no &lt;code&gt;_&lt;/code&gt;, it's a runtime error and never a silent &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Labeled loops&lt;/strong&gt; let you &lt;code&gt;break outer&lt;/code&gt; and &lt;code&gt;continue outer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enums are int constants&lt;/strong&gt;: &lt;code&gt;Level.Low&lt;/code&gt; is 0, &lt;code&gt;Level.Mid&lt;/code&gt; is 5 and &lt;code&gt;Level.High&lt;/code&gt; follows at 6.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Functions and closures
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn area(w: int, h: int = w) -&amp;gt; int { return w * h; }
fn sum(...xs: int) -&amp;gt; int {
    var t = 0;
    for (x in xs) { t += x; }
    return t;
}
fn make_counter() -&amp;gt; fn {
    var n = 0;
    fn next() -&amp;gt; int { n += 1; return n; }
    return next;
}

fn main() {
    print(area(3), area(3, 5), sum(1, 2, 3, 4));
    let c = make_counter();
    print(c(), c(), c());
    let double = fn(x: int) -&amp;gt; int { return x * 2; };
    print(double(21));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;9 15 10
1 2 3
42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;default value is evaluated at each call&lt;/strong&gt; and may use earlier parameters (&lt;code&gt;h&lt;/code&gt; defaults to &lt;code&gt;w&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;...xs&lt;/code&gt; collects extra arguments into an array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Closures capture variables.&lt;/strong&gt; &lt;code&gt;make_counter&lt;/code&gt; returns a function that remembers its own &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Functions are values, so &lt;code&gt;fn(x: int) -&amp;gt; int { ... }&lt;/code&gt; works anywhere an expression does.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Arrays, objects and strings
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let nums = [5, 3, 8, 1, 9];
    let [first, ...rest] = nums;
    print(first, rest, nums[1..3], len(nums));
    print(nums.map(fn(x) -&amp;gt; int { return x * x; }));
    print(nums.filter(fn(x) -&amp;gt; bool { return x &amp;gt; 4; }));
    print(nums.reduce(fn(a, b) -&amp;gt; int { return a + b; }, 0));
    print(sorted(nums), nums);
    print(8 in nums, "ell" in "hello");

    let user = { name: "ada", langs: ["c", "kc"] };
    user["age"] = 36;
    print(user.name, user.keys(), len(user));
    for (k, v in user) { print(k, v); }
    print("  Hello  ".trim().upper(), "a,b,c".split(","));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5 [3, 8, 1, 9] [3, 8] 5
[25, 9, 64, 1, 81]
[5, 8, 9]
26
[1, 3, 5, 8, 9] [5, 3, 8, 1, 9]
true true
ada [name, langs, age] 3
name ada
langs [c, kc]
age 36
HELLO [a, b, c]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Destructuring&lt;/strong&gt; with &lt;code&gt;let [first, ...rest] = nums;&lt;/code&gt; splits an array into a head and a tail.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slicing&lt;/strong&gt; &lt;code&gt;nums[1..3]&lt;/code&gt; copies a range. Out-of-range bounds are an error and are never silently clamped.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt;, &lt;code&gt;any&lt;/code&gt;, &lt;code&gt;all&lt;/code&gt; and more are built in. &lt;strong&gt;&lt;code&gt;sorted(a)&lt;/code&gt; returns a sorted copy&lt;/strong&gt;, while &lt;code&gt;a.sort()&lt;/code&gt; sorts in place.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;in&lt;/code&gt;&lt;/strong&gt; tests membership in arrays, substrings in strings, and keys in objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Objects&lt;/strong&gt; like &lt;code&gt;{ name: "ada" }&lt;/code&gt; are open: you can add keys with &lt;code&gt;user["age"] = 36&lt;/code&gt;, and a missing key reads as &lt;code&gt;null&lt;/code&gt; with index syntax.&lt;/li&gt;
&lt;li&gt;Strings have &lt;code&gt;upper&lt;/code&gt;, &lt;code&gt;lower&lt;/code&gt;, &lt;code&gt;trim&lt;/code&gt;, &lt;code&gt;split&lt;/code&gt;, &lt;code&gt;replace&lt;/code&gt;, &lt;code&gt;pad_left&lt;/code&gt;, &lt;code&gt;repeat&lt;/code&gt; and more.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Structs, enums and &lt;code&gt;impl&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;len2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;len2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;len2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// structs are reference types&lt;/span&gt;
    &lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25 0
10
HELLO!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A function whose first parameter is &lt;code&gt;self&lt;/code&gt; is a method (&lt;code&gt;p.len2()&lt;/code&gt;). Any other function in an &lt;code&gt;impl&lt;/code&gt; is called on the type (&lt;code&gt;Point.origin()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;impl&lt;/code&gt; also works on built-in types.&lt;/strong&gt; &lt;code&gt;impl string { ... }&lt;/code&gt; added &lt;code&gt;.shout()&lt;/code&gt; to every string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structs are reference types.&lt;/strong&gt; &lt;code&gt;let q = p;&lt;/code&gt; makes &lt;code&gt;q&lt;/code&gt; another name for the same struct, so changing &lt;code&gt;q.x&lt;/code&gt; changed &lt;code&gt;p.x&lt;/code&gt;. The same is true of arrays.&lt;/li&gt;
&lt;li&gt;Struct literals must provide exactly the declared fields, and the static checker verifies each field's type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Errors and &lt;code&gt;defer&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn parse_age(text: string) -&amp;gt; int {
    let n = int(text);
    if (n &amp;lt; 0) { throw error("age cannot be negative"); }
    return n;
}

fn main() {
    for (text in ["30", "-4"]) {
        try { print("age", parse_age(text)); }
        catch (e) { print("rejected:", e.message); }
    }

    try { let a = [1, 2, 3]; print(a[10]); }
    catch (e) { print("caught:", e.message); }

    fn demo() {
        defer print("cleanup runs last");
        print("working");
        throw "boom";
    }
    try { demo(); } catch (e) { print("error:", e); }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;age 30
rejected: age cannot be negative
caught: index 10 out of bounds (len 3)
working
cleanup runs last
error: boom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Every runtime error is catchable&lt;/strong&gt;, including out-of-bounds indexing. Inside &lt;code&gt;catch (e)&lt;/code&gt;, runtime errors give you &lt;code&gt;e.message&lt;/code&gt;, &lt;code&gt;e.file&lt;/code&gt;, &lt;code&gt;e.line&lt;/code&gt; and &lt;code&gt;e.col&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You can &lt;code&gt;throw&lt;/code&gt; any value&lt;/strong&gt; (a string, a number, an object). &lt;code&gt;error("text")&lt;/code&gt; builds a standard error object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;defer&lt;/code&gt; runs when the block exits&lt;/strong&gt;, including while an error unwinds, in reverse order. It's the main tool for cleanup.&lt;/li&gt;
&lt;li&gt;An uncaught error prints the message with the source line and a caret, and exits with status 65.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Memory and pointers
&lt;/h2&gt;

&lt;p&gt;This is what makes KingCobra a systems language. &lt;strong&gt;There is no garbage collector.&lt;/strong&gt; You free what you allocate, and the runtime catches the classic mistakes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn checksum(n: int) -&amp;gt; int {
    let p = alloc(n);
    defer free(p);               // runs on every exit path
    memset(p, 1, n);
    return p[0] + p[n - 1];
}

fn main() {
    print(checksum(16));

    let a = [1, 2, 3];
    let b = a;                   // same array, two names
    free(a);
    try { print(b[0]); }
    catch (e) { print("caught:", e.message); }

    let buf = alloc(4);
    try { buf[10] = 1; }
    catch (e) { print("caught:", e.message); }
    free(buf);

    var x = 41;
    let p = &amp;amp;x;                  // &amp;amp;x of a var is writable
    *p += 1;
    print(x);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2
caught: use after free (this array was freed)
caught: pointer write of 1 byte(s) at offset 10 is out of bounds (valid range 0..4 of allocation #2)
42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;alloc(n)&lt;/code&gt; gives you &lt;code&gt;n&lt;/code&gt; bytes.&lt;/strong&gt; &lt;code&gt;free&lt;/code&gt; releases memory, and &lt;code&gt;defer free(p)&lt;/code&gt; guarantees it happens on every path out of the function, including errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use after free is an error&lt;/strong&gt;, not silent corruption. &lt;code&gt;free(a)&lt;/code&gt; freed the array that &lt;code&gt;b&lt;/code&gt; also points to, and reading through &lt;code&gt;b&lt;/code&gt; was caught.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pointers are bounds-checked.&lt;/strong&gt; Writing at offset 10 of a 4-byte allocation raised a precise error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;&amp;amp;x&lt;/code&gt;, &lt;code&gt;*p&lt;/code&gt; and &lt;code&gt;p-&amp;gt;field&lt;/code&gt;&lt;/strong&gt; work like C. Pointers into KingCobra variables, array elements and struct fields are all checked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Double free is also an error, and you can ask KingCobra to report what you forgot to free. Given &lt;code&gt;prog.kc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"allocated, never freed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra --leaks prog.kc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;allocated, never freed
kingcobra: memory report
  leaked blocks (allocated, never freed):
          32 bytes  alloc()  at prog.kc:2:13
  total: 1 block(s), 32 byte(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Raw machine addresses (from FFI, or forged from an integer) still exist, but dereferencing them needs an &lt;code&gt;unsafe { }&lt;/code&gt; block. Anything that could actually corrupt memory has to be marked.&lt;/p&gt;

&lt;p&gt;Learn more in &lt;code&gt;docs/MEMORY.md&lt;/code&gt; and &lt;code&gt;docs/POINTERS.md&lt;/code&gt; in the repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Modules and the standard library
&lt;/h2&gt;

&lt;p&gt;Split code into files and import them. Given &lt;code&gt;mathlib.kc&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// Square of a number.&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you can use it next to the standard library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight d"&gt;&lt;code&gt;&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;regx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="s2"&gt;"mathlib.kc"&lt;/span&gt; &lt;span class="n"&gt;as&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"{\"name\":\"Ada\",\"langs\":[\"c\",\"kc\"],\"age\":36}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;langs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"^\\d+$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"12345"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\\d+"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"order #4821 shipped"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\\s*,\\s*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a , b,c"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;49 27
Ada [c, kc] 37
{"name":"Ada","langs":["c","kc"],"age":37}
true
4821
[a, b, c]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;import "file.kc" as m&lt;/code&gt;&lt;/strong&gt; brings in your own files under a name. &lt;code&gt;import std.json;&lt;/code&gt; loads a standard module.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;json.parse&lt;/code&gt; returns an open object, which you read with &lt;code&gt;doc.name&lt;/code&gt; or &lt;code&gt;doc["name"]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Run &lt;strong&gt;&lt;code&gt;kingcobra modules&lt;/code&gt;&lt;/strong&gt; to list all 21 standard modules: &lt;code&gt;system&lt;/code&gt;, &lt;code&gt;memory&lt;/code&gt;, &lt;code&gt;network&lt;/code&gt;, &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;rhex&lt;/code&gt;, &lt;code&gt;io&lt;/code&gt;, &lt;code&gt;vector&lt;/code&gt;, &lt;code&gt;regx&lt;/code&gt;, &lt;code&gt;nlp&lt;/code&gt;, &lt;code&gt;nn&lt;/code&gt;, &lt;code&gt;gui&lt;/code&gt;, &lt;code&gt;mmio&lt;/code&gt;, &lt;code&gt;acpi&lt;/code&gt;, &lt;code&gt;json&lt;/code&gt;, &lt;code&gt;csv&lt;/code&gt;, &lt;code&gt;fbin&lt;/code&gt;, &lt;code&gt;mmu&lt;/code&gt;, &lt;code&gt;game&lt;/code&gt;, &lt;code&gt;ascii_screen&lt;/code&gt;, &lt;code&gt;tts&lt;/code&gt; and &lt;code&gt;music_note&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Most modules behave the same everywhere. The ones that talk to the operating system use the native facility on each platform: &lt;code&gt;std.gui&lt;/code&gt; uses X11 on Linux and Win32 on Windows, with a headless mode for servers and tests, and &lt;code&gt;std.system&lt;/code&gt; reports the OS, CPU and memory of the machine it runs on.&lt;/li&gt;
&lt;li&gt;Imports use forward slashes on every platform, including Windows, so &lt;code&gt;import "../main.kc" as app;&lt;/code&gt; works the same everywhere.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;examples/&lt;/code&gt; folder has a runnable program for each one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10. Build a small program: word frequency
&lt;/h2&gt;

&lt;p&gt;Putting it together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// Count how often each word appears.
fn word_counts(text: string) -&amp;gt; object {
    let counts = {};
    for (w in text.lower().split(" ")) {
        if (w == "") { continue; }
        counts[w] = (counts[w] ?? 0) + 1;
    }
    return counts;
}

fn main() {
    let text = "the quick brown fox jumps over the lazy dog the end";
    let counts = word_counts(text);
    let rows = counts.items();                       // [[word, count], ...]
    rows.sort(fn(a, b) -&amp;gt; int { return b[1] - a[1]; });
    for (row in rows[..3]) {
        print(row[0].pad_right(6), row[1]);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;the    3
quick  1
brown  1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;??&lt;/code&gt; supplies a default when a key is missing, &lt;code&gt;items()&lt;/code&gt; turns an object into &lt;code&gt;[key, value]&lt;/code&gt; pairs, and &lt;code&gt;sort&lt;/code&gt; takes a comparator that returns a negative, zero or positive number. The &lt;code&gt;///&lt;/code&gt; comment above &lt;code&gt;word_counts&lt;/code&gt; is a doc comment: &lt;code&gt;help(word_counts)&lt;/code&gt; prints it.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Tooling: projects, tests, type checks
&lt;/h2&gt;

&lt;p&gt;Create a project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra new myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  created  myapp/main.kc
  created  myapp/tests/test_main.kc
  created  myapp/README.md
  created  myapp/.gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The scaffold includes a function and a test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// tests/test_main.kc
import "../main.kc" as app;

fn test_greet() {
    assert_eq(app.greet("KingCobra"), "Hello, KingCobra!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A test is any global &lt;code&gt;fn test_...()&lt;/code&gt;. It passes if it returns and fails if an &lt;code&gt;assert&lt;/code&gt; or &lt;code&gt;assert_eq&lt;/code&gt; fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; myapp
kingcobra &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tap"&gt;&lt;code&gt;&lt;span class="n"&gt;tests&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;test_main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kc&lt;/span&gt;
  &lt;span class="n"&gt;ok&lt;/span&gt;   &lt;span class="n"&gt;test_greet&lt;/span&gt;

&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;passed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Useful options: &lt;code&gt;kingcobra test -k parse&lt;/code&gt; runs tests whose name contains "parse", &lt;code&gt;-l&lt;/code&gt; lists them, and &lt;code&gt;-x -q&lt;/code&gt; stops at the first failure.&lt;/p&gt;

&lt;p&gt;KingCobra also has a &lt;strong&gt;static type checker&lt;/strong&gt;. It runs before your program does, and you can run it on its own:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"hi"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kingcobra check bad.kc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bad.kc:4:18: error: 'double' argument 1: expected int, got string
    4 |     print(double(s));
      |                  ^
1 of 1 file(s) have errors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The checker is deliberately permissive, so it catches clear mistakes without getting in your way. Skip it with &lt;code&gt;--no-typecheck&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Speed
&lt;/h2&gt;

&lt;p&gt;KingCobra is a tree-walking interpreter with a &lt;strong&gt;closure JIT&lt;/strong&gt;. After a function has been called 50 times, if it uses only integers (arithmetic, comparisons, &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;while&lt;/code&gt;, self-recursion), it's compiled to x86-64 machine code. A recursive &lt;code&gt;fib(32)&lt;/code&gt; runs in about &lt;strong&gt;0.025 s with the JIT and 0.34 s with &lt;code&gt;--no-jit&lt;/code&gt;&lt;/strong&gt; on a typical x86-64 machine (your numbers will differ). Results are identical either way.&lt;/p&gt;

&lt;p&gt;The JIT covers a specific int-only shape and only exists on x86-64, which includes the Windows x64 build. On other CPUs (Apple Silicon, ARM and RISC-V boards) everything runs in the interpreter, which is also correct but slower. Check what your build has with &lt;code&gt;kingcobra info&lt;/code&gt;. For a faster interpreter overall, build from source with &lt;code&gt;make pgo&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The expert guide (PDF):&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_Complete_Expert_Guide.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_Complete_Expert_Guide.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Language reference:&lt;/strong&gt; &lt;code&gt;docs/LANGUAGE.md&lt;/code&gt; and the formal grammar in &lt;code&gt;docs/GRAMMAR.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Examples:&lt;/strong&gt; the &lt;code&gt;examples/&lt;/code&gt; folder, one runnable program per module, plus larger programs in &lt;code&gt;examples/showcase&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools:&lt;/strong&gt; &lt;code&gt;kcide&lt;/code&gt; (terminal IDE with a debugger), &lt;code&gt;kcc&lt;/code&gt; (native compiler), &lt;code&gt;kasm&lt;/code&gt; (assembler)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Platforms and packaging:&lt;/strong&gt; &lt;code&gt;docs/PLATFORMS.md&lt;/code&gt; and &lt;code&gt;docs/PACKAGING.md&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Roadmap and stability:&lt;/strong&gt; &lt;code&gt;docs/ROADMAP.md&lt;/code&gt; and &lt;code&gt;docs/STABILITY.md&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you build something, or hit a bug or a confusing error message, please open an issue. Feedback on the language design is welcome too.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>programming</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>KingCobra 1.0: a native systems language that feels like a scripting language</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Thu, 19 Nov 2026 10:51:47 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/kingcobra-10-a-native-systems-language-that-feels-like-a-scripting-language-4o2m</link>
      <guid>https://dev.to/divyanshusinha136/kingcobra-10-a-native-systems-language-that-feels-like-a-scripting-language-4o2m</guid>
      <description>&lt;p&gt;I'm Divyanshu, I'm 18, and I've spent a long time building &lt;strong&gt;KingCobra&lt;/strong&gt;, a programming language that tries to feel like a scripting language while behaving like a systems language. &lt;strong&gt;v1.0.0 is out now&lt;/strong&gt;, and on Windows it installs with a single &lt;code&gt;choco install kingcobra&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nums = [5, 3, 8, 1, 9];
let [first, ...rest] = nums;
print(nums.map(fn(x) -&amp;gt; int { return x * x; }).filter(fn(x) -&amp;gt; bool { return x &amp;gt; 10; }));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is it?
&lt;/h2&gt;

&lt;p&gt;KingCobra has a small, readable syntax and a REPL: &lt;code&gt;let&lt;/code&gt;/&lt;code&gt;var&lt;/code&gt;, f-strings, slicing, destructuring, &lt;code&gt;match&lt;/code&gt;, structs and &lt;code&gt;impl&lt;/code&gt;, enums, &lt;code&gt;try&lt;/code&gt;/&lt;code&gt;catch&lt;/code&gt;. Underneath, it gives you things scripting languages usually hide: explicit memory, C-style pointers, a real FFI and raw machine code.&lt;/p&gt;

&lt;p&gt;The whole project (language, runtime, JIT and standard library) is plain &lt;strong&gt;C11 with no third-party build dependencies&lt;/strong&gt;. It builds with &lt;code&gt;make&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Platform&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Windows (Chocolatey, PowerShell as Administrator)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;choco install kingcobra&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows (Scoop)&lt;/td&gt;
&lt;td&gt;&lt;code&gt;scoop install https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/kingcobra.json&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Windows (PowerShell, no admin)&lt;/td&gt;
&lt;td&gt;`irm &lt;a href="https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.ps1" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.ps1&lt;/a&gt; \&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux / macOS&lt;/td&gt;
&lt;td&gt;{% raw %}`curl -fsSL &lt;a href="https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.sh" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.sh&lt;/a&gt; \&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;From source&lt;/td&gt;
&lt;td&gt;{% raw %}&lt;code&gt;git clone&lt;/code&gt;, then &lt;code&gt;make &amp;amp;&amp;amp; ./kingcobra repl&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Open a new terminal and run &lt;code&gt;kingcobra version&lt;/code&gt;. Packages exist for Debian/Ubuntu, Fedora/RHEL and musl Linux (amd64 and arm64, plus armhf and riscv64 on Debian), and the installer compiles from source when no prebuilt package fits your machine. Releases ship a &lt;code&gt;SHA256SUMS&lt;/code&gt; file and a build-provenance attestation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fn fact&lt;span class="o"&gt;(&lt;/span&gt;n: int&lt;span class="o"&gt;)&lt;/span&gt; -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; int &lt;span class="o"&gt;{&lt;/span&gt;
...     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;n &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; 2&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;return&lt;/span&gt; 1&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
...     &lt;span class="nb"&gt;return&lt;/span&gt; n * fact&lt;span class="o"&gt;(&lt;/span&gt;n - 1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
... &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; fact&lt;span class="o"&gt;(&lt;/span&gt;10&lt;span class="o"&gt;)&lt;/span&gt;
3628800
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; _ + 1
3628801
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;_&lt;/code&gt; is the last result, and errors never end the session.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory: no garbage collector, but not unchecked
&lt;/h2&gt;

&lt;p&gt;KingCobra has no GC. &lt;code&gt;free&lt;/code&gt; works on every heap value, and the mistakes that are usually silent in C become errors with a source location:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: double free (allocation #1 was already freed)
    4 |     free(buf);
      |     ^
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;defer&lt;/code&gt; runs even while an error unwinds, pointers and buffers are bounds- and lifetime-checked, and &lt;code&gt;kingcobra --leaks prog.kc&lt;/code&gt; reports what you forgot to free:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kingcobra: memory report
  leaked blocks (allocated, never freed):
          32 bytes  alloc()  at leak.kc:2:13
  total: 1 block(s), 32 byte(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Execution model
&lt;/h2&gt;

&lt;p&gt;KingCobra is a tree-walking interpreter with a &lt;strong&gt;tiered closure JIT&lt;/strong&gt;. Every program runs correctly in the interpreter from the first call. After 50 calls, a function that fits an int-only shape (up to 4 parameters, locals, arithmetic, comparisons, &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;while&lt;/code&gt;, self-recursion) is compiled once to x86-64 machine code. On my test machine, &lt;code&gt;fib(35)&lt;/code&gt; takes &lt;strong&gt;0.10 s with the JIT and 1.43 s with &lt;code&gt;--no-jit&lt;/code&gt;&lt;/strong&gt;, about 14× faster.&lt;/p&gt;

&lt;p&gt;I want to be precise about the limits. The JIT only covers that int-only shape, only on x86-64, and results are identical with it off. The interpreter itself got several times faster in earlier rounds by removing allocation and lookup overhead: interned names, pooled scopes, cached method dispatch. &lt;code&gt;make pgo&lt;/code&gt; adds a profile-guided build that is typically 10–25% faster than plain &lt;code&gt;-O2&lt;/code&gt;. &lt;code&gt;docs/PERFORMANCE.md&lt;/code&gt; has the measurements and the regressions I hit along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Batteries included: 21 native modules
&lt;/h2&gt;

&lt;p&gt;No external dependencies. Load one with &lt;code&gt;import std.&amp;lt;name&amp;gt;;&lt;/code&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;Modules&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;System &amp;amp; hardware&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;system&lt;/code&gt;, &lt;code&gt;memory&lt;/code&gt;, &lt;code&gt;rhex&lt;/code&gt; (raw machine code, &lt;code&gt;cpuid&lt;/code&gt;), &lt;code&gt;mmio&lt;/code&gt;, &lt;code&gt;acpi&lt;/code&gt;, &lt;code&gt;mmu&lt;/code&gt; (software MMU for x86/ARM64/RISC-V page tables)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Networking&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;network&lt;/code&gt; (TCP/UDP, raw sockets), &lt;code&gt;http&lt;/code&gt; (HTTP/1.1 client with verified HTTPS, gzip/deflate, cookies, small server)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data formats&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;json&lt;/code&gt; (strict RFC 8259, JSONPath, patch, schema validation), &lt;code&gt;csv&lt;/code&gt;, &lt;code&gt;fbin&lt;/code&gt; (binary files, struct pack/unpack, hexdump), &lt;code&gt;io&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Text&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;regx&lt;/code&gt; (named groups, lookahead, backrefs), &lt;code&gt;nlp&lt;/code&gt; (TF-IDF, BM25, naive Bayes, spell correction)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AI &amp;amp; math&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;nn&lt;/code&gt; (tensors, conv/LSTM/GRU/attention layers, Adam, gradient checking), &lt;code&gt;vector&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Graphics &amp;amp; games&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;game&lt;/code&gt; (2D/3D engine), &lt;code&gt;gui&lt;/code&gt; (X11 / Win32 / headless), &lt;code&gt;ascii_screen&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Audio &amp;amp; speech&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;music_note&lt;/code&gt; (notation language, DSP, WAV/MIDI), &lt;code&gt;tts&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;make test&lt;/code&gt; runs every example end to end, so the modules are exercised, not just listed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tooling, written in KingCobra where it counts
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;kingcobra check&lt;/code&gt;&lt;/strong&gt; is a static type checker. It is deliberately permissive and can be skipped with &lt;code&gt;--no-typecheck&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;kingcobra test&lt;/code&gt;&lt;/strong&gt; runs every &lt;code&gt;test_*&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;kcide&lt;/code&gt;&lt;/strong&gt; is a terminal IDE with syntax highlighting, completion, live diagnostics and a debugger (breakpoints, stepping, watches).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;kcc&lt;/code&gt;&lt;/strong&gt; compiles KingCobra to native executables (ELF, PE, Mach-O) and is itself written in KingCobra.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;kasm&lt;/code&gt;&lt;/strong&gt; is a NASM-style x86 assembler written in KingCobra (16/32/64-bit), checked byte-for-byte against GNU &lt;code&gt;as&lt;/code&gt; on 817 instructions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;KCOS&lt;/strong&gt; is a small x86-64 operating system written in KingCobra and compiled by &lt;code&gt;kcc&lt;/code&gt;. It boots in QEMU.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;About 14,000 lines of KingCobra (compiler, assembler and OS) are written in the language itself, which has been the best stress test I could give it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Honest limits
&lt;/h2&gt;

&lt;p&gt;I would rather tell you than have you find out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The JIT covers an int-only shape, and only on x86-64.&lt;/li&gt;
&lt;li&gt;The type checker is permissive on purpose.&lt;/li&gt;
&lt;li&gt;KCOS has been booted and checked in QEMU, not tried on real hardware.&lt;/li&gt;
&lt;li&gt;macOS builds with &lt;code&gt;make&lt;/code&gt; but isn't covered by CI.&lt;/li&gt;
&lt;li&gt;On Windows, closures that capture variables can misbehave in the interpreter build.&lt;/li&gt;
&lt;li&gt;KingCobra has very few users so far, and you may find bugs that I haven't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;docs/STABILITY.md&lt;/code&gt; says what you can rely on, and &lt;code&gt;docs/ROADMAP.md&lt;/code&gt; says what's next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it, and tell me what breaks
&lt;/h2&gt;

&lt;p&gt;The most useful thing you can do is write something real in it and open an issue when it misbehaves, especially around the memory model, the pointer checks and the JIT boundary. Design criticism is welcome too.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Tutorial: &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/tree/main/tutorial&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Expert guide (PDF): &lt;a href="https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_Complete_Expert_Guide.pdf" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/KingCobra/blob/main/tutorial/KingCobra_Complete_Expert_Guide.pdf&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MIT licensed. If it's interesting to you, a star helps other people find it.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>programming</category>
      <category>opensource</category>
      <category>compilers</category>
    </item>
    <item>
      <title>🚀 Announcing Pythonaibrain-Warden (v0.1.1): Industrial-Grade, npm-Inspired Package &amp; Environment Manager for Python</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Mon, 24 Aug 2026 07:59:58 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/announcing-pythonaibrain-warden-v010-industrial-grade-npm-inspired-package-environment-4o2c</link>
      <guid>https://dev.to/divyanshusinha136/announcing-pythonaibrain-warden-v010-industrial-grade-npm-inspired-package-environment-4o2c</guid>
      <description>&lt;p&gt;I am excited to announce the official release of &lt;strong&gt;&lt;code&gt;warden&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;pythonaibrain-warden&lt;/code&gt; v0.1.1)!&lt;/p&gt;

&lt;p&gt;Python package management often forces developers to choose between low-level speed and high-level reproducibility. I built &lt;code&gt;warden&lt;/code&gt; to bridge this gap by introducing an npm-inspired workflow centered on a deterministic lockfile architecture, native virtual environment isolation, and hard offline build guarantees.&lt;/p&gt;




&lt;h3&gt;
  
  
  🌟 Why I Built &lt;code&gt;warden&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🔐 &lt;strong&gt;Deterministic Lockfile Enforcement (&lt;code&gt;warden.lock&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;warden&lt;/code&gt; locks exact artifact URLs and SHA-256 hashes. Every installation executes via &lt;code&gt;pip install &amp;lt;exact-artifact&amp;gt; --no-deps&lt;/code&gt;, preventing pip's resolver from silently substituting different package versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔌 &lt;strong&gt;Thin CLI over Core API Architecture:&lt;/strong&gt; I designed every CLI subcommand as a light wrapper around &lt;code&gt;warden.core&lt;/code&gt;. Whether called from terminal scripts, unit tests, or programmatically via &lt;code&gt;import warden.core as core&lt;/code&gt;, execution behavior remains identical.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🛠️ &lt;strong&gt;Warden-Native Environments &amp;amp; Absolute Paths:&lt;/strong&gt; &lt;code&gt;warden wenv&lt;/code&gt; constructs standard CPython virtual environments natively. Subprocess calls resolve executables to absolute paths inside the active environment (&lt;code&gt;resolve_executable&lt;/code&gt;), eliminating cross-platform &lt;code&gt;PATH&lt;/code&gt; resolution bugs on Windows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🎯 &lt;strong&gt;Per-File Dependency Targets:&lt;/strong&gt; Need to run two files with mutually exclusive package requirements in the same repository? &lt;code&gt;warden target set&lt;/code&gt; provisions isolated sub-environments (&lt;code&gt;.warden/venvs/target-&amp;lt;slug&amp;gt;/&lt;/code&gt;) and dedicated lockfiles, allowing scripts like &lt;code&gt;numpy==1.24.0&lt;/code&gt; and &lt;code&gt;numpy==1.26.4&lt;/code&gt; to live side-by-side.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🏷️ &lt;strong&gt;Platform Wheel Selection:&lt;/strong&gt; Artifact resolution ranks PyPI binaries against &lt;code&gt;packaging.tags.sys_tags()&lt;/code&gt;, preventing platform mismatch errors on C-extension wheels.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🛑 &lt;strong&gt;&lt;code&gt;fix&lt;/code&gt; vs. &lt;code&gt;install&lt;/code&gt; Separation:&lt;/strong&gt; &lt;code&gt;warden fix&lt;/code&gt; repairs installed-vs-locked environment drift without ever modifying project requirements in &lt;code&gt;warden.toml&lt;/code&gt; or &lt;code&gt;warden.lock&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;📦 &lt;strong&gt;Self-Contained Offline Bundling (&lt;code&gt;.warden.wheel&lt;/code&gt;):&lt;/strong&gt; &lt;code&gt;warden build&lt;/code&gt; packages project source code, manifests, locked dependencies, and build-system tools (&lt;code&gt;setuptools&lt;/code&gt;/&lt;code&gt;wheel&lt;/code&gt;) into zero-network archives.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🚦 &lt;strong&gt;CI Reproducibility Gate (&lt;code&gt;warden verify&lt;/code&gt;):&lt;/strong&gt; Runs deep validation across manifests, lockfile consistency, SHA-256 digests, and active environment states with stable non-zero exit codes on failure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ⚡ Quick Start
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install warden from PyPI&lt;/span&gt;
pip install pythonaibrain-warden==0.1.1

&lt;span class="c"&gt;# Initialize a new project layout&lt;/span&gt;
warden init myapp
&lt;span class="nb"&gt;cd&lt;/span&gt; myapp

&lt;span class="c"&gt;# Add dependencies &amp;amp; sync active environment&lt;/span&gt;
warden install requests
warden install &lt;span class="s2"&gt;"flask@3.0.0"&lt;/span&gt;

&lt;span class="c"&gt;# Execute project entry point or verify reproducibility&lt;/span&gt;
warden run
warden verify

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  📋 Command Summary
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Commands&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Dependencies&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;install&lt;/code&gt; • &lt;code&gt;update&lt;/code&gt; • &lt;code&gt;uninstall&lt;/code&gt; • &lt;code&gt;lock&lt;/code&gt;&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Environments&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;wenv&lt;/code&gt; • &lt;code&gt;use&lt;/code&gt; • &lt;code&gt;fix&lt;/code&gt;&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Execution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;run&lt;/code&gt; • &lt;code&gt;run-file&lt;/code&gt; • &lt;code&gt;exec&lt;/code&gt; • &lt;code&gt;target&lt;/code&gt;&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Quality &amp;amp; Audit&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;verify&lt;/code&gt; • &lt;code&gt;show&lt;/code&gt; • &lt;code&gt;clean&lt;/code&gt; • &lt;code&gt;cache&lt;/code&gt;&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Bundles &amp;amp; Deploy&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;build&lt;/code&gt; • &lt;code&gt;install-wheel&lt;/code&gt; • &lt;code&gt;backup&lt;/code&gt; • &lt;code&gt;restore&lt;/code&gt; • &lt;code&gt;publish&lt;/code&gt;&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  🌐 Links &amp;amp; Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;PyPI Release:&lt;/strong&gt; &lt;a href="https://pypi.org/project/pythonaibrain-warden/0.1.1/" rel="noopener noreferrer"&gt;pythonaibrain-warden 0.1.1&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/" rel="noopener noreferrer"&gt;DivyanshuSinha136/Pythonaibrain-Warden&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>cli</category>
      <category>python</category>
      <category>venv</category>
      <category>pypi</category>
    </item>
    <item>
      <title>I Built My Own Python Package Manager. Three Bugs Taught Us More Than the Design Did.</title>
      <dc:creator>Divyanshu Sinha</dc:creator>
      <pubDate>Mon, 24 Aug 2026 07:36:04 +0000</pubDate>
      <link>https://dev.to/divyanshusinha136/i-built-my-own-python-package-manager-three-bugs-taught-us-more-than-the-design-did-4khk</link>
      <guid>https://dev.to/divyanshusinha136/i-built-my-own-python-package-manager-three-bugs-taught-us-more-than-the-design-did-4khk</guid>
      <description>&lt;p&gt;&lt;em&gt;A field report from building Pythonaibrain-Warden — an npm-inspired dependency and environment manager for Python with its own lock file, its own virtual environments, and a few opinions pip doesn't share.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The itch
&lt;/h2&gt;

&lt;p&gt;Python's dependency story is not broken, exactly. It's &lt;em&gt;plural&lt;/em&gt;. &lt;code&gt;pip&lt;/code&gt; installs things. &lt;code&gt;venv&lt;/code&gt; isolates things. &lt;code&gt;pip-tools&lt;/code&gt; locks things. &lt;code&gt;poetry&lt;/code&gt; and &lt;code&gt;pdm&lt;/code&gt; try to do all three with opinions about how. If your project needs two different versions of the same library for two different scripts — a legacy report generator still pinned to &lt;code&gt;numpy==1.24&lt;/code&gt; sitting next to a new one that needs &lt;code&gt;numpy&amp;gt;=1.26&lt;/code&gt; — none of them have a clean answer. You get a second virtualenv, a README note nobody reads, and a slow drift toward "just don't touch that script."&lt;/p&gt;

&lt;p&gt;I wanted something narrower and more opinionated: own the whole vertical slice — manifest, lock file, environments, and the install step — well enough that I could make specific, sharp decisions instead of general, safe ones. That became &lt;strong&gt;Warden&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This isn't a pitch. It's a walkthrough of what I actually built, and — more usefully — three bugs I caught by actually running the thing, not by reading the code and nodding.&lt;/p&gt;




&lt;h2&gt;
  
  
  The one rule that kept the codebase sane
&lt;/h2&gt;

&lt;p&gt;Before any of the interesting stuff, one boring decision paid for itself repeatedly: &lt;strong&gt;the CLI layer is not allowed to think.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cli.py (argparse only)
   │
   ▼
core.py  ──────────────────────►  everything else
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;warden&lt;/code&gt; subcommand is an &lt;code&gt;argparse&lt;/code&gt; definition plus a two-line function that parses arguments and calls a &lt;code&gt;core.*&lt;/code&gt; function. That's it. &lt;code&gt;core.install()&lt;/code&gt; doesn't know it was invoked from a terminal — it takes a &lt;code&gt;Manifest&lt;/code&gt;, a list of package names, a boolean, and returns an exit code. You can &lt;code&gt;import warden.core as core&lt;/code&gt; and call it directly, no subprocess, no argv.&lt;/p&gt;

&lt;p&gt;I didn't design this because it's a best practice I'd read about. I designed it because by the time Warden had grown past a dozen commands, I needed a reason to trust that "what does &lt;code&gt;install&lt;/code&gt; actually do" had exactly one answer, not one answer per code path that happened to call it. It also meant every new feature — and there were a lot — slotted into the same shape: write the logic once in &lt;code&gt;core.py&lt;/code&gt;, wire up a thin CLI wrapper, move on.&lt;/p&gt;




&lt;h2&gt;
  
  
  I didn't use &lt;code&gt;venv.EnvBuilder&lt;/code&gt;. On purpose.
&lt;/h2&gt;

&lt;p&gt;Every Python environment tool eventually calls &lt;code&gt;venv.EnvBuilder&lt;/code&gt; or shells out to &lt;code&gt;python -m venv&lt;/code&gt;. Warden doesn't. It builds the environment by hand:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the real base interpreter (&lt;code&gt;sys._base_executable&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Create the directory layout itself.&lt;/li&gt;
&lt;li&gt;Symlink (or copy, on Windows) the interpreter in.&lt;/li&gt;
&lt;li&gt;Hand-write &lt;code&gt;pyvenv.cfg&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Bootstrap &lt;code&gt;pip&lt;/code&gt; via &lt;code&gt;ensurepip&lt;/code&gt; directly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This wasn't rebellion for its own sake — it's the same algorithm &lt;code&gt;venv&lt;/code&gt; uses internally, just owned end to end instead of delegated to a stdlib module whose error messages and edge-case behavior I didn't control. It paid off almost immediately, because it's what led us straight into the first real bug.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bug #1: "python" doesn't mean what you think it means on Windows
&lt;/h3&gt;

&lt;p&gt;Early on, a Windows tester ran the most boring possible sequence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;warden init myapp
&lt;span class="nb"&gt;cd&lt;/span&gt; myapp
warden wenv default
warden install
warden run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And got:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;D:\Program Files\python.exe: Error &lt;span class="k"&gt;while&lt;/span&gt; finding module specification &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="s1"&gt;'myapp.main'&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;ModuleNotFoundError: No module named &lt;span class="s1"&gt;'myapp'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The venv existed. The package was installed &lt;em&gt;into&lt;/em&gt; it. &lt;code&gt;warden run&lt;/code&gt; even printed &lt;code&gt;Running in env 'default'&lt;/code&gt; right before failing. So why was it launching &lt;code&gt;D:\Program Files\python.exe&lt;/code&gt; — the &lt;em&gt;system&lt;/em&gt; interpreter — instead of the one sitting three directories below it?&lt;/p&gt;

&lt;p&gt;The answer is one of those platform facts that only bites you once you actually test on the platform: on Windows, &lt;code&gt;subprocess.Popen(['python', ...], env=custom_env)&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; use &lt;code&gt;custom_env['PATH']&lt;/code&gt; to resolve the bare name &lt;code&gt;python&lt;/code&gt;. &lt;code&gt;CreateProcess&lt;/code&gt; resolves the executable using the &lt;em&gt;calling&lt;/em&gt; process's own &lt;code&gt;PATH&lt;/code&gt;, before the child's environment ever comes into play. Prepending the venv's &lt;code&gt;Scripts\&lt;/code&gt; directory to a dict you're about to hand to &lt;code&gt;subprocess&lt;/code&gt; does exactly nothing on Windows. (On POSIX it works fine, which is precisely why this kind of bug survives testing on a Mac and ships broken to everyone else.)&lt;/p&gt;

&lt;p&gt;The fix wasn't a workaround bolted onto the Windows case — it was to stop relying on &lt;code&gt;PATH&lt;/code&gt; search at all:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;resolve_executable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env_path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"python"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"python3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"py"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;python_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="c1"&gt;# fall back to PATH search only for things the env doesn't provide&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every command Warden spawns now resolves to an absolute path inside the target environment before it's ever launched. Correct on Windows because there's no PATH ambiguity left to exploit; correct on POSIX because it always was, and now it's explicit instead of accidental.&lt;/p&gt;




&lt;h2&gt;
  
  
  Different files, different dependency versions, same project
&lt;/h2&gt;

&lt;p&gt;Here's the feature I're most pleased with, and it's the direct answer to the "legacy script vs. new script" problem from the opening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="k"&gt;[targets]&lt;/span&gt;
&lt;span class="s2"&gt;"scripts/legacy_report.py"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"==1.24.0"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="s2"&gt;"scripts/new_report.py"&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"==1.26.4"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myproject/
 ├── scripts/legacy_report.py   → numpy==1.24.0
 └── scripts/new_report.py      → numpy==1.26.4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important design choice here was resisting the urge to invent something clever. A "target" isn't a special sandboxed sub-concept — it's a completely ordinary Warden environment with an auto-generated name (&lt;code&gt;target-scripts-legacy-report-py&lt;/code&gt;), its own real lock file, and it shows up in &lt;code&gt;warden wenv list&lt;/code&gt; next to everything else. &lt;code&gt;warden run-file scripts/legacy_report.py&lt;/code&gt; just looks up whether that file has an override, and if it does, routes to that environment — auto-provisioning it on first use if it isn't set up yet.&lt;/p&gt;

&lt;p&gt;I tested this with two files pinned to two different &lt;code&gt;click&lt;/code&gt; versions in the same project, with the project's own default environment sitting on a &lt;em&gt;third&lt;/em&gt; version, just to make sure nothing was quietly sharing state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ warden run-file scripts/file1.py
! WARN Target env &lt;span class="s1"&gt;'target-scripts-file1-py'&lt;/span&gt; isn&lt;span class="s1"&gt;'t set up yet -- provisioning it now.
==&amp;gt; Resolving target '&lt;/span&gt;scripts/file1.py&lt;span class="s1"&gt;' -&amp;gt; env '&lt;/span&gt;target-scripts-file1-py&lt;span class="s1"&gt;'
✓ Locked 1 package(s)
✓ click==8.0.0 installed
file1.py running with click 8.0.0

$ warden run-file scripts/file2.py
! WARN Target env '&lt;/span&gt;target-scripts-file2-py&lt;span class="s1"&gt;' isn'&lt;/span&gt;t &lt;span class="nb"&gt;set&lt;/span&gt; up yet -- provisioning it now.
✓ &lt;span class="nv"&gt;click&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;8.4.2 installed
file2.py running with click 8.4.2

$ .warden/venvs/default/bin/python -c &lt;span class="s2"&gt;"import click; print(click.__version__)"&lt;/span&gt;
8.1.7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three environments, three versions, one project, and nothing silently sharing site-packages with anything else.&lt;/p&gt;




&lt;h2&gt;
  
  
  The bug that only showed up completely offline
&lt;/h2&gt;

&lt;p&gt;This one's our favorite, because it's a genuine two-layer bug and neither layer looks like a bug from the surface.&lt;/p&gt;

&lt;p&gt;Warden can package a project into a &lt;code&gt;.warden.wheel&lt;/code&gt; — source, lock file, and (by default) every dependency's actual &lt;code&gt;.whl&lt;/code&gt; file bundled in, so the whole thing installs without touching the network. A tester built one, disconnected from the internet, and ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python -m warden install-wheel myapp-0.1.0.warden.wheel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything vendored installed fine. Then, right at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;==&amp;gt; Installing the project itself in editable mode
! WARN Could not editable-install the project itself: ...
error: subprocess-exited-with-error
pip subprocess to install build dependencies did not run successfully.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Followed, inevitably, by &lt;code&gt;warden run&lt;/code&gt; failing with &lt;code&gt;ModuleNotFoundError: No module named 'myapp'&lt;/code&gt; — because the package genuinely never got installed.&lt;/p&gt;

&lt;p&gt;Here's the thing: I'd vendored &lt;em&gt;every runtime dependency&lt;/em&gt;. So why did anything need the network at all?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install -e .&lt;/code&gt; doesn't just install your package. By default it uses PEP 517 build isolation — it spins up an ephemeral, throwaway environment and fetches &lt;code&gt;setuptools&lt;/code&gt; and &lt;code&gt;wheel&lt;/code&gt; into it from PyPI, every single time, to actually perform the build. Those two packages aren't runtime dependencies. They're not in &lt;code&gt;warden.lock&lt;/code&gt;. I had never even thought to vendor them.&lt;/p&gt;

&lt;p&gt;And the reason a freshly created environment couldn't just use its own copy: I checked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;$ python3 -m venv fresh
$ fresh/bin/python -c &lt;span class="s2"&gt;"import setuptools"&lt;/span&gt;
ModuleNotFoundError: No module named &lt;span class="s1"&gt;'setuptools'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modern CPython's &lt;code&gt;ensurepip&lt;/code&gt; stopped bundling &lt;code&gt;setuptools&lt;/code&gt; and &lt;code&gt;wheel&lt;/code&gt;. It used to. It doesn't anymore. A brand-new venv today has exactly one thing in it: &lt;code&gt;pip&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fix needed both ends:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At build time&lt;/strong&gt;, vendor the project's build-system requirements too — read &lt;code&gt;[build-system].requires&lt;/code&gt; out of &lt;code&gt;pyproject.toml&lt;/code&gt;, resolve and cache &lt;code&gt;setuptools&lt;/code&gt;/&lt;code&gt;wheel&lt;/code&gt; the same way I do runtime dependencies, and drop them in &lt;code&gt;vendor/&lt;/code&gt; alongside everything else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;At install time&lt;/strong&gt;, before attempting the editable install, install those from the local vendor directory via &lt;code&gt;pip --no-index --find-links&lt;/code&gt;, then run the editable install with &lt;code&gt;--no-build-isolation&lt;/code&gt; — which tells pip "don't try to fetch anything, use what's already here."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ensure_build_tools&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;python_exe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vendor_dir&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;run_cmd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;python_exe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"import setuptools, wheel"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;python_exe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"pip"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"install"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"--disable-pip-version-check"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;vendor_dir&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"--no-index"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"--find-links"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vendor_dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;run_cmd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"setuptools"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"wheel"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I didn't trust this until I'd actually broken the network to test it — not "ran it with &lt;code&gt;--no-vendor&lt;/code&gt; unset and assumed," but literally monkeypatched &lt;code&gt;socket.getaddrinfo&lt;/code&gt; to raise for &lt;code&gt;pypi.org&lt;/code&gt; and &lt;code&gt;files.pythonhosted.org&lt;/code&gt; mid-run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;blocked_getaddrinfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;host&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pypi.org'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'files.pythonhosted.org'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gaierror&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'simulated offline'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;_orig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getaddrinfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blocked_getaddrinfo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then ran &lt;code&gt;install-wheel&lt;/code&gt; and &lt;code&gt;run&lt;/code&gt; against it. Both succeeded. That's the difference between "should work offline" and "works offline" — one of them I'd have shipped confidently and been wrong.&lt;/p&gt;




&lt;h2&gt;
  
  
  A quieter bug: brackets are not decoration
&lt;/h2&gt;

&lt;p&gt;Adding progress bars and tables (via &lt;code&gt;rich&lt;/code&gt;) was mostly straightforward, except for one thing I almost missed entirely. &lt;code&gt;rich&lt;/code&gt; parses plain strings passed to &lt;code&gt;Table&lt;/code&gt; cells and &lt;code&gt;console.status()&lt;/code&gt; as &lt;em&gt;markup&lt;/em&gt; by default — &lt;code&gt;[green]text[/]&lt;/code&gt; becomes styled green text. Convenient, until you print something that legitimately contains square brackets for an unrelated reason:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_row&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pkg[extra1,extra2]&amp;gt;=1.0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;renders as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="na"&gt;pkg&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The extras syntax — completely standard PEP 508 dependency specification — got silently eaten. In a package manager, silently corrupting a dependency specifier in &lt;em&gt;displayed output&lt;/em&gt; is merely embarrassing. It's the kind of bug that's obvious the second you see it and invisible until you specifically go looking, because the common case (&lt;code&gt;requests==2.31.0&lt;/code&gt;) never has a bracket in it.&lt;/p&gt;

&lt;p&gt;The fix was mechanical once I knew to look for it: never hand &lt;code&gt;rich&lt;/code&gt; a raw dynamic string. Always wrap it in &lt;code&gt;Text()&lt;/code&gt;, which bypasses markup parsing entirely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# wrong: table.add_row(spec_string)&lt;/span&gt;
&lt;span class="c1"&gt;# right:&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;rich&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Text&lt;/span&gt;
&lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_row&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;spec_string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I went back through every table, every status spinner, every place a package name or version spec touched &lt;code&gt;rich&lt;/code&gt; output, and fixed each one — then wrote a one-line regression test I now run whenever this code path changes: feed it &lt;code&gt;pkg[extra1,extra2]&amp;gt;=1.0&lt;/code&gt; and confirm the brackets survive.&lt;/p&gt;




&lt;h2&gt;
  
  
  What I'd tell you if you're building something similar
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Test the platform you're least likely to be sitting in front of.&lt;/strong&gt; The Windows &lt;code&gt;PATH&lt;/code&gt; bug and the "ensurepip doesn't bundle setuptools anymore" bug both share a shape: they're invisible if you only ever develop and test on the same machine, in the same network conditions, on the same OS. Neither was hard to fix once found. Both were completely invisible in code review.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A library's convenience default is not automatically your correctness default.&lt;/strong&gt; &lt;code&gt;rich&lt;/code&gt;'s markup-everywhere behavior is genuinely nice for 95% of use cases. It's actively wrong for a tool whose entire job is displaying user-supplied strings verbatim. &lt;code&gt;pip&lt;/code&gt;'s build isolation is genuinely the right default for "run pip install on a random package." It's the wrong default when you've already made an explicit, hash-verified promise about what's installed and where it came from.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reuse the abstraction you already trust instead of inventing a parallel one.&lt;/strong&gt; Per-file dependency targets could have been a bespoke sandboxing feature. Instead it's "an environment, but the name is computed instead of typed" — which meant it inherited every bit of correctness work already put into environment creation, activation, and locking, instead of needing its own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;None of this is finished, and that's fine to say out loud.&lt;/strong&gt; Warden's resolver is deliberately greedy, not backtracking — a real diamond-dependency conflict gets reported clearly instead of resolved cleverly, which is a trade I're happy with but which I say plainly in the docs rather than let people discover. Per-target locks re-resolve independently rather than sharing work across targets. Being specific about what a tool &lt;em&gt;doesn't&lt;/em&gt; do is, I'd argue, more useful to the next person than a features list that implies it does everything.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Warden is npm-inspired, not npm-derived: its own manifest (&lt;code&gt;warden.toml&lt;/code&gt;), its own hash-pinned lock file (&lt;code&gt;warden.lock&lt;/code&gt;), and environments it builds and owns itself rather than delegating to &lt;code&gt;venv&lt;/code&gt;. Full command reference in &lt;code&gt;CLI-REFERENCE.md&lt;/code&gt;, internal design writeup in &lt;code&gt;ARCHITECTURE.md&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;PyPI&lt;/strong&gt;: &lt;a href="https://pypi.org/project/pythonaibrain-warden" rel="noopener noreferrer"&gt;https://pypi.org/project/pythonaibrain-warden&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/DivyanshuSinha136/pythonaibrain-warden" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/pythonaibrain-warden&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Architecture&lt;/strong&gt;: &lt;a href="https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/blob/main/ARCHITECTURE.md" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/blob/main/ARCHITECTURE.md&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;CLI Reference&lt;/strong&gt;: &lt;a href="https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/blob/main/CLI-REFERENCE.md" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/blob/main/CLI-REFERENCE.md&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Issues&lt;/strong&gt;: &lt;a href="https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/issues" rel="noopener noreferrer"&gt;https://github.com/DivyanshuSinha136/Pythonaibrain-Warden/issues&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dependencymanager</category>
      <category>packagemanager</category>
      <category>pypi</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
