Skip to content

REPL Mode

The CacaoLang REPL (Read-Eval-Print Loop) provides an interactive environment for testing code, experimenting with features, and quick calculations.

Starting the REPL

Launch the REPL by running choco without any arguments:

./choco

You'll see the welcome message:

======================================
  CacaoLang 1.2.5 - Coco Loco
  REPL (mochalx v1.4.9)
  Type 'exit' or 'quit' to leave
======================================

choco:1>

Basic Usage

Simple Expressions

choco:1> 2 + 2;
choco:2> 10 * 5;
choco:3> "Hello, " + "World!";

Variables

Variables persist across commands:

choco:1> let x = 10;
choco:2> let y = 20;
choco:3> print x + y;
30
choco:4>

Functions

Define and use functions interactively:

choco:1> fn double(n) { return n * 2; }
choco:2> print double(5);
10
choco:3> print double(21);
42
choco:4>

REPL Commands

help

Show available commands:

choco:1> help
CacaoLang Commands:
  exit, quit     - Exit the REPL
  help           - Show this help message
  clear          - Clear all variables and functions
  vars           - Show all defined variables
  funcs          - Show all defined functions

vars

List all defined variables:

choco:1> let name = "Alice";
choco:2> let age = 30;
choco:3> let score = 95.5;
choco:4> vars
Defined variables:
  name = Alice
  age = 30
  score = 95.5
choco:5>

funcs

List all defined functions:

choco:1> fn add(a,b) { return a + b; }
choco:2> fn multiply(x,y) { return x * y; }
choco:3> funcs
Defined functions:
  add(a, b)
  multiply(x, y)
choco:4>

clear

Reset the environment:

choco:1> let x = 10;
choco:2> let y = 20;
choco:3> clear
Environment cleared.
choco:1>

exit / quit

Exit the REPL:

choco:1> exit
Goodbye!

Working with Arrays

choco:1> let numbers = [1, 2, 3, 4, 5];
choco:2> print numbers;
[1, 2, 3, 4, 5]
choco:3> print numbers[0];
1
choco:4> print len(numbers);
5
choco:5>

String Interpolation

choco:1> let name = "Bob";
choco:2> let age = 25;
choco:3> print "My name is #{name} and I'm #{age}";
My name is Bob and I'm 25
choco:4>

Multi-line Statements

The REPL automatically handles multi-line input for blocks:

choco:1> fn greet(name) {
...    return "Hello, #{name}!";
... }
choco:2> print greet("Alice");
Hello, Alice!
choco:3>

Testing Code Snippets

The REPL is perfect for testing small code snippets before adding them to your program:

Testing Math

choco:1> let radius = 5;
choco:2> let pi = 3.14159;
choco:3> let area = pi * radius * radius;
choco:4> print area;
78.53975
choco:5>

Testing String Functions

choco:1> let text = "Hello World";
choco:2> print uppercase(text);
HELLO WORLD
choco:3> print lowercase(text);
hello world
choco:4> let words = split(text, " ");
choco:5> print words;
[Hello, World]
choco:6>

Testing Loops

choco:1> for i in 0..5 {
...    print i;
... }
0
1
2
3
4
choco:2>

Using Built-in Functions

Test standard library functions:

choco:1> print sqrt(16);
4
choco:2> print pow(2, 8);
256
choco:3> print random_int(1, 10);
7
choco:4> print abs(-42);
42
choco:5>

Working with Lambdas

choco:1> let double = |x| => { return x * 2; };
choco:2> print double(5);
10
choco:3> let numbers = [1, 2, 3, 4, 5];
choco:4> let doubled = map(numbers, double);
choco:5> print doubled;
[2, 4, 6, 8, 10]
choco:6>

Error Handling

Errors are reported but don't crash the REPL:

choco:1> let x = 10 / 0;
Runtime Error: Division by zero
choco:2> print x;
Runtime Error: Undefined variable 'x'
choco:3>

Practical Examples

Quick Calculator

choco:1> let price = 29.99;
choco:2> let quantity = 3;
choco:3> let tax_rate = 0.08;
choco:4> let subtotal = price * quantity;
choco:5> let tax = subtotal * tax_rate;
choco:6> let total = subtotal + tax;
choco:7> print "Total: $#{total}";
Total: $97.1676
choco:8>

Data Analysis

choco:1> let scores = [85, 92, 78, 95, 88];
choco:2> let sum = reduce(scores, 0, |a,x| => { return a + x; });
choco:3> let count = len(scores);
choco:4> let average = sum / count;
choco:5> print "Average: #{average}";
Average: 87.6
choco:6>

String Processing

choco:1> let sentence = "the quick brown fox";
choco:2> let words = split(sentence, " ");
choco:3> let capitalized = map(words, |w| => { return uppercase(w); });
choco:4> let result = join(capitalized, " ");
choco:5> print result;
THE QUICK BROWN FOX
choco:6>

Tips for Effective REPL Usage

1. Test Before Adding to Code

Use the REPL to verify logic before adding it to your main program:

choco:1> // Test the formula
choco:2> let celsius = 25;
choco:3> let fahrenheit = (celsius * 9 / 5) + 32;
choco:4> print fahrenheit;
77
choco:5> // Works! Now add to main program

2. Explore Standard Library

choco:1> print typeof(42);
number
choco:2> print typeof("hello");
string
choco:3> print typeof([1,2,3]);
array

3. Quick Prototyping

choco:1> // Prototype pattern matching
choco:2> let day = 3;
choco:3> match day {
...    case 1 => { print "Monday"; }
...    case 2 => { print "Tuesday"; }
...    case 3 => { print "Wednesday"; }
...    default => { print "Other"; }
... }
Wednesday
choco:4>

4. Debug Expressions

choco:1> let x = 10;
choco:2> let y = 20;
choco:3> print x > y;
false
choco:4> print x < y;
true
choco:5>

Automatic Semicolon Insertion

The REPL automatically adds semicolons when needed:

choco:1> print "Hello"
Hello
choco:2> let x = 10
choco:3> print x
10

Line Numbers

Line numbers increment with each command, making it easy to track your session:

choco:1> let x = 1;
choco:2> let y = 2;
choco:3> let z = 3;
choco:4> print x + y + z;
6
choco:5>

After using clear, line numbers reset:

choco:5> clear
Environment cleared.
choco:1>

Limitations

  • No persistent state: Variables and functions are lost when you exit
  • No file I/O in REPL: File operations work but may not be practical
  • GUI functions: GUI functions start event loops that block the REPL

Workflow Tips

Develop Incrementally

choco:1> // Start with a simple function
choco:2> fn factorial(n) {
...    if n <= 1 { return 1; }
...    return n * factorial(n - 1);
... }
choco:3> // Test it
choco:4> print factorial(5);
120
choco:5> // Perfect! Copy to your .choco file

Save Your Work

When you have working code in the REPL, copy it to a file:

  1. Test in REPL
  2. Verify it works
  3. Copy to .choco file
  4. Run the file to ensure it still works

Next Steps