|
| 1 | +import { useState } from "react"; |
| 2 | +import type { CompileResult } from "./types"; |
| 3 | +import { AstView } from "../visualization/AstView"; |
| 4 | +import { IrView } from "../visualization/IrView"; |
| 5 | +import { CfgView } from "../visualization/CfgView"; |
| 6 | +import { BytecodeView } from "../visualization/BytecodeView"; |
| 7 | +import { ErrorView } from "./ErrorView"; |
| 8 | +import type { SourceRange } from "../visualization/debugUtils"; |
| 9 | +import "./CompilerOutput.css"; |
| 10 | + |
| 11 | +interface CompilerOutputProps { |
| 12 | + result: CompileResult; |
| 13 | + onOpcodeHover?: (ranges: SourceRange[]) => void; |
| 14 | +} |
| 15 | + |
| 16 | +type TabType = "ast" | "ir" | "cfg" | "bytecode" | "error"; |
| 17 | + |
| 18 | +export function CompilerOutput({ result, onOpcodeHover }: CompilerOutputProps) { |
| 19 | + const [activeTab, setActiveTab] = useState<TabType>( |
| 20 | + result.success ? "ast" : "error", |
| 21 | + ); |
| 22 | + |
| 23 | + if (!result.success) { |
| 24 | + return <ErrorView error={result.error} warnings={result.warnings} />; |
| 25 | + } |
| 26 | + |
| 27 | + const tabs: { id: TabType; label: string; disabled?: boolean }[] = [ |
| 28 | + { id: "ast", label: "AST" }, |
| 29 | + { id: "ir", label: "IR" }, |
| 30 | + { id: "cfg", label: "CFG" }, |
| 31 | + { id: "bytecode", label: "Bytecode" }, |
| 32 | + ]; |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="compiler-output"> |
| 36 | + <div className="output-tabs"> |
| 37 | + {tabs.map((tab) => ( |
| 38 | + <button |
| 39 | + key={tab.id} |
| 40 | + className={`output-tab ${activeTab === tab.id ? "active" : ""}`} |
| 41 | + onClick={() => setActiveTab(tab.id)} |
| 42 | + disabled={tab.disabled} |
| 43 | + > |
| 44 | + {tab.label} |
| 45 | + </button> |
| 46 | + ))} |
| 47 | + </div> |
| 48 | + |
| 49 | + <div className="output-content"> |
| 50 | + {activeTab === "ast" && <AstView ast={result.ast} />} |
| 51 | + {activeTab === "ir" && ( |
| 52 | + <IrView ir={result.ir} onOpcodeHover={onOpcodeHover} /> |
| 53 | + )} |
| 54 | + {activeTab === "cfg" && <CfgView ir={result.ir} />} |
| 55 | + {activeTab === "bytecode" && ( |
| 56 | + <BytecodeView |
| 57 | + bytecode={result.bytecode} |
| 58 | + onOpcodeHover={onOpcodeHover} |
| 59 | + /> |
| 60 | + )} |
| 61 | + </div> |
| 62 | + |
| 63 | + {result.warnings.length > 0 && ( |
| 64 | + <div className="output-warnings"> |
| 65 | + <h3>Warnings:</h3> |
| 66 | + <ul> |
| 67 | + {result.warnings.map((warning, i) => ( |
| 68 | + <li key={i}>{warning}</li> |
| 69 | + ))} |
| 70 | + </ul> |
| 71 | + </div> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + ); |
| 75 | +} |
0 commit comments