Skip to content

Commit

Permalink
start implementing const folding
Browse files Browse the repository at this point in the history
  • Loading branch information
pufferfish101007 committed Mar 30, 2024
1 parent ad4fac7 commit f5be703
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ pub struct IrProject {
pub threads: Vec<Thread>,
pub vars: Rc<RefCell<Vec<IrVar>>>,
pub target_names: Vec<String>,
pub costumes: Vec<Vec<IrCostume>>, // (name, assetName)
pub costumes: Vec<Vec<IrCostume>>,
pub steps: IndexMap<(String, String), Step, BuildHasherDefault<FNV1aHasher64>>,
pub sb3: Sb3Project,
}

impl IrProject {
fn const_fold(&mut self) -> Result<(), HQError> {

Check warning on line 37 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Build check

method `const_fold` is never used

Check failure on line 37 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Lint (clippy)

method `const_fold` is never used
for step in self.steps.values_mut() {
step.const_fold()?;
}
Ok(())
}
}

impl TryFrom<Sb3Project> for IrProject {
type Error = HQError;

Expand Down Expand Up @@ -502,6 +511,12 @@ impl IrBlock {
pub fn type_stack(&self) -> Rc<RefCell<Option<TypeStack>>> {
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)]
Expand Down Expand Up @@ -781,6 +796,23 @@ impl Step {
pub fn context(&self) -> Rc<ThreadContext> {
Rc::clone(&self.context)
}
pub fn const_fold(&mut self) -> Result<(), HQError> {
let mut value_stack: Vec<IrVal> = vec![];

Check warning on line 800 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Build check

unused variable: `value_stack`

Check warning on line 800 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Build check

variable does not need to be mutable

Check failure on line 800 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Lint (clippy)

unused variable: `value_stack`

Check failure on line 800 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Lint (clippy)

variable does not need to be mutable

Ok(())
}
}

pub union ValueUnion<'a> {
int: i32,

Check warning on line 807 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Build check

fields `int`, `float`, `boolean`, and `string` are never read

Check failure on line 807 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Lint (clippy)

fields `int`, `float`, `boolean`, and `string` are never read
float: f64,
boolean: bool,
string: &'a String,
}

pub struct IrVal<'a> {
tag: InputType,

Check warning on line 814 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Build check

fields `tag` and `value` are never read

Check failure on line 814 in src/ir.rs

View workflow job for this annotation

GitHub Actions / Lint (clippy)

fields `tag` and `value` are never read
value: ValueUnion<'a>
}

#[derive(Debug, Clone, PartialEq)]
Expand Down

0 comments on commit f5be703

Please sign in to comment.