News for hackers. Stuff that matters.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Saturday July 16, 2022 @05:27PM (#32122131)
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.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by p4bl0 on Saturday July 16, 2022 @05:48PM (#32122288)
Hey 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) ;).

Re:Show HN: A compiler and VM for a simple language, in 150 ... by shadowofneptune on Saturday July 16, 2022 @07:47PM (#32123004)
One question: what is the binary size of the compiler?
Re:Show HN: A compiler and VM for a simple language, in 150 ... by p4bl0 on Saturday July 16, 2022 @09:19PM (#32123516)
The native binary produced by ocamlopt is 917KB, the bytecode produced by ocamlc is 156KB.

I must admit I didn't expect such "big" files.

Out of curiosity I tested to compile a completely empty OCaml program: the native binary is 365KB and the bytecode is 21KB.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by l33t2328 on Saturday July 16, 2022 @11:47PM (#32124321)
I don’t know much about OCaml but one reason could be linking unnecessary libraries
Re:Show HN: A compiler and VM for a simple language, in 150 ... by chrisseaton on Sunday July 17, 2022 @05:12AM (#32125463)
Presumably you’re linking in a large GC etc?
Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Saturday July 16, 2022 @06:10PM (#32122409)
The 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=8746054)

Re:Show HN: A compiler and VM for a simple language, in 150 ... by p4bl0 on Saturday July 16, 2022 @06:14PM (#32122433)
> Could you compile the compiler with itself, for example?

That's only possible if you write the compiler in the language it compiles. By this account writing a Python compiler in something else than Python, for example in C, would make it incomplete? It doesn't make much sense.

> If you're taking a course (…)

You should read my post more carefully, I think.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by tiagoleifert on Saturday July 16, 2022 @06:15PM (#32122444)
> I was referring to the complete lack of flow control. Could you compile the compiler with itself, for example?

This is not a requirement for it to be a compiler.

> 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.

The purpose of OP's compiler is educational so this comparison makes no sense.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by jhgb on Sunday July 17, 2022 @09:11AM (#32126454)
While it may not be "a requirement", considering that the very first compiler I'm aware of (Böhm's) was already written in itself, it's a fairly valid question why wouldn't you make an educational compiler with these properties.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by abecedarius on Sunday July 17, 2022 @02:19PM (#32129485)
> an educational compiler with these properties

Far from the only one, but here was mine: https://codewords.recurse.com/issues/seven/dragon-taming-wit...

[dead]
Re:Show HN: A compiler and VM for a simple language, in 150 ... by mirekrusin on Sunday July 17, 2022 @04:52AM (#32125411)
You did pick up that the author is an associate professor teaching classes, not a student, right?
Re:Show HN: A compiler and VM for a simple language, in 150 ... by tiagoleifert on Sunday July 17, 2022 @07:50AM (#32126007)
How studying something super optimized to be small is more practical than learning something step by step?

I am not saying studying those tiny compilers is not useful, just that they are not a good starting point for most people. First you need to learn the basics then you can learn (and appreciate) those tiny compilers.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Sunday July 17, 2022 @04:59PM (#32131189)
It's because everything can be seen at once. The feeling of "that's it?" when you realise the underlying simplicity. Most people think compilers must be incomprehensibly complex. Yet the truth is far from that.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by rdevnull on Sunday July 17, 2022 @05:42AM (#32125534)
seriously he did tell you to read the original post and just a couple of click laters you can find out the OP is a professor not a student..
[dead]
Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Sunday July 17, 2022 @04:57AM (#32125426)
I thought personal insults and appeal to authority were frowned upon here.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by survirtual on Sunday July 17, 2022 @05:39AM (#32125530)
It is, but I think he is understandably upset because people are engaging with you in good faith but you aren’t reading or acknowledging what they write. Sometimes, it’s good to just take a step back and start with something like “Oops, I missed you are a professor and this is a teaching tool. Disregard that part. I do think, however…”

Otherwise, you’re bound to annoy someone even if you think you are technically contributing.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Sunday July 17, 2022 @05:07PM (#32131295)
The whole point is that I absolutely don't care who or what he is; everyone can learn from the C4 family, and if anything, it would be highly suited to someone teaching compilers.

Otherwise, you’re bound to annoy someone

I see the title has been changed now, but I was certainly annoyed when it was advertised here as a "complete" compiler.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by klohto on Sunday July 17, 2022 @01:04PM (#32128643)
Telling a professor to take C4x86, twice nonetheless, is itching me to create throwaway as well
Re:Show HN: A compiler and VM for a simple language, in 150 ... by a-dub on Saturday July 16, 2022 @05:39PM (#32122226)
there'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.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by compiler-guy on Saturday July 16, 2022 @10:50PM (#32124026)
Type 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.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by a-dub on Sunday July 17, 2022 @11:56AM (#32127906)
fair 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.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by userbinator on Saturday July 16, 2022 @06:13PM (#32122425)
B 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.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by p4bl0 on Saturday July 16, 2022 @06:22PM (#32122476)
> 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.

Re:Show HN: A compiler and VM for a simple language, in 150 ... by wtetzner on Sunday July 17, 2022 @06:26PM (#32132086)
I don’t see how, I would only count the input lines to the parser generator.
Re:Show HN: A compiler and VM for a simple language, in 150 ... by wtetzner on Sunday July 17, 2022 @06:25PM (#32132078)
> there's no type checking, which i'd say is a fairly integral part of a compiler...

There are compilers for dynamically typed languages.