# ElixirLang Interpreter
A simple yet educational interpreter implementation in Elixir, designed to help understand interpreter concepts and Elixir's pattern matching capabilities.
## Overview
This interpreter implements a small programming language with syntax inspired by Elixir. It demonstrates core interpreter concepts including lexical analysis, parsing, and evaluation while leveraging Elixir's powerful features.
## Features
- Integer arithmetic (`5 + 3 * 2`)
- String operations (`"hello" <> " world"`)
- Boolean expressions (`true`, `false`)
- List manipulation (`[1, 2, 3] |> length()`)
- Pattern matching (`x = 5`)
- Pipe operator (`|>`)
- Built-in functions (`length/1`, `hd/1`, `tl/1`)
- Conditional expressions (`if/else`)
## Installation
```bash
# Clone the repository
git clone https://github.com/yourusername/elixirlang.git
# Enter the directory
cd elixirlang
# Get dependencies
mix deps.get
# Run tests
mix test
```Start the REPL:
iex -S mix
iex> Elixirlang.main()# Arithmetic
╭─ λ 5 + 3 * 2
=> 11
# String concatenation
╭─ λ "Hello" <> " World"
=> "Hello World"
# Function definition and usage
╭─ λ def add(a, b) do
╰─➤ a + b
╰─➤ end
=> <function>
╭─ λ add(5, 3)
=> 8
# Pattern matching
╭─ λ x = 5
=> 5
╭─ λ y = add(x, 3)
=> 8
# Lists
╭─ λ [1, 2, 3] |> length()
=> 3
# Pipe operator with functions
╭─ λ 5 |> add(3)
=> 8lib/
├── elixirlang.ex # Main module
└── interpreter/
├── lexer.ex # Tokenization
├── parser.ex # AST generation
├── evaluator.ex # Expression evaluation
├── environment.ex # Variable/scope management
├── object.ex # Runtime objects
├── token.ex # Token definitions
├── ast.ex # AST node definitions
└── repl.ex # Interactive shell
test/
└── interpreter/ # Comprehensive test suite
-
Lexer: Converts source code into tokens
iex> input = "5 + 3" iex> lexer = Lexer.new(input) # Produces tokens: INT(5), PLUS, INT(3)
-
Parser: Builds Abstract Syntax Tree (AST)
iex> parser = Parser.new(lexer) iex> {program, _} = Parser.parse_program(parser) # Creates AST nodes representing the expression
-
Evaluator: Executes the AST
iex> {result, _env} = Evaluator.eval(program) iex> result.value # => 8
- Pattern Matching: Extensively used in parser and evaluator
- Structs: For representing tokens, AST nodes, and runtime objects
- Recursion: For tree traversal and evaluation
- Immutable Data: All state changes create new copies
- This is an educational implementation focusing on clarity over performance
- Error handling is omitted to maintain focus on core concepts
- The implementation prioritizes readability over optimization
- Built-in functions are limited to demonstrate basic concepts
- Error handling and better error messages
- More built-in functions
- Module system
- Documentation improvements
Run the comprehensive test suite:
mix testThe test suite covers:
- Lexical analysis
- Parsing
- Evaluation
- Object system
- REPL functionality
MIT License