ob-D.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; ob-D.el --- org-babel functions for the D language
  2. ;; Copyright (C) 2013 Thierry Banel
  3. ;; Author: Thierry Banel, derived from the Eric Schulte work
  4. ;; Keywords: literate programming, reproducible research
  5. ;; This file is NOT (yet) part of GNU Emacs.
  6. ;; ob-D.el is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; ob-D.el is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; the GNU General Public License can be obtained here:
  15. ;; <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Org-Babel support for evaluating Digital Mars D Language code.
  18. ;; The D language home page is here:
  19. ;; http://dlang.org/
  20. ;;; Code:
  21. (require 'ob)
  22. (require 'ob-eval)
  23. (declare-function org-entry-get "org"
  24. (pom property &optional inherit literal-nil))
  25. (defvar org-babel-tangle-lang-exts)
  26. (add-to-list 'org-babel-tangle-lang-exts '("D" . "D"))
  27. (defvar org-babel-default-header-args:D '())
  28. (defvar org-babel-D-compiler "rdmd"
  29. "Command used to compile and run a D source code file into an
  30. executable.")
  31. (defun org-babel-execute:D (body params)
  32. "Execute a block of D code with org-babel. This function is
  33. called by `org-babel-execute-src-block'."
  34. (org-babel-D-execute body params))
  35. (defun org-babel-D-execute (body params)
  36. "This function should only be called by `org-babel-execute:D'"
  37. (let* ((tmp-src-file (org-babel-temp-file "Dsrc" ".d"))
  38. (cmdline (cdr (assoc :cmdline params)))
  39. (flags (cdr (assoc :flags params)))
  40. (rdmd (format "%s %s %s"
  41. org-babel-D-compiler
  42. (mapconcat 'identity
  43. (if (listp flags) flags (list flags)) " ")
  44. ;; On Unix, keep directory separator
  45. (org-babel-process-file-name tmp-src-file)))
  46. (full-body (org-babel-D-expand body params)))
  47. (with-temp-file tmp-src-file (insert full-body))
  48. (org-babel-eval rdmd "")))
  49. (defun org-babel-D-expand (body params)
  50. "Expand a block of D code with org-babel according to
  51. its header arguments."
  52. (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  53. (colname-names (cdr (car (org-babel-get-header params :colname-names))))
  54. (main-p (not (string= (cdr (assoc :main params)) "no")))
  55. (imports (mapcar #'cdr (org-babel-get-header params :import))))
  56. (mapconcat 'identity
  57. (list
  58. "module aaa;\n"
  59. ;; imports
  60. (mapconcat
  61. (lambda (inc) (format "import %s;" inc))
  62. imports "\n")
  63. ;; variables
  64. (mapconcat 'org-babel-D-var-to-D vars "\n")
  65. (mapconcat 'org-babel-D-colnames-to-D colname-names "\n")
  66. ;; body
  67. (if main-p
  68. (org-babel-D-ensure-main-wrap body)
  69. body) "\n") "\n")))
  70. (defun org-babel-D-ensure-main-wrap (body)
  71. "Wrap body in a \"main\" function call if none exists."
  72. (if (string-match "^[ \t]*[intvod]+[ \t\n\r]*main[ \t]*(.*)" body)
  73. body
  74. (format "int main() {\n%s\nreturn(0);\n}\n" body)))
  75. (defun org-babel-prep-session:D (session params)
  76. "This function does nothing as D is a compiled language with no
  77. support for sessions"
  78. (error "D is a compiled languages -- no support for sessions"))
  79. (defun org-babel-load-session:D (session body params)
  80. "This function does nothing as D is a compiled language with no
  81. support for sessions"
  82. (error "D is a compiled languages -- no support for sessions"))
  83. ;; helper functions
  84. (defun org-babel-D-var-to-D (pair)
  85. "Convert an elisp value into a string of D code specifying a variable
  86. of the same value."
  87. (let ((var (car pair))
  88. (val (cdr pair)))
  89. (when (symbolp val)
  90. (setq val (symbol-name val))
  91. (when (= (length val) 1)
  92. (setq val (string-to-char val))))
  93. (cond
  94. ((integerp val)
  95. (format "int %S = %S;" var val))
  96. ((floatp val)
  97. (format "double %S = %S;" var val))
  98. ((stringp val)
  99. (format "string %S = \"%s\";" var val))
  100. ((listp val)
  101. (if (assoc var colname-names) ()
  102. (setq colname-names
  103. (cons (cons
  104. var
  105. (let ((i 0)) (mapcar (lambda (x) (setq i (1+ i)) (format
  106. "$%s" i))
  107. (car val))))
  108. colname-names)))
  109. (format "string[][] %S = [\n[%s]];" var
  110. (mapconcat (lambda (row)
  111. (if (listp row)
  112. (mapconcat (lambda (v) (format "\"%s\"" v))
  113. row
  114. ",")))
  115. val
  116. "],\n[")))
  117. (t
  118. (format "u32 %S = %S;" var val)))))
  119. (defun org-babel-D-colnames-to-D (pair)
  120. "Convert an elisp list of header table into a D vector
  121. specifying a variable with the name of the table"
  122. (let ((table (car pair))
  123. (headers (cdr pair)))
  124. (format "string[] %S_headers = [%s];"
  125. table
  126. (mapconcat (lambda (h) (format "%S" h)) headers ","))))
  127. (provide 'ob-D)
  128. ;;; ob-D.el ends here