WIP no default angles

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-05-16 09:43:44 +12:00
parent dd6a980915
commit 5db6564804
9 changed files with 31 additions and 97 deletions

View File

@ -6,7 +6,7 @@ use kittycad_modeling_cmds::coord::{System, KITTYCAD, OPENGL, VULKAN};
use crate::{
errors::KclErrorDetails,
execution::types::{UnitAngle, UnitLen},
execution::types::UnitLen,
parsing::ast::types::{Annotation, Expr, LiteralValue, Node, ObjectProperty},
KclError, SourceRange,
};
@ -16,7 +16,6 @@ pub(super) const SIGNIFICANT_ATTRS: [&str; 2] = [SETTINGS, NO_PRELUDE];
pub(crate) const SETTINGS: &str = "settings";
pub(crate) const SETTINGS_UNIT_LENGTH: &str = "defaultLengthUnit";
pub(crate) const SETTINGS_UNIT_ANGLE: &str = "defaultAngleUnit";
pub(crate) const SETTINGS_VERSION: &str = "kclVersion";
pub(super) const NO_PRELUDE: &str = "no_std";
@ -54,7 +53,7 @@ impl FromStr for Impl {
}
pub(crate) fn settings_completion_text() -> String {
format!("@{SETTINGS}({SETTINGS_UNIT_LENGTH} = mm, {SETTINGS_UNIT_ANGLE} = deg, {SETTINGS_VERSION} = 1.0)")
format!("@{SETTINGS}({SETTINGS_UNIT_LENGTH} = mm, {SETTINGS_VERSION} = 1.0)")
}
pub(super) fn is_significant(attr: &&Node<Annotation>) -> bool {
@ -148,16 +147,3 @@ impl UnitLen {
}
}
}
impl UnitAngle {
pub(super) fn from_str(s: &str, source_range: SourceRange) -> Result<Self, KclError> {
match s {
"deg" => Ok(UnitAngle::Degrees),
"rad" => Ok(UnitAngle::Radians),
value => Err(KclError::Semantic(KclErrorDetails {
message: format!("Unexpected value for angle units: `{value}`; expected one of `deg`, `rad`"),
source_ranges: vec![source_range],
})),
}
}
}

View File

@ -36,7 +36,7 @@ use crate::{
execution::{
cache::{CacheInformation, CacheResult},
typed_path::TypedPath,
types::{UnitAngle, UnitLen},
types::UnitLen,
},
fs::FileManager,
modules::{ModuleId, ModulePath, ModuleRepr},

View File

@ -18,7 +18,7 @@ use crate::{
memory::{ProgramMemory, Stack},
types,
types::NumericType,
EnvironmentRef, ExecOutcome, ExecutorSettings, KclValue, UnitAngle, UnitLen,
EnvironmentRef, ExecOutcome, ExecutorSettings, KclValue, UnitLen,
},
modules::{ModuleId, ModuleInfo, ModuleLoader, ModulePath, ModuleRepr, ModuleSource},
parsing::ast::types::Annotation,
@ -255,7 +255,6 @@ impl ExecState {
pub fn current_default_units(&self) -> NumericType {
NumericType::Default {
len: self.length_unit(),
angle: self.angle_unit(),
}
}
@ -263,10 +262,6 @@ impl ExecState {
self.mod_local.settings.default_length_units
}
pub fn angle_unit(&self) -> UnitAngle {
self.mod_local.settings.default_angle_units
}
pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
KclError::ImportCycle(KclErrorDetails {
message: format!(
@ -339,7 +334,6 @@ impl ModuleState {
std_path,
settings: MetaSettings {
default_length_units: Default::default(),
default_angle_units: Default::default(),
kcl_version: "0.1".to_owned(),
},
}
@ -351,7 +345,6 @@ impl ModuleState {
#[serde(rename_all = "camelCase")]
pub struct MetaSettings {
pub default_length_units: types::UnitLen,
pub default_angle_units: types::UnitAngle,
pub kcl_version: String,
}
@ -371,11 +364,6 @@ impl MetaSettings {
self.default_length_units = value;
updated_len = true;
}
annotations::SETTINGS_UNIT_ANGLE => {
let value = annotations::expect_ident(&p.inner.value)?;
let value = types::UnitAngle::from_str(value, annotation.as_source_range())?;
self.default_angle_units = value;
}
annotations::SETTINGS_VERSION => {
let value = annotations::expect_number(&p.inner.value)?;
self.kcl_version = value;
@ -385,7 +373,7 @@ impl MetaSettings {
message: format!(
"Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
annotations::SETTINGS_UNIT_LENGTH,
annotations::SETTINGS_UNIT_ANGLE
annotations::SETTINGS_VERSION
),
source_ranges: vec![annotation.as_source_range()],
}))

View File

@ -434,7 +434,7 @@ pub enum NumericType {
// Specified by the user (directly or indirectly)
Known(UnitType),
// Unspecified, using defaults
Default { len: UnitLen, angle: UnitAngle },
Default { len: UnitLen },
// Exceeded the ability of the type system to track.
Unknown,
// Type info has been explicitly cast away.
@ -445,7 +445,6 @@ impl Default for NumericType {
fn default() -> Self {
NumericType::Default {
len: UnitLen::default(),
angle: UnitAngle::default(),
}
}
}
@ -474,13 +473,6 @@ impl NumericType {
}
}
pub fn expect_default_angle(&self) -> Self {
match self {
NumericType::Default { angle, .. } => NumericType::Known(UnitType::Angle(*angle)),
_ => unreachable!(),
}
}
/// Combine two types when we expect them to be equal, erring on the side of less coercion. To be
/// precise, only adjusting one number or the other when they are of known types.
///
@ -501,8 +493,6 @@ impl NumericType {
}
(t @ Known(UnitType::Length(l1)), Default { len: l2, .. }) if l1 == l2 => (a.n, b.n, t),
(Default { len: l1, .. }, t @ Known(UnitType::Length(l2))) if l1 == l2 => (a.n, b.n, t),
(t @ Known(UnitType::Angle(a1)), Default { angle: a2, .. }) if a1 == a2 => (a.n, b.n, t),
(Default { angle: a1, .. }, t @ Known(UnitType::Angle(a2))) if a1 == a2 => (a.n, b.n, t),
_ => (a.n, b.n, Unknown),
}
@ -522,6 +512,8 @@ impl NumericType {
(at, Any) => (a.n, b.n, at),
(Any, bt) => (a.n, b.n, bt),
(Default { .. }, Known(UnitType::Angle(_))) |
(Known(UnitType::Angle(_)), Default { .. }) |
(Default { .. }, Default { .. }) | (_, Unknown) | (Unknown, _) => (a.n, b.n, Unknown),
// Known types and compatible, but needs adjustment.
@ -538,9 +530,6 @@ impl NumericType {
(t @ Known(UnitType::Length(l1)), Default { len: l2, .. }) => (a.n, l2.adjust_to(b.n, l1).0, t),
(Default { len: l1, .. }, t @ Known(UnitType::Length(l2))) => (l1.adjust_to(a.n, l2).0, b.n, t),
(t @ Known(UnitType::Angle(a1)), Default { angle: a2, .. }) => (a.n, a2.adjust_to(b.n, a1).0, t),
(Default { angle: a1, .. }, t @ Known(UnitType::Angle(a2))) => (a1.adjust_to(a.n, a2).0, b.n, t),
}
}
@ -567,14 +556,10 @@ impl NumericType {
}
(Known(UnitType::Length(l1)), Default { len: l2, .. }) if l1 == l2 || i.n == 0.0 => {}
(Known(UnitType::Angle(a1)), Default { angle: a2, .. }) if a1 == a2 || i.n == 0.0 => {}
(Default { len: l1, .. }, Known(UnitType::Length(l2))) if l1 == l2 => {
ty = Known(UnitType::Length(*l2));
}
(Default { angle: a1, .. }, Known(UnitType::Angle(a2))) if a1 == a2 => {
ty = Known(UnitType::Angle(*a2));
}
_ => return (result, Unknown),
}
@ -619,7 +604,6 @@ impl NumericType {
match suffix {
NumericSuffix::None => NumericType::Default {
len: settings.default_length_units,
angle: settings.default_angle_units,
},
NumericSuffix::Count => NumericType::Known(UnitType::Count),
NumericSuffix::Length => NumericType::Known(UnitType::Length(UnitLen::Unknown)),
@ -647,7 +631,7 @@ impl NumericType {
NumericType::Known(UnitType::Length(UnitLen::Unknown)),
)
| (
NumericType::Known(UnitType::Angle(_)) | NumericType::Default { .. },
NumericType::Known(UnitType::Angle(_)),
NumericType::Known(UnitType::Angle(UnitAngle::Unknown)),
) => true,
(Unknown, _) | (_, Unknown) => false,
@ -743,14 +727,7 @@ impl NumericType {
})
}
(Default { angle: a1, .. }, Known(UnitType::Angle(a2))) => {
let (value, ty) = a1.adjust_to(*value, *a2);
Ok(KclValue::Number {
value,
ty: Known(UnitType::Angle(ty)),
meta: meta.clone(),
})
}
(Default { .. }, Known(UnitType::Angle(_))) => Err(val.into()),
(_, _) => unreachable!(),
}
@ -2063,8 +2040,7 @@ mod test {
default
.coerce(
&NumericType::Default {
len: UnitLen::Yards,
angle: UnitAngle::default()
len: UnitLen::Yards
}
.into(),
&mut exec_state

View File

@ -7,7 +7,7 @@ use crate::{execution::types::NumericType, pretty::NumericSuffix};
pub fn human_display_number(value: f64, ty: NumericType) -> String {
match ty {
NumericType::Known(unit_type) => format!("{value}: number({unit_type})"),
NumericType::Default { len, angle } => format!("{value} (no units, defaulting to {len} or {angle})"),
NumericType::Default { len } => format!("{value} (no units, defaulting to {len})"),
NumericType::Unknown => format!("{value} (number with unknown units)"),
NumericType::Any => format!("{value} (number with any units)"),
}
@ -87,20 +87,18 @@ mod tests {
1.0,
NumericType::Default {
len: UnitLen::Mm,
angle: UnitAngle::Degrees,
}
),
"1 (no units, defaulting to mm or deg)"
"1 (no units, defaulting to mm)"
);
assert_eq!(
human_display_number(
1.0,
NumericType::Default {
len: UnitLen::Feet,
angle: UnitAngle::Radians,
}
),
"1 (no units, defaulting to ft or rad)"
"1 (no units, defaulting to ft)"
);
assert_eq!(
human_display_number(1.0, NumericType::Unknown),

View File

@ -230,10 +230,11 @@ impl Program {
pub fn change_default_units(
&self,
length_units: Option<execution::types::UnitLen>,
angle_units: Option<execution::types::UnitAngle>,
// TODO
_angle_units: Option<execution::types::UnitAngle>,
) -> Result<Self, KclError> {
Ok(Self {
ast: self.ast.change_default_units(length_units, angle_units)?,
ast: self.ast.change_default_units(length_units)?,
original_file_contents: self.original_file_contents.clone(),
})
}

View File

@ -29,7 +29,7 @@ use crate::{
errors::KclError,
execution::{
annotations,
types::{ArrayLen, UnitAngle, UnitLen},
types::{ArrayLen, UnitLen},
KclValue, Metadata, TagIdentifier,
},
parsing::{ast::digest::Digest, token::NumericSuffix, PIPE_OPERATOR},
@ -361,7 +361,6 @@ impl Node<Program> {
pub fn change_default_units(
&self,
length_units: Option<UnitLen>,
angle_units: Option<UnitAngle>,
) -> Result<Self, KclError> {
let mut new_program = self.clone();
let mut found = false;
@ -373,12 +372,6 @@ impl Node<Program> {
Expr::Name(Box::new(Name::new(&len.to_string()))),
);
}
if let Some(angle) = angle_units {
node.inner.add_or_update(
annotations::SETTINGS_UNIT_ANGLE,
Expr::Name(Box::new(Name::new(&angle.to_string()))),
);
}
// Previous source range no longer makes sense, but we want to
// preserve other things like comments.
@ -396,12 +389,6 @@ impl Node<Program> {
Expr::Name(Box::new(Name::new(&len.to_string()))),
);
}
if let Some(angle) = angle_units {
settings.inner.add_or_update(
annotations::SETTINGS_UNIT_ANGLE,
Expr::Name(Box::new(Name::new(&angle.to_string()))),
);
}
new_program.inner_attrs.push(settings);
}
@ -4247,7 +4234,7 @@ startSketchOn(XY)"#;
// Edit the ast.
let new_program = program
.change_default_units(Some(crate::execution::types::UnitLen::Mm), None)
.change_default_units(Some(crate::execution::types::UnitLen::Mm))
.unwrap();
let result = new_program.meta_settings().unwrap();
@ -4276,7 +4263,7 @@ startSketchOn(XY)
// Edit the ast.
let new_program = program
.change_default_units(Some(crate::execution::types::UnitLen::Mm), None)
.change_default_units(Some(crate::execution::types::UnitLen::Mm))
.unwrap();
let result = new_program.meta_settings().unwrap();
@ -4311,7 +4298,7 @@ startSketchOn(XY)
let program = crate::parsing::top_level_parse(code).unwrap();
let new_program = program
.change_default_units(Some(crate::execution::types::UnitLen::Cm), None)
.change_default_units(Some(crate::execution::types::UnitLen::Cm))
.unwrap();
let result = new_program.meta_settings().unwrap();

View File

@ -109,7 +109,6 @@ impl TyF64 {
pub fn to_degrees(&self) -> f64 {
let angle = match self.ty {
NumericType::Default { angle, .. } => angle,
NumericType::Known(UnitType::Angle(angle)) => angle,
_ => unreachable!(),
};
@ -121,7 +120,6 @@ impl TyF64 {
pub fn to_radians(&self) -> f64 {
let angle = match self.ty {
NumericType::Default { angle, .. } => angle,
NumericType::Known(UnitType::Angle(angle)) => angle,
_ => unreachable!(),
};

View File

@ -31,7 +31,7 @@ export PI = 3.14159265358979323846264338327950288_?
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 30,
/// angle = 30deg,
/// length = 2 * E ^ 2,
/// )
/// |> yLine(endAbsolute = 0)
@ -47,7 +47,7 @@ export E = 2.71828182845904523536028747135266250_
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 50,
/// angle = 50deg,
/// length = 10 * TAU,
/// )
/// |> yLine(endAbsolute = 0)
@ -63,7 +63,7 @@ export TAU = 6.28318530717958647692528676655900577_
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 30,
/// angle = 30deg,
/// length = 3 / cos(30deg),
/// )
/// |> yLine(endAbsolute = 0)
@ -80,7 +80,7 @@ export fn cos(@num: number(Angle)): number(Count) {}
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 50,
/// angle = 50deg,
/// length = 15 / sin(135deg),
/// )
/// |> yLine(endAbsolute = 0)
@ -97,7 +97,7 @@ export fn sin(@num: number(Angle)): number(Count) {}
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 50,
/// angle = 50deg,
/// length = 50 * tan((1/2): number(rad)),
/// )
/// |> yLine(endAbsolute = 0)
@ -185,7 +185,7 @@ export fn atan2(y: number(Length), x: number(Length)): number(rad) {}
/// ```
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> line(end = polar(angle = 30, length = 5), tag = $thing)
/// |> line(end = polar(angle = 30deg, length = 5), tag = $thing)
/// |> line(end = [0, 5])
/// |> line(end = [segEndX(thing), 0])
/// |> line(end = [-20, 10])
@ -226,7 +226,7 @@ export fn rem(
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 50,
/// angle = 50deg,
/// length = sqrt(2500),
/// )
/// |> yLine(endAbsolute = 0)
@ -312,7 +312,7 @@ export fn ceil(@input: number): number {}
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 70,
/// angle = 70deg,
/// length = min([15, 31, 4, 13, 22])
/// )
/// |> line(end = [20, 0])
@ -332,7 +332,7 @@ export fn min(
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 70,
/// angle = 70deg,
/// length = max([15, 31, 4, 13, 22])
/// )
/// |> line(end = [20, 0])
@ -352,7 +352,7 @@ export fn max(
/// exampleSketch = startSketchOn(XZ)
/// |> startProfile(at = [0, 0])
/// |> angledLine(
/// angle = 50,
/// angle = 50deg,
/// length = pow(5, exp = 2),
/// )
/// |> yLine(endAbsolute = 0)