From ee13c30d1075d1a64b0d10ad5fa227c009b71b87 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 10 Aug 2023 10:59:51 -0400 Subject: [PATCH] [rust] Sketch of ReactiveIR (prev ReactiveFunction) Initial data types for `forget_reactive_ir`, which is the Rust analogue of `ReactiveFunction` in the TS compiler. I'm renaming here for clarity, though naming suggestions are very welcome! --- compiler/forget/Cargo.lock | 7 ++ compiler/forget/Cargo.toml | 1 + .../crates/forget_reactive_ir/Cargo.toml | 16 +++++ .../crates/forget_reactive_ir/README.md | 6 ++ .../crates/forget_reactive_ir/src/lib.rs | 65 +++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 compiler/forget/crates/forget_reactive_ir/Cargo.toml create mode 100644 compiler/forget/crates/forget_reactive_ir/README.md create mode 100644 compiler/forget/crates/forget_reactive_ir/src/lib.rs diff --git a/compiler/forget/Cargo.lock b/compiler/forget/Cargo.lock index d5a1ca5b0f..89985c4388 100644 --- a/compiler/forget/Cargo.lock +++ b/compiler/forget/Cargo.lock @@ -271,6 +271,13 @@ dependencies = [ "thiserror", ] +[[package]] +name = "forget_reactive_ir" +version = "0.1.0" +dependencies = [ + "forget_hir", +] + [[package]] name = "forget_semantic_analysis" version = "0.1.0" diff --git a/compiler/forget/Cargo.toml b/compiler/forget/Cargo.toml index 6bc241340d..ce3b85445c 100644 --- a/compiler/forget/Cargo.toml +++ b/compiler/forget/Cargo.toml @@ -21,6 +21,7 @@ forget_fixtures = { path = "crates/forget_fixtures" } forget_hermes_parser = { path = "crates/forget_hermes_parser" } forget_hir = { path = "crates/forget_hir" } forget_optimization = { path = "crates/forget_optimization" } +forget_reactive_ir = { path = "crates/forget_reactive_ir" } forget_semantic_analysis = { path = "crates/forget_semantic_analysis" } forget_ssa = { path = "crates/forget_ssa" } forget_utils = { path = "crates/forget_utils" } diff --git a/compiler/forget/crates/forget_reactive_ir/Cargo.toml b/compiler/forget/crates/forget_reactive_ir/Cargo.toml new file mode 100644 index 0000000000..ca551d4266 --- /dev/null +++ b/compiler/forget/crates/forget_reactive_ir/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "forget_reactive_ir" +version = "0.1.0" +publish = false +authors.workspace = true +description.workspace = true +edition.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +repository.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +forget_hir = { workspace = true } \ No newline at end of file diff --git a/compiler/forget/crates/forget_reactive_ir/README.md b/compiler/forget/crates/forget_reactive_ir/README.md new file mode 100644 index 0000000000..28abb02bfc --- /dev/null +++ b/compiler/forget/crates/forget_reactive_ir/README.md @@ -0,0 +1,6 @@ +# forget_reactive_ir + +The Reactive IR is an intermediate representation used to encode reactivity information, used for later stages of analysis focused on memoization/reactivity. + +Unlike HIR's "flat" control-flow-graph style representation, Reactive IR is tree-shaped. Blocks can contain HIR instructions as well as additional nested +statements (used for all expressions that have control-flow semantics), and terminals such as if or for have blocks rather than pointing to them indirectly by block id. diff --git a/compiler/forget/crates/forget_reactive_ir/src/lib.rs b/compiler/forget/crates/forget_reactive_ir/src/lib.rs new file mode 100644 index 0000000000..43540c4e6c --- /dev/null +++ b/compiler/forget/crates/forget_reactive_ir/src/lib.rs @@ -0,0 +1,65 @@ +use forget_hir::{IdentifierOperand, Instruction, InstructionId, ReactiveScope}; + +#[derive(Debug)] +pub struct ReactiveFunction { + pub id: Option, + pub body: Block, + pub params: Vec, + pub is_async: bool, + pub is_generator: bool, +} + +#[derive(Debug)] +pub struct Block { + pub statements: Vec, +} + +#[derive(Debug)] +pub enum ReactiveStatement { + Instruction(Instruction), + ControlFlow(ControlFlow), + ReactiveBlock(ReactiveBlock), +} + +#[derive(Debug)] +pub struct ReactiveBlock { + pub scope: ReactiveScope, + pub block: Block, +} + +#[derive(Debug)] +pub struct ControlFlow { + pub id: InstructionId, + pub value: ControlFlowValue, +} + +#[derive(Debug)] +pub enum ControlFlowValue { + // Logical(LogicalInstruction), + // Sequence(SequenceInstruction), + // Ternary(TernaryInstruction), + // Optional(OptionalInstruction), + // Break(BreakInstruction), + // Continue(ContinueInstruction), + Return(ReturnInstruction), + // Throw(ThrowInstruction), + // Switch(SwitchInstruction), + // DoWhile(DoWhileInstruction), + // While(WhileInstruction), + // For(ForInstruction), + // ForOf(ForOfInstruction), + If(IfInstruction), + // Label(LabelInstruction), +} + +#[derive(Debug)] +pub struct ReturnInstruction { + pub value: IdentifierOperand, +} + +#[derive(Debug)] +pub struct IfInstruction { + pub test: IdentifierOperand, + pub consequent: Block, + pub alternate: Option, +}