News for hackers. Stuff that matters.
Re:Show HN: How to write a tiny compiler by Someone on Thursday February 09, 2017 @04:17PM (#13611075)
If you want to roll your own code throughout, writing your own lexer, parser, and backend, for your first attempt, pick a dead simple source language and do not may any attempt at generating efficient code.

An option would be a Basic from the '80s: no functions, all variables are global. If you think implementing a return stack is too hard, forget about supporting gosub; goto may be harmful, but it is enough to write some interesting programs (Certainly forget about computed gosub/goto in your first version)

That makes lots of stuff incredibly easy. Variable 'a' always has the same address (the simplest implementation even would just allocate room for variables a-z, a%-z% and a$-z$ regardless of whether they got used, so that 'a' has the same address in every program)

Forgetting about optimization means that, for example

  a = a + 1
  a = 2 * a
would compile to

  load a
  add 1
  store a
  load a
  mul 2
  store a
, independently of where it appears in the program. That makes your compiler consist of two steps:

  - compute a map mapping line number to generated code for that line
  - fix up the targets of goto's (in the first version, don't bother about trying to use short jumps; just assume all jumps will be long)
Inefficient? Yes, but make it work first, then make it fast (or skip that, as, frankly, if you want something for use in the real world, you should try and build on the likes of LLVM)

Don't think that's cheating; it gives you around a tenfold speed up over a basic interpreter, and back then, people sold compilers doing the above.

If you want advanced functions such as sin, do not hesitate to call into an external library. Those compilers from the '80s weren't afraid to call into ROM code, either.

Re:Show HN: How to write a tiny compiler by jfoutz on Thursday February 09, 2017 @05:24PM (#13611600)
This is a great approach. I'd probably advise adding lexical scoping, because managing the stack is pretty interesting.

Going from source to binary on your own code is incredibly satisfying and incredibly enlightening. It thoroughly drives out any "magic" in your mental model of computation.

I agree wholeheartedly use LLVM (or something like it) for anything real. tens or hundreds of thousands of hours, by really smart people, gives you optimizations a lone wolf can never duplicate.

Re:Show HN: How to write a tiny compiler by stevekemp on Saturday February 11, 2017 @12:40PM (#13623666)
That's a great suggestion. I went one step lower and wrote a (perl-based) virtual-machine. As you suggested I did the bare minimum at first, for example when I see labels I just remember their locations. Later on I patch up the offsets in the code, to make jumps to them point to the correct location.

The project seems reasonably popular, for reasons that I don't understand, but it is very simple:

https://github.com/skx/simple.vm