Live data from Hacker News

C4 – C in 4 functions

github.com

131–140 of 142 posts

Re: C4 – C in 4 functions

#131
post #94
post #79

Earlier quoted context omitted.

Exactly. You have to drop hooks into the lexer from the parser, and by the time you're done you end up with just as much code. Only it's slower than a recursive-descent parser would be, and a lot of the time parsing speed really matters because it shows up as user-visible latency.

While I have a strong dislike for lex/yacc and descendants, the "hooks" you need for C are trivial. As far as I remember the only thing you need is an ability for the lexer to check whether or not a given identifier is a variable or type. Even that is only needed if you want to report more specific information up to the parser. E.g. Clang doesn't. In Clang it is instead the parser that looks up the information in ord…

And what are the alternatives? I've always wanted a parser object (in C++ anyway) that I can add constructions to, feed it scanner output and have it build a symbol table and semantic tree. Does such a thing exist?

Re: C4 – C in 4 functions

#132

This shit doesn't scale: $ 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 c…

Self-hosing, haha. Not that I can think of. My https://github.com/darius/ichbins is shorter but it's a self-hosting Lisp compiling to C.

Re: C4 – C in 4 functions

#133
post #93

On a first skim, this looks really nice; complaints that it's unreadable are unfounded. The background that makes it readable are Wirth's Compiler Construction http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf plus precedence climbing http://en.wikipedia.org/wiki/Operator-precedence_parser#Prec...

> complaints that it's unreadable are unfounded int *a, *b; int t, *d; I can't really see how anyone can say that code that uses one-letter variable names (with the exception of the "standard ones", whose meaning is defined at the top) is readable.

OK, 'unfounded' was a little too strong. I'd change some things myself, including comments on those declarations; but if this looks like a code-golf game to you, it's not, it's a style you're not used to.

Re: C4 – C in 4 functions

#134

On a first skim, this looks really nice; complaints that it's unreadable are unfounded. The background that makes it readable are Wirth's Compiler Construction http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf plus precedence climbing http://en.wikipedia.org/wiki/Operator-precedence_parser#Prec...

> On a first skim, this looks really nice; complaints that it's unreadable are unfounded. Man, I can't even tell what this is supposed to be. My confusion is entirely founded. My thought process with articles like this goes something like "C in four functions, huh? Sounds like it could be clever. I'll just click and read the explanation... Oh, there isn't an explanation. Well, maybe this file will explain things! ...…

Yes, I'm sorry: I meant to defend this code from the charge of being pointless code golf, and inadvertently disparaged people without the background to enjoy reading it. It really is hard to follow without that background, which lots of good programmers don't have.

I hope someone will write an explanation. I'm still working on one for my own (quite different) little compiler.

Re: C4 – C in 4 functions

#136

Earlier quoted context omitted.

I don't think we really do. It is impenetrable black magick if one "knows" C -- but quite clear if one /actually/ knows C.

Ah, the No True Scotsman finally arrives to the party.

No. The printf() requires that one has read K&R. That's not a high barrier to clear. Pointers are chapter 5.

Re: C4 – C in 4 functions

#137
post #78

Earlier quoted context omitted.

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…

As you wish. From the program, line 19: tk, // current token

replace all: tk -> current_token

Re: C4 – C in 4 functions

#138

This shit doesn't scale: $ 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 c…

For the record:

  $ time ./c4 c4.c 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
  exit(0) cycle = -1428163377

  real	9m23.409s
  user	9m22.673s
  sys	0m0.020s
:)

Re: C4 – C in 4 functions

#139

Earlier quoted context omitted.

In the sense that some people won't have an idea of what's going on, this community altogether isn't particularly inclusive at all. Personally, I really don't want the topics this site covers to cater to a lowest common denominator, and I'm sure that isn't what you had in mind either, but that's the effect of taking "more of us" to mean more than you personally.

The logical leap from adding a "few hints" to everything becomes "lowest common denominator" is the size of the Grand Canyon.

Personally I don't see anything wrong with suggesting to add a few hints, but the basis of that suggestion in this case was that "there's a lot more of us who could probably get the joke" with the hints. If making it approachable to more people is inherently a good thing, the logical conclusion is to make it approachable to everyone.

With code like this, the readability obviously isn't a high priority consideration and sometimes the exact opposite of the goal, with the impenetrability sometimes being part of its charm. This is Hacker News after all, and if your reaction to of a piece of code that describes itself as "an exercise in minimalism" is to leave a snarky comment about the lack of documentation, you should probably check your news elsewhere.

If you have any interest in the subject, the initial comment "just enough features to allow self-compilation and a bit more" should give the purpose of the code away. If not, it ought to have been a clear sign of dragons.

Re: C4 – C in 4 functions

#140

Earlier quoted context omitted.

In the sense that some people won't have an idea of what's going on, this community altogether isn't particularly inclusive at all. Personally, I really don't want the topics this site covers to cater to a lowest common denominator, and I'm sure that isn't what you had in mind either, but that's the effect of taking "more of us" to mean more than you personally.

Actually, a lot of us probably don't understand what this is doing. I sure don't, but I really don't consider myself "lowest common denominator" either. I come here to learn, to be honest!

My point isn't that not knowing what this does makes you a lowest common denominator. It's that most stuff that is shared on this site at least borders on being what I'd call esoteric, and if making each individual submission more approachable or catering to a larger general audience is a goal of this community, it isn't really going that way. If it was going that way, I doubt the community would be particularly interested in this site.

This submission in particular has dubious practical use, and the description, "an exercise in minimalism", is telling of a sort of artistic intent. If you don't understand what it does, how it does it, or if you don't like it, it won't lower my opinion of you in any way, but its inclusion on this site is part of why I like to come here every now and then. I get to discuss subjects that relate to my work and hobbies, but I also get to look at weird alien code and think hard in unfamiliar terms. From what you are saying, I think you can relate.

Personally, I could glance over it and get the idea that it is a C compiler, but if you were to show me some code in written with the latest JS MVC or FRP framework, don't hold your breath for me to tell you what it does. I can't say that I fully understand this, and that's why I enjoy the rich discussion the submission spawned here.

Post reply on HN