From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Tim Van den Langenbergh Newsgroups: gmane.emacs.devel Subject: Raku evaluation for org-babel Date: Wed, 25 Mar 2020 02:32:26 +0100 Message-ID: <3092014.Nvo2iUt9sp@terra> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart3821668.2ZvUWi3UQP"; micalg="pgp-sha256"; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="100698"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Mar 25 02:36:01 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jGuxd-000Q40-4U for ged-emacs-devel@m.gmane-mx.org; Wed, 25 Mar 2020 02:36:01 +0100 Original-Received: from localhost ([::1]:57340 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jGuxb-00039l-V5 for ged-emacs-devel@m.gmane-mx.org; Tue, 24 Mar 2020 21:36:00 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34815) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jGuuU-0002F1-33 for emacs-devel@gnu.org; Tue, 24 Mar 2020 21:32:47 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jGuuP-00087h-5x for emacs-devel@gnu.org; Tue, 24 Mar 2020 21:32:45 -0400 Original-Received: from mout.gmx.net ([212.227.17.22]:46437) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jGuuO-0007zk-Oy for emacs-devel@gnu.org; Tue, 24 Mar 2020 21:32:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1585099953; bh=kAlgzaSqdvlzHwvsgToAWnCJmcCWvttjR1C5wX+wb3o=; h=X-UI-Sender-Class:From:To:Subject:Date; b=Ksl+/3EGdIKOTY5+1n8b8mtmorIRUHRk0JAe7iD/J9spUz6/C8+eH+Xe3wRg2IjRN zpj9z9d3r0J8KVikKoBLasy482SLxDelG1GBaO6H/ScZIShb+j8MIVtBvRNFFIrEYI ItCo8KMOwgqAKU7qVy37djhmxnLBwg12Nn4eOjBk= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from terra.localnet ([94.227.125.226]) by mail.gmx.com (mrgmx105 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MysW2-1jTTwR0ZLm-00vyce for ; Wed, 25 Mar 2020 02:32:33 +0100 X-Provags-ID: V03:K1:hxJ12G5ogx6I1gIE0I6QMjHA9XRIV9N9PoOXp81xV/kgp69pty8 wWissAjWarDnHjiqCbgsvOsbkxG6DI3eF8Qz/KaVvu3heU5Pj+S2yIk0wRSwFyICsQI4JXS alrCFREnwNBCXhpbTYIlZ6LwH5eeobLT66wI4/f+uSnUArgL13xOSkACqQlbJtFnNslGkaE wuavjk15BJpdabYzqVTxw== X-UI-Out-Filterresults: notjunk:1;V03:K0:JMzBn7kxQ8w=:Oq12O5cUheqeaQt5hYQK5H 01sPNtobsZoD11GmbgeiuZN7Yv+0Gb/xvu/EqGeIkxHbWkvA+lIdMDTIkZ2zj1xFZpuve5a33 YrklbLkjOKYivXL0qfQ+GTG3/1MtkUigO1v7PF3WQXHG6pwMo3v4mFItqChzxZZG0BWbUnc+Y vfgYpdyVbZOl5K+/3nYw0F4KICh4/DijQpoooKZ5wJXc49wEqtbxN25wmFOqsQl1SjagHqbm7 ts7wdLCboYj9U4sHztIqwk/k4S2ogmLTORWxXlTmaEiOcspxcjI6vNhEByDfweUoWmgO5df4p 3m9h/ZkDO7oPrX5fi+/uydDcVETMvoipxiDOu1DhZ7TY/HmlLxMHC9/cxOgUzUEsHwTSiyecp sYEZDrKbaQdvZF6QsoK/VoAdftVfAX2TP+iqdPCpD+4bwjq5/m2zMyyATuRt6UBnTwGu8FPrb nPQabGL1yYgIMOcJe5Ov6+W5LWySO6Wia1oFa0iRsRO/l/OaMIlcsnw6ZaGjPwc8uM9t3aJ44 9hIDjvK7eeGcMr35crfoc0aGbpav2PFOYJhpru9dHAcNwO24C+zL1i9KDqRqtxAwbfl0jE/j/ 2EMeB7CHU4O7wjwqlIMXbr8noxYFt/qb1aCI12OH/Kh9wQXpaAb5abog6uztHnJsh2EYbsbK6 gKkJHTiMVkORa6jOXb4EUN92Xrxv0/R1oNZ1jlI9KOhJodPMn/+Yb5DStaYQADsNsybpW3sBy L3oI+GVaCfHNG1KfSZB4d7Rg6s+NLXopLufqu9ImBk1aDhbem3GdvVpBN+FEiLt7XVgSbOiK X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 212.227.17.22 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:245775 Archived-At: --nextPart3821668.2ZvUWi3UQP Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" Hello, I have spitballed together a module that adds Raku (formerly known as Perl = 6) evaluation to Org-Babel. According to the org-babel docs I should contact this mailing list for help= to get it ready for inclusion on elpa. This is my first module for Emacs, = so I'm not familiar with proper process, I believe I have to add the .el fi= le to this e-mail, so I shall do so. If there is anything I have to do before I can proceed, I would be grateful= for any advise offered. Vale, =2DTim Van den Langenbergh =2D-- ob-raku.el ;; Copyright (C) Tim Van den Langenbergh ;; Author: Tim Van den Langenbergh ;; Keywords: literate programming, reproducible research ;; Homepage: https://github.com/tmtvl/ob-raku ;; Version: 0.05 ;; News: 0.05 --- Added initial support for parentheses and commas in strin= gs in lists without breaking the lists on return. ;; 0.04 --- Added square brackets to list splitting, so as to split e= mbedded arrays as well as lists. ;; 0.03 --- Removed the double execution, simplified the formatting o= f the Raku output, fixed hline support. ;; 0.02 --- Added support for tables, removed unneeded require statem= ents, error when trying to use a session. ;; 0.01 --- Initial release. Accept inputs, support for output and va= lue results. ;;; License: ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary: ;; Bindings for org-babel support for Raku (n=E9e Perl6). ;;; Requirements: ;; Requires a working Raku interpreter to be installed. ;; (Optionally) requires Perl6 mode to be installed. ;;; Code: (require 'ob) (require 'ob-eval) (require 'ob-ref) (add-to-list 'org-babel-tangle-lang-exts '("raku" . "raku")) (defvar org-babel-default-header-args:raku '()) (defvar org-babel-raku-command "raku" "Command to run Raku.") (defun org-babel-expand-body:raku (body params &optional processed-params) "Expand BODY according to the header arguments specified in PARAMS. Use the PROCESSED-PARAMS if defined." (let ((vars (delq nil (mapcar (lambda (pair) (when (eq (car pair) :var) (cdr pair))) (or processed-params (org-babel-process-params params)))))) (concat (mapconcat (lambda (pair) (format "my %s%s =3D %s;" (if (listp (cdr pair)) "@" "$") (car pair) (org-babel-raku-var-to-raku (cdr pair)))) vars "\n") "\n" body "\n"))) (defun org-babel-execute:raku (body params) "Execute the BODY of Raku code processing it according to PARAMS." (let* ((processed-params (org-babel-process-params params)) (session (cdr (assoc :session params))) (result-type (cdr (assoc :result-type params))) (result-params (cdr (assoc :result-params params))) (full-body (org-babel-expand-body:raku body params processed-params))) (org-babel-reassemble-table (org-babel-raku-evaluate full-body session result-type) (org-babel-pick-name (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) (org-babel-pick-name (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))) (defun org-babel-prep-session:raku (session params) "Prepare SESSION according to the header arguments specified in PARAMS." (error "Sessions are not supported for Raku")) (defun org-babel-raku-var-to-raku (var) "Convert an elisp value VAR to a Raku definition of the same value." (if (listp var) (concat "(" (mapconcat #'org-babel-raku-var-to-raku var ", ") ")") (if (equal var 'hline) "\"HLINE\"" (format "%S" var)))) (defun org-babel-raku-escape-nested-list-delimiters (list) "Escapes any commas or parentheses found in strings contained in the give= n LIST." (let ((in-string nil)) (mapconcat (lambda (string) (cond ((string=3D string "\"") (setq in-string (not in-string)) string) ((and in-string (or (string=3D string "(") (string=3D string ")") (string=3D string "[") (string=3D string "]") (string=3D string ","))) (concat "\\" string)) (t string))) (split-string list "" t) ""))) (defun org-babel-raku-unescape-parens-and-commas (string) "Unescapes parentheses and commas in STRING." ;;(replace-regexp-in-string "\\\\\([][(),]\)" "\1" string) ;; This doesn'= t work. (let ((index (string-match "\\\\[][(),]" string))) (if index (concat (substring string 0 index) (org-babel-raku-unescape-parens-and-commas (substring string (+ index 1)))) string))) (defun org-babel-raku-split-list (list) "Split LIST on a comma or parentheses, ignoring those in a string." (mapcar (lambda (pairstring) (mapcar (lambda (string) (org-babel-raku-unescape-parens-and-commas string)) (split-string pairstring "[^\\], " t))) (split-string (org-babel-raku-escape-nested-list-delimiters (substring l= ist 2 -2)) "[^\\][][()]" t))) (defun org-babel-raku-sanitize-table (table) "Recursively sanitize the values in the given TABLE." (if (listp table) (let ((sanitized-table (mapcar 'org-babel-raku-sanitize-table table))) (if (and (stringp (car sanitized-table)) (string=3D (car sanitized-table) "HLINE")) 'hline sanitized-table)) (org-babel-script-escape table))) (defun org-babel-raku-table-or-string (results) "If RESULTS look like a table, then convert them into an elisp table. Otherwise return RESULTS as a string." (cond ((or (string-prefix-p "$[" results) (string-prefix-p "$(" results)) (org-babel-raku-sanitize-table (org-babel-raku-split-list results))) ((string-prefix-p "{" results) (org-babel-raku-sanitize-table (mapcar (lambda (pairstring) (split-string pairstring " =3D> " t)) (split-string (substring results 1 -2) ", " t)))) (t (org-babel-script-escape results)))) (defun org-babel-raku-initiate-session (&optional session) "If there is not a current inferior-process-buffer in SESSION then create. Return the initialized SESSION." (unless (string=3D session "none") (if session (error "Sessions are not supported for Raku")))) (defun org-babel-raku-evaluate (body &optional session result-type) "Evaluate the BODY with Raku. If SESSION is not provided, evaluate in an external process. If RESULT-TYPE is not provided, assume \"value\"." (org-babel-raku-table-or-string (if (and session (not (string=3D session = "none"))) (org-babel-raku-evaluate-session session body result-type) (org-babel-raku-evaluate-external body result-type)))) (defconst org-babel-raku-wrapper "sub _MAIN { %s } sub _FORMATTER ($result) { return $result.gist if $result.WHAT ~~ Hash; $result.raku } \"%s\".IO.spurt(\"{ _FORMATTER(_MAIN()) }\\n\");" "Wrapper for grabbing the final value from Raku code.") (defun org-babel-raku-evaluate-external (body &optional result-type) "Evaluate the BODY with an external Raku process. If RESULT-TYPE is not provided, assume \"value\"." (if (and result-type (string=3D result-type "output")) (org-babel-eval org-babel-raku-command body) (let ((temp-file (org-babel-temp-file "raku-" ".raku"))) (org-babel-eval org-babel-raku-command (format org-babel-raku-wrapper body (org-babel-process-file-name temp-file 'noquote))) (org-babel-eval-read-file temp-file)))) (defun org-babel-raku-evaluate-session (session body &optional result-type) "Evaluate the BODY with the Raku process running in SESSION. If RESULT-TYPE is not provided, assume \"value\"." (error "Sessions are not supported for Raku")) (provide 'ob-raku) ;;; ob-raku.el ends here --nextPart3821668.2ZvUWi3UQP Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEoaHRUPlqTFTeZ4f99QrzKNnR5jUFAl56tKoACgkQ9QrzKNnR 5jVxhA//c5IdWaymVGKAFPfJRXAW+Or/mnzeQegBgO4jzisER785H6IZZAC8yM50 qHhWdittSBWdJ/mIt5sOA6D3fyWtyc2IKTFoakldq0XGcrbMZ+nKboSriluQU2ft /XJqM7V0d9B89k35dez0FwjKNClt65LtXcSwT95QzzU5gwrIBxAP8Fg5tOHViLxJ 9zaFV1brqVLNSdBzeTmbcvfVO7RADOWXSqBPxQHr7EH1xMTc1xca3fz2P29p9rhP Yh9MejalDAr3w+TrxYuoI3ankxgiAKfkmj6En2vC9GQfhodrimkx42uu6euQ8zdG fy0UhQVaMGqRenQSTfwXwJJlkcrYFnjRUZyj5K4UCTLWyXPxn9m46q09gMiJllUz Geqp/WoGPnPEv1CD/W7NoV5p7Lfr+txfiRrYqONe36nEGAMExDk3TOy6n3Wyixze OZZqSiY56KG0eFgvpAfru8WgSY7MOj5cwmjr2sRmLozegLlkA4gIlOkTAhCf0E4W Plviz1izUTRP1fJ7iz21bvRlHQojzM32uxWQ+cPopGCKro3EYhp8qBUUeWYA1Oe4 jrINYIoQV3AMO14dlIYksixp/J6yj0ExlxKVXOn1mgAZqJkO5JqdSwxAgJJ+wTW8 WfB05lorAcFLUUD2/EbV40D6+tJxo3XXCsq0r9RDMoLC3M2UWkQ= =76ys -----END PGP SIGNATURE----- --nextPart3821668.2ZvUWi3UQP--