Lurker News
I love C, but often using it for parsing, tree walking or complex error handling leads to spaghetti code.

Of course there is Swift, Ocaml or Rust for situations like that, but then you are either forced to convert the entire codebase, or add a runtime library or go through the pain of bridging C to the new language. Sometimes there are no compilers for the target architecture too when you are working on embedded stuff.
This is where Type-C comes in. It works as a preprocessor like TypeScript and calls the original C driver behind the scenes. Your code stays normal C for the most part, so you can predict how the code will be laid out. TypeC is a full superset of C11, so any C11 code compiles as is.
Want to use TypeC just for one file? Rename to .tc and use the features you want, the rest of the codebase stays the same. This works for kernel or baremetal code too as there is no runtime or extra libararies.
There are examples of TypeC code as well as real-world tests of converting existing code to TypeC on the GitHub page.
This is how the code looks like for an expression evaluator: type expr = | Num(int value) | Add(const expr l, const exprr) | Mul(const expr l, const exprr) | Div(const expr l, const exprr);

  static result(int, eval_error)
  eval(const expr *e)
  {
      int d;

      match (*e) {
      case Num(value):
          return (Ok(value));
      case Add(l, r):
          return (fit((long long)try eval(l) + try eval(r)));
      case Mul(l, r):
          return (fit((long long)try eval(l) * try eval(r)));
      case Div(l, r):
          d = try eval(r);
          if (d == 0)
              return (Err(DivByZero));
          return (fit((long long)try eval(l) / d));
      }
  }

  % typec -std=c11 -Wall -Wextra eval.tc
  $ ./a.out
  14
  division by zero
Would love to hear your thoughts and feedback!

Comments (22) 12 threads

Earlier comments
adsharma1 reply#
Why not use vlang?

I wanted to do the same thing to python. So wrote a parser for a variant of python.
It's spy-ast on pypi

stass#
Just checked spy-ast, it looks cool! I like how it's trying to approach the same problem from a different angle for a dynamically typed language.

I picked OCaml vs vlang mostly because I'm more familiar with it and it's so well suited for parsers/compilers with its ADTs and pattern matching -- ironically the same features I wanted to add to C. It has a great ecosystem of existing tools and packages as well and cross-platform support, which is good for portability.

stass#
Realized that the post is a bit light on numbers, so here're some.

I rewrote the parser of tomlc99 (2k lines) in Type-C as a test. I used result/try/defer for the error paths and left everything else unchanged, linking against the same C programs and tests.
Doing fault injection with one failed allocation per run over ~1500 runs: the original C went wrong in 34 (25 leaks, 9 use-after-free); the rewrite went wrong never, sanitizers clean.
With original toml-spec-tests files: the original leaked in 1002 of them and crashed in 9; the rewrite did neither.
This is an example of how the code looks like for an expression evaluator: type expr = | Num(int value) | Add(const expr l, const exprr) | Mul(const expr l, const exprr) | Div(const expr l, const exprr);

  static result(int, eval_error)
  eval(const expr *e)
  {
      int d;

      match (*e) {
      case Num(value):
          return (Ok(value));
      case Add(l, r):
          return (fit((long long)try eval(l) + try eval(r)));
 …
Happy to answer any questions!
Panzerschrek1 reply#
Yet another project adding bells and whistles into C without solving its core issues. Now vibe-coded.
stass1 reply#
What are the core issues? Some of the C pains are hard to solve without breaking the language.
Panzerschrek1 reply#
> What are the core issues?

Memory safety, race conditions, unhandled integer overflows. Look at all newly-discovered vulnerabilities, they almost all caused by these issues.
> Some of the C pains are hard to solve without breaking the language.
That's why it's necessary to do such breaking change, to stop using C and to switch to better languages having no issues mentioned above.

stass3 replies#
I would totally use a different language whenever it fits. The compiler for Type-C itself is written in OCaml because C is terrible for parsers, and I have a whole microkernel OS project where most of it, including all drivers, is written in Erlang.

This is not always possible, however, especially for legacy/existing projects, and that's the hole Type-C plugs. Imagine a legacy embedded FreeRTOS or RTEMS or Zephyr codebase with existing tooling where you want to introduce a little be of patter matching convenience and type safety? With Type-C you would just rename one of the files to .tc, use whatever features you need, add a new .tc rule to the Makefile and that's it. Everything else will work as it did, with the same compiler, tooling and so on. No new compiler, runtime or having to convince coworkers to switch to a new language.
For some still widely used architectures, like SH4, there isn't even Clang compiler yet, let alone Rust or Ocaml. So you need C.
This is the use case Type-C targets.

Continue this thread — 3 replies

Load more comments