more colors

This commit is contained in:
nora 2023-07-26 20:57:10 +02:00
parent 42bc96dbce
commit 8dd7c174ee

View file

@ -74,6 +74,20 @@ class Formatter {
this.word(word, chalk.green); this.word(word, chalk.green);
} }
controlFlow(word: string) {
this.word(word, chalk.magenta);
}
instruction(kind: string) {
if (kind.includes(".")) {
const [type, op] = kind.split(".");
const word = `${this.getColor(chalk.green)(type)}.${op}`;
this.word(word);
} else {
this.word(kind);
}
}
word(word: string | number, color: (s: string) => string = identity) { word(word: string | number, color: (s: string) => string = identity) {
const last = this.wordsInSexpr.length - 1; const last = this.wordsInSexpr.length - 1;
if (this.wordsInSexpr[last] > 0 && !this.freshLinebreak) { if (this.wordsInSexpr[last] > 0 && !this.freshLinebreak) {
@ -98,6 +112,10 @@ class Formatter {
this.freshLinebreak = false; this.freshLinebreak = false;
this.wordsInSexpr.pop(); this.wordsInSexpr.pop();
} }
getColor(color: (s: string) => string): (s: string) => string {
return this.color ? color : identity;
}
} }
export function writeModuleWatToString(module: Module, color = false): string { export function writeModuleWatToString(module: Module, color = false): string {
@ -209,28 +227,28 @@ function printInstr(instr: Instr, f: Formatter) {
switch (instr.kind) { switch (instr.kind) {
case "block": case "block":
case "loop": case "loop":
f.word(instr.kind); f.controlFlow(instr.kind);
printBlockType(instr.type, f); printBlockType(instr.type, f);
f.breakIndent(); f.breakIndent();
printInstrBlock(instr.instrs, f); printInstrBlock(instr.instrs, f);
f.word("end"); f.controlFlow("end");
break; break;
case "if": case "if":
if (instr.else.length === 0) { if (instr.else.length === 0) {
f.word(instr.kind); f.controlFlow(instr.kind);
printBlockType(instr.type, f); printBlockType(instr.type, f);
f.breakIndent(); f.breakIndent();
printInstrBlock(instr.then, f); printInstrBlock(instr.then, f);
f.word("end"); f.controlFlow("end");
} else { } else {
f.word(instr.kind); f.controlFlow(instr.kind);
printBlockType(instr.type, f); printBlockType(instr.type, f);
f.breakIndent(); f.breakIndent();
printInstrBlock(instr.then, f); printInstrBlock(instr.then, f);
f.word("else"); f.controlFlow("else");
f.breakIndent(); f.breakIndent();
printInstrBlock(instr.else, f); printInstrBlock(instr.else, f);
f.word("end"); f.controlFlow("end");
} }
break; break;
case "unreachable": case "unreachable":
@ -388,24 +406,24 @@ function printInstr(instr: Instr, f: Formatter) {
case "i64.extend8_s": case "i64.extend8_s":
case "i64.extend16_s": case "i64.extend16_s":
case "i64.extend32_s": case "i64.extend32_s":
f.word(instr.kind); f.instruction(instr.kind);
break; break;
case "br": case "br":
case "br_if": case "br_if":
f.word(instr.kind); f.controlFlow(instr.kind);
f.word(instr.label); f.word(instr.label);
break; break;
case "br_table": case "br_table":
f.word(instr.kind); f.controlFlow(instr.kind);
instr.labels.forEach((label) => f.word(label)); instr.labels.forEach((label) => f.word(label));
f.word(instr.label); f.word(instr.label);
break; break;
case "call": case "call":
f.word(instr.kind); f.controlFlow(instr.kind);
f.word(instr.func); f.word(instr.func);
break; break;
case "call_indirect": case "call_indirect":
f.word(instr.kind); f.controlFlow(instr.kind);
f.word(instr.table); f.word(instr.table);
f.word(instr.type); f.word(instr.type);
break; break;
@ -428,7 +446,7 @@ function printInstr(instr: Instr, f: Formatter) {
case "elem.drop": case "elem.drop":
case "memory.init": case "memory.init":
case "data.drop": case "data.drop":
f.word(instr.kind); f.instruction(instr.kind);
f.word(instr.imm); f.word(instr.imm);
break; break;
case "select": case "select":
@ -437,7 +455,7 @@ function printInstr(instr: Instr, f: Formatter) {
break; break;
case "table.copy": case "table.copy":
case "table.init": case "table.init":
f.word(instr.kind); f.instruction(instr.kind);
f.word(instr.imm1); f.word(instr.imm1);
f.word(instr.imm2); f.word(instr.imm2);
break; break;
@ -466,7 +484,7 @@ function printInstr(instr: Instr, f: Formatter) {
case "i64.store32": case "i64.store32":
case "v128.load": case "v128.load":
case "v128.store": case "v128.store":
f.word(instr.kind); f.instruction(instr.kind);
printMemarg(instr.imm, f); printMemarg(instr.imm, f);
break; break;
} }