diff options
Diffstat (limited to 'js/baba-yaga/example.baba')
-rw-r--r-- | js/baba-yaga/example.baba | 336 |
1 files changed, 71 insertions, 265 deletions
diff --git a/js/baba-yaga/example.baba b/js/baba-yaga/example.baba index 2311f86..353882b 100644 --- a/js/baba-yaga/example.baba +++ b/js/baba-yaga/example.baba @@ -1,269 +1,75 @@ -// This file demonstrates all features of Baba Yaga. - -// 1. Comments -// Single-line comments start with `//`. -myVar : 10; // Comments can also be at the end of a line. - -// 2. Types -// The language supports Int, Float, String, and Result types. -myInt : 10; // Inferred as Int -myFloat : 3.14; // Inferred as Float -myString : "Baba Yaga"; // Inferred as String - -// Type declarations are optional but enforced if provided. -myExplicitInt Int; -myExplicitInt : 20; - -myExplicitString String; -myExplicitString : "Explicitly typed string."; - -// Explicit List and Table type declarations -myExplicitList List; -myExplicitList : [1 2 3 4 5]; - -myExplicitTable Table; -myExplicitTable : { name: "John" age: 25 city: "New York" }; - -// 3. Variable and Type Declarations -// Variables are declared using an identifier followed by a colon and their value. -// Example: myVariable : value; - -// 4. Functions (Anonymous, Currying, Partial Application) - -// A simple anonymous function (x -> x + 1) -addOne : x -> x + 1; -resultAddOne : addOne 5; // resultAddOne will be 6 -io.out resultAddOne; - -// A curried function with type annotations -multiply : (x: Float) -> (Float -> Float) -> y -> x * y; -// The type signature here breaks down like: -// 1. (x: Float) - First parameter is a Float called x -// 2. -> - Returns... -// 3. (Float -> Float) - A function that takes a Float and returns a Float -// 4. -> y -> x * y - The implementation: take y, multiply x by y - -// Partial application: create a new function by applying some arguments -multiplyByTwo : multiply 2.0; -resultMultiply : multiplyByTwo 7.0; // resultMultiply will be 14.0 -io.out resultMultiply; - -// 5. Operators -// Arithmetic: +, -, *, /, % -// Comparison: =, >, <, >=, <= +// Example Baba Yaga Program +// Demonstrates core language features + +// Variables and basic operations +x : 42; +y : x + 8; +message : "Hello, Baba Yaga!"; + +// Functions and currying +add : a b -> a + b; +multiply : a b -> a * b; +addTen : add 10; + +// Higher-order functions with lists +numbers : [1, 2, 3, 4, 5]; +doubled : map (x -> x * 2) numbers; +evens : filter (x -> x % 2 = 0) doubled; +sum : reduce (acc x -> acc + x) 0 evens; + +// Pattern matching +classify : n -> + when n is + 0 then "zero" + 1 then "one" + Int then when (n > 10) is true then "big" _ then "small" + _ then "unknown"; + +// Error handling with Result types +safeDivide : a b -> + when b is + 0 then Err "Division by zero" + _ then Ok (a / b); + +// Table (object) operations +person : { name: "Alice", age: 30, city: "New York" }; + +// Output results +io.out "=== Baba Yaga Language Demo ==="; +io.out ""; + +io.out "Basic values:"; +io.out x; +io.out y; +io.out message; -// Arithmetic operations -sum : 10 + 5; // 15 -difference : 10 - 5; // 5 -product : 10 * 5; // 50 -quotient : 10 / 5; // 2 -remainder : 10 % 3; // 1 +io.out ""; +io.out "Function results:"; +io.out (addTen 5); +io.out (multiply 6 7); +io.out ""; +io.out "List operations:"; +io.out doubled; +io.out evens; io.out sum; -io.out difference; -io.out product; -io.out quotient; -io.out remainder; - -// Comparison operations -isEqual : 10 = 10; // true -isGreaterThan : 10 > 5; // true -isLessThan : 5 < 10; // true -isGreaterThanOrEqualTo : 10 >= 10; // true -isLessThanOrEqualTo : 5 <= 10; // true - -io.out isEqual; -io.out isGreaterThan; -io.out isLessThan; -io.out isGreaterThanOrEqualTo; -io.out isLessThanOrEqualTo; - -// 6. Control Flow: The `when` Expression - -// Literal Matching -checkNumber : num -> - when num is - 1 then "One" - 2 then "Two" - _ then "Something else"; // '_' matches any value - -resultCheckNumberOne : checkNumber 1; // "One" -resultCheckNumberThree : checkNumber 3; // "Something else" - -io.out resultCheckNumberOne; -io.out resultCheckNumberThree; - -// Multiple Discriminants -checkCoords : x y -> - when x y is - 0 0 then "Origin" - 1 1 then "Diagonal" - _ _ then "Somewhere else"; - -resultCheckCoordsOrigin : checkCoords 0 0; // "Origin" -resultCheckCoordsOther : checkCoords 5 10; // "Somewhere else" - -io.out resultCheckCoordsOrigin; -io.out resultCheckCoordsOther; - -// Type Matching -checkType : val -> - when val is - Bool then "It's a Boolean" - Int then "It's an Integer" - Float then "It's a Float" - String then "It's a String" - List then "It's a List" - Table then "It's a Table" - _ then "Unknown Type"; - -resultCheckTypeBool : checkType true; // "It's a Boolean" -resultCheckTypeInt : checkType 123; // "It's an Integer" -resultCheckTypeFloat : checkType 3.14; // "It's a Float" -resultCheckTypeString : checkType "abc"; // "It's a String" -resultCheckTypeList : checkType [1 2 3]; // "It's a List" -resultCheckTypeTable : checkType { name: "test" }; // "It's a Table" - -io.out resultCheckTypeBool; -io.out resultCheckTypeInt; -io.out resultCheckTypeFloat; -io.out resultCheckTypeString; -io.out resultCheckTypeList; -io.out resultCheckTypeTable; - -// List Pattern Matching -matchList : list -> - when list is - [1 2 3] then "Exact List Match" - [1 _ 3] then "List with Wildcard Match" - _ then "No List Match"; - -resultMatchListExact : matchList [1 2 3]; // "Exact List Match" -resultMatchListWildcard : matchList [1 99 3]; // "List with Wildcard Match" -resultMatchListNoMatch : matchList [4 5 6]; // "No List Match" - -io.out resultMatchListExact; -io.out resultMatchListWildcard; -io.out resultMatchListNoMatch; -// Table Pattern Matching -matchTable : table -> - when table is - { name: "Alice" age: 30 } then "Exact Table Match" - { name: "Bob" age: _ } then "Table with Wildcard Value Match" - _ then "No Table Match"; - -resultMatchTableExact : matchTable { name: "Alice" age: 30 }; // "Exact Table Match" -resultMatchTableWildcardValue : matchTable { name: "Bob" age: 99 }; // "Table with Wildcard Value Match" -resultMatchTableNoMatch : matchTable { city: "New York" }; // "No Table Match" - -io.out resultMatchTableExact; -io.out resultMatchTableWildcardValue; -io.out resultMatchTableNoMatch; - -// 7. Error Handling: The `Result` Type - -// Function returning a Result type -divide : x y -> - when y is - 0 then Err "Division by zero is not allowed." - _ then Ok (x / y); - -resultDivideOk : divide 10 2; // Result: Ok 5 -resultDivideErr : divide 5 0; // Result: Err "Division by zero is not allowed." - -// Extracting values from Result types using 'when' -finalResultOk : when resultDivideOk is - Ok val then val // 'val' binds to the inner value of Ok (5) - Err msg then 0; // If error, return 0 - -finalResultErr : when resultDivideErr is - Ok val then 0 - Err msg then msg; // 'msg' binds to the inner value of Err ("Division by zero...") - -io.out finalResultOk; -io.out finalResultErr; - -// 8. Lists -myListExample : [10 20 30 "hello"]; - -// Accessing elements by index (0-based) -firstElement : myListExample.0; // 10 -secondElement : myListExample.1; // 20 - -io.out myListExample; -io.out firstElement; -io.out secondElement; - -// 9. Tables -myTableExample : { name: "Baba Yaga" version: 1.0 isActive: true }; - -// Accessing properties by key -userName : myTableExample.name; // "Baba Yaga" -userVersion : myTableExample.version; // 1.0 - -io.out myTableExample; -io.out userName; -io.out userVersion; - -// Function within a Table -myCalculator : { - add: x y -> x + y; - subtract: x y -> x - y; -}; - -resultTableAdd : myCalculator.add 10 5; // 15 -resultTableSubtract : myCalculator.subtract 10 5; // 5 - -io.out resultTableAdd; -io.out resultTableSubtract; - -// 10. Higher-Order Functions - -// map: Applies a function to each element of a list, returning a new list. -doubledList : map (x -> x * 2) [1 2 3]; // [2 4 6] -io.out doubledList; - -// filter: Creates a new list containing only elements for which a predicate returns true. -evenNumbers : filter (x -> x % 2 = 0) [1 2 3 4 5]; // [2 4] -io.out evenNumbers; - -// reduce: Applies a function against an accumulator and each element in the list to reduce it to a single value. -sumOfList : reduce (acc item -> acc + item) 0 [1 2 3 4]; // 10 -io.out sumOfList; - -// 11. Typed Functions with Type Enforcement - -// Typed function declarations with parameter and return type annotations -add : (x: Int, y: Int) -> Int -> x + y; -multiply : (x: Number, y: Number) -> Number -> x * y; -greet : (name: String) -> String -> str.concat "Hello " name; -fullName : (first: String, last: String) -> String -> first .. " " .. last; -isEven : (n: Int) -> Bool -> n % 2 = 0; -isPositive : (n: Int) -> Bool -> n > 0; - -// Test typed functions -io.out add 5 3; -io.out multiply 2.5 3.0; -io.out greet "World"; -io.out fullName "John" "Doe"; -io.out isEven 4; -io.out isEven 5; -io.out isPositive 10; -io.out isPositive -5; - -// 12. String Functions - -// Core string operations -io.out str.length "hello"; -io.out str.upper "hello world"; -io.out str.lower "HELLO WORLD"; -io.out str.split "hello,world,test" ","; -io.out str.join ["a" "b" "c"] "-"; -io.out str.trim " hello "; -io.out str.substring "hello world" 0 5; -io.out str.replace "hello hello" "hello" "hi"; - -// String concatenation with .. operator -message : "Hello" .. " " .. "World" .. "!"; -io.out message; +io.out ""; +io.out "Pattern matching:"; +io.out (classify 0); +io.out (classify 15); +io.out (classify 3); + +io.out ""; +io.out "Error handling:"; +result1 : safeDivide 10 2; +result2 : safeDivide 10 0; +io.out result1; +io.out result2; + +io.out ""; +io.out "Table operations:"; +io.out person; + +io.out ""; +io.out "Demo complete!"; |