Writing a (dis-)assembler for 8-bit code in D - blog posts
Brian
bcallah at openbsd.org
Tue Apr 20 13:38:37 UTC 2021
On Tuesday, 20 April 2021 at 10:02:05 UTC, Dukc wrote:
> TlDr: `private` means the symbol can only be used in the same
> file.
That's clearly what I was looking for, thanks.
> You probably want to learn about `foreach` loop.
Oh, yes. I certainly know what a foreach loop is. D is hardly the
only language to provide it. I actively chose against using it;
long boring story short, it's the result of some self-bias I have
from many years on a particular kind of coding research team I
used to be on as a grad student. Looking back at the finished
code, it was a decision that didn't bear out as I was hoping. As
there are only 12 for loops (and no other types of loops, again,
on purpose) in the whole assembler, that's probably a design
decision that I would not have selected if I were going to start
over. In fact, the new parser abandons the idea already.
So I agree: converting all the for loops to foreach loops would
be a nice additional blog post. Thanks.
> `static foreach` has a lot of potential to shorten your
> assembler code.
> ```D
> sw: switch (op)
> { //iterates over the array at compile time
> static foreach(opStr; ["nop", "lxi", "stax", "inx", "inr",
> "dcr", "mvi", <...>])
> { case opStr:
> mixin(opStr)(); //inserts a call to function with name
> opStr.
> break sw; //breaks out of the switch statement
> }
>
> default: err("unknown opcode: " ~ op);
> }
> ```
>
> Even better is if you make a global array of opCodes and pass
> that to `static foreach`. The opcode array needs to be
> available in compile time, so mark it `enum` instead of
> `immutable`, like: `enum opcodes = ["nop", "lxi", "stax",
> <...>];`
I should spend some time looking at mixins. The rest you have
there makes intuitive sense looking at it. (Crazy question: is
there a way to dump internal state after semantic analysis?)
Looking at the mixins page here:
https://dlang.org/articles/mixin.html, I am already disappointed
to learn about the monkey business it won't let me do :) I was
really hoping to radically alter the syntax of D to create my own
language and then implement an entirely different language in
that (I kid, but only slightly. That's exactly what Arthur
Whitney does in his language development: K being a good example
of this.)
~Brian
More information about the Digitalmars-d
mailing list