Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a hash with enum keys in Typescript

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.

like image 951
JoseHdez_2 Avatar asked Jun 26 '16 12:06

JoseHdez_2


People also ask

How do I use enum values in TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.

Can you map over an enum?

In other words to use the map() method with an enum, we have to convert the enum keys or values to an array and call the map() method on the result. If you want to map over the values of an enum directly, you can get an array of an enum's values in the following ways. Copied!

Can we use enum as key in HashMap?

In HashMap, we can use Enum as well as any other object as a key. It doesn't allow storing null key. It allows to store the null keys as well values, but there should be only one null key object and there can be any number of null values. HashMap internally uses the HashTable.


2 Answers

define the object key inside the []

var valid_operands = {
  [InsType.LOAD]:   [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
  [InsType.STORE]:  [                    OpType.NUM_DIRECT, OpType.NUM_INDIRECT],

....

like image 119
Sachila Ranawaka Avatar answered Sep 22 '22 13:09

Sachila Ranawaka


You can use Map.

enum InsType {
    LOAD,
    STORE
}

enum OpType {
    NUM_LITERAL
}

var vo2 = new Map<InsType, [OpType]>();
vo2.set(InsType.LOAD, [OpType.NUM_LITERAL]);
like image 23
Zen Avatar answered Sep 23 '22 13:09

Zen