FunLang is a lightweight, interpreted programming language designed for learning compiler/interpreter concepts.
- Dynamic and Static Typing: Variables support both dynamic typing and optional type declarations
- First-class Functions: Functions are values that can be passed around with strict return type checking
- Control Flow: If/elif/else statements, for and while loops with break/continue support
- Data Structures: Numbers (integers and floats), strings, and lists with comprehensive operations
- Type Casting: Convert between different data types
- Built-in Functions: Several utility functions included
- LLVM Compilation: Compile FunLang code to LLVM IR and native executables
var name = value;
Variables can be reassigned after declaration:
var x = 5;
x = 10; // Reassignment
var integer = 42;
var float_num = 3.14;
var greeting = "Hello, world!";
var numbers = [1, 2, 3, 4, 5];
var mixed = [1, "two", 3.0];
fun add(a, b) {
return a + b;
};
// Functions with type annotations and return types
fun int multiply(int a, int b) {
return a * b;
};
var result = add(5, 3);
var int num = 42;
var float pi = 3.14;
var string hello = "hello world!";
var list numbers = [1, 2, 3, 4, 5];
// Strict type checking ensures type safety
var int strict_num = 42; // Must be an integer
if condition {
// code
} elif another_condition {
// code
} else {
// code
}
For loop:
for i = 0, 10 {
print(i);
}
With step value:
for i = 0, 10, 2 {
print(i); // 0, 2, 4, 6, 8
}
While loop with break/continue support:
var i = 0;
while i < 10 {
if i == 5 {
continue; // Skip iteration
}
if i == 8 {
break; // Exit loop
}
print(i);
i = i + 1;
}
Convert between different types:
// String to integer
var str_num = "42";
var num = to_int(str_num);
// String to float
var str_float = "3.14";
var float_num = to_float(str_float);
// Number to string
var num = 42;
var str = to_string(num);
// Value to list
var chars = to_list("hello"); // Creates a list of characters
print(value): Display a valueclear(): Clear the consoleis_number(value): Check if value is a numberis_string(value): Check if value is a stringis_list(value): Check if value is a listis_fun(value): Check if value is a functionlen(list): Get length of a listto_string(value): Convert value to stringto_int(value): Convert value to integerto_float(value): Convert value to floatto_list(value): Convert value to listtypeof(value): Get the type of value
Run the installation script:
./install_pip.shThis sets up FunLang for both development and regular use by:
- Creating a venv with dependencies
- Installing in editable mode (changes take effect immediately)
- Adding to your PATH (use
funlanganywhere)
Then reload your shell:
source ~/.zshrc # or ~/.bashrcSee INSTALLATION.md for detailed instructions.
Once installed, use the funlang command:
funlangIn interactive mode, you can use special commands:
run <code>- Interpret code directlycompile <code>- Compile code to LLVM IR
funlang script.flfunlang --config turkish examples/turkish_example.fl
funlang --config spanish examples/spanish_example.fl
funlang --config emoji examples/example.flfunlang --compile script.flThis generates a .ll file with LLVM intermediate representation.
funlang --build script.flThis compiles the FunLang code to a native executable.
If you haven't installed the package, you can run directly:
python3 main.py # Interactive mode
python3 main.py script.fl # Run a script
python3 main.py --config turkish script.flCheck the examples/ directory for sample programs:
example.fl: Basic language featuresexample2.fl: Binary search with type castingexample3.fl: Simple arithmeticturkish_example.fl: Full example using Turkish keywordsspanish_example.fl: Full example using Spanish keywords
Try them with different language configs:
funlang --config turkish examples/turkish_example.fl
funlang --config spanish examples/spanish_example.flFunLang is implemented in Python with both interpreter and compiler backends:
- Lexer (
lexer.py): Converts source code into tokens - Parser (
parser.py): Converts tokens into an Abstract Syntax Tree - Interpreter (
interpreter.py): Executes the AST directly - Code Generator (
codegen.py): Compiles AST to LLVM IR for native execution
This project is open-source and available for educational purposes.