Skip to content

Commit 27e57d6

Browse files
Rework list and object member accessing
1 parent e2acf68 commit 27e57d6

4 files changed

Lines changed: 204 additions & 420 deletions

File tree

src/utils/helpers.rs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use glam::*;
22
use glfw::{Key, MouseButton};
33

4-
use crate::utils::{core::*, *};
4+
use crate::utils::{core::*, Callable, Expression, Function, State, Value};
55

66
// Helper functions!
77

@@ -201,6 +201,36 @@ pub fn resolve_expression(expr: &Expression, state: &mut State) -> Value {
201201
}
202202
}
203203

204+
pub fn assign_expression(expr: &Expression, value: Value, state: &mut State, is_global: bool) -> Result<(), String> {
205+
match expr {
206+
Expression::Identifier(id) => {
207+
if is_global {
208+
state.project.global_variables.insert(id.clone(), value);
209+
} else {
210+
if state.sprite.variables.get(id).is_some() {
211+
state.sprite.set_variable(id, value);
212+
} else {
213+
state.sprite.new_variable(id, value);
214+
}
215+
}
216+
Ok(())
217+
}
218+
Expression::ListMemberAccess { list, index } => {
219+
let index_val = resolve_expression(index, state);
220+
let list = get_mut_container(list, state, is_global)?;
221+
match (list, index_val) {
222+
(Value::List(list), Value::Number(idx)) => { list[idx as usize] = value; Ok(()) }
223+
(Value::Object(obj), Value::String(s)) => {
224+
obj.insert(s.clone(), value);
225+
Ok(())
226+
}
227+
_ => Err("Invalid assignment target".into()),
228+
}
229+
}
230+
_ => Err("Invalid assignment target".into()),
231+
}
232+
}
233+
204234
pub fn evaluate_bezier(t: f32, ctrl1_y: f32, ctrl2_y: f32) -> f32 {
205235
let steps = 20;
206236
let mut closest_y = 0.0;
@@ -499,7 +529,7 @@ pub fn draw_line(start: Vec2, end: Vec2, thickness: f32, shader: &ShaderProgram,
499529
},
500530
];
501531
let indices = [0, 1, 2, 0, 2, 3];
502-
let mesh = Mesh::new(&vertices, &indices, core::DrawMode::Triangles);
532+
let mesh = Mesh::new(&vertices, &indices, DrawMode::Triangles);
503533
let mut m = Mat4::from_translation(start.extend(0.0));
504534
m = m * Mat4::from_rotation_z(angle);
505535
let texture = CPUTexture::new_filled(1, 1, [255; 4]).upload_to_gpu();
@@ -533,7 +563,7 @@ pub fn draw_rectangle(start: Vec2, end: Vec2, shader: &ShaderProgram, color: Vec
533563
},
534564
];
535565
let indices = [0, 1, 2, 0, 2, 3];
536-
let mesh = Mesh::new(&vertices, &indices, core::DrawMode::Triangles);
566+
let mesh = Mesh::new(&vertices, &indices, DrawMode::Triangles);
537567
let texture = CPUTexture::new_filled(1, 1, [255; 4]).upload_to_gpu();
538568
shader.use_program();
539569
shader.set_uniform("u_color", color);
@@ -558,7 +588,7 @@ pub fn draw_convex_polygon(xs: &Vec<f32>, ys: &Vec<f32>, shader: &ShaderProgram,
558588
})
559589
.collect();
560590
let indices = trianglulate_polygon(&vertices.iter().map(|v| v.position).collect());
561-
let mesh = Mesh::new(&vertices, &indices, core::DrawMode::Triangles);
591+
let mesh = Mesh::new(&vertices, &indices, DrawMode::Triangles);
562592
let texture = CPUTexture::new_filled(1, 1, [255; 4]).upload_to_gpu();
563593
shader.use_program();
564594
shader.set_uniform("u_color", color);
@@ -595,6 +625,44 @@ pub fn percentage_to_decibels(percentage: f32) -> f32 {
595625
}
596626

597627
// Helper functions that help other helper functions!!
628+
fn get_mut_container<'a>(
629+
expr: &'a Expression,
630+
state: &'a mut State,
631+
is_global: bool,
632+
) -> Result<&'a mut Value, String> {
633+
match expr {
634+
Expression::Identifier(id) => {
635+
if is_global {
636+
state.project.global_variables.get_mut(id)
637+
.ok_or_else(|| format!("Global variable '{}' not found", id))
638+
} else {
639+
state.sprite.variable_mut(id, state.project, state.local_vars)
640+
.ok_or_else(|| format!("Variable '{}' not found", id))
641+
}
642+
}
643+
644+
Expression::ListMemberAccess { list, index } => {
645+
let index_val = crate::utils::resolve_expression(index, state); // resolve key first
646+
let container = get_mut_container(list, state, is_global)?; // now borrow mutable
647+
match (container, index_val) {
648+
(Value::List(list), Value::Number(idx)) => {
649+
let idx = idx as usize;
650+
if idx >= list.len() {
651+
list.resize(idx + 1, Value::Null);
652+
}
653+
Ok(&mut list[idx])
654+
}
655+
(Value::Object(obj), Value::String(s)) => {
656+
Ok(obj.entry(s).or_insert(Value::Null))
657+
}
658+
_ => Err("Invalid member access target".into()),
659+
}
660+
}
661+
662+
_ => Err("Expression is not a valid container".into()),
663+
}
664+
}
665+
598666
fn cubic_bezier(t: f32, p0: f32, p1: f32, p2: f32, p3: f32) -> f32 {
599667
let u = 1.0 - t;
600668
u * u * u * p0 + 3.0 * u * u * t * p1 + 3.0 * u * t * t * p2 + t * t * t * p3

0 commit comments

Comments
 (0)