Commit Graph

99 Commits

Author SHA1 Message Date
Michael Schimmel e30444e89b Refactor math functions to a separate module
Move the `register_math` function and its associated logic into a new
`math` module. This improves organization and separation of concerns
within the RTL AST.
2026-02-22 09:39:13 +01:00
Michael Schimmel e3bfc01027 Fix: Correctly determine lambda purity
The purity of a lambda definition should always be `Pure`. The purity of
the lambda's body is only relevant when the lambda is called. This
change ensures that defining a lambda does not incorrectly mark the
parent scope as impure.
2026-02-22 09:20:44 +01:00
Michael Schimmel 6605f56756 feat: Add fastrand for PRNG support
Replaces the custom LCG implementation with `fastrand` for improved
random number generation.
This commit introduces the `fastrand` crate to the project for robust
and efficient pseudo-random number generation.
The `Environment` struct now includes a `prng` field to hold the random
number generator.
The built-in `random` function now utilizes this PRNG for generating
floating-point random numbers.
A new `seed!` function is added to allow users to seed the PRNG for
deterministic random sequences.
This change enhances the randomness capabilities of the language, making
it suitable for simulations and other applications requiring good
quality random numbers.
2026-02-22 09:08:16 +01:00
Michael Schimmel df06c51205 Add math and random functions
Register `abs`, `min`, `max`, and `random` functions to the environment.
This also includes adding `PartialOrd` implementation for `Value` to
support comparisons for `min` and `max`.
2026-02-22 08:49:14 +01:00
Michael Schimmel 589c13896f Add now() built-in function
This commit introduces the `now()` built-in function, which returns the
current timestamp in milliseconds. It also includes an integration test
to ensure that `now()` is not constant-folded during AST dumping,
verifying its non-deterministic nature.
2026-02-22 08:44:49 +01:00
Michael Schimmel f35606616b Introduce Purity enum for optimizer
Refactor the optimizer to use a Purity enum instead of a boolean for
tracking function purity. This allows for a more granular representation
of purity:

- `Impure`: Functions with side effects.
- `SideEffectFree`: Functions without side effects but may not be
  deterministic (e.g., `now()`, `random()`).
- `Pure`: Functions without side effects and are deterministic.

This change enhances the optimizer's ability to perform more aggressive
optimizations by accurately determining function purity.
2026-02-22 08:42:22 +01:00
Michael Schimmel 329b885c4b Formatting 2026-02-22 02:35:06 +01:00
Michael Schimmel 2123f1d279 feat: Add typed lambda registry to Optimizer
The Optimizer now accepts a typed lambda registry, allowing it to
perform more aggressive inlining and beta-reduction on globally defined
functions. This is a significant step towards optimizing recursive and
globally defined lambdas more effectively.
2026-02-22 02:31:55 +01:00
Michael Schimmel 8e8d85c06c Add .snap files to .geminiignore 2026-02-22 02:07:35 +01:00
Michael Schimmel e5841976c4 Refactor: Collect global usage in optimizer
Introduce `collect_usage` to gather both local and global variable
usage. This enables dead code elimination for unused global definitions.
2026-02-22 02:07:00 +01:00
Michael Schimmel 7c997ca841 Refactor: Replace optimization level with boolean flag
The optimizer now uses a simple boolean flag (`optimization`) instead of
an `optimization_level` (u32). This simplifies the optimizer's logic and
makes it easier to enable or disable optimizations.

The command-line interface and internal testing have been updated to
reflect this change.
2026-02-22 01:50:14 +01:00
Michael Schimmel d3cdafbb85 Merge branch 'main' of http://192.168.178.103:3000/Brummel/RustAst 2026-02-22 01:39:30 +01:00
Michael Schimmel 0ed52a811d Update benchmark metadata
This commit updates the benchmark metadata in several example files. The
benchmark runs and repeat counts have been slightly adjusted due to
optimizations or minor variations in execution.
2026-02-22 01:37:00 +01:00
Michael Schimmel 5790880002 Add benchmark metadata to examples 2026-02-22 00:43:39 +01:00
Michael Schimmel e87f7232e6 Refactor: Improve optimizer and DCE
This commit introduces several improvements to the AST optimizer and
dead code elimination (DCE) logic:

- **Address Mapping:** The `Get` and `Set` operations now correctly map
  local slots and global indices using the substitution map, ensuring
  that remapped variables are handled properly.
- **Parameter Slot Mapping:** Parameters are now explicitly mapped to
  their correct slots within the `Lambda` node, preventing potential
  slot reassignments in the function body.
- **Pure Function Analysis:** The `is_pure` function has been refined to
  accurately identify pure expressions, including calls to pure
  functions and stateless closures.
- **Dead Code Elimination:** DCE logic in `BoundKind::Block` has been
  enhanced to remove unused local definitions and pure, non-essential
  expressions more effectively. Global DCE is also improved.
- **Constant Folding:** `try_fold_pure` is now more robust in folding
  pure function calls with constant arguments, reducing redundant
  computations.
- **Beta Reduction:** `try_beta_reduce` has been updated to avoid
  reducing if the body contains `DefLocal`, ensuring correctness.
- **Global Purity Tracking:** A `global_purity` map is introduced to
  track the purity of global values, enabling better optimization of
  global accesses.
- **Inlinable Value Check:** The `is_inlinable_value` check now
  correctly identifies stateless closures for inlining.
- **Optimization Level:** The default `optimization_level` is set to 2
  to enable more aggressive optimizations.
2026-02-22 00:41:02 +01:00
Michael Schimmel 0107264026 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.
2026-02-22 00:16:04 +01:00
Michael Schimmel ac73aaf59e Feat: Add global inlining and DCE
Introduce global inlining by allowing the Optimizer to access the
environment's global values. This enables replacing global variable
accesses with their constant values when appropriate.

Additionally, implement Dead Code Elimination (DCE) for global
definitions. A global definition can be removed if it was only used for
inlining within the current script and has no side effects.

Update the Optimizer struct to hold an optional reference to the global
values and modify the `Optimizer::new` constructor to accept this. The
`Environment::specialize` and `Environment::compile_script` methods are
updated to pass the global values to the optimizer.

The `SubstitutionMap` is also updated to track script-local global
substitutions and used global indices, supporting both global inlining
and global DCE.
2026-02-21 23:29:24 +01:00
Michael Schimmel 1edc2e56e4 Update compiler optimizer test snapshots 2026-02-21 22:57:16 +01:00
Michael Schimmel 1aa844db22 feat: Add dead code elimination and improve optimization
Integrates dead code elimination (DCE) into the optimizer.
This phase removes expressions that have no side effects and are not the
result of the block.

Also includes several improvements to the existing optimization passes:
- DCE now correctly handles assignments to variables that are never used
  or captured.
- Explicitly tracks captured slots in the `SubstitutionMap` to prevent
  premature inlining.
- Introduces a `is_side_effect_free` helper function for more robust
  purity checks.
- Updates dependencies to include `insta` for snapshot testing.
2026-02-21 22:51:53 +01:00
Michael Schimmel b2e010e755 Add example and refine local inlining
Add a new example file `examples/def_local_inlining.myc` to demonstrate
and test local inlining.

Refine the optimizer to handle local inlining more robustly by:
- Passing a `SubstitutionMap` to `visit_node` to track local variable
  substitutions.
- Enabling constant propagation for local variables by checking
  `sub.locals` in `BoundKind::Get`.
- Updating `BoundKind::DefLocal` and `BoundKind::Set` to maintain the
  substitution map for local variables.
- Adjusting `try_beta_reduce` to use the optimizer's `visit_node` with
  the substitution map for inlining.
- Re-indexing upvalues correctly when inlining captured variables into
  lambdas.
2026-02-21 22:35:04 +01:00
Michael Schimmel ff1024ee49 Remove is_tail field from Call node
The `is_tail` field on the `BoundKind::Call` node was an intermediate
representation for tail-call optimization and is no longer needed as a
distinct field. The TCO pass now embeds this information directly into
the `RuntimeMetadata` of the `ExecNode`, which is the VM's internal AST.
This simplifies the `BoundKind::Call` structure and removes redundant
information.
2026-02-21 22:01:03 +01:00
Michael Schimmel 78dff07930 Refactor Call to include is_tail flag
The `TailCall` variant has been merged into `Call` by adding an
`is_tail` boolean field. This simplifies the AST by reducing the number
of node variants and makes it easier to handle tail call optimization.
The `tco.rs` module is responsible for setting this flag when a call
occurs in tail position.
2026-02-21 21:12:20 +01:00
Michael Schimmel f980d9befc Add PartialEq to BoundKind and Value
Implement PartialEq for BoundKind to allow for structural equality
checks during optimization. This enables the optimizer to terminate
early when a node no longer changes.

Also, implement PartialEq for Value to facilitate comparisons between
different Value variants.
2026-02-21 20:01:58 +01:00
Michael Schimmel 56b8e8389b Refactor optimizer with aggressive collapsing
The optimizer has been refactored to separate its phases and introduce
more aggressive collapsing capabilities.

Phase 2 (Cracking) now focuses on stateless transformations, making it
easier to reason about and potentially parallelize.

Phase 2.5 (Aggressive Collapsing) has been introduced, enabling
optimizations like:
- Beta-reduction for lambda literals.
- Cracking and inlining for constant closures.
- Folding intrinsics for constant arithmetic.
- Short-circuiting conditional expressions.

These changes aim to improve performance by reducing redundant
computations and code bloat. The maximum number of optimization passes
has also been increased to 5 to allow for more complex transformations.
2026-02-21 19:42:09 +01:00
Michael Schimmel 0bbe35eeec feat: Implement closure cracking and inlining
Introduces a new optimizer pass that can "crack" closures, allowing for
more aggressive specialization. It also enables inlining of upvalues
that point to immutable global variables. This removes overhead for
higher-order functions and currying when arguments are statically
resolvable.
2026-02-21 18:49:55 +01:00
Michael Schimmel 74ea38248e Update testing and debugging instructions 2026-02-21 16:04:46 +01:00
Michael Schimmel ef67a3d60d feat: Add tracing support to AST tool
Introduce a `--trace` flag to the AST tool that enables the
TracingObserver.
This allows users to see the detailed execution steps of their scripts.
Also, refactor the main and AST tool to better handle tracing and
performance
statistics.
2026-02-21 16:02:44 +01:00
Michael Schimmel d645f36700 clippy 2026-02-21 15:18:29 +01:00
Michael Schimmel 9222fb5bc8 Update benchmark results
The benchmark results in several example files have been updated. This
commit reflects slight variations in performance measurements for
various test cases, including closure scope, data structures,
destructuring, higher-order functions, macros, and tuple operations.
2026-02-21 15:16:36 +01:00
Michael Schimmel dc81b7d616 Refactor Tuple and Record to share ValueList
Introduce `ValueList` type alias for `Rc<Vec<Value>>` to reduce
boilerplate and improve readability.
Add `as_slice()` method to `Value` to provide a unified way to access
the underlying values of Tuples and Records.
Update `flatten_value` and `unpack` in `VM` to utilize the new
`as_slice()` method, simplifying logic and improving consistency.
2026-02-21 15:16:04 +01:00
Michael Schimmel 26f0ed9531 Refactor example tests into dedicated function
Extracts the logic for running example tests into a new function
`run_functional_tests` in `src/utils/tester.rs`. This function is then
called from `src/integration_test.rs` to simplify the test setup and
improve code organization. The changes also adjust the benchmark
thresholds slightly.
2026-02-21 15:01:36 +01:00
Michael Schimmel 212afd76df Refactor: Rename map to record
This commit renames `Map` to `Record` and updates all related AST nodes,
binders, type checkers, and runtime values to reflect this change. This
is a semantic change to better align with common programming language
terminology.
2026-02-21 14:51:34 +01:00
Michael Schimmel c641816b57 Update tester.rs 2026-02-20 17:21:09 +01:00
Michael Schimmel 772a0fb8ab Update record_unpack example 2026-02-20 17:18:45 +01:00
Michael Schimmel 9072cfaa14 Update benchmark timings and repeats
This commit updates the benchmark timings and repeat counts for various
example files. The `tester.rs` script has been modified to:

- Correctly parse and use the `Benchmark-Repeat` directive.
- Implement an adaptive sampling mechanism for more accurate median
  calculation, especially for long-running benchmarks.
- Improve the logic for updating and inserting benchmark and repeat
  lines in example files.
- Handle potential compilation and runtime errors during benchmark
  execution.
2026-02-20 17:11:13 +01:00
Michael Schimmel 87259584ee Refactor Record and List to use TupleData
The `Value::List` and `Value::Record` variants have been consolidated
into a single `Value::Tuple` variant. This new variant uses a
`TupleData` struct to store values and an optional `Rc<Vec<Keyword>>`
for keys, allowing it to represent both ordered lists/tuples and
key-value records.

This change simplifies the internal representation and improves
performance by allowing schema sharing for records. It also includes
updates to the compiler, runtime, and tests to reflect the new
structure.
2026-02-20 16:13:38 +01:00
Michael Schimmel ca2c85a8a4 Refactor map and record representation
Replaces the use of `BTreeMap` and `HashMap` for maps and records with
`Vec<(Keyword, Value)>` and `Vec<(Keyword, StaticType)>`. This
simplifies the data structure and allows for ordered iteration, which is
important for serialization and comparison.

Also updates the `unpack` function in `vm.rs` to handle records as well
as lists when destructuring.
2026-02-20 15:10:07 +01:00
Michael Schimmel 6ddc1c0a11 Add instruction for self-testing scripts 2026-02-20 15:00:45 +01:00
Michael Schimmel 4e812c1afb Add positional_count field to Lambda
This field is used for static optimization, determining if parameters
are purely positional.
2026-02-20 14:40:56 +01:00
Michael Schimmel 56d6c3bbde Add documentation for destructuring 2026-02-20 12:19:48 +01:00
Michael Schimmel e2279f214b Refactor lambda binding and parameter handling
Introduce `BoundKind::Parameter` to represent function parameters.
Modify `Binder` to correctly handle parameters within lambda
definitions, ensuring they are only defined within function scopes.
Update `LambdaCollector` to register the body of lambdas as templates
for global definitions.
Adjust `Dumper` to accurately represent lambda parameters.
Update `Specializer` and `TCO` to handle the new `BoundKind::Parameter`.
Refactor `Call` and `TailCall` to use a single `args` node, often a
tuple.
Adjust type signatures in RTL to use `StaticType::Tuple` for function
parameters.
2026-02-20 12:14:22 +01:00
Michael Schimmel 8d6d3be5c2 Skip benchmarks with negligible baseline 2026-02-20 10:24:14 +01:00
Michael Schimmel 4f849ebd34 Refactor type inference for collections
The type inference for collections (tuples, vectors, matrices, and
records) has been significantly refactored.

This includes:
- Introducing distinct `StaticType` variants for `Tuple`, `Vector`, and
  `Matrix`.
- Implementing more robust type checking for these collections, allowing
  for homogeneous and heterogeneous structures.
- Enhancing the `is_assignable_from` method to handle type compatibility
  between these collection types.
- Updating `Value::static_type()` to correctly infer the types of
  literal collections.
- Improving the type inference for map literals to create `Record`
  types.
- Adding comprehensive unit tests for tuple, vector, matrix, and record
  type inference.
2026-02-20 10:19:29 +01:00
Michael Schimmel 494bf554d2 Old Docs added 2026-02-20 10:09:22 +01:00
Michael Schimmel cd3f63632f Update expected output for tak.myc benchmark 2026-02-20 00:59:31 +01:00
Michael Schimmel 30cd73aa63 Refactor multiple if let bindings
Replaces nested `if let` statements with sequential `if let` bindings,
improving readability. This change is applied to `Dumper::log_bound`,
`LambdaCollector::visit_bound`, `Environment::call_object_method`, and
`Environment::call_lambda`.
2026-02-19 23:56:50 +01:00
Michael Schimmel 1331aabbe1 Update benchmarks and adjust tests
This commit updates benchmark values in several example files to reflect
minor performance variations. It also includes adjustments to
integration
and unit tests to align with recent changes in the type checking and VM
logic, specifically concerning closures and TCO handling.
2026-02-19 23:54:53 +01:00
Michael Schimmel 3c0f2ec8ce - Adjust Environment to use BoundNode for the function registry and
correctly initialize the `TypeChecker` with argument types during
  macro expansion.
- Refactor `Specializer::compile` to perform type checking with provided
  arguments before specialization and to correctly extract the return
  type.
- Enhance the `Dumper` to introspect and display specialized closure
  bodies.
- Update `LambdaCollector` to use `BoundNode` consistently.
- Modify `TypeChecker` to accept and inject specialized argument types
  for lambdas.
2026-02-19 23:18:04 +01:00
Michael Schimmel 16d9d41e3d Add LambdaCollector and Intrinsic Lookup 2026-02-19 22:26:33 +01:00
Michael Schimmel 1b49719d31 feat: Add lambda collection and specialization
Introduces `LambdaCollector` to gather lambda functions and populate the
function registry. This enables the `Specializer` to work with
user-defined functions.

The `Environment` struct is updated to manage the `function_registry`
and `monomorph_cache`, which are essential for the specialization
process.

The `link` method in `Environment` now incorporates lambda collection
and node specialization before applying TCO optimization. This ensures
that lambdas are properly processed and specialized for potential
performance gains.

The `Specializer`'s `new` constructor has been modified to accept and
initialize the `MonoCache` through an `Rc<RefCell<MonoCache>>`. This
allows the cache to be shared across different specialized functions.

Also includes minor refactoring and type adjustments in `specializer.rs`
for better clarity and consistency.
2026-02-19 22:26:24 +01:00