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

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:

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:

CommandModePrompt
/programProgram statements (default)>>>
/functionFunction definitionsfunction>>>
/classClass definitionsclass>>>
/recordRecord definitionsrecord>>>
/traitTrait definitionstrait>>>
/typeType definitionstype>>>

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:

CommandShortDescription
:help:hShow help and available commands
:quit:qExit the REPL
:clear:cClear all buffers and reset session
:vars:vShow all defined variables with types and values
:source:sShow the assembled EK9 source code
:run:rRe-compile and run the current program
:error NSet error verbosity level (0-3)
:strictToggle strict compilation mode
:editOpen source in external editor
:edit NEdit line N in the current buffer
:del N:d NDelete line N from the current buffer
:ins N:i NInsert a new line before line N
:save file:w fileSave session to file
:load file:l fileLoad session from file
:undo:uUndo 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):

  1. $VISUAL - Preferred for GUI editors
  2. $EDITOR - Standard editor variable
  3. 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

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:

For command-line options and integration with build pipelines, see Command Line.