Kwargs: leg helpers (#6459)
legLen, legAngX, legAngY moved to keyword arguments
This commit is contained in:
@ -1277,7 +1277,7 @@ const part001 = startSketchOn(XY)
|
||||
|> line(end = [3, 4], tag = $seg01)
|
||||
|> line(end = [
|
||||
min(segLen(seg01), myVar),
|
||||
-legLen(segLen(seg01), myVar)
|
||||
-legLen(hypotenuse = segLen(seg01), leg = myVar)
|
||||
])
|
||||
"#;
|
||||
|
||||
@ -1292,7 +1292,7 @@ const part001 = startSketchOn(XY)
|
||||
|> line(end = [3, 4], tag = $seg01)
|
||||
|> line(end = [
|
||||
min(segLen(seg01), myVar),
|
||||
legLen(segLen(seg01), myVar)
|
||||
legLen(hypotenuse = segLen(seg01), leg = myVar)
|
||||
])
|
||||
"#;
|
||||
|
||||
@ -1684,7 +1684,7 @@ let shape = layer() |> patternTransform(instances = 10, transform = transform)
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_math_execute_with_functions() {
|
||||
let ast = r#"const myVar = 2 + min(100, -1 + legLen(5, 3))"#;
|
||||
let ast = r#"const myVar = 2 + min(100, -1 + legLen(hypotenuse = 5, leg = 3))"#;
|
||||
let result = parse_execute(ast).await.unwrap();
|
||||
assert_eq!(
|
||||
5.0,
|
||||
|
@ -615,22 +615,6 @@ impl Args {
|
||||
Ok(numbers)
|
||||
}
|
||||
|
||||
pub(crate) fn get_hypotenuse_leg(&self) -> Result<(f64, f64, NumericType), KclError> {
|
||||
let numbers = self.get_number_array_with_types()?;
|
||||
|
||||
if numbers.len() != 2 {
|
||||
return Err(KclError::Type(KclErrorDetails {
|
||||
message: format!("Expected a number array of length 2, found `{:?}`", numbers),
|
||||
source_ranges: vec![self.source_range],
|
||||
}));
|
||||
}
|
||||
|
||||
let mut numbers = numbers.into_iter();
|
||||
let a = numbers.next().unwrap();
|
||||
let b = numbers.next().unwrap();
|
||||
Ok(NumericType::combine_eq_coerce(a, b))
|
||||
}
|
||||
|
||||
pub(crate) fn get_sketches(&self, exec_state: &mut ExecState) -> Result<(Vec<Sketch>, Sketch), KclError> {
|
||||
let Some(arg0) = self.args.first() else {
|
||||
return Err(KclError::Semantic(KclErrorDetails {
|
||||
|
@ -30,6 +30,7 @@ pub mod utils;
|
||||
|
||||
use anyhow::Result;
|
||||
pub use args::Args;
|
||||
use args::TyF64;
|
||||
use indexmap::IndexMap;
|
||||
use kcl_derive_docs::stdlib;
|
||||
use lazy_static::lazy_static;
|
||||
@ -40,7 +41,10 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::{
|
||||
docs::StdLibFn,
|
||||
errors::KclError,
|
||||
execution::{types::PrimitiveType, ExecState, KclValue},
|
||||
execution::{
|
||||
types::{NumericType, PrimitiveType, RuntimeType, UnitAngle, UnitType},
|
||||
ExecState, KclValue,
|
||||
},
|
||||
parsing::ast::types::Name,
|
||||
};
|
||||
|
||||
@ -287,8 +291,10 @@ pub enum FunctionKind {
|
||||
const DEFAULT_TOLERANCE: f64 = 0.0000001;
|
||||
|
||||
/// Compute the length of the given leg.
|
||||
pub async fn leg_length(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (hypotenuse, leg, ty) = args.get_hypotenuse_leg()?;
|
||||
pub async fn leg_length(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let hypotenuse: TyF64 = args.get_kw_arg_typed("hypotenuse", &RuntimeType::length(), exec_state)?;
|
||||
let leg: TyF64 = args.get_kw_arg_typed("leg", &RuntimeType::length(), exec_state)?;
|
||||
let (hypotenuse, leg, ty) = NumericType::combine_eq_coerce(hypotenuse, leg);
|
||||
let result = inner_leg_length(hypotenuse, leg);
|
||||
Ok(KclValue::from_number_with_type(result, ty, vec![args.into()]))
|
||||
}
|
||||
@ -296,10 +302,16 @@ pub async fn leg_length(_exec_state: &mut ExecState, args: Args) -> Result<KclVa
|
||||
/// Compute the length of the given leg.
|
||||
///
|
||||
/// ```no_run
|
||||
/// legLen(5, 3)
|
||||
/// legLen(hypotenuse = 5, leg = 3)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "legLen",
|
||||
keywords = true,
|
||||
unlabeled_first = false,
|
||||
args = {
|
||||
hypotenuse = { docs = "The length of the triangle's hypotenuse" },
|
||||
leg = { docs = "The length of one of the triangle's legs (i.e. non-hypotenuse side)" },
|
||||
},
|
||||
tags = ["utilities"],
|
||||
}]
|
||||
fn inner_leg_length(hypotenuse: f64, leg: f64) -> f64 {
|
||||
@ -307,19 +319,31 @@ fn inner_leg_length(hypotenuse: f64, leg: f64) -> f64 {
|
||||
}
|
||||
|
||||
/// Compute the angle of the given leg for x.
|
||||
pub async fn leg_angle_x(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (hypotenuse, leg, ty) = args.get_hypotenuse_leg()?;
|
||||
pub async fn leg_angle_x(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let hypotenuse: TyF64 = args.get_kw_arg_typed("hypotenuse", &RuntimeType::length(), exec_state)?;
|
||||
let leg: TyF64 = args.get_kw_arg_typed("leg", &RuntimeType::length(), exec_state)?;
|
||||
let (hypotenuse, leg, _ty) = NumericType::combine_eq_coerce(hypotenuse, leg);
|
||||
let result = inner_leg_angle_x(hypotenuse, leg);
|
||||
Ok(KclValue::from_number_with_type(result, ty, vec![args.into()]))
|
||||
Ok(KclValue::from_number_with_type(
|
||||
result,
|
||||
NumericType::Known(UnitType::Angle(UnitAngle::Degrees)),
|
||||
vec![args.into()],
|
||||
))
|
||||
}
|
||||
|
||||
/// Compute the angle of the given leg for x.
|
||||
///
|
||||
/// ```no_run
|
||||
/// legAngX(5, 3)
|
||||
/// legAngX(hypotenuse = 5, leg = 3)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "legAngX",
|
||||
keywords = true,
|
||||
unlabeled_first = false,
|
||||
args = {
|
||||
hypotenuse = { docs = "The length of the triangle's hypotenuse" },
|
||||
leg = { docs = "The length of one of the triangle's legs (i.e. non-hypotenuse side)" },
|
||||
},
|
||||
tags = ["utilities"],
|
||||
}]
|
||||
fn inner_leg_angle_x(hypotenuse: f64, leg: f64) -> f64 {
|
||||
@ -327,19 +351,31 @@ fn inner_leg_angle_x(hypotenuse: f64, leg: f64) -> f64 {
|
||||
}
|
||||
|
||||
/// Compute the angle of the given leg for y.
|
||||
pub async fn leg_angle_y(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (hypotenuse, leg, ty) = args.get_hypotenuse_leg()?;
|
||||
pub async fn leg_angle_y(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let hypotenuse: TyF64 = args.get_kw_arg_typed("hypotenuse", &RuntimeType::length(), exec_state)?;
|
||||
let leg: TyF64 = args.get_kw_arg_typed("leg", &RuntimeType::length(), exec_state)?;
|
||||
let (hypotenuse, leg, _ty) = NumericType::combine_eq_coerce(hypotenuse, leg);
|
||||
let result = inner_leg_angle_y(hypotenuse, leg);
|
||||
Ok(KclValue::from_number_with_type(result, ty, vec![args.into()]))
|
||||
Ok(KclValue::from_number_with_type(
|
||||
result,
|
||||
NumericType::Known(UnitType::Angle(UnitAngle::Degrees)),
|
||||
vec![args.into()],
|
||||
))
|
||||
}
|
||||
|
||||
/// Compute the angle of the given leg for y.
|
||||
///
|
||||
/// ```no_run
|
||||
/// legAngY(5, 3)
|
||||
/// legAngY(hypotenuse = 5, leg = 3)
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "legAngY",
|
||||
keywords = true,
|
||||
unlabeled_first = false,
|
||||
args = {
|
||||
hypotenuse = { docs = "The length of the triangle's hypotenuse" },
|
||||
leg = { docs = "The length of one of the triangle's legs (i.e. non-hypotenuse side)" },
|
||||
},
|
||||
tags = ["utilities"],
|
||||
}]
|
||||
fn inner_leg_angle_y(hypotenuse: f64, leg: f64) -> f64 {
|
||||
|
Reference in New Issue
Block a user