Ah, okay. It looks like you're taking a fairly C-like language and transforming it to C--I'd call this a transpiler rather than a compiler. You're not wrong to call it a compiler, but it's a pretty noncentral example of a compiler.
Congrats on at least having an emitter, but I'm still searching for an article that shows how to emit assembly of any kind.
I used to work on a static analyzer that did taint analysis, model checking, buffer bounds checking, and so on -- a bit like a compiler backend on steroids. If there's a specific topic you'd like an explanation on, I could be convinced to write something up.
My favorite was always context-sensitive, interprocedural points-to analysis. And dataflow analysis in the presence of higher-order controlflow constructs.
Honestly, I'd just like to see anything that outputs assembly I can run through an assembler to create an executable. I've fumblingly written a bit of this code myself, but always felt hampered by a lack of knowledge.
Well, what you just posted already highlights one difficulty: when you come to the PRINT statement, you have to emit the string and the instructions to two different places, so we're already talking about having two different emitters, or some other way of handling this. And we need to generate different non-conflicting names/addresses depending on architecture.
You said in your other post that this can be done with minor modifications, but I can already foresee a few modifications that would need to be made which aren't minor.
And then there's the problem that you may want to target more than one architecture. We can write two completely different code generators, but it would be nice if there were an architecture that could share some of the code.
I honestly can't tell if you are just trolling now, and I'm falling for it, but you seem to think this 2000 word set of tutorial on the basics of compiler design (lexing, parsing, emitting) is supposed to be the one and only document you will ever need to create the next C++.
> You said in your other post that this can be done with minor modifications
And it probably can, depending on the flavor of assembly you want to use, there are dozens (hundreds?) of them, i'm sure some will allow you to inline the string declaration. The example I gave probably doesn't even work since I haven't programmed in 8086 in close to 20 years, and I don't even remember how to set up data blocks and code blocks in it any more.
> And then there's the problem that you may want to target more than one architecture.
This is a toy compiler written by a professor of computer science meant to teach you the basics of building a compiler (lexing, parsing, emitting). This isn't a tutorial on building the next GCC.
> I honestly can't tell if you are just trolling now, and I'm falling for it, but you seem to think this 2000 word set of tutorial on the basics of compiler design (lexing, parsing, emitting) is supposed to be the one and only document you will ever need to create the next C++.
That's a fair criticism.
I'm frustrated with the lack of material on emitting assembly, but it wasn't right of me to take that out on the author of this post. I apologized in a different post.
> And it probably can, depending on the flavor of assembly you want to use, there are dozens (hundreds?) of them
How about one I can run on my machine? There are maybe 5 that are useful targets I can think of:
* x86 or ARM (depending your machine)
* LLVM
* GCC RTL
* Web assembly
* Parrot? Maybe the JVM has some low-level bytecode?
there's certainly equivalent assembler, but depending on the architecture you're targeting this can be a pretty monumental task. Even a single function call can be north of a hundred lines or something (i'm making this up :D )
I guess that's why we have things like LLVM that allow you to generate intermediate representations that get converted to a bunch of different instruction sets
For sure, i'm just telling the GP that their request for something that outputs assembly could be done with minor modifications depending on the assembly language they want to output.
gotcha. I also get the feeling that GP was more interested in being right vs actually knowing how to get the emitter to emit assembly. It felt like the followup was just gonna be ... well WHAT ABOUT BINARY?
nand2tetris is a great course that shows you how to design a computer from the chip level, build an assembler, and write a high level language + compiler that eventually produces the assembly code for the machine architecture _you_ built. I highly recommend it.
Congrats on at least having an emitter, but I'm still searching for an article that shows how to emit assembly of any kind.