<commit_before>;; -*- mode: lisp -*-

;;; language targets

(global current-target 'js)

(macro target (args)
  (local i 0)
  (while (< i (array-length args))
    (if ((= (get (get args i) 0) current-target)
	 (return (get (get args i) 1))))
    (set i (+ i 1))))

(global current-language
  (target (js 'js) (lua 'lua)))


;;; library

(target (js (function error (msg) (throw msg))))
(target (js (function type (x) (return (typeof x)))))

;; arrays

(function array-length (arr)
  (target
    (js (return arr.length))
    (lua (if ((= (get arr 0) nil) (return 0))
	     (true (return (+ #arr 1)))))))

(function array-sub (arr from upto)
  (target
    (js (return (arr.slice from upto)))
    (lua
     (do (set upto (or upto (array-length arr)))
	 (local i from)
	 (local j 0)
	 (local arr2 {})
	 (while (< i upto)
	   (set (get arr2 j) (get arr i))
	   (set i (+ i 1))
	   (set j (+ j 1)))
	 (return arr2)))))

(function array-push (arr x)
  (set (get arr (array-length arr)) x))

;; strings

(function string-length (str)
  (return (target (js str.length) (lua (string.len str)))))

(function string-start ()
  (return (target (js 0) (lua 1))))

(function string-end (str) ; last valid position
  (return (target (js (- (string-length str) 1))
		  (lua (string-length str)))))

(function string-ref (str n)
  (return (target (js (str.charAt n)) (lua (string.sub str n n)))))

(function string-sub (str from upto)
  (target
    (js (return (str.substring from upto)))
    (lua (do (if ((not (= upto nil)) (set upto (- upto 1))))
	     (return (string.sub str from upto))))))

(function string-find (str pattern start)
  (target
   (js (do (local i (str.indexOf pattern start))
	   (if ((> i 0) (return i))
	       (true (return nil)))))
   (lua (return (string.find str pattern (or start 1) true)))))

;; io

(target (js (set fs (require "fs"))))

(function read-file (filename)
  (target
    (js (return (fs.readFileSync filename "utf8")))
    (lua (do (local f (io.open filename))
	     (return (f:read "*a"))))))

(function write-file (filename data)
  (target
    (js (fs.writeFileSync filename data "utf8"))
    (lua (do (local f (io.open filename "w"))
	     (f:write data)))))

(target (js (function print (x) (console.log x))))

(function exit (code)
  (target (js (process.exit code)) (lua (os.exit code))))

;; numbers

(function parse-number (str)
  (target
    (js (do (local n (parseFloat str))
	    (if ((not (isNaN n)) (return n)))))
    (lua (return (tonumber str)))))


;;; reader

(global delimiters {})
(set (get delimiters "(") true) (set (get delimiters ")") true)<commit_msg>Replace a usage of SET with GLOBAL<commit_after>;; -*- mode: lisp -*-

;;; language targets

(global current-target 'js)

(macro target (args)
  (local i 0)
  (while (< i (array-length args))
    (if ((= (get (get args i) 0) current-target)
	 (return (get (get args i) 1))))
    (set i (+ i 1))))

(global current-language
  (target (js 'js) (lua 'lua)))


;;; library

(target (js (function error (msg) (throw msg))))
(target (js (function type (x) (return (typeof x)))))

;; arrays

(function array-length (arr)
  (target
    (js (return arr.length))
    (lua (if ((= (get arr 0) nil) (return 0))
	     (true (return (+ #arr 1)))))))

(function array-sub (arr from upto)
  (target
    (js (return (arr.slice from upto)))
    (lua
     (do (set upto (or upto (array-length arr)))
	 (local i from)
	 (local j 0)
	 (local arr2 {})
	 (while (< i upto)
	   (set (get arr2 j) (get arr i))
	   (set i (+ i 1))
	   (set j (+ j 1)))
	 (return arr2)))))

(function array-push (arr x)
  (set (get arr (array-length arr)) x))

;; strings

(function string-length (str)
  (return (target (js str.length) (lua (string.len str)))))

(function string-start ()
  (return (target (js 0) (lua 1))))

(function string-end (str) ; last valid position
  (return (target (js (- (string-length str) 1))
		  (lua (string-length str)))))

(function string-ref (str n)
  (return (target (js (str.charAt n)) (lua (string.sub str n n)))))

(function string-sub (str from upto)
  (target
    (js (return (str.substring from upto)))
    (lua (do (if ((not (= upto nil)) (set upto (- upto 1))))
	     (return (string.sub str from upto))))))

(function string-find (str pattern start)
  (target
   (js (do (local i (str.indexOf pattern start))
	   (if ((> i 0) (return i))
	       (true (return nil)))))
   (lua (return (string.find str pattern (or start 1) true)))))

;; io

(target (js (global fs (require "fs"))))

(function read-file (filename)
  (target
    (js (return (fs.readFileSync filename "utf8")))
    (lua (do (local f (io.open filename))
	     (return (f:read "*a"))))))

(function write-file (filename data)
  (target
    (js (fs.writeFileSync filename data "utf8"))
    (lua (do (local f (io.open filename "w"))
	     (f:write data)))))

(target (js (function print (x) (console.log x))))

(function exit (code)
  (target (js (process.exit code)) (lua (os.exit code))))

;; numbers

(function parse-number (str)
  (target
    (js (do (local n (parseFloat str))
	    (if ((not (isNaN n)) (return n)))))
    (lua (return (tonumber str)))))


;;; reader

(global delimiters {})
(set (get delimiters "(") true) (set (get delimiters ")") true)
{'fraction_non_alphanumeric': 0.19065849923430322, 'fraction_numerical': 0.008039816232771823, 'mean_word_length': 3.343308395677473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4338882', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1849, 'n_words': 669}