TokenQue2/src/utils/CodeChecker.ts

50 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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: 如果栈为空则程序终止如果栈不为空则恢复7个通用寄存器的值跳转到之前存入的第k条指令
*/
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;
}
}