DEV Community

Cover image for KingCobra 1.0: a native systems language that feels like a scripting language
Divyanshu Sinha
Divyanshu Sinha

Posted on

KingCobra 1.0: a native systems language that feels like a scripting language

I'm Divyanshu, I'm 17, and I've spent a long time building KingCobra, a programming language that tries to feel like a scripting language while behaving like a systems language. v1.0.0 is out now, and on Windows it installs with a single choco install kingcobra.

let nums = [5, 3, 8, 1, 9];
let [first, ...rest] = nums;
print(nums.map(fn(x) -> int { return x * x; }).filter(fn(x) -> bool { return x > 10; }));
Enter fullscreen mode Exit fullscreen mode

What is it?

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

The whole project (language, runtime, JIT and standard library) is plain C11 with no third-party build dependencies. It builds with make.

Install

Platform Command
Windows (Chocolatey, PowerShell as Administrator) choco install kingcobra
Windows (Scoop) scoop install https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/kingcobra.json
Windows (PowerShell, no admin) `irm https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.ps1 \
Linux / macOS {% raw %}`curl -fsSL https://github.com/DivyanshuSinha136/kingcobra/releases/latest/download/install.sh \
From source {% raw %}git clone, then make && ./kingcobra repl

Open a new terminal and run kingcobra version. 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 SHA256SUMS file and a build-provenance attestation.

>>> fn fact(n: int) -> int {
...     if (n < 2) { return 1; }
...     return n * fact(n - 1);
... }
>>> fact(10)
3628800
>>> _ + 1
3628801
Enter fullscreen mode Exit fullscreen mode

_ is the last result, and errors never end the session.

Memory: no garbage collector, but not unchecked

KingCobra has no GC. free works on every heap value, and the mistakes that are usually silent in C become errors with a source location:

error: double free (allocation #1 was already freed)
    4 |     free(buf);
      |     ^
Enter fullscreen mode Exit fullscreen mode

defer runs even while an error unwinds, pointers and buffers are bounds- and lifetime-checked, and kingcobra --leaks prog.kc reports what you forgot to free:

kingcobra: memory report
  leaked blocks (allocated, never freed):
          32 bytes  alloc()  at leak.kc:2:13
  total: 1 block(s), 32 byte(s)
Enter fullscreen mode Exit fullscreen mode

Execution model

KingCobra is a tree-walking interpreter with a tiered closure JIT. 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, if/while, self-recursion) is compiled once to x86-64 machine code. On my test machine, fib(35) takes 0.10 s with the JIT and 1.43 s with --no-jit, about 14× faster.

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. make pgo adds a profile-guided build that is typically 10–25% faster than plain -O2. docs/PERFORMANCE.md has the measurements and the regressions I hit along the way.

Batteries included: 21 native modules

No external dependencies. Load one with import std.<name>;.

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

make test runs every example end to end, so the modules are exercised, not just listed.

Tooling, written in KingCobra where it counts

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

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.

Honest limits

I would rather tell you than have you find out:

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

docs/STABILITY.md says what you can rely on, and docs/ROADMAP.md says what's next.

Try it, and tell me what breaks

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.

MIT licensed. If it's interesting to you, a star helps other people find it.

Top comments (0)