4 ms·
For those wondering what language this is for before clicking on it: it's not much more than what I think would traditionally be called an expression evaluator,
by userbinator 4y ago
For those wondering what language this is for before clicking on it: it's not much more than what I think would traditionally be called an expression evaluator, and very simple one at that --- it only recognises binary logic expressions, and the 150 lines doesn't include the parser or lexer either. Not a bad attempt for what is presumably a first try, but I think "complete compiler" is overselling it.
- a-dub 4y agothere's no type checking, which i'd say is a fairly integral part of a compiler... but it does use a parser generator to generate a parser that constructs an ast and it then iterates that ast to generate a bytecode. that's basically a compiler, even if the language is basic expressions.
- userbinator 4y agoB has no type checking either. On the other hand, using a parser generator and not counting its output in the line count does seem like cheating.
- p4bl0 4y ago> using a parser generator and not counting its output in the line count does seem like cheating Yeah maybe I should count OCaml's standard library lines of code too. Maybe also count expanded macros and inlined functions at each call site? At which point should I stop? Are assembly language pseudo-instructions cheating too? Common, don't be ridiculous.
- wtetzner 4y agoI don’t see how, I would only count the input lines to the parser generator.
- compiler-guy 4y agoType checking is a part of semantic analysis, which the author explicitly says isn’t present. Even so, the language it is compiling has only one type, so it isn’t possible for a program to have type errors. Just like in C++ one never need to check if a reference is null—it simply can’t be expressed in the language, thus preventing any errors of that kind, a single type prevents all kinds of type errors. One might argue that this is the most powerful way to do type checking. It simply isn’t possible to have an error.
- a-dub 4y agofair enough. regardless i think it's a work of art and meets the definition of compiler in my mind. i hope that the author chooses to publish the rest of their course materials and does so in a language i understand. i expect they are/will be fantastic.
- wtetzner 4y ago> there's no type checking, which i'd say is a fairly integral part of a compiler... There are compilers for dynamically typed languages.
- p4bl0 4y agoHey there, author here :). The language is intentionally very simple indeed, because the goal is to showcase all indispensable steps of a complete compiler. I initially wrote it for my students as an introduction to my compiler class (for the very first session). > would traditionally be called an expression evaluator It is not an evaluator, evaluators are included (the different eval functions). > the 150 lines doesn't include the parser or lexer either This is just wrong: $ cat *.ml{,l,y} *.c | sed '/^\s*$/d' | wc -l 149 and these 149 lines (okay, 169 if you count blank lines which are removed using sed in the count above) include the two `eval` and the `run` functions which are not actually part of the compiler but here for clarity (they help understanding how the successive intermediate representations of the boolean expressions work — without them the entire project is less than 130 lines). > Not a bad attempt for what is presumably a first try It's not ;). It is however targeted at beginner compsci students who have absolutely no idea how compilers are built yet :). > but I think "complete compiler" is overselling it. It does lexing & parsing to produce an AST of a boolean expression, then a traversal of this AST to transform it into a second intermediate representation (an AST of NAND-only expression), then compiles the expression into a list of instructions (and this paradigm shift is a big step that is tough to teach to some students, believe me ^^), and then emits the corresponding bytecode (which here is 1:1 with the imperative instructions), and then the VM interprets this bytecode. I don't think I'm overselling it :). The only way it is not complete — and that's written in the README — is that it has absolutely no semantics analysis (there is only one type, and no names, so there is nothing to analyze ^^), but that is not mandatory to be called a compiler. Then again, this is only the first hour of a semester long compiler class (which covers semantic analysis, don't worry) ;).
- userbinator 4y agoThe only way it is not complete — and that's written in the README — is that it has absolutely no semantics analysis I was referring to the complete lack of flow control. Could you compile the compiler with itself, for example? Then again, this is only the first hour of a semester long compiler class If you're taking a course, you might enjoy "leaping ahead" by studying C4 and C4x86. They are also VM-based, and self-compiling, yet still tiny. I think they really raised the bar on what a tiny-and-complete compiler is. (https://news.ycombinator.com/item?id=8558822 https://news.ycombinator.com/item?id=8558822 https://news.ycombinator.com/item?id=8746054 https://news.ycombinator.com/item?id=8746054)