TokenQue2/src/utils/CodeChecker.ts

50 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-11-19 21:09:20 +08:00
export class Token {
constructor(public content: string, public type: string) {}
}
export class CodeChecker {
/* - 0x00: nop, 0, 0:
- 0x01: ldc, n, number: n赋值为number
- 0x10: add, n, m: 寄存器n的值加通用寄存器m的值n中
- 0x11: div, n, m: 寄存器n中的值除以通用寄存器m的值n中1
- 0x12: and, n, m: 寄存器n中的值和寄存器m中的值按位与n中
- 0x13: or, n, m: 寄存器n中的值和寄存器m中的值按位或n中
- 0x14: xor, n, m: 寄存器n中的值和寄存器m中的值按位异或n中
- 0x15: not, n, 0: 寄存器n中的值按位翻转
- 0x16: lsl, n, m: 寄存器n中的值左移m中的值
- 0x17: lsr, n, m: 寄存器n中的值右移m中的值
- 0x20: ld, n, addr: 将地址addr处的值加载到寄存器n中
- 0x21: st, n, addr: 将寄存器n中的值保存到地址addr处
- 0x23: mov, n, m: 将寄存器m中的值复制到寄存器n中
- 0x30: cp, src, dst: 将地址为src处的数据复制到dst处2
- 0x40: jp, 0, k: 无条件跳转到第k条指令
- 0x41: ltjp, n, k: 如果寄存器2中的值小于寄存器n中的值k条指令
- 0x42: gtjp, n, k: 如果寄存器2中的值大于寄存器n中的值k条指令
- 0x43: eqjp, n, k: 如果寄存器2中的值等于寄存器n中的值k条指令
- 0x50: call, 0, k: 将7个通用寄存器及下一条指令的序号推入栈中k条指令
- 0x51: ret, 0, 0: 如果栈为空7k条指令
*/
static KeyWords = ["nop","ldc","add","div","and","or","xor","not","lsl","lsr","ld","st","mov","cp","jp","ltjp","gtjp","eqjp","call","ret"];
static Registers = ["AX","BX","CX","DX","EX","FX","GX"];
static highlightCode(code: string): Token[] {
if(code.length === 0)
return [];
let res:string[] = code.split(",");
let ans:Token[] = [];
for(let key of res)
{
const temp = key.trim();
console.log(temp);
if(this.KeyWords.includes(temp))
ans.push(new Token(key, "keyword"));
else if(this.Registers.includes(temp))
ans.push(new Token(key, "register"));
else
ans.push(new Token(key, "normal"));
ans.push(new Token(",", "normal"));
}
ans.pop();
console.log(ans);
return ans;
}
}