<commit_before>;; -*- Mode: Irken -*-

(include "lib/core.scm")

;; test ffi from irken-vm

(define write 
  (let ((p (%%cexp (string -> int) "dlsym" "write\x00")))
    (lambda (fd s)
      (%%cexp (int int int string int -> int)
              "ffi"
              p 3 fd s (string-length s))
      )))

(write 2 "yabba dabba dooooooo\n")

  <commit_msg>shiny new make-ffi macro, much easy.<commit_after>;; -*- Mode: Irken -*-

(include "lib/core.scm")

;; test ffi from irken-vm

(defmacro make-ffi
  (make-ffi name rtype () (ftype0 ...))
  -> (let (($p (%%cexp (string -> int) "dlsym" name)))
       (lambda ()
         (%%cexp (int char int ftype0 ...)
                 "ffi"
                 $p rtype 0)))
  (make-ffi name rtype nargs (formal0 ...) (ftype0 ...))
  -> (let (($p (%%cexp (string -> int) "dlsym" name)))
       (lambda (formal0 ...)
         (%%cexp (int char int ftype0 ...)
                 "ffi"
                 $p rtype nargs
                 formal0 ...))))

(define (tmpnam)
  ((make-ffi "tmpnam\x00" #\s 0 () (-> string))  ))

(define (write fd s)
  ((make-ffi "write\x00"
             #\i 3
             (fd s slen)
             (int string int -> int))
   fd s (string-length s)))

(write 2 "yabba dabba dooooooo\n")
(printn (tmpnam))
{'fraction_non_alphanumeric': 0.19191118160190326, 'fraction_numerical': 0.01506740681998414, 'mean_word_length': 1.7920353982300885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6230402', 'n_tokens_mistral': 486, 'n_tokens_neox': 465, 'n_words': 135}