How to define a hash with enum keys in Typescript

JoseHdez_2

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.

JoseHdez_2

The way I solved it was to assign each key-pair value individually.

var vo2 = { }

vo2[InsType.LOAD]  = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.STORE] = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.READ]  = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.WRITE] = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.ADD]   = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.SUB]   = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.MUL]   = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.DIV]   = [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT]
vo2[InsType.HALT]  = [OpType.NONE]
vo2[InsType.JUMP]  = [OpType.NAME]
vo2[InsType.JZERO] = [OpType.NAME]
vo2[InsType.JGTZ]  = [OpType.NAME]

It works fine. But if anyone finds a way to just declare a hash with enum keys, please go ahead and post an answer. :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to define and use an enum in typescript ?

From Dev

How to define type of an object to accept only keys from enum?

From Dev

How do I define a FactoryGirl factory that returns a Hash with stringed keys?

From Java

How to make Typescript infer the keys of an object but define type of its value?

From Dev

Typescript: enum keys as parameter to function

From Dev

Define std::hash for enum member of class template

From Dev

How to change hash keys

From Dev

Define type as "all possibilities from an enum" in Typescript

From Java

Define a list of optional keys for Typescript Record

From Dev

How to define a variable from an enum?

From Java

How to define hash tables in Bash?

From Dev

How to map enum to enum with the same keys

From Java

How to define Singleton in TypeScript

From Dev

How to define foreign keys in migrations?

From Dev

How to define foreign keys in migrations?

From Dev

How to retrieve all hash keys

From Java

How to define a non-ordinal enum in Kotlin?

From Dev

How to define the order when sorting a string enum

From Dev

How to define static constants in a Java enum?

From Dev

How to define enum length different types

From Dev

BuildConfigField to decide how to define the member (enum)

From Dev

How to define common enum in C++11?

From Dev

Scala: How to define an enum with extra attributes?

From Java

How to define an opaque type in TypeScript?

From Dev

How to define interface for class in TypeScript?

From Dev

Swift: How to encode a hash map with enum as the key?

From Dev

How to define hash function for a class including comparable?

From Dev

Typescript: define function, which transforms an object, keeping keys

From Dev

How to convert a Swift dictionary with enum keys to an NSDictionary?

Related Related

  1. 1

    how to define and use an enum in typescript ?

  2. 2

    How to define type of an object to accept only keys from enum?

  3. 3

    How do I define a FactoryGirl factory that returns a Hash with stringed keys?

  4. 4

    How to make Typescript infer the keys of an object but define type of its value?

  5. 5

    Typescript: enum keys as parameter to function

  6. 6

    Define std::hash for enum member of class template

  7. 7

    How to change hash keys

  8. 8

    Define type as "all possibilities from an enum" in Typescript

  9. 9

    Define a list of optional keys for Typescript Record

  10. 10

    How to define a variable from an enum?

  11. 11

    How to define hash tables in Bash?

  12. 12

    How to map enum to enum with the same keys

  13. 13

    How to define Singleton in TypeScript

  14. 14

    How to define foreign keys in migrations?

  15. 15

    How to define foreign keys in migrations?

  16. 16

    How to retrieve all hash keys

  17. 17

    How to define a non-ordinal enum in Kotlin?

  18. 18

    How to define the order when sorting a string enum

  19. 19

    How to define static constants in a Java enum?

  20. 20

    How to define enum length different types

  21. 21

    BuildConfigField to decide how to define the member (enum)

  22. 22

    How to define common enum in C++11?

  23. 23

    Scala: How to define an enum with extra attributes?

  24. 24

    How to define an opaque type in TypeScript?

  25. 25

    How to define interface for class in TypeScript?

  26. 26

    Swift: How to encode a hash map with enum as the key?

  27. 27

    How to define hash function for a class including comparable?

  28. 28

    Typescript: define function, which transforms an object, keeping keys

  29. 29

    How to convert a Swift dictionary with enum keys to an NSDictionary?

HotTag

Archive