View Source cure_ast (cure v0.1.0)
Cure Programming Language - AST Definitions
This module defines the Abstract Syntax Tree (AST) node types and structure for the Cure programming language. It provides comprehensive type definitions and helper functions for creating and manipulating AST nodes throughout the compilation pipeline.
Overview
The Cure AST represents the complete syntactic structure of Cure programs including:
- Programs: Collections of top-level items
 - Modules: Module definitions with exports and items
 - Functions: Function definitions with parameters, return types, and bodies
 - Types: User-defined types, primitives, and complex type expressions
 - FSMs: Finite state machine definitions with states and transitions
 - Expressions: All forms of expressions (literals, function calls, control flow)
 - Patterns: Pattern matching constructs
 - Location Information: Source position tracking for error reporting
 
AST Structure
Top-Level Items
Programs consist of top-level items:
module_def()- Module definitionsfunction_def()- Regular function definitionserlang_function_def()- Erlang interop functionstype_def()- User-defined type definitionsfsm_def()- Finite state machine definitionsimport_def()- Import statements
Expression Types
Expressions represent computations and values:
literal_expr()- Literals (numbers, strings, atoms)identifier_expr()- Variable and function namesfunction_call_expr()- Function calls with argumentsbinary_op_expr()- Binary operations (+, -, *, etc.)unary_op_expr()- Unary operations (-, not)match_expr()- Pattern matching expressionslet_expr()- Let bindingslist_expr()- List literalstuple_expr()- Tuple literalsrecord_expr()- Record constructionlambda_expr()- Anonymous functions
Type System
Type expressions represent Cure's type system:
primitive_type()- Built-in types (Int, String, Bool, etc.)dependent_type()- Parameterized types with dependenciesfunction_type()- Function type signaturesunion_type()- Union types (T | U)list_type()- List typestuple_type()- Tuple typestype_param()- Type parameters for generics
Pattern Matching
Patterns for destructuring and matching:
wildcard_pattern()- Wildcard patterns (_)literal_pattern()- Literal value patternsidentifier_pattern()- Variable binding patternslist_pattern()- List destructuring patternstuple_pattern()- Tuple destructuring patternsrecord_pattern()- Record field patternsconstructor_pattern()- Constructor patterns (Ok(x), Error(e))
FSM Support
First-class finite state machine support:
fsm_def()- FSM type definitionsstate_def()- Individual state definitionstransition()- State transitions with events, guards, and actions
Location Tracking
All AST nodes include location information for:
- Error Reporting: Precise source locations for compilation errors
 - Debugging: Source mapping for runtime errors
 - IDE Support: Language server integration
 - Documentation: API documentation generation
 
Usage Example
%% Create a simple function definition
Params = [#param{name = x, type = int_type(), location = Location}],
Body = #literal_expr{value = 42, location = Location},
Function = cure_ast:new_function(identity, Params, int_type(), undefined, Body).
%% Create an FSM definition
States = [idle, running, stopped],
Initial = idle,
StateDefs = [IdleState, RunningState, StoppedState],
FSM = cure_ast:new_fsm(state_machine, States, Initial, StateDefs).Design Principles
Immutability
AST nodes are immutable records that can be safely shared and transformed without side effects.
Composability
Complex AST structures are built by composing simpler nodes, enabling modular construction and transformation.
Location Preservation
Every AST node maintains source location information to support high-quality error messages and debugging.
Type Safety
Erlang type specifications ensure AST node integrity and enable static analysis tools to verify AST manipulation code.
Integration
This module integrates with:
- Parser: Produces AST nodes from token streams
 - Type Checker: Analyzes and annotates AST with type information
 - Compiler: Transforms AST through various compilation phases
 - Error Reporter: Uses location information for error messages
 
Performance Considerations
- Memory Efficient: Records are lightweight with minimal overhead
 - Copy-on-Write: Erlang's immutable data structures optimize memory usage
 - Pattern Matching: Efficient pattern matching on AST node types
 - Location Sharing: Location records can be shared between related nodes
 
Summary
Functions
Creates a new expression AST node with location information.
Creates a new FSM definition AST node.
Creates a new function definition AST node.
Creates a new module definition AST node.
Creates a new type definition AST node.
Types
-type dependent_type() :: #dependent_type{name :: atom(), params :: [type_param()], location :: location()}.
-type expr() :: literal_expr() | identifier_expr() | function_call_expr() | match_expr() | let_expr() | binary_op_expr() | unary_op_expr() | list_expr() | tuple_expr() | record_expr() | lambda_expr().
-type item() :: module_def() | function_def() | erlang_function_def() | type_def() | fsm_def() | import_def().
-type match_expr() :: #match_expr{expr :: expr(), patterns :: [match_clause()], location :: location()}.
-type module_def() :: #module_def{name :: atom(), exports :: [export_spec()], items :: [item()], location :: location()}.
-type pattern() :: wildcard_pattern() | literal_pattern() | identifier_pattern() | list_pattern() | tuple_pattern() | record_pattern().
-type program() :: [item()].
-type record_expr() :: #record_expr{name :: atom(), fields :: [field_expr()], location :: location()}.
-type record_pattern() :: #record_pattern{name :: atom(), fields :: [field_pattern()], location :: location()}.
-type state_def() :: #state_def{name :: atom(), transitions :: [transition()], location :: location()}.
-type type_expr() :: primitive_type() | dependent_type() | function_type() | union_type() | list_type() | tuple_type().
-type wildcard_pattern() :: #wildcard_pattern{location :: location()}.
Functions
Creates a new expression AST node with location information.
Arguments
Type- Expression type (literal, identifier, etc.)Data- Expression data appropriate for the typeLocation- Source location information
Returns
expr()- Expression AST nodeerror({unknown_expr_type, Type, Data, Location})- Unknown expression type
Supported Types
literal- Creates a literal expression from the valueidentifier- Creates an identifier expression from the name
Example
Literal = cure_ast:new_expr(literal, 42, Location),
Identifier = cure_ast:new_expr(identifier, variable_name, Location).Note
This is a limited helper function that only supports basic expression types. For complex expressions, construct the records directly.
Creates a new FSM definition AST node.
Arguments
Name- FSM name (atom)States- List of state namesInitial- Initial state nameStateDefs- List of state definitions with transitionsLocation- Source location information
Returns
fsm_def()- FSM definition AST node
Example
States = [idle, running, stopped],
StateDefs = [IdleState, RunningState, StoppedState],
FSM = cure_ast:new_fsm(counter, States, idle, StateDefs, Location).
  -spec new_function(atom(), [param()], type_expr() | undefined, expr() | undefined, expr(), location()) -> function_def().
Creates a new function definition AST node.
Arguments
Name- Function name (atom)Params- List of function parametersReturnType- Return type expression (undefined if not specified)Constraint- Optional constraint expression for the functionBody- Function body expressionLocation- Source location information
Returns
function_def()- Function definition AST node
Example
Params = [#param{name = x, type = IntType, location = Loc}],
Body = #literal_expr{value = 42, location = Loc},
Function = cure_ast:new_function(identity, Params, IntType, undefined, Body, Location).
  -spec new_module(atom(), [export_spec()], [item()], location()) -> module_def().
Creates a new module definition AST node.
Arguments
Name- Module name (atom)Exports- List of export specificationsItems- List of top-level items in the moduleLocation- Source location information
Returns
module_def()- Module definition AST node
Example
Exports = [#export_spec{name = hello, arity = 1, location = Loc}],
Items = [FunctionDef],
Module = cure_ast:new_module('MyModule', Exports, Items, Location).
  Creates a new type definition AST node.
Arguments
Name- Type name (atom)Params- List of type parameter namesDefinition- Type expression defining the typeLocation- Source location information
Returns
type_def()- Type definition AST node
Example
Params = [t],
Definition = #union_type{types = [IntType, StringType], location = Loc},
TypeDef = cure_ast:new_type_def('Maybe', Params, Definition, Location).