如何将LISP代码翻译或转换为C代码?
这是我转换成C的一些LISP。LISP是从'Calendrical Calculations'开始的,而且转换完成的时间足够长,可以从本书的'千年版'中找到(现在有第三版,相反),所以C中的所有名称都以'CCME'或'ccme'作为前缀。
/*
Gregorian year corresponding to the fixed $date$.
Original LISP code
(defun alt-gregorian-year-from-fixed (date)
;; TYPE fixed-date -> gregorian-year
(let* ((approx ; approximate year
(quotient (- date gregorian-epoch -2)
146097/400))
(start ; start of next year
(+ gregorian-epoch
(* 365 approx)
(quotient approx 4)
(- (quotient approx 100))
(quotient approx 400))))
(if (< date start)
approx
(1+ approx))))
*/
CCME_GregorianYear ccme_alt_gregorian_year_from_fixed(CCME_FixedDate date)
{
CCME_GregorianYear rv;
/*S-CODE*/
CCME_GregorianYear approx = ccme_quotient(date - ccme_gregorian_epoch() + 2, 146097.0/400.0);
CCME_GregorianYear start = ccme_gregorian_epoch() + (365 * approx) +
ccme_quotient(approx, 4) -
ccme_quotient(approx, 100) +
ccme_quotient(approx, 400);
if (date < start)
rv = approx;
else
rv = approx + 1;
/*E-CODE*/
return rv;
}
然而,这不是典型的LISP(也不特别典型的C),和LISP的一般转化为C的任何东西,但微不足道。当你处理字符串和列表(特别是字符串列表)时,这会更加真实,因为C中的内存管理变得......有趣(努力工作,存在问题)。
这两种语言都是极其不同。 Lisp的依赖于机械的大量存在:
- 代码
- 标准列表数据结构与特定的语义
- 垃圾收集
与C分析和评估Lisp代码当然,没有这些东西。当你将所有这些东西集中在一起并在C中使用时,你将会遇到一个复杂而难以理解的编程环境。只是学习Lisp会容易得多!
我只参加了本科生的基础编译课程,对我来说真的很难,我知道你绝对不可能100%做到这一点,但我只想知道是否存在任何部分解决方案,如果目标代码是一个koza-genetic-programming lisp树会使它eaiser转换为C代码? – user1567527 2012-08-12 00:56:34
有C/C++的遗传编程库;在C中实现相同的事情比直接试图翻译Lisp更容易。 – 2012-08-12 01:21:35
此外,还有一个名为[Stalin](https://en.wikipedia.org/wiki/Stalin_(Scheme_implementation))的编译器,可以将Lisp代码转换为优化的C代码。 – 2015-02-03 22:35:24
LISP的任何特定方言?你为什么要将LISP转换为C?你对列表有多好? – 2012-08-12 00:41:51
用于遗传编程的koza样式lisp树(www.genetic-programming.org)。我想完成我的遗传编程问题,然后将最终解决方案从lisp树转换为c代码,我对列表不太满意(什么类型的列表?) – user1567527 2012-08-12 00:46:14
当LISP不支持'很多刺激愚蠢括号',它是LISt处理的简称。整个语言是围绕处理列表建立的。我的直觉是你在LSIP工作会更好,而不是尝试转换为C. – 2012-08-12 00:51:56