Humanity

Edit the world by your favorite way

GaucheでGaucheインタープリタ再実装


そのまんまじゃ面白くないので gosh の -E オプションみたいに
一番外側の括弧を自動的に囲んで評価できるようにした。

repl.scm

(use gauche.interactive)

(define *wrap* #f)

; この関数を評価することで
; 一番外側の括弧を入力しなくても
; よかったりよくなかったり
(define (wrap/paren)
  (set! *wrap* (if *wrap* #f #t)))

(define (read-atoms)
  (let1 atom (read)
    (if (eof-object? atom)
      '()
      (cons atom (read-atoms)))))

; REPL
(define (repl)
  (display "> ")
  (flush)
  (let1 expr (if *wrap* (read-atoms) (read))
    (unless (eof-object? expr)
      (print (eval expr (interaction-environment)))
      (repl))))

; ヘルプ
(define (help)
  (print "Gauche repl:\n"
         "\tThis repl is not so different from gosh interpreter\n"
         "\texcept for message when a error occurs.\n"
         "\nOptions:\n"
         "\t-e\t\teval wrap/paren before entering REPL."))
(define (help!)
  (print "E478: Don't panic!"))

; arguments
(define (args-proc opt)
  (when (string=? opt "-e")   ; オプション随時追加
    (wrap/paren)))

; main
(define (main args)
  ; オプション処理
  (if (pair? (cdr args))
    (for-each args-proc (cdr args)))
  ; REPL
  (guard (e (else
              (print e)
              (repl)))
         (repl))
  0)

実行して

C:\~~~\>gosh repl.scm -e
> help^D        ; EOF(linux:Ctrl-D, Win:Ctrl-Z)で評価される。
Gauche repl:
        This repl is not so different from gosh interpreter
        except for message when a error occurs.

Options:
        -e              eval wrap/paren before entering REPL.
#<undef>
> exit^D

gauche.interactive は d を使えるようにするためとか。
もっと便利にしていきたいなー



Windowsでもreadline使えるようにならないかな・・・
いっそ組み込んでみるかな?
でもviモードはさすがに難しいか。。。



試してみようと思ったきっかけ
http://cadr.g.hatena.ne.jp/mokehehe/20080204/impl