Live data from Hacker News

Tiny-C Compiler (2001)

iro.umontreal.ca

1–10 of 82 posts

Re: Tiny-C Compiler (2001)

#4
post #2

It’s worth noting that this is a compiler for the Tiny-C language, and not as one might think a tiny compiler for the C language.

It's probably better to call it an interpreter, since it will also run the program and print the values of all non-zero variables afterward.

Calling it a compiler is (to me) really stretching things, I can't see any code to emit any other form of the code, it's all aimed at evaluating (executing) it.

Edit: oops, I didn't read the code closely enough, it does emit code but only internally, that code is what gets executed. Thanks for the corrections!

Re: Tiny-C Compiler (2001)

#5
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 language, and Tiny BASIC[1] a real tiny language that was used on early personal computers. These are pretty common first projects for folks interested in doing an interpreter.

Here's why real languages are better than stripped down languages: Anyone with programming knowledge can implement a Brainfuck interpreter in a few hours and run any Brainfuck program. Anyone with a tiny bit of CS knowledge can implement a Tiny BASIC interpreter in just a day and then you can run any real Tiny BASIC program from the late 70s. It's cool to run real programs people actually used. With this stripped down C, there are no pre-made real programs...

0:https://en.wikipedia.org/wiki/Brainfuck 1:https://en.wikipedia.org/wiki/Tiny_BASIC

Re: Tiny-C Compiler (2001)

#6
post #2

It’s worth noting that this is a compiler for the Tiny-C language, and not as one might think a tiny compiler for the C language.

Yes, a better title would be:

Compiler for the Tiny-C Language (2001)

In fact, that is exactly how the source code describes itself in the comments.

Re: Tiny-C Compiler (2001)

#7
I did my CS degree at umontreal and this was an assignment in a second year class. This was a pretty interesting introduction to compilers, and even if this is a toy subset of C, this was challenging, at least for me. We would get 0 if there were any memory leak, so we were pretty paranoid about it.

The second assignment was writing a Scheme interpreter.

Re: Tiny-C Compiler (2001)

#8
post #4
post #2

It’s worth noting that this is a compiler for the Tiny-C language, and not as one might think a tiny compiler for the C language.

It's probably better to call it an interpreter, since it will also run the program and print the values of all non-zero variables afterward. Calling it a compiler is (to me) really stretching things, I can't see any code to emit any other form of the code, it's all aimed at evaluating (executing) it. Edit: oops, I didn't read the code closely enough, it does emit code but only internally, that code is what gets execu…

It compiles to a sort of byte code that is executed by a stack based virtual machine.

Re: Tiny-C Compiler (2001)

#9
It's unfortunately not self-compiling, but has a structure which is very reminiscent of C4 --- another tiny C-subset compiler + stack-based VM which is self-compiling:

https://news.ycombinator.com/item?id=8558822

The 26 predefined integer variables make this look like a variant of minimal BASIC, except with structured control flow instead of only GOTO.

Re: Tiny-C Compiler (2001)

#10
post #4
post #2

It’s worth noting that this is a compiler for the Tiny-C language, and not as one might think a tiny compiler for the C language.

It's probably better to call it an interpreter, since it will also run the program and print the values of all non-zero variables afterward. Calling it a compiler is (to me) really stretching things, I can't see any code to emit any other form of the code, it's all aimed at evaluating (executing) it. Edit: oops, I didn't read the code closely enough, it does emit code but only internally, that code is what gets execu…

It is a compiler rather than a direct evaluator, since it generates bytecode for a stack VM --- and also includes the interpreter for that (look at the bottom).
Post reply on HN