ombre: Various additions and improvements

2fba52bcb5a1cc185b3f49e16ea9d7af037a5824
Alexis Sellier committed ago 1 parent 15e6bd09
ombre/src/lisp.rs +4 -3
12 12
pub mod evaluator;
13 13
pub mod list;
14 14
pub mod parser;
15 15
pub mod scanner;
16 16
17 -
use std::error::Error;
17 +
use std::error;
18 18
19 -
pub use evaluator::{Env, Evaluator, Expr};
19 +
pub use dynamic::{Dynamic, IsDynamic};
20 +
pub use evaluator::{Env, Error, Evaluator, Expr, Result};
20 21
pub use parser::Parser;
21 22
pub use scanner::Scanner;
22 23
23 24
/// File extension for language files.
24 25
pub const FILE_EXTENSION: &str = "scm";
27 28
pub fn parse(input: &str) -> impl Iterator<Item = parser::Result<Expr>> + '_ {
28 29
    Parser::new(Scanner::new(input))
29 30
}
30 31
31 32
/// Evaluate expressions returned by a parser. Returns the result and modified environment.
32 -
pub fn evaluate<E: Error + 'static>(
33 +
pub fn evaluate<E: error::Error + 'static>(
33 34
    exprs: impl Iterator<Item = Result<Expr, E>>,
34 35
    env: &mut evaluator::Env<'_>,
35 36
) -> evaluator::Result<Expr> {
36 37
    Evaluator::new(exprs).eval(env)
37 38
}
ombre/src/lisp/dynamic.rs +1 -9
1 1
//! Dynamic type support. Extends the language with user-defined types.
2 2
use std::any::Any;
3 3
use std::fmt;
4 4
use std::rc::Rc;
5 5
6 -
use crate::lisp::evaluator::Expr;
7 -
8 6
/// Dynamic type.
9 7
#[derive(Clone)]
10 8
pub struct Dynamic(Rc<dyn IsDynamic>);
11 9
12 10
impl Dynamic {
20 18
        Rc::as_ptr(&self.0) as *const Dynamic
21 19
    }
22 20
23 21
    /// Try to cast to a concrete type.
24 22
    pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
25 -
        self.0.as_any().downcast_ref()
23 +
        self.0.as_ref().as_any().downcast_ref()
26 24
    }
27 25
28 26
    /// Try to cast to a concrete type, giving ownership.
29 27
    /// Returns `None` if there are other references to the inner type.
30 28
    pub fn downcast<T: 'static>(self) -> Option<T> {
33 31
34 32
        Some(inner)
35 33
    }
36 34
}
37 35
38 -
impl From<Dynamic> for Expr {
39 -
    fn from(value: Dynamic) -> Self {
40 -
        Expr::Dynamic(value)
41 -
    }
42 -
}
43 -
44 36
impl PartialEq for Dynamic {
45 37
    fn eq(&self, other: &Self) -> bool {
46 38
        self.0.eq(&*other.0)
47 39
    }
48 40
}
ombre/src/lisp/evaluator.rs +16 -0
227 227
228 228
    /// Create a [`Dynamic`] expression.
229 229
    pub fn dynamic<T: IsDynamic>(value: T) -> Self {
230 230
        Self::Dynamic(Dynamic::new(value))
231 231
    }
232 +
233 +
    /// Downcast to a concrete type.
234 +
    pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
235 +
        if let Expr::Dynamic(d) = self {
236 +
            return d.downcast_ref();
237 +
        }
238 +
        None
239 +
    }
240 +
241 +
    /// Downcast to a concrete type, giving ownership.
242 +
    pub fn downcast<T: 'static>(self) -> Option<T> {
243 +
        if let Expr::Dynamic(d) = self {
244 +
            return d.downcast();
245 +
        }
246 +
        None
247 +
    }
232 248
}
233 249
234 250
impl From<f64> for Expr {
235 251
    fn from(value: f64) -> Self {
236 252
        Expr::Float(value)
ombre/src/paint/shape.rs +24 -0
130 130
    pub fn rotation(self, angle: f32, center: impl Into<Point2D>) -> Self {
131 131
        let center = center.into();
132 132
        let rotation = Rotation::new(angle, center);
133 133
        Self { rotation, ..self }
134 134
    }
135 +
136 +
    /// Convert to a [`Shape`].
137 +
    pub fn to_shape(self) -> Shape<U> {
138 +
        Shape::Rectangle(self)
139 +
    }
135 140
}
136 141
137 142
#[derive(Debug, Clone)]
138 143
pub enum Shape<U> {
139 144
    Rectangle(Rectangle<U>),
173 178
    fn from(rect: Rect<f32, U>) -> Self {
174 179
        Shape::Rectangle(Rectangle::from(rect))
175 180
    }
176 181
}
177 182
183 +
impl<U: fmt::Debug + 'static> crate::lisp::dynamic::IsDynamic for Shape<U> {
184 +
    fn eq(&self, other: &dyn crate::lisp::dynamic::IsDynamic) -> bool {
185 +
        other
186 +
            .as_any()
187 +
            .downcast_ref::<Self>()
188 +
            .is_some_and(|other| self.eq(other))
189 +
    }
190 +
}
191 +
178 192
/// Geometry can be converted into renderable vertices.
179 193
///
180 194
/// This trait allows different geometric shapes to be rendered by converting
181 195
/// them into a sequence of vertices that define their visual representation.
182 196
/// Implementations should generate the appropriate vertex data for the shape,
319 333
            radius,
320 334
            sides,
321 335
            zdepth: ZDepth::default(),
322 336
        }
323 337
    }
338 +
339 +
    /// Convert to a [`Shape`].
340 +
    pub fn to_shape(self) -> Shape<U> {
341 +
        Shape::Circle(self)
342 +
    }
324 343
}
325 344
326 345
impl ToGeometry for Circle<Local> {
327 346
    type Transformed = Circle<Screen>;
328 347
454 473
            to: t * self.to,
455 474
            zdepth: self.zdepth,
456 475
            rotation: self.rotation,
457 476
        }
458 477
    }
478 +
479 +
    /// Convert to a [`Shape`].
480 +
    pub fn to_shape(self) -> Shape<U> {
481 +
        Shape::Line(self)
482 +
    }
459 483
}
460 484
461 485
impl ToGeometry for Line<Local> {
462 486
    type Transformed = Line<Screen>;
463 487