Explorar o código

Add mlton experiments

rexim %!s(int64=6) %!d(string=hai) anos
pai
achega
a708ba2567

+ 5 - 2
TODO.org

@@ -16,8 +16,11 @@
     - [X] [[https://www.linux-nantes.org/~fmonnier/OCaml/ocaml-wrapping-c.html][How to wrap C functions to OCaml]]
       - [X] [[file:ocaml-c/][file:./ocaml-c/]]
         - [X] Too complicated
-  - [ ] MLton
-    - [ ] [[http://www.mlton.org/][MLton]]
+  - [-] MLton
+    - [-] [[http://www.mlton.org/][MLton]]
+      - [-] [[file:mlton-c/][file:./mlton-c/]]
+        - [X] Compilation is extremely slow.
+        - [ ] 
 - [ ] Quick video rendering
   - [ ] [[https://gstreamer.freedesktop.org/][GStreamer: open source multimedia framework]]
   - [ ] [[https://ffmpeg.org/libavcodec.html][Libavcodec Documentation]]

+ 2 - 0
mlton-c/.gitignore

@@ -0,0 +1,2 @@
+hello-world
+export.h

+ 2 - 0
mlton-c/Makefile

@@ -0,0 +1,2 @@
+say_hello: hello-world.sml hello_world.c
+	mlton -default-ann "allowFFI true" -export-header export.h hello-world.sml hello_world.c

+ 10 - 0
mlton-c/default.nix

@@ -0,0 +1,10 @@
+with import <nixpkgs> {}; {
+    mltoncEnv = stdenv.mkDerivation {
+        name = "mltonc-env";
+        buildInputs = [ stdenv
+                        gcc
+                        pkgconfig
+                        mlton
+                      ];
+    };
+}

+ 1 - 0
mlton-c/example/.gitignore

@@ -0,0 +1 @@
+import

+ 2 - 0
mlton-c/example/Makefile

@@ -0,0 +1,2 @@
+import: import.sml ffi-import.c
+	mlton -default-ann 'allowFFI true' -export-header export.h  import.sml ffi-import.c

+ 23 - 0
mlton-c/example/ffi-import.c

@@ -0,0 +1,23 @@
+#include "export.h"
+
+Int32 FFI_INT = 13;
+Word32 FFI_WORD = 0xFF;
+Bool FFI_BOOL = 1;
+Real64 FFI_REAL = 3.14159;
+
+Char8 ffi (Pointer a1, Int32 a1len, Pointer a2, Pointer a3, Int32 n) {
+        double *ds = (double*)a1;
+        int *pi = (int*)a2;
+        char *pc = (char*)a3;
+        int i;
+        double sum;
+
+        sum = 0.0;
+        for (i = 0; i < a1len; ++i) {
+                sum += ds[i];
+                ds[i] += n;
+        }
+        *pi = (int)sum;
+        *pc = 'c';
+        return 'c';
+}

+ 24 - 0
mlton-c/example/import.sml

@@ -0,0 +1,24 @@
+(* main.sml *)
+
+(* Declare ffi to be implemented by calling the C function ffi. *)
+val ffi = _import "ffi" public: real array * int * int ref * char ref * int -> char;
+open Array
+
+val size = 10
+val a = tabulate (size, fn i => real i)
+val ri = ref 0
+val rc = ref #"0"
+val n = 17
+
+(* Call the C function *)
+val c = ffi (a, Array.length a, ri, rc, n)
+
+(* FFI_INT is declared as public in ffi-import.c *)
+val (nGet, nSet) = _symbol "FFI_INT" public: (unit -> int) * (int -> unit);
+
+val _ = print (concat [Int.toString (nGet ()), "\n"])
+
+val _ =
+   print (if c = #"c" andalso !ri = 45 andalso !rc = c
+             then "success\n"
+          else "fail\n")

+ 3 - 0
mlton-c/hello-world.sml

@@ -0,0 +1,3 @@
+val sayHello = _import "say_hello" public: int * string -> int;
+
+val _ = sayHello (10, "World");

+ 8 - 0
mlton-c/hello_world.c

@@ -0,0 +1,8 @@
+#include <stdio.h>
+#include "export.h"
+
+Int32 say_hello(Int32 n, Pointer name)
+{
+    printf("Hello %s!\n", (unsigned char*) name);
+    return n;
+}