feat: Implement purity inference for optimization

This commit introduces purity inference to the compiler's optimizer.
This allows for more aggressive constant folding and dead code
elimination by tracking which functions and global variables
are free of side effects.

Key changes include:

- Added `global_purity` field to `Optimizer` and `Environment`.
- Modified `Optimizer::is_pure` to recursively determine if an
  AST node represents a pure computation.
- Introduced `Optimizer::try_fold_pure` to replace the old
  `try_fold_intrinsic`, enabling folding of pure function calls
  with constant arguments.
- Updated `Environment::register_native` and
  `Environment::register_constant`
  to optionally record purity.
- Added purity flags to several built-in functions in `core.rs` and
  `datetime.rs`.
- A new example `optimizer_purity.myc` demonstrates the new feature.
This commit is contained in:
Michael Schimmel
2026-02-22 00:16:04 +01:00
parent ac73aaf59e
commit 0107264026
6 changed files with 184 additions and 73 deletions
+5
View File
@@ -0,0 +1,5 @@
(do
(def PI 3.1415)
(def area (fn [x] (* PI x)))
(area 10)
)