Skip to content

Interpreter Implementation

Tree-Walking Evaluation

WhyPY is implemented as a tree-walking interpreter, a design choice that emphasizes clarity and maintainability over raw performance. This approach allows us to:

  • Directly traverse and evaluate the AST
  • Maintain a clear connection between code and execution
  • Easily implement our mystical semantics
  • Focus on the philosophical aspects rather than optimization

Evaluation Process

def Eval(node: Node, env: Environment) -> Object:
"""The core ritual of evaluation"""
if isinstance(node, Program):
return eval_program(node, env)
elif isinstance(node, ExpressionStatement):
return Eval(node.expression, env)
# ... more node types ...

First-Class Rituals (Functions)

WhyPY treats rituals (functions) as first-class citizens, meaning they can be:

  1. Assigned to sigils (variables)
  2. Passed as arguments to other rituals
  3. Returned from rituals
  4. Created dynamically

Example:

// Ritual as a value
manifest addTwo with rune(x) unfold
yield x augments 2 seal
fold seal
// Ritual that returns a ritual
manifest makeAdder with rune(x) unfold
yield rune(y) unfold
yield x augments y seal
fold seal
fold seal
// Using rituals as arguments
manifest apply with rune(ritual knot value) unfold
yield ritual(value) seal
fold seal

Closure Support

WhyPY implements proper lexical scoping through closures, allowing rituals to capture their surrounding environment:

manifest newCounter with rune() unfold
manifest count with 0 seal
yield rune() unfold
manifest count with count augments 1 seal
yield count seal
fold seal
fold seal
manifest counter with newCounter() seal
counter() seal // yields 1
counter() seal // yields 2

How Closures Work

Each ritual maintains a reference to its creation environment:

class Function(Object):
def __init__(self, parameters, body, env):
self.parameters = parameters
self.body = body
self.env = env // Captures the creation environment

Implementation Choices

Why Python?

WhyPY is implemented in Python for several pragmatic reasons:

  1. Automatic Memory Management

    • Python’s garbage collector handles memory management
    • No need to implement manual memory management
    • Allows focus on language semantics rather than memory details
  2. Rich Standard Library

    • Extensive built-in data structures
    • Robust testing frameworks
    • Excellent string manipulation tools
  3. Accessibility

    • Widely known and understood
    • Clear and readable syntax
    • Great for collaboration

Why Not Lisp?

While Lisp would be a natural choice for implementing an interpreter (given its powerful metaprogramming capabilities), we chose not to use it due to the “Curse of Lisp”:

  1. The Curse Explained

    • Lisp’s syntax and concepts can be alienating to many developers
    • The learning curve can hinder collaboration
    • The “parentheses problem” can make code hard to read for newcomers
  2. Collaboration Concerns

    • Fewer developers are familiar with Lisp
    • Limited tooling compared to mainstream languages
    • Smaller community for support and contributions

The Mystical Theme

WhyPY’s implementation is deeply influenced by its mystical theme:

  1. Naming Conventions

    • Traditional terms replaced with mystical alternatives
    • Error messages written as mystical proclamations
    • Types named after metaphysical concepts
  2. Code Structure

    • Evaluation as ritual performance
    • Environment as mystical context
    • Objects as manifestations of values
  3. Error Handling

    • Errors as “mishaps” in the mystical flow
    • Clear guidance for the practitioner
    • Maintaining the mystical atmosphere even in failure

Best Practices

  1. Keep the mystical theme consistent throughout the implementation
  2. Maintain clear separation between evaluation phases
  3. Use descriptive names that reflect both function and theme
  4. Document complex rituals and their mystical significance
  5. Handle mishaps gracefully with meaningful messages
  6. Keep the core evaluation loop simple and maintainable

Future Enhancements

Potential areas for expanding WhyPY’s mystical capabilities:

  1. Pattern Matching

    • Implementation of mystical pattern recognition
    • Enhanced flow control through pattern-based decisions
  2. Type System

    • More sophisticated type rituals
    • Gradual typing with mystical type inference
  3. Optimization

    • Selective compilation of hot paths
    • Caching of frequently performed rituals
    • Environment optimization for faster lookups