with-c-syntax は、 Common Lisp に C 言語の記法を持ち込む一発ネタのパッ ケージです。全くもって真面目な用途は想定していません。
現在、 C プリプロセッサを除く、 ISO C 90 の freestanding 環境に相当す る全ての機能を使用できます。
with-c-syntax is a fun package which introduces the C language syntax into Common Lisp. (Yes, this package is not for practical coding, I think.)
At this stage, this package has almost all features of ISO C 90 freestanding implementation. (The lacking part is the full implementation of the C Preprocessor.)
- asdf
- cl-yacc
- alexandria
- named-readtables
(load "with-c-syntax.asd")
(asdf:load-system :with-c-syntax)
(load "with-c-syntax-test.asd")
(asdf:test-system :with-c-syntax)
CL-USER> (with-c-syntax:with-c-syntax ()
format \( t \, "Hello World!" \) \;
)
Hello World!
NIL
For suppressing Lisp’s syntax, you need many backshash escapes.
#{
and }#
reader macro escapes them and wrap its contents
into with-c-syntax
. You can use it to write simply:
;; enables #{ }# reader macros.
CL-USER> (named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
...
CL-USER> #{ format (t, "Hello World!"); }#
Hello World!
NIL
This example shows you can call a Lisp function (cl:format
) with C syntax.
(named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
#{
int i, sum = 0;
for (i = 0; i <= 100; ++ i )
sum += i;
return sum;
}#
;; => 5050
(named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
(defun array-transpose (arr)
(destructuring-bind (i-max j-max) (array-dimensions arr)
#{
int i,j,temp;
for (i = 0; i < i-max; i ++) {
for (j = i + 1; j < j-max; j ++) {
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
// pprint (arr) ;
}
}
}#)
arr)
(array-transpose (make-array '(3 3)
:initial-contents '((0 1 2) (3 4 5) (6 7 8))))
; => #2A((0 3 6) (1 4 7) (2 5 8))
(named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
#{
int sum-of-list (list) {
int list-length = length(list);
int i, ret = 0;
for (i = 0; i < list-length; ++ i) {
ret += nth(i, list);
}
return ret;
}
}#
(sum-of-list '(1 2 3 4 5 6 7 8 9 10)) ; => 55
(named-readtables:in-readtable with-c-syntax:with-c-syntax-readtable)
(defun w-c-s-duff-device (to-seq from-seq cnt)
#{
int * to = & to-seq;
int * from = & from-seq;
int n = (cnt + 7) / 8;
n = floor(n); /* Lisp's CL:/ produces rational */
switch (cnt % 8) {
case 0 : do { * to ++ = * from ++;
case 7 : * to ++ = * from ++;
case 6 : * to ++ = * from ++;
case 5 : * to ++ = * from ++;
case 4 : * to ++ = * from ++;
case 3 : * to ++ = * from ++;
case 2 : * to ++ = * from ++;
case 1 : * to ++ = * from ++;
} while (-- n > 0);
}
}#
to-seq)
(setf arr1 (make-array 20 :initial-element 1))
(setf arr2 (make-array 20 :initial-element 2))
(w-c-s-duff-device arr1 arr2 10)
arr1 ;; => #(2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1)
Please see these docstrings or comments:
- Macro
with-c-syntax
- defreadtable of
with-c-syntax-readtable
- Variable
*with-c-syntax-reader-level*
- Variable
*with-c-syntax-reader-case*
Copyright (c) 2014 YOKOTA Yuki <[email protected]>
This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details.
Please see: https://github.com/y2q-actionman/with-c-syntax/wiki