如何在Typescript中使用枚举键定义哈希

时间:2016-06-26 12:08:48

标签: hash typescript enums

我在TypeScript中创建了一个RAM Machine模拟器,所以我创建了一个RAM可以拥有的指令类型的枚举:

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.
}

我必须确保每条指令都有一个有效的操作数类型。 我定义有效操作数的方法是这样的:

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],
}

但我发现TypeScript'编译器'并不关心我在键值中的含义 - 我可以将LOAD:更改为LOADXYZ:并且它不会引起注意。

此外,当我尝试将其更改为:

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

警告':' expected at line XX col YY(那些是.的位置)。我正在使用Atom TypeScript插件工作,如果有帮助的话。任何帮助表示赞赏。

4 个答案:

答案 0 :(得分:8)

我解决它的方法是分别分配每个密钥对值。

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]

工作正常。但是如果有人找到了用枚举键声明哈希的方法,请继续并发布答案。 :)

答案 1 :(得分:7)

您可以使用Map

enum InsType {
    LOAD,
    STORE
}

enum OpType {
    NUM_LITERAL
}

var vo2 = new Map<InsType, [OpType]>();
vo2.set(InsType.LOAD, [OpType.NUM_LITERAL]);

答案 2 :(得分:2)

您可以使用:key in Enum

export let MyHashMap: { [key in KeyType]: IMyObject }

苏:

export enum KeyType {
    A = 'A',
    B = 'B'
}

export interface IMyObject {
    type: KeyType,
    title: string
}

export const MyObject: IMyObject[] = [
    { type: KeyType.A, title: 'my a' },
    { type: KeyType.B, title: 'my b' }
]

//this is the code you want
export let MyHashMap: { [key in KeyType]: IMyObject }


MyHashMap = MyObject.reduce((map, obj) => {
    map[obj.type] = obj;
    return map;
}, {} as { [key in KeyType]: IMyObject });

答案 3 :(得分:0)

[]

内定义对象键
var valid_operands = {
  [InsType.LOAD]:   [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
  [InsType.STORE]:  [                    OpType.NUM_DIRECT, OpType.NUM_INDIRECT],

....