Live data from Hacker News

Tiny-C Compiler (2001)

iro.umontreal.ca

31–40 of 82 posts

Re: Tiny-C Compiler (2001)

#31
post #27

Recently I'm working on toy C compiler and x86 Assembler in TypeScript[1] and I can confirm that the amount of work that have to be done to compile and print simple Hello World is astronomically huge (as the satisfaction) [1] https://github.com/Mati365/ts-c-compiler

This isn't a C compiler though. It's a compiler for a language called Tiny-C.

Re: Tiny-C Compiler (2001)

#32
post #27

Recently I'm working on toy C compiler and x86 Assembler in TypeScript[1] and I can confirm that the amount of work that have to be done to compile and print simple Hello World is astronomically huge (as the satisfaction) [1] https://github.com/Mati365/ts-c-compiler

This isn't a C compiler though. It's a compiler for a language called Tiny-C.

[deleted]

Re: Tiny-C Compiler (2001)

#33
post #26

first assignment would be to add the multiply and divide operators... I admit I have trouble understanding how the VM run() function works... anybody can give some insight?

The function runs through the program by incrementing the program counter (*pc++) and dispatching what instruction it sees. It's a stack-based VM so individual instructions are pushed onto and popped from the stack depending on the operation. Is there anything specific you don't grok? Happy to help.

Re: Tiny-C Compiler (2001)

#34

Earlier quoted context omitted.

I'm quite confused, not the same project at all? To me tiny c compiler always meant the bellard page. Super useful stuff for micro hacky projects.

I understand the confusion: it is more about "syntax associativity" (tiny C) compiler --> "This is a compiler for the Tiny-C language" vs Tiny (C compiler) --> "TinyCC [...] is a small but hyper fast C compiler" That's it! ;-)

Now obviously the next step is to make a tiny tiny c compiler compiler.

Re: Tiny-C Compiler (2001)

#35
That's a cute project, thanks for sharing.

I hacked in support for ">", ">=", and "<=" to match the "<" support, but I just noticed that ints are truncated, so the maximum value stored in a variable is 127.

Re: Tiny-C Compiler (2001)

#36

This is an interpreter for a super restricted subset of C and it looks well written from a pedagogical standpoint (keeps thing pretty simple, fairly easy to read). But it's slightly awk to strip-down a language (what features do you keep, what do you lose?). I think it's more fun to build an interpreter for an actual tiny language. In my next book I have interpreters for Brainfuck [0], an obfuscated kind of joke of a…

Another language that's more modern and currently useful but which is very tiny to write an interpreter for is Lua [0][1]. Currently the official Lua interpreter has around 30k LOC which I find pretty amusing for a language used so widely in games and for scripting purposes [2]. Of course it's still at least an order of magnitude larger than a small Tiny BASIC interpreter but the fact it's a current language used in so many places makes it even more interesting to make your for-fun implementation.

Also related to small language implementations I find notable PicoC [3] which is a C interpreter written in around 3k LOC of C. Past discussion about it here 13 years ago [4].

[0]: https://www.lua.org/about.html

[1]: https://www.lua.org/spe.html

[2]: https://en.wikipedia.org/wiki/Lua_(programming_language)

[3]: https://gitlab.com/zsaleeba/picoc

[4]: https://news.ycombinator.com/item?id=1658890

Re: Tiny-C Compiler (2001)

#37

Not to be confused with https://bellard.org/tcc/ , which is a tiny compiler for the C language.

It is sad that tcc is unmaintained as it would be really useful in small embedded systems. I just tried it on Debian and compilation fails without #undefining CONFIG_TCC_MALLOC_HOOKS in lib/bcheck.c. After compilation it passes tests, but they warn that it could be unreliable.

Re: Tiny-C Compiler (2001)

#38

This is an interpreter for a super restricted subset of C and it looks well written from a pedagogical standpoint (keeps thing pretty simple, fairly easy to read). But it's slightly awk to strip-down a language (what features do you keep, what do you lose?). I think it's more fun to build an interpreter for an actual tiny language. In my next book I have interpreters for Brainfuck [0], an obfuscated kind of joke of a…

While I appreciate your point.

1. You use the example of a tiny basic of a 'real language' and I don't see how tiny basic is a 'real language', but tiny C is a stripped down language.

2. You can build on this to make a full c implementation. A minimal c implementation that can potentially bootstrap a full c environment is more useful than a brainfuck interpreter.

Re: Tiny-C Compiler (2001)

#39

Not to be confused with https://bellard.org/tcc/ , which is a tiny compiler for the C language.

It is sad that tcc is unmaintained as it would be really useful in small embedded systems. I just tried it on Debian and compilation fails without #undefining CONFIG_TCC_MALLOC_HOOKS in lib/bcheck.c. After compilation it passes tests, but they warn that it could be unreliable.

Try chibicc. It's x86_64 native and so much more readable as a codebase than TCC.

Re: Tiny-C Compiler (2001)

#40
Author here. Just for context tinyc.c was created in 2000 (I found the file in my archives and the last modification date is January 12, 2001). I was not aware at the time of Fabrice Bellard's work which after all won the IOCCC in 2001, so the confusion with TCC was not intentional. My tinyc.c was meant to teach the basics of compilers in a relatively accessible way, from parsing to AST to code generation to bytecode interpreter. And yes it is the subset of C that is tiny, not a tiny compiler for the full C language.
Post reply on HN