forked from mirrors/org-mode
ob-C.el: Fix a number a regressions related to table parameters
* ob-C.el (org-babel-C-expand-C, org-babel-C-header-to-C, org-babel-C-expand-D): Add a `TYPE' parameter to `org-babel-C-header-to-C' so that the helper functions return the correct type. Previoulsy for tables of non-strings the helper functions would attempt to return a string, leading to compilation errors. * ob-C.el (org-babel-C-header-to-C): Correctly quote header elements. Before, if the elisp value was not a string, it would not quote it, leading to a syntax error. * ob-C.el (org-babel-C-utility-header-to-C): Include `string.h' in header `get_column_num' requires `strcmp', leading to compilation errors. * ob-C.el (org-babel-C-var-to-C): prefered declaration of a table in D differs from C and C++. Example in C: `int table[2][3] = ...;'. In D: `int[3][2] table=...;' This patch is mainly based on Asa Zeren <asaizeren@gmail.com> patch of Nov 5 2020, which was not commited at that time.
This commit is contained in:
parent
24a7ed50b3
commit
38f87a26b5
75
lisp/ob-C.el
75
lisp/ob-C.el
|
@ -258,7 +258,14 @@ its header arguments."
|
||||||
(when colnames
|
(when colnames
|
||||||
(org-babel-C-utility-header-to-C))
|
(org-babel-C-utility-header-to-C))
|
||||||
;; tables headers
|
;; tables headers
|
||||||
(mapconcat 'org-babel-C-header-to-C colnames "\n")
|
(mapconcat (lambda (head)
|
||||||
|
(let* ((tblnm (car head))
|
||||||
|
(tbl (cdr (car (let* ((el vars))
|
||||||
|
(while (not (or (equal tblnm (caar el)) (not el)))
|
||||||
|
(setq el (cdr el)))
|
||||||
|
el))))
|
||||||
|
(type (org-babel-C-val-to-base-type tbl)))
|
||||||
|
(org-babel-C-header-to-C head type))) colnames "\n")
|
||||||
;; body
|
;; body
|
||||||
(if main-p
|
(if main-p
|
||||||
(org-babel-C-ensure-main-wrap body)
|
(org-babel-C-ensure-main-wrap body)
|
||||||
|
@ -290,7 +297,14 @@ its header arguments."
|
||||||
(when colnames
|
(when colnames
|
||||||
(org-babel-C-utility-header-to-C))
|
(org-babel-C-utility-header-to-C))
|
||||||
;; tables headers
|
;; tables headers
|
||||||
(mapconcat 'org-babel-C-header-to-C colnames "\n")
|
(mapconcat (lambda (head)
|
||||||
|
(let* ((tblnm (car head))
|
||||||
|
(tbl (cdr (car (let* ((el vars))
|
||||||
|
(while (not (or (equal tblnm (caar el)) (not el)))
|
||||||
|
(setq el (cdr el)))
|
||||||
|
el))))
|
||||||
|
(type (org-babel-C-val-to-base-type tbl)))
|
||||||
|
(org-babel-C-header-to-C head type))) colnames "\n")
|
||||||
;; body
|
;; body
|
||||||
(if main-p
|
(if main-p
|
||||||
(org-babel-C-ensure-main-wrap body)
|
(org-babel-C-ensure-main-wrap body)
|
||||||
|
@ -407,21 +421,17 @@ of the same value."
|
||||||
(data (cdr formatted)))
|
(data (cdr formatted)))
|
||||||
(pcase org-babel-c-variant
|
(pcase org-babel-c-variant
|
||||||
((or `c `cpp)
|
((or `c `cpp)
|
||||||
(format "%s\n%s"
|
(format "%s %s%s = %s;"
|
||||||
(format "typedef %s %s_type;" type var)
|
type
|
||||||
(format "%s %s%s = %s;"
|
var
|
||||||
type
|
suffix
|
||||||
var
|
data))
|
||||||
suffix
|
|
||||||
data)))
|
|
||||||
(`d
|
(`d
|
||||||
(format "%s\n%s"
|
(format "%s%s %s = %s;"
|
||||||
(format "alias %s_type = %s;" var type)
|
type
|
||||||
(format "%s%s %s = %s;"
|
suffix
|
||||||
type
|
var
|
||||||
suffix
|
data))))))
|
||||||
var
|
|
||||||
data)))))))
|
|
||||||
|
|
||||||
(defun org-babel-C-table-sizes-to-C (pair)
|
(defun org-babel-C-table-sizes-to-C (pair)
|
||||||
"Create constants of table dimensions, if PAIR is a table."
|
"Create constants of table dimensions, if PAIR is a table."
|
||||||
|
@ -441,10 +451,10 @@ into a column number."
|
||||||
(pcase org-babel-c-variant
|
(pcase org-babel-c-variant
|
||||||
((or `c `cpp)
|
((or `c `cpp)
|
||||||
(concat
|
(concat
|
||||||
(if (eq org-babel-c-variant 'c)
|
"
|
||||||
"extern "
|
#ifndef _STRING_H
|
||||||
"extern \"C\" ")
|
#include <string.h>
|
||||||
"int strcmp (const char *, const char *);
|
#endif
|
||||||
int get_column_num (int nbcols, const char** header, const char* column)
|
int get_column_num (int nbcols, const char** header, const char* column)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
|
@ -464,33 +474,40 @@ int get_column_num (int nbcols, const char** header, const char* column)
|
||||||
}
|
}
|
||||||
")))
|
")))
|
||||||
|
|
||||||
(defun org-babel-C-header-to-C (head)
|
(defun org-babel-C-header-to-C (head type)
|
||||||
"Convert an elisp list of header table into a C or D vector
|
"Convert an elisp list of header table into a C or D vector
|
||||||
specifying a variable with the name of the table."
|
specifying a variable with the name of the table."
|
||||||
|
(message "%S" type)
|
||||||
(let ((table (car head))
|
(let ((table (car head))
|
||||||
(headers (cdr head)))
|
(headers (cdr head))
|
||||||
|
(typename (pcase type
|
||||||
|
('integerp "int")
|
||||||
|
('floatp "double")
|
||||||
|
('stringp (pcase org-babel-c-variant
|
||||||
|
((or 'c 'cpp) "const char*")
|
||||||
|
('d "string"))))))
|
||||||
(concat
|
(concat
|
||||||
(pcase org-babel-c-variant
|
(pcase org-babel-c-variant
|
||||||
((or `c `cpp)
|
((or `c `cpp)
|
||||||
(format "const char* %s_header[%d] = {%s};"
|
(format "const char* %s_header[%d] = {%s};"
|
||||||
table
|
table
|
||||||
(length headers)
|
(length headers)
|
||||||
(mapconcat (lambda (h) (format "%S" h)) headers ",")))
|
(mapconcat (lambda (h) (format "\"%s\"" h)) headers ",")))
|
||||||
(`d
|
(`d
|
||||||
(format "string[%d] %s_header = [%s];"
|
(format "string[%d] %s_header = [%s];"
|
||||||
(length headers)
|
(length headers)
|
||||||
table
|
table
|
||||||
(mapconcat (lambda (h) (format "%S" h)) headers ","))))
|
(mapconcat (lambda (h) (format "\"%s\"" h)) headers ","))))
|
||||||
"\n"
|
"\n"
|
||||||
(pcase org-babel-c-variant
|
(pcase org-babel-c-variant
|
||||||
((or `c `cpp)
|
((or `c `cpp)
|
||||||
(format
|
(format
|
||||||
"%s_type %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
|
"%s %s_h (int row, const char* col) { return %s[row][get_column_num(%d,%s_header,col)]; }"
|
||||||
table table table (length headers) table))
|
typename table table (length headers) table))
|
||||||
(`d
|
(`d
|
||||||
(format
|
(format
|
||||||
"%s_type %s_h (size_t row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
|
"%s %s_h (size_t row, string col) { return %s[row][get_column_num(%s_header,col)]; }"
|
||||||
table table table table))))))
|
typename table table table))))))
|
||||||
|
|
||||||
(provide 'ob-C)
|
(provide 'ob-C)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue