Use arrays for multiple geometry (#5770)
* Parse [T] instead of T[] for array types Signed-off-by: Nick Cameron <nrc@ncameron.org> * homogenous arrays, type coercion, remove solid set and sketch set, etc Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -180,15 +180,9 @@ pub enum OpKclValue {
|
||||
Sketch {
|
||||
value: Box<OpSketch>,
|
||||
},
|
||||
Sketches {
|
||||
value: Vec<OpSketch>,
|
||||
},
|
||||
Solid {
|
||||
value: Box<OpSolid>,
|
||||
},
|
||||
Solids {
|
||||
value: Vec<OpSolid>,
|
||||
},
|
||||
Helix {
|
||||
value: Box<OpHelix>,
|
||||
},
|
||||
@ -234,7 +228,7 @@ impl From<&KclValue> for OpKclValue {
|
||||
ty: ty.clone(),
|
||||
},
|
||||
KclValue::String { value, .. } => Self::String { value: value.clone() },
|
||||
KclValue::MixedArray { value, .. } => {
|
||||
KclValue::MixedArray { value, .. } | KclValue::HomArray { value, .. } => {
|
||||
let value = value.iter().map(Self::from).collect();
|
||||
Self::Array { value }
|
||||
}
|
||||
@ -260,29 +254,11 @@ impl From<&KclValue> for OpKclValue {
|
||||
artifact_id: value.artifact_id,
|
||||
}),
|
||||
},
|
||||
KclValue::Sketches { value } => {
|
||||
let value = value
|
||||
.iter()
|
||||
.map(|sketch| OpSketch {
|
||||
artifact_id: sketch.artifact_id,
|
||||
})
|
||||
.collect();
|
||||
Self::Sketches { value }
|
||||
}
|
||||
KclValue::Solid { value } => Self::Solid {
|
||||
value: Box::new(OpSolid {
|
||||
artifact_id: value.artifact_id,
|
||||
}),
|
||||
},
|
||||
KclValue::Solids { value } => {
|
||||
let value = value
|
||||
.iter()
|
||||
.map(|solid| OpSolid {
|
||||
artifact_id: solid.artifact_id,
|
||||
})
|
||||
.collect();
|
||||
Self::Solids { value }
|
||||
}
|
||||
KclValue::Helix { value } => Self::Helix {
|
||||
value: Box::new(OpHelix {
|
||||
artifact_id: value.artifact_id,
|
||||
|
@ -8,11 +8,11 @@ use crate::{
|
||||
execution::{
|
||||
annotations,
|
||||
cad_op::{OpArg, OpKclValue, Operation},
|
||||
kcl_value::{FunctionSource, NumericType, PrimitiveType, RuntimeType},
|
||||
kcl_value::{FunctionSource, NumericType, RuntimeType},
|
||||
memory,
|
||||
state::ModuleState,
|
||||
BodyType, EnvironmentRef, ExecState, ExecutorContext, KclValue, Metadata, Plane, PlaneType, Point3d,
|
||||
TagEngineInfo, TagIdentifier,
|
||||
BodyType, EnvironmentRef, ExecState, ExecutorContext, KclValue, Metadata, PlaneType, TagEngineInfo,
|
||||
TagIdentifier,
|
||||
},
|
||||
modules::{ModuleId, ModulePath, ModuleRepr},
|
||||
parsing::ast::types::{
|
||||
@ -23,7 +23,7 @@ use crate::{
|
||||
},
|
||||
source_range::SourceRange,
|
||||
std::{
|
||||
args::{Arg, FromKclValue, KwArgs},
|
||||
args::{Arg, KwArgs},
|
||||
FunctionKind,
|
||||
},
|
||||
CompilationError,
|
||||
@ -647,11 +647,11 @@ impl ExecutorContext {
|
||||
let result = self
|
||||
.execute_expr(&expr.expr, exec_state, metadata, &[], statement_kind)
|
||||
.await?;
|
||||
coerce(result, &expr.ty, exec_state).map_err(|value| {
|
||||
coerce(&result, &expr.ty, exec_state).ok_or_else(|| {
|
||||
KclError::Semantic(KclErrorDetails {
|
||||
message: format!(
|
||||
"could not coerce {} value to type {}",
|
||||
value.human_friendly_type(),
|
||||
result.human_friendly_type(),
|
||||
expr.ty
|
||||
),
|
||||
source_ranges: vec![expr.into()],
|
||||
@ -663,72 +663,14 @@ impl ExecutorContext {
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce(value: KclValue, ty: &Node<Type>, exec_state: &mut ExecState) -> Result<KclValue, KclValue> {
|
||||
let ty = RuntimeType::from_parsed(ty.inner.clone(), exec_state, (&value).into())
|
||||
fn coerce(value: &KclValue, ty: &Node<Type>, exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
let ty = RuntimeType::from_parsed(ty.inner.clone(), exec_state, value.into())
|
||||
.map_err(|e| {
|
||||
exec_state.err(e);
|
||||
value.clone()
|
||||
})?
|
||||
.ok_or_else(|| value.clone())?;
|
||||
if value.has_type(&ty) {
|
||||
return Ok(value);
|
||||
}
|
||||
})
|
||||
.ok()??;
|
||||
|
||||
// TODO coerce numeric types
|
||||
|
||||
if let KclValue::Object { value, meta } = value {
|
||||
return match ty {
|
||||
RuntimeType::Primitive(PrimitiveType::Plane) => {
|
||||
let origin = value
|
||||
.get("origin")
|
||||
.and_then(Point3d::from_kcl_val)
|
||||
.ok_or_else(|| KclValue::Object {
|
||||
value: value.clone(),
|
||||
meta: meta.clone(),
|
||||
})?;
|
||||
let x_axis = value
|
||||
.get("xAxis")
|
||||
.and_then(Point3d::from_kcl_val)
|
||||
.ok_or_else(|| KclValue::Object {
|
||||
value: value.clone(),
|
||||
meta: meta.clone(),
|
||||
})?;
|
||||
let y_axis = value
|
||||
.get("yAxis")
|
||||
.and_then(Point3d::from_kcl_val)
|
||||
.ok_or_else(|| KclValue::Object {
|
||||
value: value.clone(),
|
||||
meta: meta.clone(),
|
||||
})?;
|
||||
let z_axis = value
|
||||
.get("zAxis")
|
||||
.and_then(Point3d::from_kcl_val)
|
||||
.ok_or_else(|| KclValue::Object {
|
||||
value: value.clone(),
|
||||
meta: meta.clone(),
|
||||
})?;
|
||||
|
||||
let id = exec_state.next_uuid();
|
||||
let plane = Plane {
|
||||
id,
|
||||
artifact_id: id.into(),
|
||||
origin,
|
||||
x_axis,
|
||||
y_axis,
|
||||
z_axis,
|
||||
value: PlaneType::Uninit,
|
||||
// TODO use length unit from origin
|
||||
units: exec_state.length_unit(),
|
||||
meta,
|
||||
};
|
||||
|
||||
Ok(KclValue::Plane { value: Box::new(plane) })
|
||||
}
|
||||
_ => Err(KclValue::Object { value, meta }),
|
||||
};
|
||||
}
|
||||
|
||||
Err(value)
|
||||
value.coerce(&ty, exec_state)
|
||||
}
|
||||
|
||||
impl BinaryPart {
|
||||
@ -1450,6 +1392,11 @@ fn update_memory_for_tags_of_geometry(result: &mut KclValue, exec_state: &mut Ex
|
||||
}
|
||||
}
|
||||
}
|
||||
KclValue::MixedArray { value, .. } | KclValue::HomArray { value, .. } => {
|
||||
for v in value {
|
||||
update_memory_for_tags_of_geometry(v, exec_state)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
|
@ -23,8 +23,8 @@ type Point3D = kcmc::shared::Point3d<f64>;
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Geometry {
|
||||
Sketch(Box<Sketch>),
|
||||
Solid(Box<Solid>),
|
||||
Sketch(Sketch),
|
||||
Solid(Solid),
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
@ -52,8 +52,8 @@ impl Geometry {
|
||||
#[serde(tag = "type")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum Geometries {
|
||||
Sketches(Vec<Box<Sketch>>),
|
||||
Solids(Vec<Box<Solid>>),
|
||||
Sketches(Vec<Sketch>),
|
||||
Solids(Vec<Solid>),
|
||||
}
|
||||
|
||||
impl From<Geometry> for Geometries {
|
||||
@ -65,150 +65,6 @@ impl From<Geometry> for Geometries {
|
||||
}
|
||||
}
|
||||
|
||||
/// A sketch or a group of sketches.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SketchSet {
|
||||
Sketch(Box<Sketch>),
|
||||
Sketches(Vec<Box<Sketch>>),
|
||||
}
|
||||
|
||||
impl SketchSet {
|
||||
pub fn meta(&self) -> Vec<Metadata> {
|
||||
match self {
|
||||
SketchSet::Sketch(sg) => sg.meta.clone(),
|
||||
SketchSet::Sketches(sg) => sg.iter().flat_map(|sg| sg.meta.clone()).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SketchSet> for Vec<Sketch> {
|
||||
fn from(value: SketchSet) -> Self {
|
||||
match value {
|
||||
SketchSet::Sketch(sg) => vec![*sg],
|
||||
SketchSet::Sketches(sgs) => sgs.into_iter().map(|sg| *sg).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sketch> for SketchSet {
|
||||
fn from(sg: Sketch) -> Self {
|
||||
SketchSet::Sketch(Box::new(sg))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Sketch>> for SketchSet {
|
||||
fn from(sg: Box<Sketch>) -> Self {
|
||||
SketchSet::Sketch(sg)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Sketch>> for SketchSet {
|
||||
fn from(sg: Vec<Sketch>) -> Self {
|
||||
if sg.len() == 1 {
|
||||
SketchSet::Sketch(Box::new(sg[0].clone()))
|
||||
} else {
|
||||
SketchSet::Sketches(sg.into_iter().map(Box::new).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Sketch>>> for SketchSet {
|
||||
fn from(sg: Vec<Box<Sketch>>) -> Self {
|
||||
if sg.len() == 1 {
|
||||
SketchSet::Sketch(sg[0].clone())
|
||||
} else {
|
||||
SketchSet::Sketches(sg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SketchSet> for Vec<Box<Sketch>> {
|
||||
fn from(sg: SketchSet) -> Self {
|
||||
match sg {
|
||||
SketchSet::Sketch(sg) => vec![sg],
|
||||
SketchSet::Sketches(sgs) => sgs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Sketch> for Vec<Box<Sketch>> {
|
||||
fn from(sg: &Sketch) -> Self {
|
||||
vec![Box::new(sg.clone())]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Sketch>> for Vec<Box<Sketch>> {
|
||||
fn from(sg: Box<Sketch>) -> Self {
|
||||
vec![sg]
|
||||
}
|
||||
}
|
||||
|
||||
/// A solid or a group of solids.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SolidSet {
|
||||
Solid(Box<Solid>),
|
||||
Solids(Vec<Box<Solid>>),
|
||||
}
|
||||
|
||||
impl From<Solid> for SolidSet {
|
||||
fn from(eg: Solid) -> Self {
|
||||
SolidSet::Solid(Box::new(eg))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Solid>> for SolidSet {
|
||||
fn from(eg: Box<Solid>) -> Self {
|
||||
SolidSet::Solid(eg)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Solid>> for SolidSet {
|
||||
fn from(eg: Vec<Solid>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
SolidSet::Solid(Box::new(eg[0].clone()))
|
||||
} else {
|
||||
SolidSet::Solids(eg.into_iter().map(Box::new).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Solid>>> for SolidSet {
|
||||
fn from(eg: Vec<Box<Solid>>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
SolidSet::Solid(eg[0].clone())
|
||||
} else {
|
||||
SolidSet::Solids(eg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SolidSet> for Vec<Box<Solid>> {
|
||||
fn from(eg: SolidSet) -> Self {
|
||||
match eg {
|
||||
SolidSet::Solid(eg) => vec![eg],
|
||||
SolidSet::Solids(egs) => egs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Solid> for Vec<Box<Solid>> {
|
||||
fn from(eg: &Solid) -> Self {
|
||||
vec![Box::new(eg.clone())]
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<Solid>> for Vec<Box<Solid>> {
|
||||
fn from(eg: Box<Solid>) -> Self {
|
||||
vec![eg]
|
||||
}
|
||||
}
|
||||
|
||||
/// Data for an imported geometry.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
@ -228,17 +84,29 @@ pub struct ImportedGeometry {
|
||||
#[serde(tag = "type", rename_all = "camelCase")]
|
||||
#[allow(clippy::vec_box)]
|
||||
pub enum SolidOrImportedGeometry {
|
||||
Solid(Box<Solid>),
|
||||
ImportedGeometry(Box<ImportedGeometry>),
|
||||
SolidSet(Vec<Box<Solid>>),
|
||||
SolidSet(Vec<Solid>),
|
||||
}
|
||||
|
||||
impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
|
||||
fn from(value: SolidOrImportedGeometry) -> Self {
|
||||
match value {
|
||||
SolidOrImportedGeometry::Solid(s) => crate::execution::KclValue::Solid { value: s },
|
||||
SolidOrImportedGeometry::ImportedGeometry(s) => crate::execution::KclValue::ImportedGeometry(*s),
|
||||
SolidOrImportedGeometry::SolidSet(s) => crate::execution::KclValue::Solids { value: s },
|
||||
SolidOrImportedGeometry::SolidSet(mut s) => {
|
||||
if s.len() == 1 {
|
||||
crate::execution::KclValue::Solid {
|
||||
value: Box::new(s.pop().unwrap()),
|
||||
}
|
||||
} else {
|
||||
crate::execution::KclValue::HomArray {
|
||||
value: s
|
||||
.into_iter()
|
||||
.map(|s| crate::execution::KclValue::Solid { value: Box::new(s) })
|
||||
.collect(),
|
||||
ty: crate::execution::PrimitiveType::Solid,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -246,7 +114,6 @@ impl From<SolidOrImportedGeometry> for crate::execution::KclValue {
|
||||
impl SolidOrImportedGeometry {
|
||||
pub(crate) fn ids(&self) -> Vec<uuid::Uuid> {
|
||||
match self {
|
||||
SolidOrImportedGeometry::Solid(s) => vec![s.id],
|
||||
SolidOrImportedGeometry::ImportedGeometry(s) => vec![s.id],
|
||||
SolidOrImportedGeometry::SolidSet(s) => s.iter().map(|s| s.id).collect(),
|
||||
}
|
||||
|
@ -6,13 +6,12 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
memory::{self, EnvironmentRef},
|
||||
MetaSettings,
|
||||
MetaSettings, Point3d,
|
||||
};
|
||||
use crate::{
|
||||
errors::KclErrorDetails,
|
||||
execution::{
|
||||
ExecState, ExecutorContext, Face, Helix, ImportedGeometry, Metadata, Plane, Sketch, SketchSet, Solid, SolidSet,
|
||||
TagIdentifier,
|
||||
ExecState, ExecutorContext, Face, Helix, ImportedGeometry, Metadata, Plane, Sketch, Solid, TagIdentifier,
|
||||
},
|
||||
parsing::{
|
||||
ast::types::{
|
||||
@ -21,7 +20,10 @@ use crate::{
|
||||
},
|
||||
token::NumericSuffix,
|
||||
},
|
||||
std::{args::Arg, StdFnProps},
|
||||
std::{
|
||||
args::{Arg, FromKclValue},
|
||||
StdFnProps,
|
||||
},
|
||||
CompilationError, KclError, ModuleId, SourceRange,
|
||||
};
|
||||
|
||||
@ -58,6 +60,13 @@ pub enum KclValue {
|
||||
#[serde(skip)]
|
||||
meta: Vec<Metadata>,
|
||||
},
|
||||
// An array where all values have a shared type (not necessarily the same principal type).
|
||||
HomArray {
|
||||
value: Vec<KclValue>,
|
||||
// The type of values, not the array type.
|
||||
#[serde(skip)]
|
||||
ty: PrimitiveType,
|
||||
},
|
||||
Object {
|
||||
value: KclObjectFields,
|
||||
#[serde(skip)]
|
||||
@ -74,15 +83,9 @@ pub enum KclValue {
|
||||
Sketch {
|
||||
value: Box<Sketch>,
|
||||
},
|
||||
Sketches {
|
||||
value: Vec<Box<Sketch>>,
|
||||
},
|
||||
Solid {
|
||||
value: Box<Solid>,
|
||||
},
|
||||
Solids {
|
||||
value: Vec<Box<Solid>>,
|
||||
},
|
||||
Helix {
|
||||
value: Box<Helix>,
|
||||
},
|
||||
@ -139,48 +142,46 @@ impl JsonSchema for FunctionSource {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SketchSet> for KclValue {
|
||||
fn from(sg: SketchSet) -> Self {
|
||||
match sg {
|
||||
SketchSet::Sketch(value) => KclValue::Sketch { value },
|
||||
SketchSet::Sketches(value) => KclValue::Sketches { value },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Sketch>>> for KclValue {
|
||||
fn from(sg: Vec<Box<Sketch>>) -> Self {
|
||||
KclValue::Sketches { value: sg }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SolidSet> for KclValue {
|
||||
fn from(eg: SolidSet) -> Self {
|
||||
match eg {
|
||||
SolidSet::Solid(eg) => KclValue::Solid { value: eg },
|
||||
SolidSet::Solids(egs) => KclValue::Solids { value: egs },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Box<Solid>>> for KclValue {
|
||||
fn from(eg: Vec<Box<Solid>>) -> Self {
|
||||
impl From<Vec<Sketch>> for KclValue {
|
||||
fn from(mut eg: Vec<Sketch>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
KclValue::Solid { value: eg[0].clone() }
|
||||
KclValue::Sketch {
|
||||
value: Box::new(eg.pop().unwrap()),
|
||||
}
|
||||
} else {
|
||||
KclValue::Solids { value: eg }
|
||||
KclValue::HomArray {
|
||||
value: eg
|
||||
.into_iter()
|
||||
.map(|s| KclValue::Sketch { value: Box::new(s) })
|
||||
.collect(),
|
||||
ty: crate::execution::PrimitiveType::Sketch,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Solid>> for KclValue {
|
||||
fn from(mut eg: Vec<Solid>) -> Self {
|
||||
if eg.len() == 1 {
|
||||
KclValue::Solid {
|
||||
value: Box::new(eg.pop().unwrap()),
|
||||
}
|
||||
} else {
|
||||
KclValue::HomArray {
|
||||
value: eg.into_iter().map(|s| KclValue::Solid { value: Box::new(s) }).collect(),
|
||||
ty: crate::execution::PrimitiveType::Solid,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KclValue> for Vec<SourceRange> {
|
||||
fn from(item: KclValue) -> Self {
|
||||
match item {
|
||||
KclValue::TagDeclarator(t) => vec![SourceRange::new(t.start, t.end, t.module_id)],
|
||||
KclValue::TagIdentifier(t) => to_vec_sr(&t.meta),
|
||||
KclValue::Solid { value } => to_vec_sr(&value.meta),
|
||||
KclValue::Solids { value } => value.iter().flat_map(|eg| to_vec_sr(&eg.meta)).collect(),
|
||||
KclValue::Sketch { value } => to_vec_sr(&value.meta),
|
||||
KclValue::Sketches { value } => value.iter().flat_map(|eg| to_vec_sr(&eg.meta)).collect(),
|
||||
KclValue::Helix { value } => to_vec_sr(&value.meta),
|
||||
KclValue::ImportedGeometry(i) => to_vec_sr(&i.meta),
|
||||
KclValue::Function { meta, .. } => to_vec_sr(&meta),
|
||||
@ -190,6 +191,7 @@ impl From<KclValue> for Vec<SourceRange> {
|
||||
KclValue::Number { meta, .. } => to_vec_sr(&meta),
|
||||
KclValue::String { meta, .. } => to_vec_sr(&meta),
|
||||
KclValue::MixedArray { meta, .. } => to_vec_sr(&meta),
|
||||
KclValue::HomArray { value, .. } => value.iter().flat_map(Into::<Vec<SourceRange>>::into).collect(),
|
||||
KclValue::Object { meta, .. } => to_vec_sr(&meta),
|
||||
KclValue::Module { meta, .. } => to_vec_sr(&meta),
|
||||
KclValue::Uuid { meta, .. } => to_vec_sr(&meta),
|
||||
@ -209,9 +211,7 @@ impl From<&KclValue> for Vec<SourceRange> {
|
||||
KclValue::TagDeclarator(t) => vec![SourceRange::new(t.start, t.end, t.module_id)],
|
||||
KclValue::TagIdentifier(t) => to_vec_sr(&t.meta),
|
||||
KclValue::Solid { value } => to_vec_sr(&value.meta),
|
||||
KclValue::Solids { value } => value.iter().flat_map(|eg| to_vec_sr(&eg.meta)).collect(),
|
||||
KclValue::Sketch { value } => to_vec_sr(&value.meta),
|
||||
KclValue::Sketches { value } => value.iter().flat_map(|eg| to_vec_sr(&eg.meta)).collect(),
|
||||
KclValue::Helix { value } => to_vec_sr(&value.meta),
|
||||
KclValue::ImportedGeometry(i) => to_vec_sr(&i.meta),
|
||||
KclValue::Function { meta, .. } => to_vec_sr(meta),
|
||||
@ -222,6 +222,7 @@ impl From<&KclValue> for Vec<SourceRange> {
|
||||
KclValue::String { meta, .. } => to_vec_sr(meta),
|
||||
KclValue::Uuid { meta, .. } => to_vec_sr(meta),
|
||||
KclValue::MixedArray { meta, .. } => to_vec_sr(meta),
|
||||
KclValue::HomArray { value, .. } => value.iter().flat_map(Into::<Vec<SourceRange>>::into).collect(),
|
||||
KclValue::Object { meta, .. } => to_vec_sr(meta),
|
||||
KclValue::Module { meta, .. } => to_vec_sr(meta),
|
||||
KclValue::KclNone { meta, .. } => to_vec_sr(meta),
|
||||
@ -245,15 +246,14 @@ impl KclValue {
|
||||
KclValue::Number { meta, .. } => meta.clone(),
|
||||
KclValue::String { value: _, meta } => meta.clone(),
|
||||
KclValue::MixedArray { value: _, meta } => meta.clone(),
|
||||
KclValue::HomArray { value, .. } => value.iter().flat_map(|v| v.metadata()).collect(),
|
||||
KclValue::Object { value: _, meta } => meta.clone(),
|
||||
KclValue::TagIdentifier(x) => x.meta.clone(),
|
||||
KclValue::TagDeclarator(x) => vec![x.metadata()],
|
||||
KclValue::Plane { value } => value.meta.clone(),
|
||||
KclValue::Face { value } => value.meta.clone(),
|
||||
KclValue::Sketch { value } => value.meta.clone(),
|
||||
KclValue::Sketches { value } => value.iter().flat_map(|sketch| &sketch.meta).copied().collect(),
|
||||
KclValue::Solid { value } => value.meta.clone(),
|
||||
KclValue::Solids { value } => value.iter().flat_map(|sketch| &sketch.meta).copied().collect(),
|
||||
KclValue::Helix { value } => value.meta.clone(),
|
||||
KclValue::ImportedGeometry(x) => x.meta.clone(),
|
||||
KclValue::Function { meta, .. } => meta.clone(),
|
||||
@ -276,29 +276,6 @@ impl KclValue {
|
||||
Some(ast.as_source_range())
|
||||
}
|
||||
|
||||
pub(crate) fn get_solid_set(&self) -> Result<SolidSet> {
|
||||
match self {
|
||||
KclValue::Solid { value } => Ok(SolidSet::Solid(value.clone())),
|
||||
KclValue::Solids { value } => Ok(SolidSet::Solids(value.clone())),
|
||||
KclValue::MixedArray { value, .. } => {
|
||||
let solids: Vec<_> = value
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, v)| {
|
||||
v.as_solid().map(|v| v.to_owned()).map(Box::new).ok_or_else(|| {
|
||||
anyhow::anyhow!(
|
||||
"expected this array to only contain solids, but element {i} was actually {}",
|
||||
v.human_friendly_type()
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(SolidSet::Solids(solids))
|
||||
}
|
||||
_ => anyhow::bail!("Not a solid or solids: {:?}", self),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) fn none() -> Self {
|
||||
Self::KclNone {
|
||||
@ -315,9 +292,7 @@ impl KclValue {
|
||||
KclValue::TagDeclarator(_) => "TagDeclarator",
|
||||
KclValue::TagIdentifier(_) => "TagIdentifier",
|
||||
KclValue::Solid { .. } => "Solid",
|
||||
KclValue::Solids { .. } => "Solids",
|
||||
KclValue::Sketch { .. } => "Sketch",
|
||||
KclValue::Sketches { .. } => "Sketches",
|
||||
KclValue::Helix { .. } => "Helix",
|
||||
KclValue::ImportedGeometry(_) => "ImportedGeometry",
|
||||
KclValue::Function { .. } => "Function",
|
||||
@ -327,6 +302,7 @@ impl KclValue {
|
||||
KclValue::Number { .. } => "number",
|
||||
KclValue::String { .. } => "string (text)",
|
||||
KclValue::MixedArray { .. } => "array (list)",
|
||||
KclValue::HomArray { .. } => "array (list)",
|
||||
KclValue::Object { .. } => "object",
|
||||
KclValue::Module { .. } => "module",
|
||||
KclValue::Type { .. } => "type",
|
||||
@ -481,6 +457,14 @@ impl KclValue {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_sketch(&self) -> Option<&Sketch> {
|
||||
if let KclValue::Sketch { value } = self {
|
||||
Some(value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_mut_sketch(&mut self) -> Option<&mut Sketch> {
|
||||
if let KclValue::Sketch { value } = self {
|
||||
Some(value)
|
||||
@ -578,6 +562,215 @@ impl KclValue {
|
||||
self_ty.subtype(ty)
|
||||
}
|
||||
|
||||
/// Coerce `self` to a new value which has `ty` as it's closest supertype.
|
||||
///
|
||||
/// If the result is Some, then:
|
||||
/// - result.principal_type().unwrap().subtype(ty)
|
||||
///
|
||||
/// If self.principal_type() == ty then result == self
|
||||
pub fn coerce(&self, ty: &RuntimeType, exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
match ty {
|
||||
RuntimeType::Primitive(ty) => self.coerce_to_primitive_type(ty, exec_state),
|
||||
RuntimeType::Array(ty, len) => self.coerce_to_array_type(ty, *len, exec_state),
|
||||
RuntimeType::Tuple(tys) => self.coerce_to_tuple_type(tys, exec_state),
|
||||
RuntimeType::Union(tys) => self.coerce_to_union_type(tys, exec_state),
|
||||
RuntimeType::Object(tys) => self.coerce_to_object_type(tys, exec_state),
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce_to_primitive_type(&self, ty: &PrimitiveType, exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
let value = match self {
|
||||
KclValue::MixedArray { value, .. } | KclValue::HomArray { value, .. } if value.len() == 1 => &value[0],
|
||||
_ => self,
|
||||
};
|
||||
match ty {
|
||||
// TODO numeric type coercions
|
||||
PrimitiveType::Number(_ty) => match value {
|
||||
KclValue::Number { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::String => match value {
|
||||
KclValue::String { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::Boolean => match value {
|
||||
KclValue::Bool { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::Sketch => match value {
|
||||
KclValue::Sketch { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::Solid => match value {
|
||||
KclValue::Solid { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::Plane => match value {
|
||||
KclValue::Plane { .. } => Some(value.clone()),
|
||||
KclValue::Object { value, meta } => {
|
||||
let origin = value.get("origin").and_then(Point3d::from_kcl_val)?;
|
||||
let x_axis = value.get("xAxis").and_then(Point3d::from_kcl_val)?;
|
||||
let y_axis = value.get("yAxis").and_then(Point3d::from_kcl_val)?;
|
||||
let z_axis = value.get("zAxis").and_then(Point3d::from_kcl_val)?;
|
||||
|
||||
let id = exec_state.mod_local.id_generator.next_uuid();
|
||||
let plane = Plane {
|
||||
id,
|
||||
artifact_id: id.into(),
|
||||
origin,
|
||||
x_axis,
|
||||
y_axis,
|
||||
z_axis,
|
||||
value: super::PlaneType::Uninit,
|
||||
// TODO use length unit from origin
|
||||
units: exec_state.length_unit(),
|
||||
meta: meta.clone(),
|
||||
};
|
||||
|
||||
Some(KclValue::Plane { value: Box::new(plane) })
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
PrimitiveType::ImportedGeometry => match value {
|
||||
KclValue::ImportedGeometry { .. } => Some(value.clone()),
|
||||
_ => None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce_to_array_type(&self, ty: &PrimitiveType, len: ArrayLen, exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
match self {
|
||||
KclValue::HomArray { value, ty: aty } => {
|
||||
// TODO could check types of values individually
|
||||
if aty != ty {
|
||||
return None;
|
||||
}
|
||||
|
||||
let value = match len {
|
||||
ArrayLen::None => value.clone(),
|
||||
ArrayLen::NonEmpty => {
|
||||
if value.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
value.clone()
|
||||
}
|
||||
ArrayLen::Known(n) => {
|
||||
if n != value.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
value[..n].to_vec()
|
||||
}
|
||||
};
|
||||
|
||||
Some(KclValue::HomArray { value, ty: ty.clone() })
|
||||
}
|
||||
KclValue::MixedArray { value, .. } => {
|
||||
let value = match len {
|
||||
ArrayLen::None => value.clone(),
|
||||
ArrayLen::NonEmpty => {
|
||||
if value.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
value.clone()
|
||||
}
|
||||
ArrayLen::Known(n) => {
|
||||
if n != value.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
value[..n].to_vec()
|
||||
}
|
||||
};
|
||||
|
||||
let rt = RuntimeType::Primitive(ty.clone());
|
||||
let value = value
|
||||
.iter()
|
||||
.map(|v| v.coerce(&rt, exec_state))
|
||||
.collect::<Option<Vec<_>>>()?;
|
||||
|
||||
Some(KclValue::HomArray { value, ty: ty.clone() })
|
||||
}
|
||||
KclValue::KclNone { .. } if len.satisfied(0) => Some(KclValue::HomArray {
|
||||
value: Vec::new(),
|
||||
ty: ty.clone(),
|
||||
}),
|
||||
value if len.satisfied(1) => {
|
||||
if value.has_type(&RuntimeType::Primitive(ty.clone())) {
|
||||
Some(KclValue::HomArray {
|
||||
value: vec![value.clone()],
|
||||
ty: ty.clone(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce_to_tuple_type(&self, tys: &[PrimitiveType], exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
match self {
|
||||
KclValue::MixedArray { value, .. } | KclValue::HomArray { value, .. } => {
|
||||
if value.len() < tys.len() {
|
||||
return None;
|
||||
}
|
||||
let mut result = Vec::new();
|
||||
for (i, t) in tys.iter().enumerate() {
|
||||
result.push(value[i].coerce_to_primitive_type(t, exec_state)?);
|
||||
}
|
||||
|
||||
Some(KclValue::MixedArray {
|
||||
value: result,
|
||||
meta: Vec::new(),
|
||||
})
|
||||
}
|
||||
KclValue::KclNone { meta, .. } if tys.is_empty() => Some(KclValue::MixedArray {
|
||||
value: Vec::new(),
|
||||
meta: meta.clone(),
|
||||
}),
|
||||
value if tys.len() == 1 => {
|
||||
if value.has_type(&RuntimeType::Primitive(tys[0].clone())) {
|
||||
Some(KclValue::MixedArray {
|
||||
value: vec![value.clone()],
|
||||
meta: Vec::new(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn coerce_to_union_type(&self, tys: &[RuntimeType], exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
for t in tys {
|
||||
if let Some(v) = self.coerce(t, exec_state) {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn coerce_to_object_type(&self, tys: &[(String, RuntimeType)], _exec_state: &mut ExecState) -> Option<KclValue> {
|
||||
match self {
|
||||
KclValue::Object { value, .. } => {
|
||||
for (s, t) in tys {
|
||||
// TODO coerce fields
|
||||
if !value.get(s)?.has_type(t) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
// TODO remove non-required fields
|
||||
Some(self.clone())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn principal_type(&self) -> Option<RuntimeType> {
|
||||
match self {
|
||||
KclValue::Bool { .. } => Some(RuntimeType::Primitive(PrimitiveType::Boolean)),
|
||||
@ -592,18 +785,17 @@ impl KclValue {
|
||||
}
|
||||
KclValue::Plane { .. } => Some(RuntimeType::Primitive(PrimitiveType::Plane)),
|
||||
KclValue::Sketch { .. } => Some(RuntimeType::Primitive(PrimitiveType::Sketch)),
|
||||
KclValue::Sketches { .. } => Some(RuntimeType::Array(PrimitiveType::Sketch)),
|
||||
KclValue::Solid { .. } => Some(RuntimeType::Primitive(PrimitiveType::Solid)),
|
||||
KclValue::Solids { .. } => Some(RuntimeType::Array(PrimitiveType::Solid)),
|
||||
KclValue::ImportedGeometry(..) => Some(RuntimeType::Primitive(PrimitiveType::ImportedGeometry)),
|
||||
KclValue::MixedArray { value, .. } => Some(RuntimeType::Tuple(
|
||||
value
|
||||
.iter()
|
||||
.map(|v| v.principal_type().and_then(RuntimeType::primitive))
|
||||
.collect::<Option<Vec<_>>>()?,
|
||||
)),
|
||||
KclValue::HomArray { ty, value, .. } => Some(RuntimeType::Array(ty.clone(), ArrayLen::Known(value.len()))),
|
||||
KclValue::Face { .. } => None,
|
||||
KclValue::Helix { .. }
|
||||
| KclValue::ImportedGeometry(..)
|
||||
| KclValue::Function { .. }
|
||||
| KclValue::Module { .. }
|
||||
| KclValue::TagIdentifier(_)
|
||||
@ -712,12 +904,11 @@ impl KclValue {
|
||||
KclValue::TagIdentifier(tag) => Some(format!("${}", tag.value)),
|
||||
// TODO better Array and Object stringification
|
||||
KclValue::MixedArray { .. } => Some("[...]".to_owned()),
|
||||
KclValue::HomArray { .. } => Some("[...]".to_owned()),
|
||||
KclValue::Object { .. } => Some("{ ... }".to_owned()),
|
||||
KclValue::Module { .. }
|
||||
| KclValue::Solid { .. }
|
||||
| KclValue::Solids { .. }
|
||||
| KclValue::Sketch { .. }
|
||||
| KclValue::Sketches { .. }
|
||||
| KclValue::Helix { .. }
|
||||
| KclValue::ImportedGeometry(_)
|
||||
| KclValue::Function { .. }
|
||||
@ -732,7 +923,8 @@ impl KclValue {
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum RuntimeType {
|
||||
Primitive(PrimitiveType),
|
||||
Array(PrimitiveType),
|
||||
Array(PrimitiveType, ArrayLen),
|
||||
Union(Vec<RuntimeType>),
|
||||
Tuple(Vec<PrimitiveType>),
|
||||
Object(Vec<(String, RuntimeType)>),
|
||||
}
|
||||
@ -747,7 +939,9 @@ impl RuntimeType {
|
||||
Type::Primitive(pt) => {
|
||||
PrimitiveType::from_parsed(pt, exec_state, source_range)?.map(RuntimeType::Primitive)
|
||||
}
|
||||
Type::Array(pt) => PrimitiveType::from_parsed(pt, exec_state, source_range)?.map(RuntimeType::Array),
|
||||
Type::Array(pt) => {
|
||||
PrimitiveType::from_parsed(pt, exec_state, source_range)?.map(|t| RuntimeType::Array(t, ArrayLen::None))
|
||||
}
|
||||
Type::Object { properties } => properties
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
@ -763,15 +957,37 @@ impl RuntimeType {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn human_friendly_type(&self) -> String {
|
||||
match self {
|
||||
RuntimeType::Primitive(ty) => ty.to_string(),
|
||||
RuntimeType::Array(ty, ArrayLen::None) => format!("an array of {}", ty.display_multiple()),
|
||||
RuntimeType::Array(ty, ArrayLen::NonEmpty) => format!("one or more {}", ty.display_multiple()),
|
||||
RuntimeType::Array(ty, ArrayLen::Known(n)) => format!("an array of {n} {}", ty.display_multiple()),
|
||||
RuntimeType::Union(tys) => tys
|
||||
.iter()
|
||||
.map(Self::human_friendly_type)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" or "),
|
||||
RuntimeType::Tuple(tys) => format!(
|
||||
"an array with values of types ({})",
|
||||
tys.iter().map(PrimitiveType::to_string).collect::<Vec<_>>().join(", ")
|
||||
),
|
||||
RuntimeType::Object(_) => format!("an object with fields {}", self),
|
||||
}
|
||||
}
|
||||
|
||||
// Subtype with no coercion, including refining numeric types.
|
||||
fn subtype(&self, sup: &RuntimeType) -> bool {
|
||||
use RuntimeType::*;
|
||||
|
||||
match (self, sup) {
|
||||
(Primitive(t1), Primitive(t2)) => t1 == t2,
|
||||
// TODO arrays could be covariant
|
||||
(Primitive(t1), Primitive(t2)) | (Array(t1), Array(t2)) => t1 == t2,
|
||||
(Array(t1, l1), Array(t2, l2)) => t1 == t2 && l1.subtype(*l2),
|
||||
(Tuple(t1), Tuple(t2)) => t1 == t2,
|
||||
(Tuple(t1), Array(t2)) => t1.iter().all(|t| t == t2),
|
||||
(Tuple(t1), Array(t2, l2)) => (l2.satisfied(t1.len())) && t1.iter().all(|t| t == t2),
|
||||
(Union(ts1), Union(ts2)) => ts1.iter().all(|t| ts2.contains(t)),
|
||||
(t1, Union(ts2)) => ts2.contains(t1),
|
||||
// TODO record subtyping - subtype can be larger, fields can be covariant.
|
||||
(Object(t1), Object(t2)) => t1 == t2,
|
||||
_ => false,
|
||||
@ -790,12 +1006,21 @@ impl fmt::Display for RuntimeType {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
RuntimeType::Primitive(t) => t.fmt(f),
|
||||
RuntimeType::Array(t) => write!(f, "[{t}]"),
|
||||
RuntimeType::Array(t, l) => match l {
|
||||
ArrayLen::None => write!(f, "[{t}]"),
|
||||
ArrayLen::NonEmpty => write!(f, "[{t}; 1+]"),
|
||||
ArrayLen::Known(n) => write!(f, "[{t}; {n}]"),
|
||||
},
|
||||
RuntimeType::Tuple(ts) => write!(
|
||||
f,
|
||||
"[{}]",
|
||||
ts.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(", ")
|
||||
),
|
||||
RuntimeType::Union(ts) => write!(
|
||||
f,
|
||||
"{}",
|
||||
ts.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" | ")
|
||||
),
|
||||
RuntimeType::Object(items) => write!(
|
||||
f,
|
||||
"{{ {} }}",
|
||||
@ -809,6 +1034,34 @@ impl fmt::Display for RuntimeType {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum ArrayLen {
|
||||
None,
|
||||
NonEmpty,
|
||||
Known(usize),
|
||||
}
|
||||
|
||||
impl ArrayLen {
|
||||
pub fn subtype(self, other: ArrayLen) -> bool {
|
||||
match (self, other) {
|
||||
(_, ArrayLen::None) => true,
|
||||
(ArrayLen::NonEmpty, ArrayLen::NonEmpty) => true,
|
||||
(ArrayLen::Known(size), ArrayLen::NonEmpty) if size > 0 => true,
|
||||
(ArrayLen::Known(s1), ArrayLen::Known(s2)) if s1 == s2 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// True if the length constraint is satisfied by the supplied length.
|
||||
fn satisfied(self, len: usize) -> bool {
|
||||
match self {
|
||||
ArrayLen::None => true,
|
||||
ArrayLen::NonEmpty => len > 0,
|
||||
ArrayLen::Known(s) => len == s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum PrimitiveType {
|
||||
Number(NumericType),
|
||||
@ -817,6 +1070,7 @@ pub enum PrimitiveType {
|
||||
Sketch,
|
||||
Solid,
|
||||
Plane,
|
||||
ImportedGeometry,
|
||||
}
|
||||
|
||||
impl PrimitiveType {
|
||||
@ -848,6 +1102,19 @@ impl PrimitiveType {
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn display_multiple(&self) -> String {
|
||||
match self {
|
||||
PrimitiveType::Number(NumericType::Known(unit)) => format!("numbers({unit})"),
|
||||
PrimitiveType::Number(_) => "numbers".to_owned(),
|
||||
PrimitiveType::String => "strings".to_owned(),
|
||||
PrimitiveType::Boolean => "bools".to_owned(),
|
||||
PrimitiveType::Sketch => "Sketches".to_owned(),
|
||||
PrimitiveType::Solid => "Solids".to_owned(),
|
||||
PrimitiveType::Plane => "Planes".to_owned(),
|
||||
PrimitiveType::ImportedGeometry => "imported geometries".to_owned(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PrimitiveType {
|
||||
@ -860,6 +1127,7 @@ impl fmt::Display for PrimitiveType {
|
||||
PrimitiveType::Sketch => write!(f, "Sketch"),
|
||||
PrimitiveType::Solid => write!(f, "Solid"),
|
||||
PrimitiveType::Plane => write!(f, "Plane"),
|
||||
PrimitiveType::ImportedGeometry => write!(f, "imported geometry"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1879,15 +1879,6 @@ let w = f() + f()
|
||||
parse_execute(ast).await.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serialize_memory_item() {
|
||||
let mem = KclValue::Solids {
|
||||
value: Default::default(),
|
||||
};
|
||||
let json = serde_json::to_string(&mem).unwrap();
|
||||
assert_eq!(json, r#"{"type":"Solids","value":[]}"#);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn kcl_test_ids_stable_between_executions() {
|
||||
let code = r#"sketch001 = startSketchOn(XZ)
|
||||
|
Reference in New Issue
Block a user