←back to thread

517 points petercooper | 4 comments | | HN request time: 3.728s | source
Show context
abecedarius ◴[] No.8559116[source]
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...
replies(6): >>8559784 #>>8559904 #>>8560891 #>>8560993 #>>8561018 #>>8561057 #
pvidler ◴[] No.8560993[source]
Most of it was readable, but the printf on lines 57--59 made me retch. I see what it's doing, but it's not what I'd call easily maintainable:

  printf("%8.4s", &"LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,"
         "OR ,XOR ,AND ,EQ ,NE ,LT ,GT ,LE ,GE ,SHL ,SHR ,ADD ,SUB ,MUL ,DIV ,MOD ,"
         "OPEN,READ,CLOS,PRTF,MALC,MSET,MCMP,EXIT,"[*++le * 5]);
replies(1): >>8561345 #
manish_gill ◴[] No.8561345[source]
I'd like to know why this was downvoted, and if people who down voted it can explain what the code is doing please?
replies(1): >>8561499 #
maffydub ◴[] No.8561499[source]
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 that I pass next (padded to 8 characters). There then follows a string containing all of the operation names, in numerical order, padded to 4 characters and separated by commas (so the start of each is 5 apart). Finally, we do a lookup into this string (treating it as an array) at offset "* ++le * 5", i.e. the integer representing the operation multipled by 5 (5 being the number of characters between the start of each operation name). Doing this lookup gives us a char, but actually we wanted the pointer to this char (as we want printf to print out this char and the following 3 chars), so we take the address of this char (the & at the beginning of the whole expression).

It's concise, but not exactly self-documenting.

Does that make sense?

(I didn't downvote.)

replies(1): >>8561631 #
peterfirefly ◴[] No.8561631[source]
How is that not self-documenting if one knows C?
replies(1): >>8562807 #
1. maffydub ◴[] No.8562807[source]
I think you and I might disagree on the meaning of self-documenting. ;)
replies(1): >>8563208 #
2. peterfirefly ◴[] No.8563208[source]
I don't think we really do.

It is impenetrable black magick if one "knows" C -- but quite clear if one /actually/ knows C.

replies(1): >>8563590 #
3. moron4hire ◴[] No.8563590[source]
Ah, the No True Scotsman finally arrives to the party.
replies(1): >>8564632 #
4. peterfirefly ◴[] No.8564632{3}[source]
No. The printf() requires that one has read K&R. That's not a high barrier to clear. Pointers are chapter 5.