่จ็ฎใขใใซ่ซใ่ใใชใใ, SๅผใใC++ใธใฎ็ฟป่จณๆฉใๆธใใฆใพใใ ้ขๅใซใชใฃใฆใใใพใใ
ๅฎ่ฃ ใใใใณใ ใใๆๅคง้่ฆใใคใใๅฎ่กไพ
$ gosh test.scm << EOS
(progn
(include stdio.h)
'(using name space std)
(define (int int) (f x)
(* (+ 1 2 3 4) (/ (- 10)))
(* x (f (- x 1)))))
EOS
#include <stdio.h>
using name space std;
int f (int x) {
((1+2+3+4)*(1/-10));
return (x*f((x-1)));
}
้ขๆฐใฎๅๆๅฎใใชใใใณใฌๆฐๆใกๆชใใชใใจใๆใฃใใ cout << ...
ใงใๅฎ่ฃ
ใใใใใชใใจใๆใฃใฆใใ ๆๆฅญใ็ตใฃใใฎใงใ้ใณใใใใซใใพใใ
(define (translate exp)
(if (symbol? exp) (symbol->string exp)
(if (number? exp) (number->string exp)
(if (null? exp) ""
(case (car exp)
((progn) (string-join (map translate (cdr exp)) "\n"))
((include) (format "#include <~a>\n" (cadr exp)))
((quote)
(string-append
(string-join (map symbol->string (cadr exp)) " ")
";"))
((define)
(let ((funt (caadr exp))
(argt (cdadr exp))
(fun (caaddr exp))
(args (cdaddr exp)))
(define (append-return ls)
(if (= (length ls) 1)
(list (string-append "return " (car ls)))
(cons (car ls) (append-return (cdr ls)))))
(format
"~a ~a (~a) {\n\t~a;\n}"
funt fun
(string-join
(map (cut format "~a ~a" <> <>) argt args) ", ")
(string-join
(append-return (map translate (cdddr exp))) ";\n\t"))))
((+ - * /)
(cond ((and (eq? (car exp) '-) (= (length exp) 2))
(format "-~a" (cadr exp)))
((and (eq? (car exp) '/) (= (length exp) 2))
(format "(1/~a)" (translate (cadr exp))))
(else
(string-append
"("
(string-join
(map translate (cdr exp))
(symbol->string (car exp)))
")"))))
(else
(format "~a(~a)" (car exp)
(string-join (map translate (cdr exp)) ", "))))))))
(display
(translate (read)))