From 9d0a24d4cdd2efa74af2e35672d14e745044fa46 Mon Sep 17 00:00:00 2001 From: Snorre Date: Wed, 19 Feb 2025 21:38:32 +0100 Subject: [PATCH] broken as hell --- src/app/bin.rs | 14 ++++---- src/app/gui/document/line.rs | 4 +-- src/lib/node/if_else.rs | 63 ++++++++++++++++++++++++++++++++++++ src/lib/node/mod.rs | 5 +++ 4 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/lib/node/if_else.rs diff --git a/src/app/bin.rs b/src/app/bin.rs index 72bf3fe..37be1ad 100644 --- a/src/app/bin.rs +++ b/src/app/bin.rs @@ -1,5 +1,7 @@ use std::{ - cell::RefCell, collections::{HashMap, HashSet}, rc::Rc + cell::RefCell, + collections::{HashMap, HashSet}, + rc::Rc, }; pub mod gui; @@ -16,7 +18,7 @@ use libopenbirch::node::{add::Add, constant::Constant, divide::Divide, multiply: use raylib::{ RaylibHandle, RaylibThread, color::Color, - ffi::{MouseButton, TextLength, Vector2}, + ffi::{LoadCodepoints, MouseButton, TextLength, TextureFilter, Vector2}, prelude::{Font, RaylibDraw, RaylibDrawHandle}, text::RaylibFont, texture::Texture2D, @@ -28,7 +30,7 @@ struct FontStore { fonts_to_be_loaded: HashSet, } -static CHAR_SET: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ1234567890(){}[]./,?><|\\=+-_*&^%$#@!~`·×≤≥±∞≈∝≠∅∈∉∩∪⊂⊃⊆⊇¬∧∨∃∀⇒⇔→↔↑↓ℵ"; +static CHAR_SET: &'static str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ1234567890(){}[]./,:;'\"?><|\\=+-_*&^%$#@!~`·×≤≥±∞≈∝≠∅∈∉∩∪⊂⊃⊆⊇¬∧∨∃∀⇒⇔→↔↑↓ℵ \t\n\r"; impl FontStore { // const SIZE_PARAGRAPH: f32 = 20.; @@ -71,7 +73,7 @@ impl FontStore { for size in self.fonts_to_be_loaded.clone() { assert!(size != 0, "HOW IS THIS ZERO?"); println!("Loading {} in {size}px", self.font); - let font = Rc::new(handle.load_font_ex(&thread, &self.font, size, Some(CHAR_SET))?); + let font = Rc::new(handle.load_font_ex(&thread, &self.font, size, None)?); self.fonts.insert(size, font.clone()); } self.fonts_to_be_loaded.clear(); @@ -120,7 +122,7 @@ fn main() -> Result<(), String> { let (mut rl, thread) = raylib::init() .size(640, 480) .title("Openbirch GUI") - .msaa_4x() + // .msaa_4x() .resizable() .build(); let mut clay = clay_layout::Clay::new((640., 480.).into()); @@ -138,7 +140,7 @@ fn main() -> Result<(), String> { let meth3 = Multiply::new(meth2.into(), meth.into()); gui.document.add_line(Line { - segments: vec![LineSegment::Math(Rc::new(meth3.into()), 16)], + segments: vec![LineSegment::Math("".into(), Rc::new(meth3.into()), 16)], }); let mut previous_left_button = false; diff --git a/src/app/gui/document/line.rs b/src/app/gui/document/line.rs index 1e852ce..3e1ce36 100644 --- a/src/app/gui/document/line.rs +++ b/src/app/gui/document/line.rs @@ -43,7 +43,7 @@ pub struct Theme { pub enum LineSegment { Text(String, u16), // Link(String, Link), - Math(Rc, u16), + Math(String, Rc, u16), } impl LineSegment { @@ -57,7 +57,7 @@ impl LineSegment { .color(theme.text_color) .end(), ), - LineSegment::Math(node_enum, font_size) => { + LineSegment::Math(_source, node_enum, font_size) => { draw_math_enum(node_enum.clone(), clay, theme, *font_size, state); } } diff --git a/src/lib/node/if_else.rs b/src/lib/node/if_else.rs new file mode 100644 index 0000000..2ea3e09 --- /dev/null +++ b/src/lib/node/if_else.rs @@ -0,0 +1,63 @@ +use std::rc::Rc; + +use super::{Environment, Node, NodeEnum, Precedence}; + +#[derive(Clone, Debug, PartialEq, PartialOrd)] +pub enum Bool { + True, + False, +} + +#[derive(Clone, Debug, PartialEq, PartialOrd)] +pub enum ElseBranchEnum { + ElseIf(Rc), + Block(Vec>), +} + +#[derive(Clone, Debug, PartialEq, PartialOrd)] +pub struct IfElse { + condition: Rc, + true_branch: Vec>, + else_branch: ElseBranchEnum, +} + +impl Node for IfElse { + fn evaluate(&self, env: &mut Environment) -> Result, String> { + let condition_evaluated = self.condition.evaluate(env)?; + + if let super::NodeEnum::Bool(bool) = condition_evaluated { + } else { + return Err(format!( + "Cannot evaluate {} to a bool", + condition_evaluated.as_string(Some(env)) + )); + } + } + + fn as_string(&self, env: Option<&Environment>) -> String { + format!( + "if {} then\n{}\nelse\n{}", + self.condition.as_string(env), + self.true_branch + .iter() + .map(|x| x.as_string(env)) + .reduce(|a, b| a + "\n" + &b) + .unwrap() + .as_str(), + match &self.else_branch { + ElseBranchEnum::ElseIf(if_else) => if_else.as_string(env), + ElseBranchEnum::Block(vec) => + vec.iter() + .map(|x| x.as_string(env)) + .reduce(|a, b| a + "\n" + &b) + .unwrap() + + "\nend", + } + .as_str() + ) + } + + fn precedence(&self) -> Precedence { + Precedence::Primary + } +} diff --git a/src/lib/node/mod.rs b/src/lib/node/mod.rs index 8e94f39..ca5a2a6 100644 --- a/src/lib/node/mod.rs +++ b/src/lib/node/mod.rs @@ -8,6 +8,7 @@ use divide::Divide; use empty::Empty; use enum_dispatch::enum_dispatch; use function::Function; +use if_else::{Bool, IfElse}; use multiply::Multiply; use node_ref::NodeRef; use subtract::Subtract; @@ -26,6 +27,7 @@ mod assign; mod empty; mod function; mod call; +mod if_else; #[enum_dispatch] #[enum_dispatch(Debug, Clone, PartialEq, PartialOrd, ToString)] @@ -46,6 +48,9 @@ pub enum NodeEnum { Function, Call, + Bool, + IfElse, + // Logical // In, // Or,