@samitouri / QOS-React-2 / commits / ee13c30d10

[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!

Joe Savona committed Aug 10, 2023 at 10:59 UTC ee13c30d1075d1a64b0d10ad5fa227c009b71b87
5 files changed +95
compiler/forget/Cargo.lock
+7
@@ -271,6 +271,13 @@ dependencies = [
271 "thiserror",
272 ]
273
274 +[[package]]
275 +name = "forget_reactive_ir"
276 +version = "0.1.0"
277 +dependencies = [
278 + "forget_hir",
279 +]
280 +
281 [[package]]
282 name = "forget_semantic_analysis"
283 version = "0.1.0"
compiler/forget/Cargo.toml
+1
@@ -21,6 +21,7 @@ forget_fixtures = { path = "crates/forget_fixtures" }
21 forget_hermes_parser = { path = "crates/forget_hermes_parser" }
22 forget_hir = { path = "crates/forget_hir" }
23 forget_optimization = { path = "crates/forget_optimization" }
24 +forget_reactive_ir = { path = "crates/forget_reactive_ir" }
25 forget_semantic_analysis = { path = "crates/forget_semantic_analysis" }
26 forget_ssa = { path = "crates/forget_ssa" }
27 forget_utils = { path = "crates/forget_utils" }
compiler/forget/crates/forget_reactive_ir/Cargo.toml new
+16
@@ -0,0 +1,16 @@
1 +[package]
2 +name = "forget_reactive_ir"
3 +version = "0.1.0"
4 +publish = false
5 +authors.workspace = true
6 +description.workspace = true
7 +edition.workspace = true
8 +homepage.workspace = true
9 +keywords.workspace = true
10 +license.workspace = true
11 +repository.workspace = true
12 +
13 +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
14 +
15 +[dependencies]
16 +forget_hir = { workspace = true }
\ No newline at end of file
compiler/forget/crates/forget_reactive_ir/README.md new
+6
@@ -0,0 +1,6 @@
1 +# forget_reactive_ir
2 +
3 +The Reactive IR is an intermediate representation used to encode reactivity information, used for later stages of analysis focused on memoization/reactivity.
4 +
5 +Unlike HIR's "flat" control-flow-graph style representation, Reactive IR is tree-shaped. Blocks can contain HIR instructions as well as additional nested
6 +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.
compiler/forget/crates/forget_reactive_ir/src/lib.rs new
+65
@@ -0,0 +1,65 @@
1 +use forget_hir::{IdentifierOperand, Instruction, InstructionId, ReactiveScope};
2 +
3 +#[derive(Debug)]
4 +pub struct ReactiveFunction {
5 + pub id: Option<String>,
6 + pub body: Block,
7 + pub params: Vec<IdentifierOperand>,
8 + pub is_async: bool,
9 + pub is_generator: bool,
10 +}
11 +
12 +#[derive(Debug)]
13 +pub struct Block {
14 + pub statements: Vec<ReactiveStatement>,
15 +}
16 +
17 +#[derive(Debug)]
18 +pub enum ReactiveStatement {
19 + Instruction(Instruction),
20 + ControlFlow(ControlFlow),
21 + ReactiveBlock(ReactiveBlock),
22 +}
23 +
24 +#[derive(Debug)]
25 +pub struct ReactiveBlock {
26 + pub scope: ReactiveScope,
27 + pub block: Block,
28 +}
29 +
30 +#[derive(Debug)]
31 +pub struct ControlFlow {
32 + pub id: InstructionId,
33 + pub value: ControlFlowValue,
34 +}
35 +
36 +#[derive(Debug)]
37 +pub enum ControlFlowValue {
38 + // Logical(LogicalInstruction),
39 + // Sequence(SequenceInstruction),
40 + // Ternary(TernaryInstruction),
41 + // Optional(OptionalInstruction),
42 + // Break(BreakInstruction),
43 + // Continue(ContinueInstruction),
44 + Return(ReturnInstruction),
45 + // Throw(ThrowInstruction),
46 + // Switch(SwitchInstruction),
47 + // DoWhile(DoWhileInstruction),
48 + // While(WhileInstruction),
49 + // For(ForInstruction),
50 + // ForOf(ForOfInstruction),
51 + If(IfInstruction),
52 + // Label(LabelInstruction),
53 +}
54 +
55 +#[derive(Debug)]
56 +pub struct ReturnInstruction {
57 + pub value: IdentifierOperand,
58 +}
59 +
60 +#[derive(Debug)]
61 +pub struct IfInstruction {
62 + pub test: IdentifierOperand,
63 + pub consequent: Block,
64 + pub alternate: Option<Block>,
65 +}