2023-08-24 15:34:51 -07:00
|
|
|
//! Functions implemented for language execution.
|
|
|
|
|
2024-12-10 17:51:51 -08:00
|
|
|
pub mod appearance;
|
2024-07-16 07:45:43 -05:00
|
|
|
pub mod args;
|
2024-09-14 00:10:17 -04:00
|
|
|
pub mod array;
|
2024-07-26 15:14:51 -04:00
|
|
|
pub mod assert;
|
2025-01-07 19:10:53 -08:00
|
|
|
pub mod axis_or_reference;
|
2024-06-17 12:13:19 -07:00
|
|
|
pub mod chamfer;
|
2025-04-23 21:26:09 -07:00
|
|
|
pub mod clone;
|
2025-03-19 19:26:57 -07:00
|
|
|
pub mod csg;
|
2025-03-19 15:12:27 -07:00
|
|
|
pub mod edge;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod extrude;
|
2024-03-05 11:52:45 -08:00
|
|
|
pub mod fillet;
|
2024-03-25 17:07:53 -07:00
|
|
|
pub mod helix;
|
2024-09-04 14:34:33 -07:00
|
|
|
pub mod loft;
|
2023-09-13 15:09:07 -07:00
|
|
|
pub mod math;
|
2024-09-25 16:12:18 -07:00
|
|
|
pub mod mirror;
|
2024-02-11 15:08:54 -08:00
|
|
|
pub mod patterns;
|
2024-09-04 14:34:33 -07:00
|
|
|
pub mod planes;
|
2024-03-26 19:07:16 -07:00
|
|
|
pub mod revolve;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod segment;
|
2023-11-09 09:58:20 -06:00
|
|
|
pub mod shapes;
|
2024-06-17 13:10:40 -07:00
|
|
|
pub mod shell;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod sketch;
|
2024-12-11 12:59:02 -08:00
|
|
|
pub mod sweep;
|
2025-02-26 16:45:19 -08:00
|
|
|
pub mod transform;
|
2023-08-28 14:58:24 -07:00
|
|
|
pub mod utils;
|
2023-08-24 15:34:51 -07:00
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
use anyhow::Result;
|
2024-07-29 13:18:55 -07:00
|
|
|
pub use args::Args;
|
2024-12-05 12:09:35 -05:00
|
|
|
use indexmap::IndexMap;
|
2023-11-08 20:23:59 -06:00
|
|
|
use lazy_static::lazy_static;
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2023-08-24 15:34:51 -07:00
|
|
|
use crate::{
|
2023-11-08 20:23:59 -06:00
|
|
|
docs::StdLibFn,
|
2024-07-16 07:45:43 -05:00
|
|
|
errors::KclError,
|
2025-05-13 08:29:38 +12:00
|
|
|
execution::{types::PrimitiveType, ExecState, KclValue},
|
2025-03-24 20:58:55 +13:00
|
|
|
parsing::ast::types::Name,
|
2023-08-24 15:34:51 -07:00
|
|
|
};
|
|
|
|
|
2024-09-16 15:10:33 -04:00
|
|
|
pub type StdFn = fn(
|
|
|
|
&mut ExecState,
|
|
|
|
Args,
|
|
|
|
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<KclValue, KclError>> + Send + '_>>;
|
2024-04-12 21:32:57 -07:00
|
|
|
|
2023-11-08 20:23:59 -06:00
|
|
|
lazy_static! {
|
|
|
|
static ref CORE_FNS: Vec<Box<dyn StdLibFn>> = vec![
|
2024-12-10 17:51:51 -08:00
|
|
|
Box::new(crate::std::appearance::Appearance),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::extrude::Extrude),
|
2024-11-05 14:10:35 -06:00
|
|
|
Box::new(crate::std::segment::SegEnd),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::segment::SegEndX),
|
|
|
|
Box::new(crate::std::segment::SegEndY),
|
2024-11-05 14:10:35 -06:00
|
|
|
Box::new(crate::std::segment::SegStart),
|
|
|
|
Box::new(crate::std::segment::SegStartX),
|
|
|
|
Box::new(crate::std::segment::SegStartY),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::segment::LastSegX),
|
|
|
|
Box::new(crate::std::segment::LastSegY),
|
|
|
|
Box::new(crate::std::segment::SegLen),
|
|
|
|
Box::new(crate::std::segment::SegAng),
|
2024-11-25 13:07:03 -05:00
|
|
|
Box::new(crate::std::segment::TangentToEnd),
|
2025-01-04 12:18:29 -05:00
|
|
|
Box::new(crate::std::shapes::CircleThreePoint),
|
2024-10-28 20:52:51 -04:00
|
|
|
Box::new(crate::std::shapes::Polygon),
|
2025-04-11 21:59:11 +01:00
|
|
|
Box::new(crate::std::sketch::InvoluteCircular),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::sketch::Line),
|
|
|
|
Box::new(crate::std::sketch::XLine),
|
|
|
|
Box::new(crate::std::sketch::YLine),
|
|
|
|
Box::new(crate::std::sketch::AngledLine),
|
|
|
|
Box::new(crate::std::sketch::AngledLineThatIntersects),
|
|
|
|
Box::new(crate::std::sketch::StartSketchOn),
|
2025-04-25 16:01:35 -05:00
|
|
|
Box::new(crate::std::sketch::StartProfile),
|
2024-05-21 03:44:02 -04:00
|
|
|
Box::new(crate::std::sketch::ProfileStartX),
|
|
|
|
Box::new(crate::std::sketch::ProfileStartY),
|
|
|
|
Box::new(crate::std::sketch::ProfileStart),
|
2023-11-08 20:23:59 -06:00
|
|
|
Box::new(crate::std::sketch::Close),
|
|
|
|
Box::new(crate::std::sketch::Arc),
|
|
|
|
Box::new(crate::std::sketch::TangentialArc),
|
|
|
|
Box::new(crate::std::sketch::BezierCurve),
|
2025-04-26 15:31:51 -05:00
|
|
|
Box::new(crate::std::sketch::Subtract2D),
|
2024-03-12 12:54:45 -07:00
|
|
|
Box::new(crate::std::patterns::PatternLinear2D),
|
|
|
|
Box::new(crate::std::patterns::PatternLinear3D),
|
|
|
|
Box::new(crate::std::patterns::PatternCircular2D),
|
|
|
|
Box::new(crate::std::patterns::PatternCircular3D),
|
2024-06-27 22:20:51 -05:00
|
|
|
Box::new(crate::std::patterns::PatternTransform),
|
2024-11-22 19:43:09 -06:00
|
|
|
Box::new(crate::std::patterns::PatternTransform2D),
|
2025-03-19 15:12:27 -07:00
|
|
|
Box::new(crate::std::edge::GetOppositeEdge),
|
|
|
|
Box::new(crate::std::edge::GetNextAdjacentEdge),
|
|
|
|
Box::new(crate::std::edge::GetPreviousAdjacentEdge),
|
|
|
|
Box::new(crate::std::edge::GetCommonEdge),
|
2024-12-11 12:59:02 -08:00
|
|
|
Box::new(crate::std::sweep::Sweep),
|
2024-09-04 14:34:33 -07:00
|
|
|
Box::new(crate::std::loft::Loft),
|
2024-07-26 15:14:51 -04:00
|
|
|
Box::new(crate::std::assert::Assert),
|
2025-04-22 12:44:52 -05:00
|
|
|
Box::new(crate::std::assert::AssertIs),
|
2025-02-26 16:45:19 -08:00
|
|
|
Box::new(crate::std::transform::Scale),
|
|
|
|
Box::new(crate::std::transform::Translate),
|
|
|
|
Box::new(crate::std::transform::Rotate),
|
2025-03-19 19:26:57 -07:00
|
|
|
Box::new(crate::std::csg::Intersect),
|
|
|
|
Box::new(crate::std::csg::Union),
|
|
|
|
Box::new(crate::std::csg::Subtract),
|
2023-11-08 20:23:59 -06:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn name_in_stdlib(name: &str) -> bool {
|
|
|
|
CORE_FNS.iter().any(|f| f.name() == name)
|
|
|
|
}
|
|
|
|
|
2024-06-24 22:39:04 -07:00
|
|
|
pub fn get_stdlib_fn(name: &str) -> Option<Box<dyn StdLibFn>> {
|
|
|
|
CORE_FNS.iter().find(|f| f.name() == name).cloned()
|
|
|
|
}
|
|
|
|
|
2025-02-22 20:16:29 +13:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct StdFnProps {
|
|
|
|
pub name: String,
|
|
|
|
pub deprecated: bool,
|
2025-04-03 22:44:52 +13:00
|
|
|
pub include_in_feature_tree: bool,
|
2025-02-22 20:16:29 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StdFnProps {
|
|
|
|
fn default(name: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
name: name.to_owned(),
|
|
|
|
deprecated: false,
|
2025-04-03 22:44:52 +13:00
|
|
|
include_in_feature_tree: false,
|
2025-02-22 20:16:29 +13:00
|
|
|
}
|
|
|
|
}
|
2025-04-03 22:44:52 +13:00
|
|
|
|
|
|
|
fn include_in_feature_tree(mut self) -> Self {
|
|
|
|
self.include_in_feature_tree = true;
|
|
|
|
self
|
|
|
|
}
|
2025-02-22 20:16:29 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn std_fn(path: &str, fn_name: &str) -> (crate::std::StdFn, StdFnProps) {
|
2025-02-20 19:33:21 +13:00
|
|
|
match (path, fn_name) {
|
2025-02-22 20:16:29 +13:00
|
|
|
("math", "cos") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::cos(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::cos"),
|
2025-02-22 20:16:29 +13:00
|
|
|
),
|
|
|
|
("math", "sin") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::sin(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::sin"),
|
2025-02-22 20:16:29 +13:00
|
|
|
),
|
|
|
|
("math", "tan") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::tan(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::tan"),
|
2025-02-22 20:16:29 +13:00
|
|
|
),
|
2025-04-30 15:59:19 +12:00
|
|
|
("math", "acos") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::acos(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::acos"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "asin") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::asin(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::asin"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "atan") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::atan(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::atan"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "atan2") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::atan2(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::atan2"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "sqrt") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::sqrt(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::sqrt"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
|
|
|
|
("math", "abs") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::abs(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::abs"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "rem") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::rem(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::rem"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "round") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::round(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::round"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "floor") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::floor(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::floor"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "ceil") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::ceil(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::ceil"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "min") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::min(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::min"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "max") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::max(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::max"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "pow") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::pow(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::pow"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "log") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::log(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::log"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "log2") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::log2(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::log2"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "log10") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::log10(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::log10"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
|
|
|
("math", "ln") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::ln(e, a)),
|
2025-05-13 08:29:38 +12:00
|
|
|
StdFnProps::default("std::math::ln"),
|
|
|
|
),
|
|
|
|
("math", "legLen") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::leg_length(e, a)),
|
|
|
|
StdFnProps::default("std::math::legLen"),
|
|
|
|
),
|
|
|
|
("math", "legAngX") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::leg_angle_x(e, a)),
|
|
|
|
StdFnProps::default("std::math::legAngX"),
|
|
|
|
),
|
|
|
|
("math", "legAngY") => (
|
|
|
|
|e, a| Box::pin(crate::std::math::leg_angle_y(e, a)),
|
|
|
|
StdFnProps::default("std::math::legAngY"),
|
2025-04-30 15:59:19 +12:00
|
|
|
),
|
2025-03-24 21:55:24 +13:00
|
|
|
("sketch", "circle") => (
|
|
|
|
|e, a| Box::pin(crate::std::shapes::circle(e, a)),
|
|
|
|
StdFnProps::default("std::sketch::circle"),
|
|
|
|
),
|
2025-04-03 22:44:52 +13:00
|
|
|
("prelude", "helix") => (
|
|
|
|
|e, a| Box::pin(crate::std::helix::helix(e, a)),
|
|
|
|
StdFnProps::default("std::helix").include_in_feature_tree(),
|
|
|
|
),
|
2025-05-06 16:09:59 +12:00
|
|
|
("transform", "mirror2d") => (
|
2025-04-03 22:44:52 +13:00
|
|
|
|e, a| Box::pin(crate::std::mirror::mirror_2d(e, a)),
|
2025-05-06 16:09:59 +12:00
|
|
|
StdFnProps::default("std::transform::mirror2d"),
|
2025-04-03 22:44:52 +13:00
|
|
|
),
|
2025-05-06 16:09:59 +12:00
|
|
|
("sketch", "revolve") => (
|
2025-04-03 22:44:52 +13:00
|
|
|
|e, a| Box::pin(crate::std::revolve::revolve(e, a)),
|
2025-05-06 16:09:59 +12:00
|
|
|
StdFnProps::default("std::sketch::revolve").include_in_feature_tree(),
|
2025-04-03 22:44:52 +13:00
|
|
|
),
|
2025-04-24 22:01:27 +12:00
|
|
|
("prelude", "offsetPlane") => (
|
|
|
|
|e, a| Box::pin(crate::std::planes::offset_plane(e, a)),
|
|
|
|
StdFnProps::default("std::offsetPlane").include_in_feature_tree(),
|
|
|
|
),
|
2025-04-28 14:20:38 +12:00
|
|
|
("solid", "fillet") => (
|
|
|
|
|e, a| Box::pin(crate::std::fillet::fillet(e, a)),
|
|
|
|
StdFnProps::default("std::solid::fillet").include_in_feature_tree(),
|
|
|
|
),
|
|
|
|
("solid", "chamfer") => (
|
|
|
|
|e, a| Box::pin(crate::std::chamfer::chamfer(e, a)),
|
|
|
|
StdFnProps::default("std::solid::chamfer").include_in_feature_tree(),
|
|
|
|
),
|
|
|
|
("solid", "shell") => (
|
|
|
|
|e, a| Box::pin(crate::std::shell::shell(e, a)),
|
|
|
|
StdFnProps::default("std::solid::shell").include_in_feature_tree(),
|
|
|
|
),
|
|
|
|
("solid", "hollow") => (
|
|
|
|
|e, a| Box::pin(crate::std::shell::hollow(e, a)),
|
|
|
|
StdFnProps::default("std::solid::hollow").include_in_feature_tree(),
|
|
|
|
),
|
2025-05-13 08:29:38 +12:00
|
|
|
("array", "map") => (
|
|
|
|
|e, a| Box::pin(crate::std::array::map(e, a)),
|
|
|
|
StdFnProps::default("std::array::map"),
|
|
|
|
),
|
|
|
|
("array", "reduce") => (
|
|
|
|
|e, a| Box::pin(crate::std::array::reduce(e, a)),
|
|
|
|
StdFnProps::default("std::array::reduce"),
|
|
|
|
),
|
|
|
|
("array", "push") => (
|
|
|
|
|e, a| Box::pin(crate::std::array::push(e, a)),
|
|
|
|
StdFnProps::default("std::array::push"),
|
|
|
|
),
|
|
|
|
("array", "pop") => (
|
|
|
|
|e, a| Box::pin(crate::std::array::pop(e, a)),
|
|
|
|
StdFnProps::default("std::array::pop"),
|
|
|
|
),
|
|
|
|
("prelude", "clone") => (
|
|
|
|
|e, a| Box::pin(crate::std::clone::clone(e, a)),
|
|
|
|
StdFnProps::default("std::clone").include_in_feature_tree(),
|
|
|
|
),
|
2025-02-20 19:33:21 +13:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-21 10:56:55 +13:00
|
|
|
pub(crate) fn std_ty(path: &str, fn_name: &str) -> (PrimitiveType, StdFnProps) {
|
2025-03-08 03:53:34 +13:00
|
|
|
match (path, fn_name) {
|
2025-04-22 11:00:53 +12:00
|
|
|
("types", "Sketch") => (PrimitiveType::Sketch, StdFnProps::default("std::types::Sketch")),
|
|
|
|
("types", "Solid") => (PrimitiveType::Solid, StdFnProps::default("std::types::Solid")),
|
|
|
|
("types", "Plane") => (PrimitiveType::Plane, StdFnProps::default("std::types::Plane")),
|
|
|
|
("types", "Face") => (PrimitiveType::Face, StdFnProps::default("std::types::Face")),
|
|
|
|
("types", "Helix") => (PrimitiveType::Helix, StdFnProps::default("std::types::Helix")),
|
|
|
|
("types", "Edge") => (PrimitiveType::Edge, StdFnProps::default("std::types::Edge")),
|
|
|
|
("types", "Axis2d") => (PrimitiveType::Axis2d, StdFnProps::default("std::types::Axis2d")),
|
|
|
|
("types", "Axis3d") => (PrimitiveType::Axis3d, StdFnProps::default("std::types::Axis3d")),
|
2025-03-08 03:53:34 +13:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:41:04 -07:00
|
|
|
pub struct StdLib {
|
2024-12-05 12:09:35 -05:00
|
|
|
pub fns: IndexMap<String, Box<dyn StdLibFn>>,
|
2023-11-08 20:23:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for StdLib {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-12-05 17:59:37 -06:00
|
|
|
f.debug_struct("StdLib").field("fns.len()", &self.fns.len()).finish()
|
2023-11-08 20:23:59 -06:00
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StdLib {
|
|
|
|
pub fn new() -> Self {
|
2023-11-08 20:23:59 -06:00
|
|
|
let fns = CORE_FNS
|
|
|
|
.clone()
|
2023-11-07 12:12:18 -06:00
|
|
|
.into_iter()
|
|
|
|
.map(|internal_fn| (internal_fn.name(), internal_fn))
|
|
|
|
.collect();
|
2023-08-25 13:41:04 -07:00
|
|
|
|
2024-12-05 17:59:37 -06:00
|
|
|
Self { fns }
|
2023-09-05 16:02:27 -07:00
|
|
|
}
|
|
|
|
|
2024-03-01 14:23:30 -08:00
|
|
|
// Get the combined hashmaps.
|
2024-12-05 12:09:35 -05:00
|
|
|
pub fn combined(&self) -> IndexMap<String, Box<dyn StdLibFn>> {
|
2024-12-05 17:59:37 -06:00
|
|
|
self.fns.clone()
|
2024-03-01 14:23:30 -08:00
|
|
|
}
|
|
|
|
|
2023-11-08 20:23:59 -06:00
|
|
|
pub fn get(&self, name: &str) -> Option<Box<dyn StdLibFn>> {
|
2023-09-05 16:02:27 -07:00
|
|
|
self.fns.get(name).cloned()
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
2023-11-09 09:58:20 -06:00
|
|
|
|
2025-03-24 20:58:55 +13:00
|
|
|
pub fn get_either(&self, name: &Name) -> FunctionKind {
|
|
|
|
if let Some(name) = name.local_ident() {
|
|
|
|
if let Some(f) = self.get(name.inner) {
|
|
|
|
return FunctionKind::Core(f);
|
|
|
|
}
|
2023-11-09 09:58:20 -06:00
|
|
|
}
|
2025-03-24 20:58:55 +13:00
|
|
|
|
|
|
|
FunctionKind::UserDefined
|
2023-11-09 09:58:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains_key(&self, key: &str) -> bool {
|
2024-12-05 17:59:37 -06:00
|
|
|
self.fns.contains_key(key)
|
2023-11-09 09:58:20 -06:00
|
|
|
}
|
2023-08-25 13:41:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for StdLib {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
2023-08-24 15:34:51 -07:00
|
|
|
}
|
|
|
|
|
Remove just one enum (#1096)
# Problem
This is my proposal for fixing #1107 . I've only done it for one stdlib function, `tangentialArcTo` -- if y'all like it, I'll apply this idea to the rest of the stdlib.
Previously, if users want to put a tag on the arc, the function's parameters change type.
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is object
tangentialArcTo({to: [x, y], tag: "myTag"}, %)
```
# Solution
My proposal in #1006 is that KCL should have optional values. This means we can change the stdlib `tangentialArcTo` function to use them. In this PR, the calls are now like
```
// Tag missing: first param is array
tangentialArcTo([x, y], %)
// Tag present: first param is array still, but we now pass a tag at the end.
tangentialArcTo([x, y], %, "myTag")
```
This adds an "option" type to KCL typesystem, but it's not really revealed to users (no KCL types are revealed to users right now, they write untyped code and only interact with types when they get type errors upon executing programs). Also adds a None type, which is the default case of the Optional enum.
2023-12-18 23:49:32 -06:00
|
|
|
#[derive(Debug)]
|
2023-11-09 09:58:20 -06:00
|
|
|
pub enum FunctionKind {
|
|
|
|
Core(Box<dyn StdLibFn>),
|
|
|
|
UserDefined,
|
|
|
|
}
|
|
|
|
|
2025-03-31 15:28:15 -04:00
|
|
|
/// The default tolerance for modeling commands in [`kittycad_modeling_cmds::length_unit::LengthUnit`].
|
|
|
|
const DEFAULT_TOLERANCE: f64 = 0.0000001;
|