Bob/bob-language-extension/example.bob
Bobby Lucero 17c15e5bad Various changes
Arrays, loops, ternary, and other major features. Check doc
2025-08-06 21:42:09 -04:00

111 lines
2.2 KiB
Plaintext

// Bob Language Example - Demonstrates syntax highlighting
// Variable declarations
var message = "Hello, Bob!";
var number = 42;
var pi = 3.14159;
var isActive = true;
var empty = none;
// Binary and hex numbers
var binaryNum = 0b1010; // 10 in decimal
var hexNum = 0xFF; // 255 in decimal
// Print statements
print(message);
print("Number: " + toString(number));
print("Pi: " + toString(pi));
// Array operations
var numbers = [1, 2, 3, 4, 5];
var fruits = ["apple", "banana", "cherry"];
print("Array length: " + len(numbers));
print("First element: " + numbers[0]);
numbers[2] = 99; // Array assignment
push(numbers, 6); // Add element
var lastElement = pop(numbers); // Remove and get last element
// Function definition
func factorial(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// While loop with break
var counter = 0;
while (counter < 10) {
if (counter == 5) {
break;
}
counter = counter + 1;
}
// For loop with continue
var sum = 0;
for (var i = 0; i < 10; i = i + 1) {
if (i % 2 == 0) {
continue;
}
sum = sum + i;
}
// Do-while loop
var doCounter = 0;
do {
doCounter = doCounter + 1;
} while (doCounter < 5);
// Anonymous function
var double = func(x) {
return x * 2;
};
// Ternary operator
var max = 10 > 5 ? 10 : 5;
var status = age >= 18 ? "adult" : "minor";
// String multiplication
var repeated = "hello" * 3; // "hellohellohello"
var numRepeated = 3 * "hi"; // "hihihi"
// Logical operators
var a = true && false;
var b = true || false;
var c = !false;
// Bitwise operators
var d = 5 & 3;
var e = 5 | 3;
var f = 5 ^ 3;
var g = 5 << 1;
var h = 5 >> 1;
// Compound assignment
var value = 10;
value += 5;
value *= 2;
value -= 3;
// New built-in functions
var randomValue = random();
sleep(100); // Sleep for 100ms
printRaw("No newline here");
eval("print('Dynamic code execution!');");
// Assertions
assert(factorial(5) == 120, "Factorial calculation failed");
assert(sum == 25, "Sum calculation failed");
// Type checking
var typeOfMessage = type(message);
var typeOfNumber = type(number);
var typeOfArray = type(numbers);
// Time function
var currentTime = time();
print("All tests passed!");