As you may or may not have noticed, Notch (the Minecraft guy) today announced his current project 0x10c (see here). The game features a 16bit CPU which is programmed using assembler, you can look up the specification here (this is based on v1.1). I myself happen to like assembler, yet I thought it might be cool to try to get something different. My first thought was writing a small compiler but let's don't overdo it, right? I'm currently experimenting with Scala and this proofed to be a nice opportunity to get my hands dirty: I wrote a very basic interpreter which allows you to write DCPU16 programs in a more C-like syntax (though it is (almost) completely homologous to the original assembler, so no variables and extensions and stuff).
Why would I post it here? - Well, I haven't found a better place and no community for the game yet.
First, the grammar of the C-like language explained by a few examples:
As you can see it is pretty limited right now and still lacks a feature present in the assembler ([next word + register] as a value) and is somewhat more prohibitive as it does not allow literals as the left hand operator for assignments (it also doesn't allow push as a left-hand operand). The last limitation is that in the assembler you are able to place labels on the instructions right after ifs whereas this language does not allow it. The biggest problem the program currently has is that it does not display errors or warnings, consider this version a pre-pre-alpha.
// this is a comment
// each statement should be on a separate line and may not contain line breaks
// as of know, comments also need to be on their own line
// all computations use `op`=, so A = A * B or even A = C * B are illegal
A *= B
X >>= I
Z = 1
// you can only access the registers given by the specification and the special values or literals (no negatives)
A /= PEEK
Z += 42
Y -= 0x2A
C |= [0x110]
PUSH = J
A %= O
PC &= [SP]
B ^= POP
// you can mark lines with a location (as in assembler)
LOC myLocation: A += B
// 'goto x' exists as a shorthand for SET PC _
// 'call x' is the same as JSR x
// there also are ifs! you may not nest them, may not put LOCs in the consequence
if C == B then C += B
if C > B then jmp A
if C & A then call B
if A != 1 then A = 1
// you may only use the conditions given by the specification, 'C & A' = 'C != 0 && A != 0'
Usage of the program is as follows:
java -jar dcpu16-rewrite.jar -"<inputfile>" -"<outputfile>"
This will rewrite inputfile in assembler to outputfile.
Example input file:
LOC test: A = 10
B = 20
A *= B
[0x500] = A
push = A
LOC test2: if A == B then C = PC
The download also contains the scala-libray, that's why it is a bit heavy (~8mb).
test: SET A, 0xA
SET B, 0x14
MUL A, B
SET [0x500], A
SET PC B
SET PUSH, A
test2: IFE A, B
SET C, PC