about summary refs log tree commit diff stats
path: root/js/baba-yaga/examples
diff options
context:
space:
mode:
Diffstat (limited to 'js/baba-yaga/examples')
-rw-r--r--js/baba-yaga/examples/js-interop-demo.baba95
-rw-r--r--js/baba-yaga/examples/js-interop-simple.baba49
2 files changed, 144 insertions, 0 deletions
diff --git a/js/baba-yaga/examples/js-interop-demo.baba b/js/baba-yaga/examples/js-interop-demo.baba
new file mode 100644
index 0000000..cb23ba0
--- /dev/null
+++ b/js/baba-yaga/examples/js-interop-demo.baba
@@ -0,0 +1,95 @@
+// js-interop-demo.baba - Demonstration of JavaScript interop features
+
+io.out "=== Baba Yaga JavaScript Interop Demo ===";
+io.out "";
+
+// 1. Mathematical operations
+io.out "1. Mathematical Operations:";
+absResult : io.callJS "Math.abs" [-42];
+minResult : io.callJS "Math.min" [10, 5, 8, 15];
+maxResult : io.callJS "Math.max" [10, 5, 8, 15];
+
+io.out ("Math.abs(-42) = " .. when absResult is Ok x then x _ then "error");
+io.out ("Math.min([10,5,8,15]) = " .. when minResult is Ok x then x _ then "error");
+io.out ("Math.max([10,5,8,15]) = " .. when maxResult is Ok x then x _ then "error");
+io.out "";
+
+// 2. JSON processing
+io.out "2. JSON Processing:";
+jsonData : "{\"name\": \"Alice\", \"age\": 30, \"scores\": [85, 92, 78]}";
+parseResult : io.callJS "JSON.parse" [jsonData];
+
+when parseResult is
+  Ok jsObj then (
+    io.out "Successfully parsed JSON object";
+    
+    // Convert to Baba Yaga table
+    tableResult : io.objectToTable jsObj;
+    when tableResult is
+      Ok table then (
+        io.out "Converted to Baba Yaga table";
+        
+        // Access properties
+        nameResult : io.getProperty jsObj "name";
+        ageResult : io.getProperty jsObj "age";
+        
+        when (nameResult, ageResult) is
+          (Ok name, Ok age) then (
+            io.out ("Name: " .. name);
+            io.out ("Age: " .. age);
+          )
+          _ then io.out "Error accessing properties";
+      )
+      Err msg then io.out ("Table conversion error: " .. msg);
+  )
+  Err msg then io.out ("JSON parse error: " .. msg);
+
+io.out "";
+
+// 3. Array processing
+io.out "3. Array Processing:";
+babaList : [1, 2, 3, 4, 5];
+jsArray : io.listToJSArray babaList;
+stringifyResult : io.callJS "JSON.stringify" [jsArray];
+
+when stringifyResult is
+  Ok jsonStr then io.out ("Baba Yaga list as JSON: " .. jsonStr)
+  Err msg then io.out ("Stringify error: " .. msg);
+
+// Process JavaScript array
+jsArrayStr : "[10, 20, 30, 40, 50]";
+jsParseResult : io.callJS "JSON.parse" [jsArrayStr];
+
+when jsParseResult is
+  Ok jsArr then (
+    listResult : io.jsArrayToList jsArr;
+    when listResult is
+      Ok babaArr then (
+        // Process with Baba Yaga functions
+        doubled : map (x -> x * 2) babaArr;
+        filtered : filter (x -> x > 50) doubled;
+        
+        io.out ("Processed array: " .. join ", " (map (x -> x) filtered));
+      )
+      Err msg then io.out ("List conversion error: " .. msg);
+  )
+  Err msg then io.out ("Array parse error: " .. msg);
+
+io.out "";
+
+// 4. Error handling demonstration
+io.out "4. Error Handling:";
+errorResult : io.callJS "nonExistentFunction" [42];
+when errorResult is
+  Ok _ then io.out "Unexpected success"
+  Err msg then io.out ("Expected error caught: " .. msg);
+
+io.out "";
+io.out "=== Demo Complete ===";
+io.out "";
+io.out "This demonstrates safe JavaScript interop with:";
+io.out "- Mathematical operations (Math.abs, Math.min, Math.max)";
+io.out "- JSON parsing and stringification"; 
+io.out "- Type conversions between JS and Baba Yaga";
+io.out "- Array processing pipelines";
+io.out "- Explicit error handling with Result types";
diff --git a/js/baba-yaga/examples/js-interop-simple.baba b/js/baba-yaga/examples/js-interop-simple.baba
new file mode 100644
index 0000000..b937503
--- /dev/null
+++ b/js/baba-yaga/examples/js-interop-simple.baba
@@ -0,0 +1,49 @@
+// js-interop-simple.baba - Simple JavaScript interop demonstration
+
+io.out "=== JavaScript Interop Demo ===";
+io.out "";
+
+// Mathematical operations
+io.out "Math Operations:";
+absResult : io.callJS "Math.abs" [-42];
+io.out absResult;
+
+minResult : io.callJS "Math.min" [10, 5, 8];
+io.out minResult;
+
+maxResult : io.callJS "Math.max" [10, 5, 8];  
+io.out maxResult;
+
+io.out "";
+
+// JSON operations
+io.out "JSON Operations:";
+jsonStr : "{\"name\": \"Alice\", \"age\": 30}";
+parseResult : io.callJS "JSON.parse" [jsonStr];
+io.out parseResult;
+
+// Property access
+propResult : when parseResult is
+  Ok obj then io.getProperty obj "name"
+  Err msg then Err msg;
+
+io.out propResult;
+
+io.out "";
+
+// Array operations  
+io.out "Array Operations:";
+babaList : [1, 2, 3, 4, 5];
+jsArray : io.listToJSArray babaList;
+jsonResult : io.callJS "JSON.stringify" [jsArray];
+io.out jsonResult;
+
+io.out "";
+
+// Error handling
+io.out "Error Handling:";
+errorResult : io.callJS "invalidFunction" [];
+io.out errorResult;
+
+io.out "";
+io.out "Demo complete!";