ocaml bindings for chibi-scheme VM
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add demo binary (bin/main.ml)

+81
+3
bin/dune
··· 1 + (executable 2 + (name main) 3 + (libraries chibi_ocaml))
+78
bin/main.ml
··· 1 + (* chibi-ocaml demo program *) 2 + open Chibi_ocaml.Chibi 3 + 4 + let () = 5 + Printf.printf "=== chibi-ocaml demo ===\n\n"; 6 + 7 + (* 1. Basic evaluation *) 8 + Printf.printf "--- Basic Evaluation ---\n"; 9 + with_context (fun ctx -> 10 + let result = Eval.to_int ctx "(+ 1 2 3 4 5)" in 11 + Printf.printf " (+ 1 2 3 4 5) = %d\n" result; 12 + 13 + let result = Eval.to_string ctx "(map (lambda (x) (* x x)) '(1 2 3 4 5))" in 14 + Printf.printf " (map square '(1 2 3 4 5)) = %s\n" result); 15 + 16 + (* 2. Foreign functions *) 17 + Printf.printf "\n--- Foreign Functions ---\n"; 18 + with_context (fun ctx -> 19 + Env.define_fn2 ctx "ocaml-add" (fun a b -> 20 + Value.of_int ctx (Sexp.to_int a + Sexp.to_int b)); 21 + let result = Eval.to_int ctx "(ocaml-add 100 200)" in 22 + Printf.printf " (ocaml-add 100 200) = %d\n" result; 23 + 24 + Env.define_fn1 ctx "ocaml-reverse" (fun s -> 25 + let str = Sexp.to_string s in 26 + let rev = String.init (String.length str) (fun i -> 27 + str.[String.length str - 1 - i]) in 28 + Value.of_string ctx rev); 29 + let result = Eval.string ctx "(ocaml-reverse \"hello\")" in 30 + Printf.printf " (ocaml-reverse \"hello\") = %s\n" (Sexp.to_string result)); 31 + 32 + (* 3. Streaming data from OCaml to Scheme *) 33 + Printf.printf "\n--- Streaming ---\n"; 34 + with_context (fun ctx -> 35 + let numbers = List.to_seq [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] in 36 + let stream = Stream.of_int_seq ctx numbers in 37 + let sum = Stream.fold ctx 38 + ~expr:"(lambda (x acc) (+ acc x))" 39 + ~init:(Value.of_int ctx 0) 40 + stream in 41 + Printf.printf " sum of 1..10 via stream = %d\n" (Sexp.to_int sum); 42 + 43 + let words = List.to_seq ["hello"; "from"; "ocaml"; "streams"] in 44 + let stream = Stream.of_string_seq ctx words in 45 + let proc = Eval.string ctx "(lambda (s) (string-length s))" in 46 + let lengths = Stream.map ctx ~proc stream in 47 + let results = Stream.to_int_list lengths in 48 + Printf.printf " word lengths = [%s]\n" 49 + (String.concat "; " (List.map string_of_int results))); 50 + 51 + (* 4. Multiple independent VMs *) 52 + Printf.printf "\n--- Multiple VMs ---\n"; 53 + let vm1 = Context.create () in 54 + let vm2 = Context.create () in 55 + let _ = Eval.string vm1 "(define x 42)" in 56 + let _ = Eval.string vm2 "(define x 99)" in 57 + Printf.printf " VM1: x = %d\n" (Eval.to_int vm1 "x"); 58 + Printf.printf " VM2: x = %d\n" (Eval.to_int vm2 "x"); 59 + Context.destroy vm1; 60 + Context.destroy vm2; 61 + 62 + (* 5. Sandboxed VM *) 63 + Printf.printf "\n--- Sandboxed VM ---\n"; 64 + let config = Context.sandboxed_config 65 + ~heap_size:(1024 * 1024) 66 + ~max_heap_size:(4 * 1024 * 1024) 67 + ~capabilities:[Sandbox.Module_import] 68 + () in 69 + with_context ~config (fun ctx -> 70 + (* Use a recursive Scheme function since SRFI libraries may not be available *) 71 + let _ = Eval.string ctx 72 + "(define (sum-range n acc) (if (= n 0) acc (sum-range (- n 1) (+ acc n))))" in 73 + let result = Eval.to_int ctx "(sum-range 100 0)" in 74 + Printf.printf " sum 1..100 (sandboxed) = %d\n" result; 75 + Printf.printf " heap size: %d bytes\n" (Context.heap_size ctx); 76 + Printf.printf " max heap: %d bytes\n" (Context.heap_max_size ctx)); 77 + 78 + Printf.printf "\n=== demo complete ===\n"