Rendered at 05:31:36 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
13 minutes ago [-]
adsharma 5 hours ago [-]
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 1 hours ago [-]
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.
onlyrealcuzzo 6 hours ago [-]
> Of course there is Swift, Ocaml or Rust
Presumably C++ is the first thing you should mention.
stass 1 hours ago [-]
My list of other languages was not exhaustive, just like C++ matches. :-D
On a serious note, it's a fair point: it does have std::variant, std::visit and std::expected in C++23, but the do force you to embrace C++ verboseness: e.g. you have to write a per-case visitor for std::visit compile-time check. It also still lacks exhaustive matching, distinct types, and has a different ABI from C.
I felt like Swift/OCaml were closer in spirit to what I was trying to achieve in Type-C.
wffurr 6 hours ago [-]
Which is famous for the ergonomics of variant and pattern matching...
aman179102 37 minutes ago [-]
[flagged]
Panzerschrek 5 minutes ago [-]
Yet another project adding bells and whistles into C without solving its core issues. Now vibe-coded.
xiaoliangliang 2 hours ago [-]
[flagged]
stass 1 days ago [-]
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 expr r)
| Mul(const expr l, const expr r)
| Div(const expr l, const expr r);
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)));
…
I wanted to do the same thing to python. So wrote a parser for a variant of python.
It's spy-ast on pypi
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.
Presumably C++ is the first thing you should mention.
On a serious note, it's a fair point: it does have std::variant, std::visit and std::expected in C++23, but the do force you to embrace C++ verboseness: e.g. you have to write a per-case visitor for std::visit compile-time check. It also still lacks exhaustive matching, distinct types, and has a different ABI from C.
I felt like Swift/OCaml were closer in spirit to what I was trying to achieve in Type-C.
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 expr r) | Mul(const expr l, const expr r) | Div(const expr l, const expr r);
Happy to answer any questions!