EK9 REPL - Interactive Development
The EK9 REPL (Read-Eval-Print-Loop) provides an interactive environment for exploring the language, prototyping code, and learning EK9 syntax. Unlike simple REPLs, EK9's REPL includes IDE-quality features such as syntax highlighting, intelligent tab completion, external editor integration, and session management.
Contents
- Starting the REPL
- Basic Usage
- Syntax Highlighting
- Tab Completion
- Mode Switching
- Meta-Commands Reference
- External Editor Integration
- Session Management
- Buffer Editing
- Error Handling
- Hover Help
- Complete Example Session
- Tips and Best Practices
- Comparison with Other REPLs
- Summary
Starting the REPL
Launch the REPL from the command line using the -repl flag:
- $ ek9 -repl ↵
- EK9 REPL v0.0.1 (type :help for commands)
- >>>
For enhanced error messages with suggestions, combine with error verbosity flags:
- $ ek9 -repl -E2 ↵
The -E2 flag enables "Did you mean?" suggestions for typos. See Error Verbosity Levels for detailed examples of all verbosity levels.
Basic Usage
Enter EK9 statements at the prompt. Variable declarations show type and value information. Use stdout.println() to display expression results:
- >>> x <- 42
- x: Integer = 42
- >>> name <- "EK9"
- name: String = EK9
- >>> isReady <- true
- isReady: Boolean = true
- >>> stdout.println(5 + 3)
- 8
- >>> stdout.println("Hello" + " World")
- Hello World
Variables persist throughout your session and can be used in subsequent statements:
- >>> stdout.println(x * 2)
- 84
- >>> stdout.println("Value is: " + $x)
- Value is: 42
Syntax Highlighting
The REPL provides full syntax highlighting as you type, making code easier to read and errors easier to spot. The color scheme matches EK9's standard highlighting:
- Keywords - if, for, while, class, function, etc.
- Types - Integer, String, Boolean, Float, etc.
- Strings - Text in quotes
- Numbers - Integer and floating-point literals
- Comments - Lines starting with //
- Variables - User-defined names
- Operators - +, -, <-, :=, etc.
- Stream Operations - cat, filter, map, head, etc.
Tab Completion
Press Tab to complete keywords, types, functions, variables, and methods.
Keyword and Type Completion
- >>> func[TAB]
- function
- >>> Int[TAB]
- Integer
Variable Completion
After defining variables, they appear in completion:
- >>> userName <- "Alice"
- userName: String = Alice
- >>> user[TAB]
- userName
Method Completion After "."
Type a variable name followed by a dot and press Tab to see available methods:
- >>> stdout.[TAB]
- method print(value as String)
- method println()
- method println(value as String)
This works for any variable - the REPL inspects the variable's type and shows all available methods with their parameter signatures.
Mode Switching
The REPL supports different modes for defining various EK9 constructs. Use slash commands to switch modes:
| Command | Mode | Prompt |
|---|---|---|
| /program | Program statements (default) | >>> |
| /function | Function definitions | function>>> |
| /class | Class definitions | class>>> |
| /record | Record definitions | record>>> |
| /trait | Trait definitions | trait>>> |
| /type | Type definitions | type>>> |
Example: Defining and Using a Function
- >>> /function
- function>>> square()
- function>>> -> n as Integer
- function>>> <- result as Integer: n * n
- function>>>
- function square() defined
- function>>> /program
- >>> stdout.println(square(7))
- 49
- >>> stdout.println(square(12))
- 144
An empty line in a definition mode signals the end of the current definition. The REPL compiles the definition and reports success or shows any errors.
Meta-Commands Reference
Meta-commands start with a colon and control the REPL session:
| Command | Short | Description |
|---|---|---|
| :help | :h | Show help and available commands |
| :quit | :q | Exit the REPL |
| :clear | :c | Clear all buffers and reset session |
| :vars | :v | Show all defined variables with types and values |
| :source | :s | Show the assembled EK9 source code |
| :run | :r | Re-compile and run the current program |
| :error N | Set error verbosity level (0-3) | |
| :strict | Toggle strict compilation mode | |
| :edit | Open source in external editor | |
| :edit N | Edit line N in the current buffer | |
| :del N | :d N | Delete line N from the current buffer |
| :ins N | :i N | Insert a new line before line N |
| :save file | :w file | Save session to file |
| :load file | :l file | Load session from file |
| :undo | :u | Undo the last change |
External Editor Integration
For complex editing, open the entire session source in your preferred external editor:
- >>> :edit
- [Opens editor with complete session source]
- [Edit, save, and close the editor]
- Source updated from editor.
The REPL uses these environment variables (in order of priority):
- $VISUAL - Preferred for GUI editors
- $EDITOR - Standard editor variable
- Platform default - vi on Unix/Mac, notepad on Windows
After you save and close the editor, the REPL reloads the modified source and recompiles it. Any syntax errors are reported immediately.
Session Management
Saving Sessions
Save your current session to a file for later use:
- >>> :save mysession.ek9
- Session saved to mysession.ek9
Loading Sessions
Restore a previously saved session:
- >>> :load mysession.ek9
- Session loaded from mysession.ek9
- >>> :vars
- x: Integer = 42
- name: String = EK9
Persistent History
Command history is automatically saved to ~/.ek9_repl_history and persists between sessions. Use the up and down arrow keys to navigate through previous commands.
Buffer Editing
View and modify your current code with buffer editing commands:
Viewing Source
- >>> :source
- 1: x <- 42
- 2: name <- "EK9"
- 3: stdout.println(name)
Editing a Line
- >>> :edit 2
- [Line 2 appears for editing]
- name <- "EK9 Language"
- Line 2 updated.
Deleting and Inserting Lines
- >>> :del 3
- Line 3 deleted.
- >>> :ins 2
- [Enter new line to insert before line 2]
- count <- 100
- Line inserted.
Undoing Changes
- >>> :undo
- Undo: Line insertion reverted.
Error Handling
The REPL uses EK9's comprehensive error diagnostic system. Control verbosity with :error N where N is 0-3:
- >>> :error 2
- Error level set to 2 (Visual + Suggestions)
With :error 2, typos show suggestions:
- >>> count <- 10
- count: Integer = 10
- >>> coutn + 5
- ❌ error:[E50001]: 'coutn' not resolved
- ├─ 💡 help: did you mean 'count as Integer'?
See Error Verbosity Levels for detailed examples of all four levels (-E0, -E1, -E2, -E3) with sample output.
Hover Help (Ctrl+H)
Press Ctrl+H after typing a symbol name to see its type information and documentation:
- >>> stdout[Ctrl+H]
- stdout : Stdout
- Kind: Variable
- Defined at: Stdout.ek9 Line 1
This works for any symbol - variables, functions, types, and methods.
Complete Example Session
Here's a complete session demonstrating multiple features:
- $ ek9 -repl -E2 ↵
- EK9 REPL v0.0.1 (type :help for commands)
- // Explore with expressions
- >>> stdout.println(2 ** 10)
- 1024
- // Define some variables
- >>> base <- 5
- base: Integer = 5
- >>> height <- 10
- height: Integer = 10
- // Define a function to calculate area
- >>> /function
- function>>> triangleArea()
- function>>> -> b as Integer
- function>>> -> h as Integer
- function>>> <- area as Float: (b * h) / 2.0
- function>>>
- function triangleArea() defined
- // Use the function
- function>>> /program
- >>> stdout.println(triangleArea(base, height))
- 25.0
- // Make a typo - see suggestion
- >>> stdout.println(triangleAera(3, 4))
- ❌ error:[E50001]: 'triangleAera' not resolved
- ├─ 💡 help: did you mean 'triangleArea'?
- // Fix and retry
- >>> stdout.println(triangleArea(3, 4))
- 6.0
- // Save our work
- >>> :save geometry.ek9
- Session saved to geometry.ek9
- // Exit
- >>> :quit
- Goodbye!
Tips and Best Practices
- Use :source frequently - See the complete assembled code to understand how your session translates to EK9
- Use :vars to track state - Check what variables are defined and their current values
- Start with -E2 - The "Did you mean?" suggestions help catch typos immediately
- Use :edit for complex changes - Opening your preferred editor is faster for multi-line edits
- Save frequently with :save - Preserve your work before experimenting with risky changes
- Use Tab completion liberally - It's faster and prevents typos
- Check method signatures - Type variable. and Tab to see available methods with parameter types
Comparison with Other REPLs
EK9's REPL provides IDE-quality features that match or exceed other language REPLs:
| Feature | EK9 | Python | Ruby IRB | JShell | Scala |
|---|---|---|---|---|---|
| Syntax Highlighting | ✓ Full | ✗ | Basic | ✗ | Basic |
| Keyword Completion | ✓ | ✓ | ✓ | ✓ | ✓ |
| Method Completion (".") | ✓ | IPython | Pry | ✓ | ✓ |
| External Editor | ✓ $VISUAL/$EDITOR | %edit | Limited | ✗ | ✗ |
| Mode Switching | ✓ /function etc. | ✗ | ✗ | ✗ | :paste |
| Buffer Editing | ✓ :edit N, :del, :ins | ✗ | ✗ | /edit | ✗ |
| Session Save/Load | ✓ :save/:load | %store | ✗ | /save | ✗ |
| Undo | ✓ :undo | ✗ | ✗ | ✗ | ✗ |
| Persistent History | ✓ | ✓ | ✓ | ✗ | Limited |
| Error Verbosity Levels | ✓ :error 0-3 | ✗ | ✗ | ✗ | ✗ |
| Hover Help | ✓ Ctrl+H | ✗ | ✗ | ✗ | :info |
Summary
The EK9 REPL provides a professional interactive development environment with:
- Full syntax highlighting for all EK9 constructs
- Intelligent tab completion for keywords, types, variables, and methods
- External editor integration using $VISUAL or $EDITOR
- Mode switching for defining functions, classes, records, traits, and types
- Session management with save, load, and persistent history
- Buffer editing with edit, delete, insert, and undo
- Error diagnostics with configurable verbosity levels
- Hover help for symbol information
For command-line options and integration with build pipelines, see Command Line.