I'm making a RAM Machine emulator in TypeScript, so I made an enum of the instruction types that a RAM can have:
enum InsType {
LOAD, // Put value from specified register (or literal) into accumulator.
STORE, // Store value from accumulator into specified register.
READ, // Read from input tape and write into specified register.
WRITE, // Write to output tape from specified register.
ADD, // Add value into accumulator.
SUB, // Subtract value from accumulator.
MUL, // Multiply accumulator by referenced (or literal) value.
DIV, // Divide accumulator by referenced (or literal) value.
HALT, // Stop program execution.
JUMP, // Jump unconditionally to line specified by tag.
JZERO, // Jump to line specified by tag if accumulator value is zero.
JGTZ, // Jump to line specified by tag if acc value is greater than zero.
}
I have to make sure each instruction has a valid operand type. My way of defining the valid operands is like this:
var valid_operands = {
LOAD: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
STORE: [ OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
READ: [ OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
WRITE: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
ADD: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
SUB: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
MUL: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
DIV: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
HALT: [OpType.NONE],
JUMP: [OpType.NAME],
JZERO: [OpType.NAME],
JGTZ: [OpType.NAME],
}
But I find that the TypeScript 'compiler' doesn't care what I put in the key values-- I can change LOAD:
to LOADXYZ:
and it won't bat an eye.
Also, when I try to change it to this:
var valid_operands = {
InsType.LOAD: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
...
It warns ':' expected at line XX col YY
(those being the position of the .
). I'm using the Atom TypeScript plugin to work, if it helps. Any help is appreciated.