7 ms·
I found the code surprisingly readable. Most variables names are what I expected them to mean despite their shortness: pc, sp, bp are registers, a is the accum
by GuB-42 7y ago
I found the code surprisingly readable.
Most variables names are what I expected them to mean despite their shortness: pc, sp, bp are registers, a is the accumulator, fd is a file descriptor (of the input file, what else?), tk is for the token, t is temporary, etc... For the less obvious ones, it is usually not that hard to infer their meaning from either the code or comments.
Because yes, they are comments, not many, but they are helpful. For example, the VM has unusual instructions (for me) like LEV and ADJ, and they are commented. The "obvious" ones like MUL and SHR are not.
The variable names are not "needlessly cryptic". I've seen (and written, not proud of it) a lot of needlessly cryptic variable names, and believe me, these are crystal clear by comparison. Here, there is a clear influence from assembly mnemonics that really helps understanding.
Now on the why. This is minimalism, and minimizing the number of comments and variable name length is part of it. It is actually a very interesting exercise.
The golden rule in making understandable code is making it as short as possible. There is a limited amount of space on your screen and in your mind, and the shorter your code is, the more you can see/understand at once. Of course, too much is too much, you don't want to do things IOCCC style, and striking a balance is difficult. So once in a while, reading or writing very compact code can help you understand where shaving off characters is fine and where it really hurts understanding.
- kerkeslager 7y ago> Most variables names are what I expected them to mean despite their shortness: pc, sp, bp are registers, a is the accumulator, fd is a file descriptor (of the input file, what else?), tk is for the token, t is temporary, etc... For the less obvious ones, it is usually not that hard to infer their meaning from either the code or comments. Maybe you have the right background to channel the author's particular form of abbreviation, but I have no idea what bp is supposed to stand for, despite having read and understood how it's being used in the program. If that name was supposed to communicate something, I am really not sure sure what it was. Yes, I understand it's probably a reference to some register in some architecture, but that's only effective communication to an audience with a background in that architecture. Even if you know pc = program counter, what exactly does that communicate to someone who doesn't know assembly language? Of course, using "pc" allows a person with a background in assembly to quickly grok what that variable is, so there are upsides. A best-of-both-worlds approach might be to name it pc and teach people who aren't familiar with assembly language what that means with a comment, like: int *pc; // program counter, points to the current instruction Ask yourself, even if the person knows a = accumulator, what is the accumulator accumulating? When you realize that that isn't even a really sensible question in the context because "accumulating" isn't even really what that variable does, then I have to wonder why you think that's a good name for that variable. > Because yes, they are comments, not many, but they are helpful. For example, the VM has unusual instructions (for me) like LEV and ADJ, and they are commented. The "obvious" ones like MUL and SHR are not. > The variable names are not "needlessly cryptic". I've seen (and written, not proud of it) a lot of needlessly cryptic variable names, and believe me, these are crystal clear by comparison. Here, there is a clear influence from assembly mnemonics that really helps understanding. That's helpful if you know the specific assembly language the author is referencing. But if you don't know assembly language, or if you know a different assembly language, it's not helpful. That SHR instruction you said is obvious doesn't exist in MIPS[1], which is what a lot of assembly beginners will get introduced to. Oh and by the way, which assembly is being imitated isn't documented, so you can't even look that up easily. The "unusual instructions (for me)" aside is telling: not everyone is you. If your variable names only communicate to you, they don't communicate (an activity that famously involves just one person[2]). > The golden rule in making understandable code is making it as short as possible. That's total nonsense. Code becomes understandable when you see it as communication, which starts with understanding who your audience is, and catering your communication to their vocabulary. If your audience has a strong background in x86 assembly, they probably have the vocabulary they need to understand this program. But that is actually a quite narrow audience. Just to be clear, I don't think this is a bad program. There's a lot to be said for choosing a goal and following through with it, and some of this code is downright brilliant. But effective communication, it is not. [1] http://inst.eecs.berkeley.edu/~cs61c/resources/MIPS_Green_Sheet.pdf http://inst.eecs.berkeley.edu/~cs61c/resources/MIPS_Green_Sh... [2] https://www.xkcd.com/1984/ https://www.xkcd.com/1984/
- psychoslave 7y agosince all seems so clear to you, would you mind give some translation of each non plain English word used, or some glossaries explaining each? Including: p -> position, pointer ? lp -> location pointer ? bss ??? e -> expression ? emitted code? le -> location of emitted code? Num number? -> why 128, I guess it's related to ASCII ending at 127 Fun function? Sys ??? Glo ??? Loc location? Id identifier? [reserved keyword are mostly complete word] Char (charset, sign) Else Enum (enumeration, roll) If Int Return Sizeof (size of, heft) While Assign (assignation, peg) Cond (condition, ply) Lor (logical? or, ere) Lan (logical? and, also) Or Xor (exclusive or, otherwise) And Eq (equals, dows) Ne (not equals, jars) Lt (lower than, Gt (greater than, Le (lower equal Ge (greater equal Shl (shift left, haw) Shr (shift right, gee) Add Sub (subtract, take) Mul (multiply, time) Div (divide, rive) Mod (modulo, lap) Inc (increment, amp/eke/pip) Dec (decrement, ebb/dip) Brak (break, blow) between parentheses I provided a guess, and a real English word that could carry the same meaning, generally in less than four letters. I didn't go further in the code so far.
- DagAgren 7y ago> The golden rule in making understandable code is making it as short as possible. Absolutely not true, in any way. And I fail to see a single reasonable argument for why writing "tk" should be better than writing "token". Just name things using real words. Cryptic abbreviation does not help anyone.
- JustSomeNobody 7y agoOnce you know “tk” means token, you don’t need it to be “token”.
- kick 7y agoAbsolutely true. C4 doesn't go far enough. The source of my favorite C compiler, on the other hand: http://www.kparc.com/b/ http://www.kparc.com/b/
- still_grokking 7y agoThis source would look beautiful as pattern on a t-shirt or socks. :-)
- userbinator 7y agoYou will mentally read "token" every time you see "tk" anyway. The same way you "grep", "ls" or "cd" --- it easily becomes as natural as any other language, unless you consciously try to stop yourself from learning.
- 0xffff2 7y agoI read "grep", "ls" and "cd" as "grep", "el ess" and "cee dee". I don't even know what grep and ls stand for.
- coolreader18 7y agols is a shortening of "list", i.e. list directory contents, and grep is apparently Global Regular Expression Print.
- LeifCarrotson 7y agoWhy not just write out ProgramCounter, StackPointer, Accumulator, InputFileDescriptor etc? It doesn't take significantly longer to type. It's faster to read because you can recognize the shape of those words and don't have to mentally substitute the actual works. Code is for reading, not for writing.
- cestith 7y agoMost of these abbreviations are well established. 'pc', 'sp', and 'a' are the names of those registers in many assembly languages.
- cr0sh 7y agoTo clarify, you don't usually see just "a" for an accumulator, as there are usually more than one accumulator-style registers in a CPU, and in many cases they are split along byte (possibly word) boundaries. So you end up with accumulators called "A" and "B", but are composed of registers "AX" and "AY", and "BX" and "BY", with each being one byte (or word) wide; X and Y being high and low bytes/words of the register (and dependent on "endian-ess" too). Sometimes you even get where multiple registers can be referenced by a singular name - "D" is a popular choice, and may be made up of "A" and "B" (being low/high "registers" of the larger word). IIRC, the 6809 was like this (?) - A and B were 16 bit registers, but could be referenced as a 32-bit word "D" (or maybe I am thing of the 68k or some other architecture - it's been a long while). The only other time I have ever seen singular letters used for registers in assembly was for very old pre-microcomputer systems (beasts like the Univac and System/360 - though I think the PDP-8 had similar style). Also some of the very early "microcontrollers" (which were more like glorified sequencers with some extra memory and rudimentary branching, if any) had similar "registers" (Radio Shack once sold, as a part of their "Science Fair" electronic kits, a "Microcomputer Trainer" that was something like a very small 4-bit microcontroller with 128 bytes of memory or something like that - to teach assembler and a bit of hardware interfacing - it had "small" registers like that referred to in single letters).
- gmfawcett 7y ago
- laumars 7y ago> The golden rule in making understandable code is making it as short as possible. If that were the case people wouldn't even bother with assembler mnemonics, comments and white spacing. People would write their Javascript / CSS minified from the outset and code golfing would be a best practice rather than a niche activity that some developers do for fun. I do actually get the point you're trying to make in your post and you do raise some valid points but that sentence is massively overreaching and thus works against you.