我正在用TypeScript做一个RAM机器模拟器,所以我做了一个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插件工作,如果它有帮助的话。
定义[]
内部的对象键
var valid_operands = {
[InsType.LOAD]: [OpType.NUM_LITERAL, OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
[InsType.STORE]: [ OpType.NUM_DIRECT, OpType.NUM_INDIRECT],
…
您可以使用Map。
enum InsType {
LOAD,
STORE
}
enum OpType {
NUM_LITERAL
}
var vo2 = new Map<InsType, [OpType]>();
vo2.set(InsType.LOAD, [OpType.NUM_LITERAL]);
您可以使用: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 });
我解决这个问题的方法是单独分配每个密钥对的值。
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]
很好。但是如果有人找到了一种方法来声明一个枚举键的哈希值,请继续并发布答案。:)