2012年7月9日月曜日

[Gauche]CiSEでFizzBuzz

CiSE(C in S-Expression)でFizzBuzzってみます。
;; cise-compile.scm
(use gauche.cgen)
(use gauche.cgen.cise)
(use gauche.parseopt)

(define (main args)
  (let-args (cdr args)
    ((infile "i=s" #f)
     (outfile "o=s" #f))

    (unless (and infile outfile)
      (display #`"usage: gosh ,(car args) -i 'input-file' -o 'output-file'\n")
      (exit -1))
    
    (call-with-input-file infile
      (^ (in)
  (call-with-output-file outfile
    (^ (out)
       (cise-translate in out)))))))
;; cise-test.cise
(.include <stdio.h>)

(define-cfn main (argc::int argv::char**) ::int
  (dotimes (i 30)
    (case (% (+ i 1) 15)
      ((0) (printf "FizzBuzz\n"))
      ((3 6 9 12) (printf "Fizz\n"))
      ((5 10) (printf "Buzz\n"))
      (else (printf "%d\n" (+ i 1)))))
  (return 0))
 > gosh cise-compile.scm -i cise-test.cise -o cise-test.c
 > gcc -o cise-test cise-test.c

0 件のコメント:

コメントを投稿

コメントを投稿