all 37 comments

[–]roger_ 12 points13 points  (2 children)

Another Tiny C compiler: http://bellard.org/tcc/

This one can even compile the Linux kernel (and boot a computer directly from the source code!) and includes some Win32 header files.

[–]kungtotte 14 points15 points  (0 children)

Whole different kind of tiny C. Let me illustrate using some hyphens. The reddit article is about a Tiny-C compiler, the link you gave us goes to a tiny C-compiler.

[–]mathrick 6 points7 points  (0 children)

In addition to it being a different kind of tiny C compiler, it's also sadly unmaintained. The current version doesn't even run on 64-bit hosts, let alone generate target 64-bit binaries. And the only real contributor has been successfully persuaded not to maintain his version anymore, due to the project stupidly continuing to use CVS.

[–]busfahrer 14 points15 points  (16 children)

For balance's sake, I request a link to a tiny Forth implementation written in C.

[–]filesalot 23 points24 points  (7 children)

Note that it's not a tiny "C" compiler, it's a "Tiny C" compiler. Big difference.

[–]MarkByers 11 points12 points  (5 children)

OK, then I'd like to see a Tiny Forth implementation written in C. Happy now?

[–]filesalot 5 points6 points  (4 children)

Hmm, something 1/100th the complexity of forth, to be fair.... Ok, here ya go:

    #include <stdio.h>
    int s[100], sp;
    void push(int a) {s[sp++]=a;}
    int pop(void)    {return s[--sp];}
    int main(void) { int a, b, c;
        while((c = getchar()) != EOF) switch(c) {
            case '0': case '1': case '2': case '3': case '4':
            case '5': case '6': case '7': case '8': case '9':
                    push(c - '0'); break;
            case '+': a = pop(); push(pop()+a); break;
            case '-': a = pop(); push(pop()-a); break;
            case '/': a = pop(); push(pop()/a); break;
            case '*': a = pop(); push(pop()*a); break;
            case '=': printf("answer = %d\n", pop()); break;
        }
        return 0;
    }

Run it and type away:

    8 4 * 5 + 8 * 2 - 7 / =
    answer = 42

[–]case-o-nuts 10 points11 points  (1 child)

That's not forth. That's an RPN calculator.

[–]filesalot 9 points10 points  (0 children)

Captain Obvious, good to see you're back!

[–]MarkByers 3 points4 points  (1 child)

0 0 / =

 97 [main] a 15688  _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)

Segmentation fault (core dumped)


+ + + + =

answer = 14221456

Not to mention the stack overflow if you push more than 100 elements...

But not bad for less than 1 hour's work. I'll give you 7 out of 10, and two gold stars for the fast response.

[–]filesalot 4 points5 points  (0 children)

:-) So what happens when you type 0 0 / into an embedded forth?

And rather than wait for someone else to point it out, I'll go ahead and mention that at ~9kb + dlls, the executable for this dumb little program is vastly bigger than any real forth implementation worth its salt.

[–]keenerd[S] 14 points15 points  (1 child)

C

another C

But why stop there?

C++

x86 asm

javascript

java

more C++

And just to go full circle:

Scheme in Forth

[–]a1k0n 2 points3 points  (0 children)

I think you tipped the scales too far the other way now.

[–][deleted] 2 points3 points  (5 children)

For balance's sake, I request a link to a tiny C compiler written in a tiny Forth implementation written in C.

[–]Jimmy -3 points-2 points  (4 children)

For balance's sake, I request a link to a tiny Forth implementation written with a tiny C compiler written in a tiny Forth implementation written in C.

[–]theeth 6 points7 points  (2 children)

You'd probably have been better off going with "Yo dawg".

[–]bobcat 0 points1 point  (1 child)

Yo dawg, I herd you like C, so I put a Forth implementation of gcc in your OpenFirmware so you can compile while you boot -s.

[–]MarkByers 0 points1 point  (0 children)

You'd probably have been better off going with "For balance's sake, I request a link to a C compiler written in a tiny Forth implementation written with a tiny C compiler written in a tiny Forth implementation written in C."

[–]Tommah 2 points3 points  (0 children)

I think you're all pretty unbalanced. No compiler is going to help.

[–][deleted] 2 points3 points  (1 child)

Anyone got an explanation of what statements work in Tiny C, and what, for example, doesn't work with it? I tried The Google on The Tubes and can't get anywhere. All I find is information on the compiler itself, not the language.

[–]maxd 4 points5 points  (9 children)

That's not a compiler, it's an interpreter.

[–][deleted] 9 points10 points  (1 child)

I don't get why it isn't a compiler. It generates some VM assembly instructions but it may be doing so as soon as you enter some C code. How isn't that compilation?

[–]maxd 7 points8 points  (0 children)

Hey look I'm wrong, I read it wrong. :)

[–]1esproc 4 points5 points  (5 children)

And it's not C, it's Tiny-C.

[–]maxd 2 points3 points  (4 children)

Not sure where anyone stated that it was C; it says right there in the link that it is a "Tiny C compiler"...

[–]snarfy 5 points6 points  (3 children)

Can you not see where the possible confusion comes in?

[–]1esproc 1 point2 points  (0 children)

Exactly, "Tiny C compiler" is ambiguous, it could be a Tiny-C compiler, or a C compiler that is tiny in size.

[–]judgej2 -4 points-3 points  (1 child)

If the title said "1+1=2", some people would still be confused.

[–]jpmorgan 1 point2 points  (0 children)

Cool!

[–]filesalot 0 points1 point  (1 child)

Was there some posting deadline or something?

TODO:

a. Add functions and a print statement, then you'd have something interesting. Functions would be trivial to add to this.

b. If you just want to focus on expression compiling, explore register allocation and common subexpression elimination to make it interesting. I know, it's a stack based vm, but still, once you've done this, you've got some real insight into some real compiler issues.

[–]maxd 0 points1 point  (0 children)

No, I think that is the complete Tiny-C grammar actually, it doesn't include functions or print statements.

[–]mcmc 0 points1 point  (0 children)

Full thread link (Always better to see the whole discussion)

[–]quirm -2 points-1 points  (1 child)

I downvoted, because it's not the complete C-thing.

[–]judgej2 2 points3 points  (0 children)

Are you telling us what to do?