$ time ./c4 c4.c c4.c hello.c
hello, world
exit(0) cycle = 9
exit(0) cycle = 22614
exit(0) cycle = 9273075
real 0m0.067s
user 0m0.067s
sys 0m0.000s
$ time ./c4 c4.c c4.c c4.c hello.c
hello, world
exit(0) cycle = 9
exit(0) cycle = 22614
exit(0) cycle = 9273075
exit(0) cycle = 933197195
real 0m5.834s
user 0m5.827s
sys 0m0.000s
$ time ./c4 c4.c c4.c c4.c c4.c hello.c
Just kidding. :) Amazingly cool! Does anybody have a smaller self-hosing compiler & bytecode vm?C4 – C in 4 functions
121–130 of 142 posts
Re: C4 – C in 4 functions
#122I honestly think this is ridiculous. Sure, this is an incredible feat, and congrats. But serioulsy, I would be ashamed to publish such unreadable code under my name. What about naming your variables with descriptive names? What about extracting complex conditions into well named function to understand what is going on (thus defeating the purpose of the "4 functions") ? This list could go on forever... Writing softwar…
The goal here is to write a compiler that can compile itself.
./c4 c4.c c4.c hello.c
Granted, most compilers can do the equivalent, but it's rarely so simple to invoke.Re: C4 – C in 4 functions
#123Earlier quoted context omitted.
It's taking an integer (representing an operation) and printing out the name of that operation. First thing to say is that "* ++le" is the integer representing the operation to perform. This basically walks through the array of instructions returning each integer in turn. Starting at the beginning of the line, we have "printf" with a format string of "%8.4s". This means print out the first 4 characters of the string…
How is that not self-documenting if one knows C?
Re: C4 – C in 4 functions
#124Re: C4 – C in 4 functions
#125Earlier quoted context omitted.
I agree with you completely - The code explains what you're doing, comments explain why you did it that way. Ideally, any comments that explain what you're doing would end-up being redundant when looking at the code. I think this code details a special case of the above though, in that it comments what the enums are instead of just naming the enums. I give that a pass strictly because this code needs to be able to co…
Its not that simple though, error fixes and edge cases often obfuscate something that was understandable. A why comment is never bad, but a what comment is often as valuable as a test
Re: C4 – C in 4 functions
#126Re: C4 – C in 4 functions
#127Earlier quoted context omitted.
Unless you're talking about a large piece of software composed entirely of single character functions and variable names, I pretty much disagree. Verbose variable names do not magically teach those reading a piece of code how it works, simultaneously they tend to make it impossible to write many kinds of expressions concisely, and consequently they regularly damage the readability of more complex pieces of code (e.g.…
I mostly strongly disagree. I see no value in naming a variable 'tk', 'pp', or 'bt'. It can only help to make the code more readable with less context. I do not need to understand compilers in detail to know what this program is doing, except, for the names being useless. And if I do understand them but have not spent half an hour or probably much more to digest the exact system by which it operates, I would be compl…
tk, // current tokenRe: C4 – C in 4 functions
#128Earlier quoted context omitted.
I agree with you completely - The code explains what you're doing, comments explain why you did it that way. Ideally, any comments that explain what you're doing would end-up being redundant when looking at the code. I think this code details a special case of the above though, in that it comments what the enums are instead of just naming the enums. I give that a pass strictly because this code needs to be able to co…
Its not that simple though, error fixes and edge cases often obfuscate something that was understandable. A why comment is never bad, but a what comment is often as valuable as a test
Re: C4 – C in 4 functions
#129Earlier quoted context omitted.
I think you and I might disagree on the meaning of self-documenting. ;)
I don't think we really do. It is impenetrable black magick if one "knows" C -- but quite clear if one /actually/ knows C.
Re: C4 – C in 4 functions
#130Earlier quoted context omitted.
That's a nice one. The big difference is that it doesn't compile itself -- not that there's anything magical about that, but it's a kind of threshold of seriousness.
One of the things I've wondered about is how small one could make an ISO C89-compliant compiler (that would be able to compile itself), and all these tiny compiler projects have inspired me to revisit that thought now... I've written pieces of compilers like expression parsers and tokenisers, and even then I felt like it wouldn't be so hard (if I had the time) to write a full compiler. These are all great for dispell…
C89 I think has too much complexity for the amount of power it offers. lcc is a nice example: Norman Ramsey said he asked one of the authors what he learned in writing it, and got an answer like "Not to write a compiler in C." But anyway the book about it https://sites.google.com/site/lccretargetablecompiler/ is very good. http://www.cs.princeton.edu/~appel/modern/ is my favorite general text.