1use std::{str::FromStr, sync::Arc};
2
3use anyhow::Result;
4use indexmap::IndexMap;
5use kittycad_modeling_cmds::units::{UnitAngle, UnitLength};
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9#[cfg(feature = "artifact-graph")]
10use crate::execution::{Artifact, ArtifactCommand, ArtifactGraph, ArtifactId};
11use crate::{
12 CompilationError, EngineManager, ExecutorContext, KclErrorWithOutputs, SourceRange,
13 errors::{KclError, KclErrorDetails, Severity},
14 exec::DefaultPlanes,
15 execution::{
16 EnvironmentRef, ExecOutcome, ExecutorSettings, KclValue, SketchVarId, annotations,
17 cad_op::Operation,
18 id_generator::IdGenerator,
19 memory::{ProgramMemory, Stack},
20 types::NumericType,
21 },
22 modules::{ModuleId, ModuleInfo, ModuleLoader, ModulePath, ModuleRepr, ModuleSource},
23 parsing::ast::types::{Annotation, NodeRef},
24};
25
26#[derive(Debug, Clone)]
28pub struct ExecState {
29 pub(super) global: GlobalState,
30 pub(super) mod_local: ModuleState,
31}
32
33pub type ModuleInfoMap = IndexMap<ModuleId, ModuleInfo>;
34
35#[derive(Debug, Clone)]
36pub(super) struct GlobalState {
37 pub path_to_source_id: IndexMap<ModulePath, ModuleId>,
39 pub id_to_source: IndexMap<ModuleId, ModuleSource>,
41 pub module_infos: ModuleInfoMap,
43 pub mod_loader: ModuleLoader,
45 pub errors: Vec<CompilationError>,
47 pub artifacts: ArtifactState,
49 pub root_module_artifacts: ModuleArtifactState,
51}
52
53#[cfg(feature = "artifact-graph")]
54#[derive(Debug, Clone, Default)]
55pub(super) struct ArtifactState {
56 pub artifacts: IndexMap<ArtifactId, Artifact>,
59 pub graph: ArtifactGraph,
61}
62
63#[cfg(not(feature = "artifact-graph"))]
64#[derive(Debug, Clone, Default)]
65pub(super) struct ArtifactState {}
66
67#[cfg(feature = "artifact-graph")]
69#[derive(Debug, Clone, Default, PartialEq, Serialize)]
70pub struct ModuleArtifactState {
71 pub artifacts: IndexMap<ArtifactId, Artifact>,
73 #[serde(skip)]
76 pub unprocessed_commands: Vec<ArtifactCommand>,
77 pub commands: Vec<ArtifactCommand>,
79 pub operations: Vec<Operation>,
82}
83
84#[cfg(not(feature = "artifact-graph"))]
85#[derive(Debug, Clone, Default, PartialEq, Serialize)]
86pub struct ModuleArtifactState {}
87
88#[derive(Debug, Clone)]
89pub(super) struct ModuleState {
90 pub id_generator: IdGenerator,
92 pub stack: Stack,
93 pub pipe_value: Option<KclValue>,
96 pub being_declared: Option<String>,
100 pub sketch_block: Option<SketchBlockState>,
102 pub inside_stdlib: bool,
105 pub module_exports: Vec<String>,
107 pub settings: MetaSettings,
109 pub(super) explicit_length_units: bool,
110 pub(super) path: ModulePath,
111 pub artifacts: ModuleArtifactState,
113
114 pub(super) allowed_warnings: Vec<&'static str>,
115 pub(super) denied_warnings: Vec<&'static str>,
116}
117
118#[derive(Debug, Clone, Default)]
119pub(super) struct SketchBlockState {
120 pub sketch_vars: Vec<KclValue>,
121 pub constraints: Vec<kcl_ezpz::Constraint>,
122}
123
124impl ExecState {
125 pub fn new(exec_context: &super::ExecutorContext) -> Self {
126 ExecState {
127 global: GlobalState::new(&exec_context.settings),
128 mod_local: ModuleState::new(ModulePath::Main, ProgramMemory::new(), Default::default()),
129 }
130 }
131
132 pub(super) fn reset(&mut self, exec_context: &super::ExecutorContext) {
133 let global = GlobalState::new(&exec_context.settings);
134
135 *self = ExecState {
136 global,
137 mod_local: ModuleState::new(self.mod_local.path.clone(), ProgramMemory::new(), Default::default()),
138 };
139 }
140
141 pub fn err(&mut self, e: CompilationError) {
143 self.global.errors.push(e);
144 }
145
146 pub fn warn(&mut self, mut e: CompilationError, name: &'static str) {
148 debug_assert!(annotations::WARN_VALUES.contains(&name));
149
150 if self.mod_local.allowed_warnings.contains(&name) {
151 return;
152 }
153
154 if self.mod_local.denied_warnings.contains(&name) {
155 e.severity = Severity::Error;
156 } else {
157 e.severity = Severity::Warning;
158 }
159
160 self.global.errors.push(e);
161 }
162
163 pub fn warn_experimental(&mut self, feature_name: &str, source_range: SourceRange) {
164 let Some(severity) = self.mod_local.settings.experimental_features.severity() else {
165 return;
166 };
167 let error = CompilationError {
168 source_range,
169 message: format!("Use of {feature_name} is experimental and may change or be removed."),
170 suggestion: None,
171 severity,
172 tag: crate::errors::Tag::None,
173 };
174
175 self.global.errors.push(error);
176 }
177
178 pub fn clear_units_warnings(&mut self, source_range: &SourceRange) {
179 self.global.errors = std::mem::take(&mut self.global.errors)
180 .into_iter()
181 .filter(|e| {
182 e.severity != Severity::Warning
183 || !source_range.contains_range(&e.source_range)
184 || e.tag != crate::errors::Tag::UnknownNumericUnits
185 })
186 .collect();
187 }
188
189 pub fn errors(&self) -> &[CompilationError] {
190 &self.global.errors
191 }
192
193 pub async fn into_exec_outcome(self, main_ref: EnvironmentRef, ctx: &ExecutorContext) -> ExecOutcome {
197 ExecOutcome {
200 variables: self.mod_local.variables(main_ref),
201 filenames: self.global.filenames(),
202 #[cfg(feature = "artifact-graph")]
203 operations: self.global.root_module_artifacts.operations,
204 #[cfg(feature = "artifact-graph")]
205 artifact_graph: self.global.artifacts.graph,
206 errors: self.global.errors,
207 default_planes: ctx.engine.get_default_planes().read().await.clone(),
208 }
209 }
210
211 pub(crate) fn stack(&self) -> &Stack {
212 &self.mod_local.stack
213 }
214
215 pub(crate) fn mut_stack(&mut self) -> &mut Stack {
216 &mut self.mod_local.stack
217 }
218
219 pub fn next_uuid(&mut self) -> Uuid {
220 self.mod_local.id_generator.next_uuid()
221 }
222
223 pub fn id_generator(&mut self) -> &mut IdGenerator {
224 &mut self.mod_local.id_generator
225 }
226
227 #[cfg(feature = "artifact-graph")]
228 pub(crate) fn add_artifact(&mut self, artifact: Artifact) {
229 let id = artifact.id();
230 self.mod_local.artifacts.artifacts.insert(id, artifact);
231 }
232
233 pub(crate) fn push_op(&mut self, op: Operation) {
234 #[cfg(feature = "artifact-graph")]
235 self.mod_local.artifacts.operations.push(op);
236 #[cfg(not(feature = "artifact-graph"))]
237 drop(op);
238 }
239
240 #[cfg(feature = "artifact-graph")]
241 pub(crate) fn push_command(&mut self, command: ArtifactCommand) {
242 self.mod_local.artifacts.unprocessed_commands.push(command);
243 #[cfg(not(feature = "artifact-graph"))]
244 drop(command);
245 }
246
247 pub(super) fn next_module_id(&self) -> ModuleId {
248 ModuleId::from_usize(self.global.path_to_source_id.len())
249 }
250
251 pub(super) fn id_for_module(&self, path: &ModulePath) -> Option<ModuleId> {
252 self.global.path_to_source_id.get(path).cloned()
253 }
254
255 pub(super) fn add_path_to_source_id(&mut self, path: ModulePath, id: ModuleId) {
256 debug_assert!(!self.global.path_to_source_id.contains_key(&path));
257 self.global.path_to_source_id.insert(path, id);
258 }
259
260 pub(crate) fn add_root_module_contents(&mut self, program: &crate::Program) {
261 let root_id = ModuleId::default();
262 let path = self
264 .global
265 .path_to_source_id
266 .iter()
267 .find(|(_, v)| **v == root_id)
268 .unwrap()
269 .0
270 .clone();
271 self.add_id_to_source(
272 root_id,
273 ModuleSource {
274 path,
275 source: program.original_file_contents.to_string(),
276 },
277 );
278 }
279
280 pub(super) fn add_id_to_source(&mut self, id: ModuleId, source: ModuleSource) {
281 self.global.id_to_source.insert(id, source);
282 }
283
284 pub(super) fn add_module(&mut self, id: ModuleId, path: ModulePath, repr: ModuleRepr) {
285 debug_assert!(self.global.path_to_source_id.contains_key(&path));
286 let module_info = ModuleInfo { id, repr, path };
287 self.global.module_infos.insert(id, module_info);
288 }
289
290 pub fn get_module(&mut self, id: ModuleId) -> Option<&ModuleInfo> {
291 self.global.module_infos.get(&id)
292 }
293
294 #[cfg(all(test, feature = "artifact-graph"))]
295 pub(crate) fn modules(&self) -> &ModuleInfoMap {
296 &self.global.module_infos
297 }
298
299 #[cfg(all(test, feature = "artifact-graph"))]
300 pub(crate) fn root_module_artifact_state(&self) -> &ModuleArtifactState {
301 &self.global.root_module_artifacts
302 }
303
304 pub fn current_default_units(&self) -> NumericType {
305 NumericType::Default {
306 len: self.length_unit(),
307 angle: self.angle_unit(),
308 }
309 }
310
311 pub fn length_unit(&self) -> UnitLength {
312 self.mod_local.settings.default_length_units
313 }
314
315 pub fn angle_unit(&self) -> UnitAngle {
316 self.mod_local.settings.default_angle_units
317 }
318
319 pub(super) fn circular_import_error(&self, path: &ModulePath, source_range: SourceRange) -> KclError {
320 KclError::new_import_cycle(KclErrorDetails::new(
321 format!(
322 "circular import of modules is not allowed: {} -> {}",
323 self.global
324 .mod_loader
325 .import_stack
326 .iter()
327 .map(|p| p.to_string_lossy())
328 .collect::<Vec<_>>()
329 .join(" -> "),
330 path,
331 ),
332 vec![source_range],
333 ))
334 }
335
336 pub(crate) fn pipe_value(&self) -> Option<&KclValue> {
337 self.mod_local.pipe_value.as_ref()
338 }
339
340 pub(crate) fn error_with_outputs(
341 &self,
342 error: KclError,
343 main_ref: Option<EnvironmentRef>,
344 default_planes: Option<DefaultPlanes>,
345 ) -> KclErrorWithOutputs {
346 let module_id_to_module_path: IndexMap<ModuleId, ModulePath> = self
347 .global
348 .path_to_source_id
349 .iter()
350 .map(|(k, v)| ((*v), k.clone()))
351 .collect();
352
353 KclErrorWithOutputs::new(
354 error,
355 self.errors().to_vec(),
356 main_ref
357 .map(|main_ref| self.mod_local.variables(main_ref))
358 .unwrap_or_default(),
359 #[cfg(feature = "artifact-graph")]
360 self.global.root_module_artifacts.operations.clone(),
361 #[cfg(feature = "artifact-graph")]
362 Default::default(),
363 #[cfg(feature = "artifact-graph")]
364 self.global.artifacts.graph.clone(),
365 module_id_to_module_path,
366 self.global.id_to_source.clone(),
367 default_planes,
368 )
369 }
370
371 #[cfg(feature = "artifact-graph")]
372 pub(crate) async fn build_artifact_graph(
373 &mut self,
374 engine: &Arc<Box<dyn EngineManager>>,
375 program: NodeRef<'_, crate::parsing::ast::types::Program>,
376 ) -> Result<(), KclError> {
377 let mut new_commands = Vec::new();
378 let mut new_exec_artifacts = IndexMap::new();
379 for module in self.global.module_infos.values_mut() {
380 match &mut module.repr {
381 ModuleRepr::Kcl(_, Some(outcome)) => {
382 new_commands.extend(outcome.artifacts.process_commands());
383 new_exec_artifacts.extend(outcome.artifacts.artifacts.clone());
384 }
385 ModuleRepr::Foreign(_, Some((_, module_artifacts))) => {
386 new_commands.extend(module_artifacts.process_commands());
387 new_exec_artifacts.extend(module_artifacts.artifacts.clone());
388 }
389 ModuleRepr::Root | ModuleRepr::Kcl(_, None) | ModuleRepr::Foreign(_, None) | ModuleRepr::Dummy => {}
390 }
391 }
392 new_commands.extend(self.global.root_module_artifacts.process_commands());
395 new_exec_artifacts.extend(self.global.root_module_artifacts.artifacts.clone());
398 let new_responses = engine.take_responses().await;
399
400 self.global.artifacts.artifacts.extend(new_exec_artifacts);
403
404 let initial_graph = self.global.artifacts.graph.clone();
405
406 let graph_result = crate::execution::artifact::build_artifact_graph(
408 &new_commands,
409 &new_responses,
410 program,
411 &mut self.global.artifacts.artifacts,
412 initial_graph,
413 );
414
415 let artifact_graph = graph_result?;
416 self.global.artifacts.graph = artifact_graph;
417
418 Ok(())
419 }
420
421 #[cfg(not(feature = "artifact-graph"))]
422 pub(crate) async fn build_artifact_graph(
423 &mut self,
424 _engine: &Arc<Box<dyn EngineManager>>,
425 _program: NodeRef<'_, crate::parsing::ast::types::Program>,
426 ) -> Result<(), KclError> {
427 Ok(())
428 }
429}
430
431impl GlobalState {
432 fn new(settings: &ExecutorSettings) -> Self {
433 let mut global = GlobalState {
434 path_to_source_id: Default::default(),
435 module_infos: Default::default(),
436 artifacts: Default::default(),
437 root_module_artifacts: Default::default(),
438 mod_loader: Default::default(),
439 errors: Default::default(),
440 id_to_source: Default::default(),
441 };
442
443 let root_id = ModuleId::default();
444 let root_path = settings.current_file.clone().unwrap_or_default();
445 global.module_infos.insert(
446 root_id,
447 ModuleInfo {
448 id: root_id,
449 path: ModulePath::Local {
450 value: root_path.clone(),
451 original_import_path: None,
452 },
453 repr: ModuleRepr::Root,
454 },
455 );
456 global.path_to_source_id.insert(
457 ModulePath::Local {
458 value: root_path,
459 original_import_path: None,
460 },
461 root_id,
462 );
463 global
464 }
465
466 pub(super) fn filenames(&self) -> IndexMap<ModuleId, ModulePath> {
467 self.path_to_source_id.iter().map(|(k, v)| ((*v), k.clone())).collect()
468 }
469
470 pub(super) fn get_source(&self, id: ModuleId) -> Option<&ModuleSource> {
471 self.id_to_source.get(&id)
472 }
473}
474
475impl ArtifactState {
476 #[cfg(feature = "artifact-graph")]
477 pub fn cached_body_items(&self) -> usize {
478 self.graph.item_count
479 }
480
481 pub(crate) fn clear(&mut self) {
482 #[cfg(feature = "artifact-graph")]
483 {
484 self.artifacts.clear();
485 self.graph.clear();
486 }
487 }
488}
489
490impl ModuleArtifactState {
491 pub(crate) fn clear(&mut self) {
492 #[cfg(feature = "artifact-graph")]
493 {
494 self.artifacts.clear();
495 self.unprocessed_commands.clear();
496 self.commands.clear();
497 self.operations.clear();
498 }
499 }
500
501 #[cfg(not(feature = "artifact-graph"))]
502 pub(crate) fn extend(&mut self, _other: ModuleArtifactState) {}
503
504 #[cfg(feature = "artifact-graph")]
506 pub(crate) fn extend(&mut self, other: ModuleArtifactState) {
507 self.artifacts.extend(other.artifacts);
508 self.unprocessed_commands.extend(other.unprocessed_commands);
509 self.commands.extend(other.commands);
510 self.operations.extend(other.operations);
511 }
512
513 #[cfg(feature = "artifact-graph")]
517 pub(crate) fn process_commands(&mut self) -> Vec<ArtifactCommand> {
518 let unprocessed = std::mem::take(&mut self.unprocessed_commands);
519 let new_module_commands = unprocessed.clone();
520 self.commands.extend(unprocessed);
521 new_module_commands
522 }
523}
524
525impl ModuleState {
526 pub(super) fn new(path: ModulePath, memory: Arc<ProgramMemory>, module_id: Option<ModuleId>) -> Self {
527 ModuleState {
528 id_generator: IdGenerator::new(module_id),
529 stack: memory.new_stack(),
530 pipe_value: Default::default(),
531 being_declared: Default::default(),
532 sketch_block: Default::default(),
533 module_exports: Default::default(),
534 explicit_length_units: false,
535 path,
536 settings: Default::default(),
537 artifacts: Default::default(),
538 allowed_warnings: Vec::new(),
539 denied_warnings: Vec::new(),
540 inside_stdlib: false,
541 }
542 }
543
544 pub(super) fn variables(&self, main_ref: EnvironmentRef) -> IndexMap<String, KclValue> {
545 self.stack
546 .find_all_in_env(main_ref)
547 .map(|(k, v)| (k.clone(), v.clone()))
548 .collect()
549 }
550}
551
552impl SketchBlockState {
553 pub(crate) fn next_sketch_var_id(&self) -> SketchVarId {
554 SketchVarId(self.sketch_vars.len())
555 }
556}
557
558#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq, ts_rs::TS)]
559#[ts(export)]
560#[serde(rename_all = "camelCase")]
561pub struct MetaSettings {
562 pub default_length_units: UnitLength,
563 pub default_angle_units: UnitAngle,
564 pub experimental_features: annotations::WarningLevel,
565 pub kcl_version: String,
566}
567
568impl Default for MetaSettings {
569 fn default() -> Self {
570 MetaSettings {
571 default_length_units: UnitLength::Millimeters,
572 default_angle_units: UnitAngle::Degrees,
573 experimental_features: annotations::WarningLevel::Deny,
574 kcl_version: "1.0".to_owned(),
575 }
576 }
577}
578
579impl MetaSettings {
580 pub(crate) fn update_from_annotation(
581 &mut self,
582 annotation: &crate::parsing::ast::types::Node<Annotation>,
583 ) -> Result<(bool, bool), KclError> {
584 let properties = annotations::expect_properties(annotations::SETTINGS, annotation)?;
585
586 let mut updated_len = false;
587 let mut updated_angle = false;
588 for p in properties {
589 match &*p.inner.key.name {
590 annotations::SETTINGS_UNIT_LENGTH => {
591 let value = annotations::expect_ident(&p.inner.value)?;
592 let value = super::types::length_from_str(value, annotation.as_source_range())?;
593 self.default_length_units = value;
594 updated_len = true;
595 }
596 annotations::SETTINGS_UNIT_ANGLE => {
597 let value = annotations::expect_ident(&p.inner.value)?;
598 let value = super::types::angle_from_str(value, annotation.as_source_range())?;
599 self.default_angle_units = value;
600 updated_angle = true;
601 }
602 annotations::SETTINGS_VERSION => {
603 let value = annotations::expect_number(&p.inner.value)?;
604 self.kcl_version = value;
605 }
606 annotations::SETTINGS_EXPERIMENTAL_FEATURES => {
607 let value = annotations::expect_ident(&p.inner.value)?;
608 let value = annotations::WarningLevel::from_str(value).map_err(|_| {
609 KclError::new_semantic(KclErrorDetails::new(
610 format!(
611 "Invalid value for {} settings property, expected one of: {}",
612 annotations::SETTINGS_EXPERIMENTAL_FEATURES,
613 annotations::WARN_LEVELS.join(", ")
614 ),
615 annotation.as_source_ranges(),
616 ))
617 })?;
618 self.experimental_features = value;
619 }
620 name => {
621 return Err(KclError::new_semantic(KclErrorDetails::new(
622 format!(
623 "Unexpected settings key: `{name}`; expected one of `{}`, `{}`",
624 annotations::SETTINGS_UNIT_LENGTH,
625 annotations::SETTINGS_UNIT_ANGLE
626 ),
627 vec![annotation.as_source_range()],
628 )));
629 }
630 }
631 }
632
633 Ok((updated_len, updated_angle))
634 }
635}