1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// 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: =, >, <, >=, <=
// Arithmetic operations
sum : 10 + 5; // 15
difference : 10 - 5; // 5
product : 10 * 5; // 50
quotient : 10 / 5; // 2
remainder : 10 % 3; // 1
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;
|