Commit Graph

309 Commits

Author SHA1 Message Date
Michael Schimmel 961168f3b6 Refactor analyzer to return impurity
The `Analyzer` is now responsible for determining the purity of `Define`
bindings. Previously, this responsibility was left to the caller.

Added a new example `design-flaw.myc` to test this change.
Updated `soa_series.myc` due to the purity change.
2026-03-10 16:13:39 +01:00
Michael Schimmel 348b1686f2 Refactor soa_series example to use a constant for ticks 2026-03-10 15:34:10 +01:00
Michael Schimmel 8339ee413e Doc: Use exhaustive matching in LambdaCollector 2026-03-09 11:49:17 +01:00
Michael Schimmel 11fc1e0e48 Add 'again' macro for recursive expansion
The 'again' macro is a new addition to the macro system, allowing for
recursive macro expansion. This commit introduces the necessary AST
nodes, macro expansion logic, and an integration test to ensure its
functionality. The `soa_series.myc` example has also been updated to
demonstrate its usage.
2026-03-09 11:22:32 +01:00
Michael Schimmel fa23a4c125 Refactor UntypedKind::Parameter to Identifier
The `Parameter` kind in `UntypedKind` was only used for identifiers that
were being declared or referenced. This commit renames it to
`Identifier` and updates all the necessary code to reflect this change.
This simplifies the AST and makes it more consistent.

Additionally, a new macro `repeat` has been added to
`src/ast/system.myc`.
2026-03-09 11:08:30 +01:00
Michael Schimmel cb4507b2da Refactor AST-Tool evaluation logic
Consolidate AST source code evaluation logic to handle script strings,
file paths, and stdin more uniformly. Introduce a `read_stdin` helper
function.
2026-03-09 10:36:07 +01:00
Michael Schimmel 66171b0802 Fix: Implement ~ and ~@ syntax to prevent parser deadlock
Adds parsing for the `~` (placeholder) and `~@` (splice) syntax.

- `~expr` parses as a placeholder node.
- `~@expr` parses as a splice node.
2026-03-08 13:45:14 +01:00
Michael Schimmel 6f2d9b0e47 Create system.myc 2026-03-08 13:10:00 +01:00
Michael Schimmel b622f7f8bd Remove unused system library and simplify environment initialization
The `system.myc` file, containing macros and core utilities, has been
removed as its functionality is now handled by an embedded string. This
simplifies the build process and eliminates a file that was no longer
strictly necessary.

Additionally, the `with_cwd()` method on the `Environment` has been
removed. The environment now automatically adds the current working
directory and its `rtl` subdirectory to the search paths during
initialization. This streamlines the setup for file-based usage and
ensures standard search paths are always considered.
2026-03-08 13:09:53 +01:00
Michael Schimmel 4dfdc75545 Refactor: Introduce system library
- Add `rtl/system.myc` as the system library, containing core utilities
  like `while` and `cache`.
- Remove the duplicate `prelude.myc` from `src/ast/rtl/`.
- Modify `Environment::collect_dependencies` to implicitly add `#use
  system` when necessary.
- Update `Environment::with_cwd` to also add the `rtl` subdirectory to
  search paths.
- Add `target/` to `.geminiignore` to exclude build artifacts.
- Adjust example `sma.myc` to use the new `cache` macro and reduce
  sample data size.
- Update `rtl/sma.myc` to correctly initialize the `history` series with
  its `length`.
- Add `preload_dependencies` calls to `dump_ast` and `run_script` for
  proper module loading.
2026-03-08 12:09:32 +01:00
Michael Schimmel 595bcf09e5 Update examples to use StreamNode and Series with lookback
The `PipelineNode` has been refactored into `StreamNode`. This commit
updates the example files to reflect this change and also updates the
`series` constructor to accept a lookback parameter.

The `PipelineNode` was an artifact of a previous implementation and is
no longer needed. The `StreamNode` is the appropriate type for
representing the output of a pipe operation.

The `series` function now requires a `lookback` argument to specify the
maximum number of items to store. This makes the behavior of series more
explicit and prevents accidental unbounded memory growth.
2026-03-07 23:48:09 +01:00
Michael Schimmel d08fab4f73 Refactor inlining logic into try_inline function
This commit extracts the logic for inlining function calls into a new
private
method, `try_inline`. This improves code organization and reduces
duplication in the `Optimizer`'s `visit_node` method.

The extracted method handles the preparation of substitutions and the
recursive visiting of the inlined node. It also ensures that the
substitution map state is correctly synchronized back to the calling
context.
2026-03-07 21:10:48 +01:00
Michael Schimmel f88992da61 Add Stream support for field accessors
The type checker now correctly handles unwrapping `Stream(T)` types for
lambda arguments.
A new `build_map_stream` function has been added to `rtl/streams.rs` to
facilitate field access on streams.
The `create-random-ohlc` function now returns a `Stream` instead of a
`Series`.
Examples have been updated to reflect these changes and demonstrate
stream usage.
2026-03-07 20:47:05 +01:00
Michael Schimmel 2023df2f62 Add plan for automatic documentation generation 2026-03-07 00:07:54 +01:00
Michael Schimmel 00ce13b17e Refactor compile pipeline into helper function 2026-03-06 23:28:22 +01:00
Michael Schimmel f797ac37bf Refactor: Simplify type checking context
The type checker's fallback logic for non-lambda nodes was unnecessarily
complex. This commit simplifies it by directly checking the node within
a new, basic type context.

Additionally, the environment now explicitly wraps non-lambda AST nodes
in a lambda before type checking, ensuring consistent handling. This
aligns the type checking process for all top-level expressions.
2026-03-06 23:19:52 +01:00
Michael Schimmel 84ef3f9aed feat: Enhance #use directive for directories and cwd
The `#use` directive now supports referencing entire directories,
automatically including all `.myc` files within them. Additionally,
environments are now initialized with the current working directory as a
default search path, simplifying module resolution.
2026-03-06 21:07:27 +01:00
Michael Schimmel a59367ba61 Implement #use directive for dependency management
Introduces the `#use` preprocessor directive, allowing Myc scripts to
explicitly declare dependencies on other Myc libraries. This change
moves dependency management from a command-line argument to an in-script
declaration, ensuring scripts are self-contained and can correctly
resolve macros and other symbols.

Key features include:
- Declarations at the beginning of a file, evaluated before parsing.
- Support for relative paths and a new `->` separator.
- Automatic resolution of dependencies from specified search paths.
- Idempotent loading to prevent duplicate parsing and evaluation.
- No AST pollution; dependency management is a compile-time concern.

The compiler's lexer has been updated to recognize `#` as a comment
character, and the environment now manages search paths and loaded
modules. This lays the groundwork for more complex library structures
and improved code organization.
2026-03-06 20:43:45 +01:00
Michael Schimmel d4ca9c4620 Refactor SMA implementation to lib/sma.myc
Move the SMA implementation from examples/err.myc and examples/sma.myc
to a new dedicated file in lib/. This change also cleans up unused code
and improves the SMA logic.
2026-03-06 17:22:42 +01:00
Michael Schimmel 8fa2ca4cb5 Fix: Reindex address in Set bound kind 2026-03-06 17:02:43 +01:00
Michael Schimmel 7b6f6e52bd Fix: Improve series type handling and coercions
This commit enhances the series type handling in `src/ast/rtl/series.rs`
by introducing helper methods `as_float` and `as_int` in
`src/ast/types.rs` to manage type conversions more robustly.

It also updates the function overload resolution in `src/ast/types.rs`
to prioritize exact parameter matches before falling back to implicit
coercions.

Finally, the example `err.myc` has been updated to reflect a more
accurate implementation of a Simple Moving Average (SMA) calculation.
2026-03-06 16:31:50 +01:00
Michael Schimmel 7e3b000d99 Handle Int values in ScalarSeries<f64> push 2026-03-06 16:03:49 +01:00
Michael Schimmel dfc963ef13 Fix: Remove unnecessary Rc::new from LambdaCollector
The `LambdaCollector` was cloning nodes and wrapping them in `Rc`.
However, the `registry` itself is already holding `Rc` clones of the
nodes. This commit removes the redundant `Rc::new` to simplify the code.

This change also addresses a performance regression observed in
benchmarks due to unnecessary atomic overhead from `Arc` in the
`Keyword` type.
2026-03-06 14:18:51 +01:00
Michael Schimmel 1f5aefb216 Remove redundant tail call resolution
The `resolve_tail_calls` function is being called twice in some
execution paths, which is unnecessary and can lead to incorrect
behavior. This commit removes the redundant calls and simplifies the
execution flow.
2026-03-06 12:45:08 +01:00
Michael Schimmel 13dc6beb52 Refactor tail call resolution logic
The tail call resolution logic was duplicated in `Environment::call` and
`VM::run`. This commit extracts the tail call resolution logic into a
single method `VM::resolve_tail_calls` and uses it in both places.

Additionally, this commit adds support for series indexing as a form of
tail call, allowing for direct access to series elements through the
`series(index)` syntax. This is useful for back-referencing in
time-series data.

A new example `err.myc` is added to demonstrate basic series usage and
error handling.
2026-03-06 12:35:00 +01:00
Michael Schimmel b9d88f019e Remove unused err.myc example
Update benchmark output for soa_series.myc.
Also, fix the tester to not panic when a baseline is missing, and
instead just report it as missing.
2026-03-06 12:07:39 +01:00
Michael Schimmel 1342c7ca8e Fix optimizer slot clash during inlining and correct closure reduction
order

   - Add new_for_inlining to SubstitutionMap to preserve the next_slot
     counter, preventing local slot collisions when merging
     scopes.
   - Update Inliner to request fresh slots for parameters during beta
     reduction instead of forcing identity mappings.
   - Fix execution order in Optimizer::visit_node for closure inlining
     to run prepare_beta_reduction before visiting the function
     body, preventing corrupted upvalue captures.
   - Fix CLI --no-opt flag to correctly disable the optimizer in the ast
     binary.
   - Add integration test to prevent future slot clash regressions.
2026-03-06 11:58:28 +01:00
Michael Schimmel 3ded62d836 feat: Add --no-opt flag and a failing test case
Introduce a --no-opt flag to disable compiler optimizations and add a
new integration test case to reproduce a slot clash issue during
inlining.
2026-03-06 11:47:31 +01:00
Michael Schimmel 32a1f21463 Add language specification document
This commit introduces the BNF for the Myc language, along with its core
semantics, data types, and evaluation logic. It also details the macro
system and provides an overview of the standard library. This document
serves as a foundational context for LLMs to understand and generate Myc
code.
2026-03-06 11:35:18 +01:00
Michael Schimmel 9ba4f795d1 Change series field type from :msg to :text
Update example to use :text for string fields in series. This aligns
with the new :text type support in the series implementation.
2026-03-05 15:07:31 +01:00
Michael Schimmel 6ebf31e2a0 Refactor specializer to not fold scalars
The specializer now correctly identifies when a compiled value is a
scalar and avoids folding it into a constant node. Instead, it updates
the callee to the specialized version if it's an object. This ensures
that scalar folding does not interfere with the program's execution.
2026-03-05 14:35:29 +01:00
Michael Schimmel 831525b402 Add series, SMA, and WMA indicators
Introduce new indicators for Simple Moving Average (SMA) and Weighted
Moving Average (WMA), along with their Hull Moving Average (HMA)
derivative.

Also refactors series implementation to use `RefCell` for interior
mutability and adds `SeriesMember` trait for better dynamic series
access. Updates `soa_series.myc` example to reflect new series creation
and push syntax.
2026-03-05 14:07:42 +01:00
Michael Schimmel d1b8d03604 Update benchmark and repeat counts
This commit updates the benchmark and repeat counts for various example
files. These changes reflect potential performance improvements or
variations observed during testing.
2026-03-03 18:55:31 +01:00
Michael Schimmel 8c4db9a5ba Refactor function signatures to use slices 2026-03-03 18:13:20 +01:00
Michael Schimmel 7c38dee243 Refactor AST nodes to use Rc
This commit refactors various AST node types to use `Rc` (Reference
Counting) instead of `Box`. This change is primarily driven by the need
for shared ownership and more efficient handling of potentially
recursive or shared data structures within the AST.

The main benefits of this change include:

- **Reduced cloning:** `Rc` allows multiple parts of the AST to refer to
  the same node without needing to clone the entire node. This is
  particularly beneficial for optimization passes where nodes might be
  visited multiple times.
- **Enabling sharing:** It makes it easier to represent scenarios where
  a single AST node might be a child of multiple parent nodes.
- **Performance improvements:** By avoiding unnecessary deep copies of
  AST nodes, this change can lead to performance gains, especially
  during complex compilation phases.
2026-03-03 16:07:42 +01:00
Michael Schimmel 78c36cf08d Refactor MacroRegistry to use Rc<MacroMap>
This change introduces Rc<MacroMap> to allow for copy-on-write semantics
when defining macros in nested scopes. When a macro is defined in a
scope that is already shared, the MacroMap is cloned before
modification, ensuring that changes to one scope do not affect others
that share the same map.
2026-03-03 15:55:55 +01:00
Michael Schimmel 078b520c37 Add type aliases for registries
Introduces `GlobalFunctionRegistry` and `GlobalAnalyzedRegistry` type
aliases to clarify the usage of `HashMap`s for storing global functions
and their analyzed versions. This change also refactors the
`LambdaCollector` and `Optimizer` to use these new aliases, improving
code readability and maintainability.
2026-03-03 15:51:47 +01:00
Michael Schimmel a18642fd7b Update example output comments
Add `Output` comments to the example files to reflect the actual output
of the programs. This helps with verifying correctness and understanding
example behavior.
2026-03-02 23:46:44 +01:00
Michael Schimmel 2eb7a2e136 Formatting 2026-03-02 23:13:36 +01:00
Michael Schimmel 5bc69c267b Refactor Binder to use Diagnostics
The Binder and its helper functions have been updated to accept and
propagate a `Diagnostics` struct. This allows for better error reporting
during the binding phase, enabling the compiler to continue processing
even after encountering errors and collect all issues before halting.

The `BoundKind::Error` node and `StaticType::Error` are introduced as
"poison" nodes/types. These nodes indicate that an error occurred during
compilation, preventing further valid processing of that specific AST
fragment but allowing the compiler to continue with other parts of the
code.

The `Parser` has also been updated to return a `Diagnostics` struct,
enabling it to report errors during the initial parsing stage while
still attempting to build a partial AST. This adheres to the same error
recovery strategy.
2026-03-02 23:12:51 +01:00
Michael Schimmel f7cb6655af feat: Specialize pipeline types for performance
Introduces type specialization for reactive pipeline nodes and their
data buffers. This eliminates the overhead of generic `Value` enums and
`SharedValueSeries` when dealing with known scalar or record types.

The architecture shifts buffer instantiation from the VM to the runtime
(RTL), leveraging type information from the AST. A new `out_type` field
is added to `BoundKind::Pipe` to store the static type of the pipeline's
output, determined by the type checker.

This enables the creation of specialized `RingBuffer<T>` and
`SharedRecordSeries` (for Struct-of-Arrays layout) when the output type
is known, significantly improving memory usage and processing speed for
time-series data, especially in financial analysis.
2026-03-02 22:03:24 +01:00
Michael Schimmel a4af142719 Refactor random number generation to factory
- Use a `make-random` factory to create isolated random number
  generators.
- Remove the global `prng` from the `Environment`.
- Ensure `make-random` can be called with or without a seed.
2026-03-02 14:01:36 +01:00
Michael Schimmel f3459baf43 Add create-ticker function
Implements the `create-ticker` function, which allows for the creation
of a ticker stream based on a provided condition closure. This function
is crucial for synchronizing pipeline execution based on specific events
or conditions.
2026-03-02 13:15:56 +01:00
Michael Schimmel 86a6db28d8 Update pipeline.myc 2026-03-02 00:32:15 +01:00
Michael Schimmel 2457eec1bf feat: Implement Pipeline support
Adds support for pipelines, allowing streams to be chained together and
transformed.
This includes new types for `PipeStream` and `SourceAdapter`, along with
modifications
to the `Environment` to manage pipeline execution. A new example
`pipeline.myc`
demonstrates its usage.
2026-03-02 00:32:07 +01:00
Michael Schimmel 807903efbb Add Optional type for filter pipes
Introduces `StaticType::Optional` to represent values that can be either
of a specific type `T` or `Void`. This is crucial for handling
situations in filter pipes where an expression might not always produce
a value.

The type checker now correctly deduces and propagates this `Optional`
type, and the pipeline operator (`pipe`) is updated to unwrap
`Optional(T)` results, yielding `T`. This ensures that the type system
accurately reflects the potential absence of values in intermediate
pipeline steps.

Also includes a minor rename in the tuple-struct example from `pipe` to
`p` for clarity, and registers the `streams` module.
2026-03-01 23:19:00 +01:00
Michael Schimmel b177aa8854 feat: Add SharedValueSeries and PipelineNode
Introduces `SharedValueSeries` for read-only access to generic `Value`
buffers, and `PipelineNode` which combines an `ObservableStream` with a
`Series` view. This is a foundational step for implementing the `pipe`
operator.
2026-03-01 23:12:09 +01:00
Michael Schimmel 2579e2b1fd Add pipeline generator registration
Introduce a new field to `Environment` to store a list of pipeline
generator functions.
Add a `run_pipeline` method to `Environment` that iterates through and
executes all registered generators until they are exhausted.
Implement `ObservableStream` trait for `RootStream` and `PipeStream`.
Add a `StreamNode` wrapper for `ObservableStream` to be used as a script
object.
Register `create-random-ohlc` as a native function that creates a
`RootStream`, sets up a OHLC record layout, and registers a stateful
generator closure in the environment's pipeline generators.
2026-03-01 22:18:52 +01:00
Michael Schimmel 50f33b2c30 Feat: Add Pipe Node
Introduces a new `Pipe` node to the Abstract Syntax Tree (AST).
This node represents a functional composition pattern, allowing
for chaining of operations.

The changes include:
- Defining the `Pipe` variant in `BoundKind` and `UntypedKind`.
- Implementing parsing logic for the `pipe` keyword.
- Adding handling for the `Pipe` node in various compiler passes
  (analyzer, binder, captures, dumper, macros, optimizer, TCO,
  type checker).
- Including a placeholder implementation for `Pipe` execution in the
  VM.
- Updating integration tests to use the new `pipe` syntax.
2026-03-01 21:52:10 +01:00
Michael Schimmel a98e51c762 feat: Add streams module for reactive pipeline
Introduces the streams module, defining core reactive primitives like
Signal,
Stream, Observer, RootStream, and PipeStream. This module lays the
groundwork
for building reactive data processing pipelines.

Includes new types for managing reactive data flow and a basic test
suite to
verify the functionality of RootStream and PipeStream.
2026-03-01 21:33:37 +01:00