From d9724c7e006fecfdd0eec955b98148ecdfad214e Mon Sep 17 00:00:00 2001 From: Jan Kassens Date: Sun, 13 Nov 2022 11:00:29 -0500 Subject: [PATCH] [playground] option to codegen output in HIR tab We might need to revisit the tabs vs. options split, but for now this just adds a checkbox toggle that outputs codegenned JS instead of HIR in the HIR tab. Open to ideas to organize this in the future... --- .../components/Editor/HIRTabContent.tsx | 26 +++++++++++++++++-- compiler/forget/src/index.ts | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx b/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx index e164fde0e4..ef6f5d3d86 100644 --- a/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx +++ b/compiler/forget/packages/playground/components/Editor/HIRTabContent.tsx @@ -9,6 +9,9 @@ import clsx from "clsx"; import { HIR } from "babel-plugin-react-forget"; import { useState } from "react"; +import generate from "@babel/generator"; +import prettier from "prettier"; +import prettierParserBabel from "prettier/parser-babel"; const { parseFunctions, @@ -20,12 +23,14 @@ const { leaveSSA, lower, printHIR, + codegen, } = HIR; export default function HIRTabContent({ source }: { source: string }) { const astFunctions = parseFunctions(source); const [flags, setFlags] = useState({ + codegen: true, eliminateRedundantPhi: true, inferReferenceEffects: true, inferMutableRanges: true, @@ -53,8 +58,17 @@ export default function HIRTabContent({ source }: { source: string }) { if (flags.leaveSSA) { leaveSSA(ir); } - const textHIR = printHIR(ir.body); - body =
{textHIR}
; + const output = flags.codegen + ? prettier.format( + generate(codegen(ir)).code.replace("\n\n", "\n"), + { + semi: true, + parser: "babel", + plugins: [prettierParserBabel], + } + ) + : printHIR(ir.body); + body =
{output}
; } catch (e: any) { body =
error: ${e.toString()}
; } @@ -77,6 +91,7 @@ type Flags = { inferReferenceEffects: boolean; inferMutableRanges: boolean; leaveSSA: boolean; + codegen: boolean; }; function CompilerFlagsEditor({ @@ -118,6 +133,13 @@ function CompilerFlagsEditor({ setFlags({ ...flags, leaveSSA }); }} /> + { + setFlags({ ...flags, codegen }); + }} + /> ); diff --git a/compiler/forget/src/index.ts b/compiler/forget/src/index.ts index 31e8f3c971..28252de910 100644 --- a/compiler/forget/src/index.ts +++ b/compiler/forget/src/index.ts @@ -32,6 +32,7 @@ import leaveSSA from "./HIR/LeaveSSA"; import traverse, { NodePath } from "@babel/traverse"; import * as t from "@babel/types"; import { parse } from "@babel/parser"; +import codegen from "./HIR/Codegen"; function parseFunctions( source: string @@ -64,6 +65,7 @@ export const HIR = { printHIR, Environment, leaveSSA, + codegen, }; export default BabelPlugin;