Welcome to FlavorLang! This tutorial will guide you through the fundamentals of this cooking-inspired programming language. We'll start with the basics and gradually move on towards more advanced features.
- Getting Started
- Variables and Constants
- Basic Output and Input
- Data Types
- Control Flow
- Functions
- Error Handling
- Working with Arrays
- File Operations
- Standard Library Functions
- Imports & Exports
FlavorLang is a programming language that uses cooking-inspired syntax to make programming more approachable & fun. Files use the .flv
extension.
Let's start with the classic "Hello, World!" program:
serve("Hello, world!");
Use let
to declare variables that can be modified:
let name = "Chef";
let age = 25;
serve("Hello,", name); # Output: Hello, Chef
Use const
for values that shouldn't change:
const PI = 3.14159;
const RESTAURANT_NAME = "Flavor House";
serve
is FlavorLang's print function. It has some special features:
serve("Basic output"); # Adds newline by default
serve("No newline", False); # Doesn't add newline
serve("Multiple", "values", "work"); # Spaces between values
Use sample()
to get user input:
let favorite = sample("What's your favorite dish?");
serve("Ah,", favorite, "is delicious!");
FlavorLang supports these basic data types:
- Strings:
"Hello"
- Integers:
42
- Floats:
3.14
- Booleans:
True
,False
Type conversion functions:
let num_str = "123";
let num = int(num_str); # String to integer
let float_num = float("3.14"); # String to float
let str_num = string(42); # Number to string
let temperature = 180;
if temperature > 180 {
serve("Too hot!");
} elif temperature == 180 {
serve("Perfect temperature!");
} else {
serve("Too cold!");
}
for
loops allow you to iterate over a range or a collection.
You can use the range operator (exclusive ..
or inclusive ..=
) to specify the start & end of the loop. Use the optional by
keyword to specify a step:
# Count up
for i in 1..5 {
serve(i); # Counts: 1, 2, 3, 4
}
# Count down
for i in 10..=1 by -2 {
serve(i); # Counts: 10, 8, 6, 4, 2
}
let recipes = ["Cake", "Cookies", "Pasta"];
for recipe in recipes {
serve("Preparing:", recipe);
}
serve("All recipes are ready!");
while
loops allow you to iterate whilst a condition holds True
.
let ingredients = 3;
while ingredients > 0 {
serve("Adding ingredient...");
ingredients = ingredients - 1;
}
const dish = "pasta";
check dish {
is "pasta":
serve("Italian cuisine!");
break;
is "sushi":
serve("Japanese cuisine!");
break;
else:
serve("Unknown cuisine!");
}
Functions in FlavorLang are created using the create
keyword and can return values using deliver
:
create add_ingredients(a, b) {
deliver a + b; # Must use deliver for returning values
}
create mix_ingredients(ingredients) {
serve("Mixing:", ingredients);
# No deliver = void return (implicit)
}
# Using the functions
let result = add_ingredients(5, 3);
serve(result); # Output: 8
mix_ingredients("flour and water");
Use try
, rescue
, and optionally finish
for error handling:
try {
let number = int("not a number");
serve("This won't run!");
} rescue {
serve("Oops! Couldn't convert to number.");
} finish {
serve("This always runs!");
}
Raise errors using burn
:
create check_temperature(temp) {
if temp > 200 {
burn("Temperature too high!", temp);
}
serve("Temperature is OK:", temp);
}
Arrays in FlavorLang support various operations:
# Creating arrays
let ingredients = ["flour", "sugar", "eggs"];
# Accessing elements
serve(ingredients[0]); # Output: flour
# Array operations
ingredients[^+] = "milk"; # Append
ingredients[+^] = "butter"; # Prepend
serve(ingredients[-^]); # Remove and return first element
serve(ingredients[^-]); # Remove and return last element
# Slicing
serve(ingredients[1:3]); # Elements from index 1 to 2
serve(ingredients[::-1]); # Reverse array
serve(ingredients[::2]); # Every second element
# 2D arrays
let recipe_matrix = [
["Cake", "60 min"],
["Cookies", "25 min"]
];
serve(recipe_matrix[0][1]); # Output: 60 min
FlavorLang provides three main file operations:
# Writing to a file
plate_file("recipe.txt", "Flour: 2 cups\nSugar: 1 cup");
# Appending to a file
garnish_file("recipe.txt", "\nEggs: 2");
# Reading from a file
let recipe = taste_file("recipe.txt");
serve(recipe);
FlavorLang includes several useful built-in functions:
# Random numbers
let random_float = random(); # 0.0 to 1.0
let random_int = random(10); # 0 to 10
let random_range = random(5, 10); # 5 to 10
# Time operations
let timestamp = get_time(); # Unix timestamp
# Array/String length
let len = length("Hello"); # 5
# Delay execution
sleep(1000); # Pause for 1 second
FlavorLang supports modularity so you can split your code into separate files and only expose what’s necessary!
Use the export
keyword to mark functions, variables, or constants as public. Items declared without export
remain private to the file.
export create triple(x) {
deliver x * 3;
}
create hiddenFunc() {}
export let someVar = triple(5);
To use exported items in another file, use the import
keyword at the beginning of the file.
import "24_export.flv";
serve(someVar); # Output: 15
serve(hiddenFunc()); # This won't work!
This tutorial covers the main features of FlavorLang. You can now start creating your own programs using these culinary programming concepts! Remember that like cooking, programming gets better with practice, so don't be afraid to experiment with different combinations of these features.
This project is licensed under the Apache 2.0 License — see the LICENSE file for details.
© 2024-2025 Kenneth Oliver. All rights reserved.