Skip to content

Quick Start

Get up and running with CacaoLang in just a few minutes!

Your First Choco Program

Create a file called hello.choco:

print "Hello, CacaoLang!";

Run it:

./choco hello.choco

Output:

Hello, CacaoLang!

Basic Syntax

Variables

// Declare variables with 'let'
let name = "Alice";
let age = 30;
let score = 95.5;
let isActive = true;

print name;
print age;

String Interpolation

let name = "Bob";
let age = 25;

// Use #{} for string interpolation
let message = "My name is #{name} and I'm #{age} years old";
print message;

Functions

// Define functions with 'fn'
fn greet(name) {
    return "Hello, #{name}!";
}

let greeting = greet("World");
print greeting;  // Hello, World!

Arrays

let numbers = [1, 2, 3, 4, 5];
let fruits = ["apple", "banana", "cherry"];

print numbers[0];  // 1
print fruits[1];   // banana

print len(numbers);  // 5

Control Flow

let age = 18;

if age >= 18 {
    print "Adult";
} else {
    print "Minor";
}

Loops

// For loop
for i in 0..5 {
    print i;
}

// While loop
let count = 0;
while count < 5 {
    print count;
    count = count + 1;
}

Common Patterns

Working with Arrays

let numbers = [1, 2, 3, 4, 5];

// Map: transform each element
let doubled = map(numbers, |x| => { return x * 2; });
print doubled;  // [2, 4, 6, 8, 10]

// Filter: keep only matching elements
let evens = filter(numbers, |x| => { return x % 2 == 0; });
print evens;  // [2, 4]

// Reduce: combine into single value
let sum = reduce(numbers, 0, |acc, x| => { return acc + x; });
print sum;  // 15

String Operations

let text = "Hello World";

// String functions
print uppercase(text);  // HELLO WORLD
print lowercase(text);  // hello world
print len(text);        // 11

// Split and join
let words = split(text, " ");
print words;  // ["Hello", "World"]

let joined = join(words, "-");
print joined;  // Hello-World

Pattern Matching

let day = 3;

match day {
    case 1 => { print "Monday"; }
    case 2 => { print "Tuesday"; }
    case 3 => { print "Wednesday"; }
    default => { print "Other day"; }
}

Structs

// Define a struct
struct Person {
    name,
    age,
    city
}

// Create an instance
let alice = Person {
    name: "Alice",
    age: 30,
    city: "NYC"
};

// Access fields
print alice.name;  // Alice
print alice.age;   // 30

File I/O

// Write to file
write_file("output.txt", "Hello from Choco!");

// Read from file
let content = read_file("output.txt");
print content;

// Append to file
append_file("output.txt", "\nNew line!");

// Check if file exists
if file_exists("output.txt") == true {
    print "File exists!";
}

User Input

let name = input("What is your name? ");
print "Hello, #{name}!";

let age_str = input("Enter your age: ");
let age = int(age_str);

if age >= 18 {
    print "You are an adult";
} else {
    print "You are a minor";
}

Error Handling

try {
    print "Attempting operation...";
    throw "Something went wrong!";
    print "This won't execute";
} catch err {
    print "Error caught: #{err}";
}

print "Program continues";

Running Programs

Execute a Choco file

./choco myprogram.choco

Start the REPL

./choco

Compile to executable

./choco compile myprogram.choco
./myprogram

Next Steps

Now that you've learned the basics: