about summary refs log tree commit diff stats
path: root/js/scripting-lang/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/README.md')
-rw-r--r--js/scripting-lang/README.md320
1 files changed, 147 insertions, 173 deletions
diff --git a/js/scripting-lang/README.md b/js/scripting-lang/README.md
index 73ccbf1..5890a06 100644
--- a/js/scripting-lang/README.md
+++ b/js/scripting-lang/README.md
@@ -1,220 +1,194 @@
-# Simple Scripting Language
+# Baba Yaga
+## A Scripting Language
 
-A functional programming language with immutable variables, first-class functions, and pattern matching.
+Baba Yaga is a combinator-based scripting language that aims to be dangerously functional-brained, has minimal syntax, an intuitive approach to pattern matching, and hopefully enough of a standard library to be useful...but not too much of a standard library to be difficult to recall.
 
-## Features
+This whole thing started as an aesthetic curiosity, and continued on from there. I wanted to be able to do pattern matching like this: 
 
-- **Immutable Variables**: Variables cannot be reassigned
-- **First-Class Functions**: Functions can be passed as arguments and stored in data structures
-- **Lexical Scoping**: Functions create their own scope
-- **Pattern Matching**: Case expressions with wildcard support
-- **Table Literals**: Lua-style tables with both array-like and key-value entries
-- **Standard Library**: Built-in higher-order functions (`map`, `compose`, `pipe`, `apply`, `filter`, `reduce`, `fold`, `curry`)
-- **IO Operations**: Built-in input/output operations (`..in`, `..out`, `..assert`)
-- **Floating Point Arithmetic**: Full support for decimal numbers
-- **Unary Minus**: Support for negative numbers (e.g., `-1`, `-3.14`)
+```plaintext
+factorial : n -> 
+  when n is
+    0 then 1
+    _ then n * (factorial (n - 1));
+```
 
-## Syntax
+I've implemented a whole bunch of [forths](https://git.sr.ht/~eli_oat/chupacabra), and a couple schemes, but never have I ever implemented something like a "regular" programming language. And, while, an [ML-flavored](https://en.wikipedia.org/wiki/Standard_ML) programming language isn't exactly regular, this has grown from an aesthetic curiosity to a full-blown aesthetic indulgence.
 
-### Basic Operations
-```
-/* Arithmetic */
-x : 5 + 3;
-y : 10 - 2;
-z : 4 * 3;
-w : 15 / 3;
-neg : -5;  /* Unary minus */
-
-/* Comparisons */
-result : x > y;
-equal : a = b;
-not_equal : a != b;
-
-/* Logical */
-and_result : true and false;
-or_result : true or false;
-```
+Baba Yaga supports...
 
-### Variables and Functions
-```
-/* Immutable variables */
-x : 42;
-y : "hello";
+- **Function definitions** using arrow syntax with lexical scoping
+- **Pattern matching** with a single `when ... is ... then` expression that handles wildcards and arbitrarily nested features
+- **Tables** inspired by Lua's tables, with array-like and key-value entries, including boolean keys
+- **Function references** using an `@` operator for higher-order programming
+- **IO Operations** including input, output, and assertions, plus an `..emit` and `..listen` pattern for interfacing a functional core with the outside world. This contains side effects to a very limited surface area
+- **Standard Library** with a complete set of arithmetic, comparison, logical, and higher-order combinators...I think (let me know if I'm missing anything)
+- **APL-style operations** with element-wise and immutable table operations so that you can use broadcasting instead of looping
+- **Function composition** with `compose`, `pipe`, and `via` operators, supporting a bunch of different ways to chain functions together
+- **Currying by default** - all functions are automatically curried
+- **Combinator-based architecture** everything is "just" a function call or reference under the hood...because this supposedly made parsing easier...but I'm not yet totally sold on that reasoning
+
+## Example Script
+```plaintext
+/* Basic arithmetic */
+result : 5 + 3 * 2;
+..out result;
 
 /* Function definition */
-f : x -> x * 2;
+factorial : n -> 
+  when n is
+    0 then 1
+    _ then n * (factorial (n - 1));
 
-/* Function call */
-result : f 5;
-```
+/* Function composition */
+double : x -> x * 2;
+increment : x -> x + 1;
+composed : compose @double @increment 5;
+..out composed;
 
-### Tables
+/* Pattern matching */
+classify : x y -> 
+  when x y is
+    0 0 then "both zero"
+    0 _ then "x is zero"
+    _ 0 then "y is zero"
+    _ _ then "neither zero";
+
+/* Tables */
+person : {name: "Baba Yaga", age: 99, active: true};
+..out person.name;
+..out person["age"];
+
+numbers : {1, 2, 3, 4, 5};
+doubled : map @double numbers;
+..out doubled[1]; 
+
+/* APL-style element-wise operations over tables */
+table1 : {a: 1, b: 2, c: 3};
+table2 : {a: 10, b: 20, c: 30};
+sum : each @add table1 table2;
+..out sum.a;
 ```
-/* Table literal */
-table : {1, 2, 3, key: "value"};
 
-/* Table access */
-first : table[1];
-value : table.key;
-nested : table.key.subkey;
+Baba Yaga files should use either the `.txt` file extension, or the `.baba` extension.
+
+## Key Features
+
+### Function Application
+
+Functions are applied using juxtaposition (space-separated), and you can use parentheses to help disambiguate precedence: 
+```plaintext
+f x          /* Apply function f to argument x */
+f x y        /* Apply f to x, then apply result to y */
+f (g x)      /* Apply g to x, then apply f to result */
 ```
 
 ### Pattern Matching
-```
-/* Case expression */
-result : case x of
-    1 : "one"
-    2 : "two"
-    _ : "other";
-```
 
-### IO Operations
+Use `when` expressions for pattern matching:
+```plaintext
+result : when value is
+  0 then "zero"
+  1 then "one"
+  _ then "other";
 ```
-/* Output */
-..out "Hello, World!";
 
-/* Input */
-name : ..in;
+### Tables
 
-/* Assertion */
-..assert x = 5;
-```
+Create and access data structures:
+```plaintext
+/* Array-like */
+numbers : {1, 2, 3, 4, 5};
 
-### Standard Library
+/* Key-value pairs */
+person : {name: "Beatrice", age: 26};
+
+/* Boolean keys */
+flags : {true: "enabled", false: "disabled"};
 ```
-/* Map */
-double : x -> x * 2;
-squared : map @double 5;
 
-/* Filter */
-isPositive : x -> x > 0;
-filtered : filter @isPositive 5;
+### Function References
 
-/* Compose */
-f : x -> x + 1;
-g : x -> x * 2;
-h : compose @f @g;
-result : h 5;  /* (5 * 2) + 1 = 11 */
+Use the `@` to make reference to functions as arguments for other functions: 
+```plaintext
+numbers : {1, 2, 3, 4, 5};
+doubled : map @double numbers;
 ```
 
-## Usage
+## Combinators and Higher-Order Functions
 
-### Running Scripts
-```bash
-node lang.js script.txt
-```
+Baba Yaga tries to provide a comprehensive set of combinators for functional programming:
 
-### Testing
-The project uses a structured testing approach with unit and integration tests:
-
-#### Unit Tests
-Located in `tests/` directory, each focusing on a specific language feature:
-- `01_lexer_basic.txt` - Basic lexer functionality
-- `02_arithmetic_operations.txt` - Arithmetic operations
-- `03_comparison_operators.txt` - Comparison operators
-- `04_logical_operators.txt` - Logical operators
-- `05_io_operations.txt` - IO operations
-- `06_function_definitions.txt` - Function definitions
-- `07_case_expressions.txt` - Case expressions and pattern matching
-- `08_first_class_functions.txt` - First-class function features
-- `09_tables.txt` - Table literals and access
-- `10_standard_library.txt` - Standard library functions
-
-#### Integration Tests
-Test combinations of multiple features:
-- `integration_01_basic_features.txt` - Basic feature combinations
-- `integration_02_pattern_matching.txt` - Pattern matching with other features
-- `integration_03_functional_programming.txt` - Functional programming patterns
-
-#### Running Tests
-```bash
-# Run all tests
-./run_tests.sh
+### Core Combinators
 
-# Run individual tests
-node lang.js tests/01_lexer_basic.txt
-node lang.js tests/integration_01_basic_features.txt
-```
+- `map f x` - Transform elements in collections
+- `filter p x` - Select elements based on predicates  
+- `reduce f init x` - Accumulate values into a single result
+- `each f x` - Multi-argument element-wise operations
 
-## Implementation Details
+### Function Composition
 
-### Architecture
-- **Lexer**: Tokenizes input into tokens (numbers, identifiers, operators, etc.)
-- **Parser**: Builds Abstract Syntax Tree (AST) from tokens
-- **Interpreter**: Executes AST with scope management
+- `compose f g` - Right-to-left composition (mathematical style)
+- `pipe f g` - Left-to-right composition (pipeline style)
+- `via` - Kinda like `.` composition syntax: `f via g via h`
 
-### Key Components
-- **Token Types**: Supports all basic operators, literals, and special tokens
-- **AST Nodes**: Expression, statement, and declaration nodes
-- **Scope Management**: Lexical scoping with proper variable resolution
-- **Error Handling**: Comprehensive error reporting for parsing and execution
+### Table Operations
 
-## Recent Fixes
+All table operations are immutable so they return new tables. 
 
-### ✅ Parser Ambiguity with Unary Minus Arguments (Latest Fix)
-- **Issue**: `filter @isPositive -3` was incorrectly parsed as binary operation instead of function call with unary minus argument
-- **Root Cause**: Parser treating `FunctionReference MINUS` as binary minus operation
-- **Solution**: Added special case in `parseExpression()` to handle `FunctionReference MINUS` pattern
-- **Status**: ✅ Resolved - Standard library functions now work with negative arguments
+- `t.map`, `t.filter`, `t.set`, `t.delete`, `t.merge`, `t.get`, `t.has`, `t.length`
 
-### ✅ Unary Minus Operator
-- **Issue**: Stack overflow when parsing negative numbers (e.g., `-1`)
-- **Root Cause**: Parser lacked specific handling for unary minus operator
-- **Solution**: Added `UnaryMinusExpression` parsing and evaluation
-- **Status**: ✅ Resolved - All tests passing
+### When to Use Which Combinator
 
-### ✅ IO Operation Parsing
-- **Issue**: IO operations not parsed correctly at top level
-- **Solution**: Moved IO parsing to proper precedence level
-- **Status**: ✅ Resolved
+- Use `map` for general collections, `t.map` to emphasize table operations
+- Use `map` for single-table transformations, `each` for combining multiple collections.
 
-### ✅ Decimal Number Support
-- **Issue**: Decimal numbers not handled correctly
-- **Solution**: Updated lexer and interpreter to use `parseFloat()`
-- **Status**: ✅ Resolved
 
-## Known Issues
+### Standard Library
 
-### 🔄 Logical Operator Precedence
-- **Issue**: Logical operators (`and`, `or`, `xor`) have incorrect precedence relative to function calls
-- **Example**: `isEven 10 and isPositive 5` is parsed as `isEven(10 and isPositive(5))` instead of `(isEven 10) and (isPositive 5)`
-- **Impact**: Complex expressions with logical operators may not evaluate correctly
-- **Status**: 🔄 In Progress - Working on proper operator precedence hierarchy
-- **Workaround**: Use parentheses to explicitly group expressions: `(isEven 10) and (isPositive 5)`
+The language includes a comprehensive standard library:
 
-### 🔄 Parentheses Parsing with Logical Operators
-- **Issue**: Some expressions with logical operators inside parentheses fail to parse
-- **Example**: `add (multiply 3 4) (isEven 10 and isPositive 5)` may fail with parsing errors
-- **Status**: 🔄 In Progress - Related to logical operator precedence issue
+**Arithmetic**: `add`, `subtract`, `multiply`, `divide`, `modulo`, `power`, `negate`  
+**Comparison**: `equals`, `notEquals`, `lessThan`, `greaterThan`, `lessEqual`, `greaterEqual`  
+**Logical**: `logicalAnd`, `logicalOr`, `logicalXor`, `logicalNot`  
+**Higher-Order**: `map`, `compose`, `pipe`, `apply`, `filter`, `reduce`, `fold`, `curry`, `each`  
+**Enhanced**: `identity`, `constant`, `flip`, `on`, `both`, `either`  
+**Table Operations**: `t.map`, `t.filter`, `t.set`, `t.delete`, `t.merge`, `t.get`, `t.has`, `t.length`
 
-## Development
+## Architecture
 
-### File Structure
-```
-.
-├── lang.js              # Main implementation
-├── test.txt             # Comprehensive test file
-├── tests/               # Unit and integration tests
-│   ├── 01_lexer_basic.txt
-│   ├── 02_arithmetic_operations.txt
-│   ├── ...
-│   ├── integration_01_basic_features.txt
-│   ├── integration_02_pattern_matching.txt
-│   └── integration_03_functional_programming.txt
-├── run_tests.sh         # Test runner script
-├── FIXME.md             # Issues and fixes documentation
-└── README.md            # This file
+Baba Yaga uses a combinator-based architecture where all operations are translated to function calls:
+
+1. **Lexer**: Converts source code into tokens
+2. **Parser**: Translates tokens into AST, converting operators to combinator calls
+3. **Interpreter**: Executes combinator functions from the standard library
+
+The idea behind this approach is that it should eliminate parsing ambiguity while preserving syntax and enabling functional programming patterns like currying and composition, but the implementation is likely a little bit janky.
+
+## Testing
+
+Run the complete test suite!
+```bash
+./run_tests.sh
 ```
 
-### Debugging
-Enable debug mode by setting `DEBUG=true`:
+This assumes you are using bun, but I've also tested extensively with both node and the browser, too. I haven't validated it, yet, but I think Baba Yaga should work with quickjs, too.
+
+### Debug Mode
+
+Enable debug output for development using the flag `DEBUG=1` or `DEBUG=2`:
 ```bash
-DEBUG=true node lang.js script.txt
+DEBUG=1 node lang.js your-script.txt
 ```
 
-## Contributing
+This'll output a lot of debug info, including the AST (as JSON). 
+
+### Adding Features
+
+If you wanna add language features, the easiest way to do it is to see if you can implement them in Baba Yaga script, if you need to get into the guts of the thing, though, you wanna follow this pattern, 
 
-1. Create focused unit tests for new features
-2. Add integration tests for feature combinations
-3. Update documentation
-4. Run the full test suite before submitting changes 
\ No newline at end of file
+1. Add tests in `tests/`
+2. Add tokens in `lexer.js`
+3. Add parsing logic in `parser.js`
+4. Add evaluation logic in `lang.js`
+5. Validate against the tests you added earlier
+6. Update documentation
\ No newline at end of file
cc?h=hlt&id=8923d6f658d09de800164b8fc6514475f49307b4'>^
36594a43 ^


a5840f21 ^
36594a43 ^
0a2026a6 ^



0a2026a6 ^




0a2026a6 ^




36594a43 ^

0a2026a6 ^





0a2026a6 ^
36594a43 ^
0a2026a6 ^
36594a43 ^
0a2026a6 ^
ac0e9db5 ^
781f2462 ^


ac0e9db5 ^
36594a43 ^
05d17773 ^
35064671 ^
0a2026a6 ^




36594a43 ^










35064671 ^



decaddb4 ^
363be37f ^

9ea76483 ^
363be37f ^
35064671 ^

9ea76483 ^
363be37f ^
36594a43 ^





35064671 ^
36594a43 ^
35064671 ^
36594a43 ^

35064671 ^
36594a43 ^
0a2026a6 ^




36594a43 ^





100157d1 ^

36594a43 ^
























a316f1e4 ^




7232de70 ^
36594a43 ^

100157d1 ^






36594a43 ^




96ac511e ^
7e423e26 ^



7381b17c ^
7e423e26 ^
7381b17c ^
c5e965d8 ^

7381b17c ^
c5e965d8 ^
7e423e26 ^

a316f1e4 ^














286d7620 ^
96f19e1e ^
363be37f ^
99a2c1a7 ^
96f19e1e ^
ac0e9db5 ^
5db2faeb ^

05d17773 ^
96f19e1e ^
f1a6f323 ^
96f19e1e ^

100157d1 ^
518e6c5e ^


100157d1 ^
bc643692 ^
100157d1 ^
35457609 ^
d7494165 ^

100157d1 ^



bc643692 ^
100157d1 ^
35457609 ^
d7494165 ^

100157d1 ^
051c4738 ^
100157d1 ^

bc643692 ^
100157d1 ^

35457609 ^
d7494165 ^

100157d1 ^
051c4738 ^
100157d1 ^
bc643692 ^
100157d1 ^
bc643692 ^
100157d1 ^
35457609 ^
d7494165 ^

35457609 ^
d7494165 ^

100157d1 ^


bc643692 ^
100157d1 ^
35457609 ^
d7494165 ^

100157d1 ^
96ac511e ^

3a787ca7 ^
96ac511e ^
3a787ca7 ^
96ac511e ^
72c4fb2d ^





a4ef18b1 ^

bc643692 ^
a4ef18b1 ^
35457609 ^
d7494165 ^

a4ef18b1 ^
96ac511e ^

bc643692 ^
96ac511e ^
35457609 ^
d7494165 ^


96ac511e ^


bc643692 ^
96ac511e ^
35457609 ^
d7494165 ^



f02842a9 ^


bc643692 ^
f02842a9 ^
35457609 ^
d7494165 ^



0060093e ^


286ca5a4 ^
0060093e ^
286ca5a4 ^
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352