emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 5b0a42c7a2f2c619a874f4714875626992a6525b 10529 bytes (raw)
name: lisp/ob-haxe.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
 
;;; ob-haxe.el --- org-babel functions for haxe evaluation

;; Copyright (C) 2020 Free Software Foundation, Inc.

;; Author: Ian Martins
;; Keywords: literate programming, reproducible research
;; Homepage: http://orgmode.org

;; This file is not part of GNU Emacs.

;; GNU Emacs 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 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Org-Babel support for evaluating haxe source code.

;;; Code:
(require 'ob)

(defvar org-babel-tangle-lang-exts)
(add-to-list 'org-babel-tangle-lang-exts '("haxe" . "hx"))

(defvar org-babel-default-header-args:haxe '()
  "Default header args for haxe source blocks.")

(defconst org-babel-header-args:haxe '((target . '(interp neko hashlink))
                                       (imports . :any))
  "Haxe-specific header arguments.
org-babel supports three of the platforms the haxe compiler can target.

  `interp' runs the haxe compiler with the `--interp' option.
  `neko' and `hashlink' compile to bytecode and run the
  respective VMs.")

(defvar org-babel-haxe-command "haxe"
  "Name of the haxe command.")

(defcustom org-babel-haxe-hline-to "null"
  "Replace hlines in incoming tables with this when translating to haxe."
  :group 'org-babel
  :version "25.2"
  :package-version '(Org . "9.3")
  :type 'string)

(defcustom org-babel-haxe-null-to 'hline
  "Replace `null' in haxe tables with this before returning."
  :group 'org-babel
  :version "25.2"
  :package-version '(Org . "9.3")
  :type 'symbol)

(defun org-babel-execute:haxe (body params)
  "Execute a haxe source block with BODY code and PARAMS params."
  (let* ((fullclassname (or (cdr (assq :classname params)) ; class and package
                            (org-babel-haxe-find-classname body)
                            "Main"))
         (classname (if (seq-contains fullclassname ?.)    ; just class name
                        (file-name-extension fullclassname)
                      fullclassname))
         (packagename (if (seq-contains fullclassname ?.)  ; just package name
                          (file-name-base fullclassname)
                        ""))
         (packagedir (if (not (seq-empty-p packagename))   ; package name as a path
                         (replace-regexp-in-string "\\\." "/" packagename)))
         (src-file (concat (replace-regexp-in-string "\\\." "/" fullclassname) ".hx"))
         (cmdline (or (cdr (assq :cmdline params)) ""))
         (target-name (cdr (assq :target params)))
         (target (cond ((string= target-name "neko") "-neko main.n -cmd \"neko main.n\"")
                       ((string= target-name "hashlink") "-hl main.hl -cmd \"hl main.hl\"")
                       (t "--interp")))
         (cmd (concat org-babel-haxe-command
                      " " cmdline " -main " fullclassname " " target))
         (result-type (cdr (assq :result-type params)))
         (result-params (cdr (assq :result-params params)))
         (tmp-file (and (eq result-type 'value)
                        (org-babel-temp-file "haxe-")))
         (full-body (org-babel-expand-body:haxe
                     body params classname packagename result-type tmp-file)))

    ;; created package-name directories if missing
    (unless (or (not packagedir) (file-exists-p packagedir))
      (make-directory packagedir 'parents))
    (with-temp-file src-file (insert full-body))
    (org-babel-reassemble-table
     (org-babel-haxe-evaluate cmd target-name result-type result-params tmp-file)
     (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))))))

;; helper functions

(defun org-babel-haxe-find-classname (body)
  "Try to find fully qualified classname in BODY."
  (let ((package (if (string-match "package \\\([^ ]*\\\);" body)
                     (match-string 1 body)))
        (class (if (string-match "class \\\([^ \n]*\\\)" body)
                   (match-string 1 body))))
    (or (and package class (concat package "." class))
        class)))

(defun org-babel-expand-body:haxe (body params classname packagename
                                        result-type tmp-file)
  "Expand BODY with PARAMS.
BODY could be a few statements, or could include a full class
definition specifying package, imports, and class.  Because we
allow this flexibility in what the source block can contain, it
is simplest to expand the code block from the inside out.

CLASSNAME name of the class, which may have been specified
in multiple ways.

PACKAGENAME name of the java package containing this class.

RESULT-TYPE output or value.

TMP-FILE name of tempfile to write to if value `result-type'."
  (let* ((var-lines (org-babel-variable-assignments:haxe params))
         (imports-val (assq :imports params))
         (imports (if imports-val
                      (split-string (org-babel-read (cdr imports-val) nil) " ")
                    nil)))
    (with-temp-buffer
      (insert body)

      ;; wrap main
      (goto-char (point-min))
      (when (not (re-search-forward "static .*function main\(\)" nil t))
        (while (re-search-forward "^[[:space:]]*import .*;$" nil t) ; if imports are defined, move past them
          (goto-char (1+ (match-end 0))))
        (insert "public static function main() {\n")
        (goto-char (point-max))
        (insert "\n}"))

      ;; insert variables
      (when var-lines
        (goto-char (point-min))
        (if (re-search-forward "^[[:space:]]*class .*\n?{[[:space:]]*$" nil t)
            (goto-char (1+ (match-end 0))))
        (insert (mapconcat 'identity var-lines "\n"))
        (insert "\n"))

      ;; special handling to return value
      (when (eq result-type 'value)
        (goto-char (point-min))
        (re-search-forward "^[[:space:]]*class .*\n?{" nil t)
        (insert "\n")
        (insert "    public static function main() {\n")
        (insert (format "        var output = File.write(\"%s\");\n" (org-babel-process-file-name tmp-file 'noquote)))
        (insert "        output.writeString(haxe.Json.stringify(_main()));\n")
        (insert "        output.close();\n")
        (insert "    }\n")
        (search-forward "function main()") ; rename existing main
        (replace-match "function _main()"))

      ;; wrap class
      (goto-char (point-min))
      (when (not (re-search-forward "^[[:space:]]*class [^ ]*" nil t))
        (while (re-search-forward "^[[:space:]]*import .*;$" nil t) ; if imports are defined, move past them
          (goto-char (1+ (match-end 0))))
        (insert (concat  "class " (file-name-base classname) " {\n"))
        (goto-char (point-max))
        (insert "\n}"))

      ;; add imports
      (if (eq result-type 'value)
          (setq imports (append '("sys.io.File") imports)))
      (when imports
        (if (re-search-forward "^[[:space:]]*package .*;$" nil t) ; if package is defined, move past it
            (goto-char (1+ (match-end 0))))
        (goto-char (point-min))
        (insert (mapconcat (lambda (ii) (concat "import " ii ";\n")) imports "\n")))

      ;; add package at the top
      (goto-char (point-min))
      (when (not (re-search-forward "^[[:space:]]*package .*;$" nil t))
        (insert (concat "package " packagename ";\n\n")))

      (buffer-string)))) ; return expanded body

(defun org-babel-variable-assignments:haxe (params)
  "Return a list of haxe statements assigning the block's variables.
variables are contained in PARAMS."
  (mapcar
   (lambda (pair)
     (format "    static var %s %s = %s;"
             (car pair)
             (if (and (sequencep (cdr pair))
                      (not (stringp (cdr pair)))) ":Array<Dynamic> " "")
             (org-babel-haxe-var-to-haxe (cdr pair))))
   (org-babel--get-vars params)))

(defun org-babel-haxe-var-to-haxe (var)
  "Convert an elisp value to a haxe variable.
Convert an elisp value, VAR, into a string of haxe source code
specifying a variable of the same value."
  (cond ((and (sequencep var)
              (not (stringp var)))
         (concat "[" (mapconcat #'org-babel-haxe-var-to-haxe var ", ") "]"))
        ((equal var 'hline)
         org-babel-haxe-hline-to)
        (t
         (format "%S" (if (stringp var) (substring-no-properties var) var)))))

(defun org-babel-haxe-table-or-string (results)
  "Convert RESULTS into an appropriate elisp value.
If the results look like a list or vector, then convert them into an
Emacs-lisp table, otherwise return the results as a string."
  (let ((res (org-babel-script-escape results)))
    ;; (mapcar (lambda (el) (message "el %s %s" el (eq 'null el))) res)
    (if (listp res)
        (mapcar (lambda (el) (if (eq 'null el)
                               org-babel-haxe-null-to
                             el))
              res)
      res)))

(defun org-babel-haxe-evaluate
  (cmd target result-type result-params tmp-file)
  "Evaluate using an external haxe process.
CMD the command to execute.

TARGET the platform the haxe compiler should target.

If RESULT-TYPE equals 'output then return standard output as a
string.  If RESULT-TYPE equals 'value then return the value
returned by the source block, as elisp.

RESULT-PARAMS input params used to format the reponse.

TMP-FILE filename of the tempfile to store the returned value in
for 'value RESULT-TYPE.  Not used for 'output RESULT-TYPE."
  (let ((raw (cond ((eq result-type 'output)
                    (org-babel-eval cmd ""))
                   (t
                    (org-babel-eval cmd "")
                    (org-babel-eval-read-file tmp-file)))))
    (if (and
         (or (string= target "neko")
             (string= target "hashlink"))
         (eq result-type 'output))
        (setq raw (substring raw 0 -1))) ; neko and hashlink runtimes add a trailing endline
    (org-babel-result-cond result-params
      raw
      (org-babel-haxe-table-or-string raw))))

(provide 'ob-haxe)

;;; ob-haxe.el ends here

debug log:

solving 5b0a42c7a ...
found 5b0a42c7a in https://list.orgmode.org/orgmode/CAC=rjb6FnA=WuMK_qfto=rFfH=QZHet_-7AXd26NQ-Ktjd5y8w@mail.gmail.com/

applying [1/1] https://list.orgmode.org/orgmode/CAC=rjb6FnA=WuMK_qfto=rFfH=QZHet_-7AXd26NQ-Ktjd5y8w@mail.gmail.com/
diff --git a/lisp/ob-haxe.el b/lisp/ob-haxe.el
new file mode 100644
index 000000000..5b0a42c7a

Checking patch lisp/ob-haxe.el...
Applied patch lisp/ob-haxe.el cleanly.

index at:
100644 5b0a42c7a2f2c619a874f4714875626992a6525b	lisp/ob-haxe.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).