From f5be703da16a460778615f149b6682f2231f4a14 Mon Sep 17 00:00:00 2001 From: Pufferfish101007 <50246616+pufferfish101007@users.noreply.github.com> Date: Sat, 30 Mar 2024 17:39:38 +0000 Subject: [PATCH] start implementing const folding --- src/ir.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/ir.rs b/src/ir.rs index 3894d70..fdc61d3 100644 --- a/src/ir.rs +++ b/src/ir.rs @@ -28,11 +28,20 @@ pub struct IrProject { pub threads: Vec, pub vars: Rc>>, pub target_names: Vec, - pub costumes: Vec>, // (name, assetName) + pub costumes: Vec>, pub steps: IndexMap<(String, String), Step, BuildHasherDefault>, pub sb3: Sb3Project, } +impl IrProject { + fn const_fold(&mut self) -> Result<(), HQError> { + for step in self.steps.values_mut() { + step.const_fold()?; + } + Ok(()) + } +} + impl TryFrom for IrProject { type Error = HQError; @@ -502,6 +511,12 @@ impl IrBlock { pub fn type_stack(&self) -> Rc>> { Rc::clone(&self.type_stack) } + + pub fn is_const(&self) -> bool { + use IrOpcode::*; + matches!(self.opcode(), math_number { .. } | math_whole_number { .. } | math_integer { .. } | math_angle { .. } | math_positive_number { .. } | text { .. }) + } + } #[derive(Clone, Debug, PartialEq)] @@ -781,6 +796,23 @@ impl Step { pub fn context(&self) -> Rc { Rc::clone(&self.context) } + pub fn const_fold(&mut self) -> Result<(), HQError> { + let mut value_stack: Vec = vec![]; + + Ok(()) + } +} + +pub union ValueUnion<'a> { + int: i32, + float: f64, + boolean: bool, + string: &'a String, +} + +pub struct IrVal<'a> { + tag: InputType, + value: ValueUnion<'a> } #[derive(Debug, Clone, PartialEq)]