C89cc.sh – standalone C89/ELF64 compiler in pure portable shell - Comments

C89cc.sh – standalone C89/ELF64 compiler in pure portable shell

dzwdz

Summarizing the HN thread:


idk, this just echoes the Rust compiler in PHP for me. a few years ago i would've thought this is a really cool project. not because it's in any way practical, but because it's a show of skill in an obscure language and dedication. you gotta be crazy (in a good way) to work on something like this, and i admire that

if you automate "the boring, mechanical work"... well, that kinda feels like automating the point of it away, doesn't it.

i'd be happy to hear about the shell portability tricks, the bnf-to-shell compiler sounds interesting, but instead i've got 8000 lines of shell that the author couldn't even be bothered to write

"i did $COMPLICATED_THING in $SILLY_ENVIRONMENT" just doesn't really have its punch nowadays, i guess

alexandria

if you automate "the boring, mechanical work"... well, that kinda feels like automating the point of it away, doesn't it.

i'd be happy to hear about the shell portability tricks, the bnf-to-shell compiler sounds interesting, but instead i've got 8000 lines of shell that the author couldn't even be bothered to write

Oh my god, thank you for highlighting this. I did ask myself if it was AI generated at the start of reading, saw some things that set off some mental flags, and came here to check.

I wholeheartedly agree. The point in something like this is that you're doing it partly to see if it can be done, and partly as performance art. It's all about the very human struggle of figuring out ways of fitting things into places where they shouldn't be, and then sitting down to actually do that. My interest drops into the negative now knowing it's vibecoded.

gaigalas

Single standalone file, no external tools used, PATH='' (empty), portable (bash, dash, ksh, zsh), produces x86 ELF executables, has mini-libc builtin.

Usage:

printf 'int main(){puts("hello");return 0;}' | sh c89cc.sh > hello

chmod +x hello

./hello

angry_octet

I can't think of a reason to use c89cc.sh, but I salute this effort nonetheless.

jonahx

gorgeous!

t-3

Why not POSIX or some common external tools where it makes sense? Most of those big switch statements could be easily replaced with some standard programs that already exist everywhere.

dmitrygr

Many parts of this are clearly autogenerated, but that in no way diminishes the sickening impressiveness of it!

phire

It does make it a little hard to understand how the parser/ast_builder works.

But the rest seems easy enough to understand.

userbinator

It would be far more interesting to look at what this was "compiled" from; it looks like the output of a state-machine generator.

gaigalas

Yes! The main parser and emitter come from a BNF parser generator, also written in portable shell (to be released though).

kelsey98765431

Would be a lot better if it came with tests. Please do this justice and dont let it rot as a gist, make a real repo and add some docs and at least smoke tests or some kind. Thanks

fuhsnn

Don't understand why you were downvoted. An untested C compiler is simply worthless.

gaigalas

This gist is a concatenation of several shell script modules which form a comprehensive parser library for the portable shell.

The main parser and emitter are BFN-generated (that's why they look so mechanical). The BNF parser generator is also written in portable shell.

All modules have comprehensive tests, but it is still lacking documentation and not ready for prime time!

jey

It targets x86-64/ELF? I thought it would target `sh` to be portable?

gaigalas
saagarjha

SuperH is not that portable.

_ache_

I'm tempted to execute it, but it may as well be shellcode I couldn't tell.

JackSmith_YC

Pure shell. Love the minimalism here... especially when every tiny CLI tool these days seems to require a 50MB node_modules folder just to run. There’s a certain Zen in doing things with zero dependencies. Reminds me of why I got into Unix in the first place.

tho2u3i4o23497

Node stuff atleast "works" - you've not seen real dependency hell until you've seen the horrible world of the Python ML ecosystem.

self_awareness

"Claude please generate me a C compiler in bash"

I mean, today it's possible to generate it in Tcl, Elisp, Windows BAT, Powershell.

The effort is just 1 prompt.

gaigalas

Here's a prototype parser from 10 months ago, when this was not possible yet:

https://gist.github.com/alganet/23df53c567b8a0bf959ecbc7b689...

Here is me 10 years ago experimenting on parsing stuff with sed:

https://gist.github.com/alganet/542f46865420529c9bd2

---

Yes, c89cc.sh was definitely AI-assisted. However, I do carry extensive knowledge of the portable shell that was essential for the AI to complete it.

You'll find tricks inside c89cc.sh that don't exist anywhere, except in other code from me (like the ksh93 fix for local dynamic scoping or the alias/macro read -n1 polyfill).

The WHY is pretty obvious: I want to show that the portable shell is not a toy.

uecker

I am tempted to click the "report abuse" link ;-)

dmead

This is vibe coded right?

wengo314

if one could boostrap tcc with it, then it might be a viable tool.

dzwdz

Summarizing the HN thread:


idk, this just echoes the Rust compiler in PHP for me. a few years ago i would've thought this is a really cool project. not because it's in any way practical, but because it's a show of skill in an obscure language and dedication. you gotta be crazy (in a good way) to work on something like this, and i admire that

if you automate "the boring, mechanical work"... well, that kinda feels like automating the point of it away, doesn't it.

i'd be happy to hear about the shell portability tricks, the bnf-to-shell compiler sounds interesting, but instead i've got 8000 lines of shell that the author couldn't even be bothered to write

"i did $COMPLICATED_THING in $SILLY_ENVIRONMENT" just doesn't really have its punch nowadays, i guess

alexandria

if you automate "the boring, mechanical work"... well, that kinda feels like automating the point of it away, doesn't it.

i'd be happy to hear about the shell portability tricks, the bnf-to-shell compiler sounds interesting, but instead i've got 8000 lines of shell that the author couldn't even be bothered to write

Oh my god, thank you for highlighting this. I did ask myself if it was AI generated at the start of reading, saw some things that set off some mental flags, and came here to check.

I wholeheartedly agree. The point in something like this is that you're doing it partly to see if it can be done, and partly as performance art. It's all about the very human struggle of figuring out ways of fitting things into places where they shouldn't be, and then sitting down to actually do that. My interest drops into the negative now knowing it's vibecoded.

gaigalas

Single standalone file, no external tools used, PATH='' (empty), portable (bash, dash, ksh, zsh), produces x86 ELF executables, has mini-libc builtin.

Usage:

printf 'int main(){puts("hello");return 0;}' | sh c89cc.sh > hello

chmod +x hello

./hello

angry_octet

I can't think of a reason to use c89cc.sh, but I salute this effort nonetheless.

jonahx

gorgeous!

t-3

Why not POSIX or some common external tools where it makes sense? Most of those big switch statements could be easily replaced with some standard programs that already exist everywhere.

dmitrygr

Many parts of this are clearly autogenerated, but that in no way diminishes the sickening impressiveness of it!

phire

It does make it a little hard to understand how the parser/ast_builder works.

But the rest seems easy enough to understand.

userbinator

It would be far more interesting to look at what this was "compiled" from; it looks like the output of a state-machine generator.

gaigalas

Yes! The main parser and emitter come from a BNF parser generator, also written in portable shell (to be released though).

kelsey98765431

Would be a lot better if it came with tests. Please do this justice and dont let it rot as a gist, make a real repo and add some docs and at least smoke tests or some kind. Thanks

fuhsnn

Don't understand why you were downvoted. An untested C compiler is simply worthless.

gaigalas

This gist is a concatenation of several shell script modules which form a comprehensive parser library for the portable shell.

The main parser and emitter are BFN-generated (that's why they look so mechanical). The BNF parser generator is also written in portable shell.

All modules have comprehensive tests, but it is still lacking documentation and not ready for prime time!

jey

It targets x86-64/ELF? I thought it would target `sh` to be portable?

gaigalas
saagarjha

SuperH is not that portable.

_ache_

I'm tempted to execute it, but it may as well be shellcode I couldn't tell.

JackSmith_YC

Pure shell. Love the minimalism here... especially when every tiny CLI tool these days seems to require a 50MB node_modules folder just to run. There’s a certain Zen in doing things with zero dependencies. Reminds me of why I got into Unix in the first place.

tho2u3i4o23497

Node stuff atleast "works" - you've not seen real dependency hell until you've seen the horrible world of the Python ML ecosystem.

self_awareness

"Claude please generate me a C compiler in bash"

I mean, today it's possible to generate it in Tcl, Elisp, Windows BAT, Powershell.

The effort is just 1 prompt.

gaigalas

Here's a prototype parser from 10 months ago, when this was not possible yet:

https://gist.github.com/alganet/23df53c567b8a0bf959ecbc7b689...

Here is me 10 years ago experimenting on parsing stuff with sed:

https://gist.github.com/alganet/542f46865420529c9bd2

---

Yes, c89cc.sh was definitely AI-assisted. However, I do carry extensive knowledge of the portable shell that was essential for the AI to complete it.

You'll find tricks inside c89cc.sh that don't exist anywhere, except in other code from me (like the ksh93 fix for local dynamic scoping or the alias/macro read -n1 polyfill).

The WHY is pretty obvious: I want to show that the portable shell is not a toy.

uecker

I am tempted to click the "report abuse" link ;-)

dmead

This is vibe coded right?

wengo314

if one could boostrap tcc with it, then it might be a viable tool.