manywaypark's Blog
개발, 검색, 함수

그냥 막연히 make-string-input-stream으로 input stream을 만들어서 cxml:parse-stream에 걸면 될 줄로 생각했는데, binary stream을 만들어야한다.
;;; simple binary stream for string parsing with cxml (just a wrapper for string).
(defclass bin-stream (fundamental-binary-input-stream)
((string-input-stream
:initarg :string-input-stream
:accessor string-input-stream)))

(defmethod stream-read-byte ((s bin-stream))
(char-code (or (read-char (string-input-stream s) nil)
(return-from stream-read-byte :eof))))

;;; makes binary stream of a string for parsing with cxml.
(defun make-bin-stream (string)
(assert (stringp string))
(make-instance 'bin-stream :string-input-stream (make-string-input-stream string)))
다음과 같이 테스트
CL-USER> (make-bin-stream "<foo><bar/></foo>")
#<BIN-STREAM {1002C69051}>
CL-USER> (dom:node-name (dom:document-element (cxml:parse-stream * (cxml-dom:make-dom-builder))))
"foo"
CL-USER>
binary stream wrapper를 만드는데 시간이 좀 걸렸다.

참고:
http://www.mikemac.com/mikemac/clim/gray-streams.html (맨 밑에 보면 binary stream을 만들려면, fundamental-binary-*-stream를 상속하고 stream-read-byte 또는 stream-write-byte method를 구현하라고 나온다).
http://www.cs.queensu.ca/software_docs/gnudev/gcl-ansi/gcl_1178.html (read-char의 optional parameter에 eof-error-p를 주의. nil로 설정!! nil로 설정하지 않으면 end-of-file condition error가 뜬다.)

happy hackin'


초기화 파일을 응용하여 명령행에서 유닉스 스크립트처럼 lisp 파일을 실행하는 법에 대해 간단히 기술한다.

초기화 파일 위치:
  1. 시스템 수준: 기본값은 SBCL_HOME/sbclrc, 없으면, /etc/sbclrc이다. 명령행에서 --sysinit로 지정가능.
  2. 사용자 수준: 기본값은 HOME/.sbclrc이지만 명령행에서 --userinit로 지정가능.
상기 초기화 파일 중에 적절한 것을 선택해서 다음의 내용을 추가한다. 파일이 없으면 만든다.

shebang(#!) 형태의 lisp 프로그램 실행을 위한 초기화 파일 내용:
;;; If the first user-processable command-line argument is a filename,
;;; disable the debugger, load the file handling shebang-line and quit.
(let ((script (and (second *posix-argv*)
(probe-file (second *posix-argv*)))))
(when script
;; Handle shebang-line
(set-dispatch-macro-character #\# #\!
(lambda (stream char arg)
(declare (ignore char arg))
(read-line stream)))
;; Disable debugger
(setf *invoke-debugger-hook*
(lambda (condition hook)
(declare (ignore hook))
;; Uncomment to get backtraces on errors
;; (sb-debug:backtrace 20)
(format *error-output* "Error: ~A~%" condition)
(quit)))
(load script)
(quit)))

test program(hello.lisp):
#!/usr/bin/sbcl --noinform
(write-line "Hello, World!")

실행:
$ chmod +x hello.lisp
$ ./hello.lisp
Hello, World!
$ sbcl hello.lisp
This is SBCL 0.9.14, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
Hello, World!

참고: http://www.sbcl.org/manual/Initialization-Files.html

2007-10-24
cl-launch를 사용하면 sbcl을 포함한 다른 CL implementation에서도 쓸 수 있는 좀 더 일반적인 방법으로 common lisp 프로그램을 명령행에서 실행할 수 있다.

happy hackin'

ASDF brief

함수형 언어/Lisp 2007. 3. 23. 01:14 by manywaypark
ASDF는 "Another System Definition Facility"의 약자로서 간편하게 package들을 관리할 수 있게 해준다. 여기서는 간단한 사용법을 살펴본다.

package 설치(install):
자동 다운로드 및 설치가 되는 경우
;;; imports asdf
(require 'asdf)
;;; imports asdf-install
(require 'asdf-install)
;;; install some package
(asdf-install:install 'some-package)
참고 및 다운로드/설치 가능한 패키지 리스트
설치시 선택한 위치(system-wide 또는 personal)에 파일들이 다운로드 되고 설치된다.
경로는 system-wide는 /usr/lib/sbcl, personal은 ~/.sbcl이다.
일단 한번 설치한 후에는 간단히 (require 'some-package)로 불러서 쓸 수 있다.

자동 설치가 지원되지 않는 것들은 수동 다운로드 후에 파일을 로드하는 조금 성가신 작업이 필요하다.
package 로드(load):
;;; imports asdf
(require 'asdf)
;;; loads some package from disk (current dir. has some-package-on-disk.asd file).
(asdf:operate 'asdf:load-op :some-package-on-disk)

load path 추가: 패키지를 load할 경로 추가하기. 참고
;;; adds asdf load path
(pushnew "/path/to/your/registry/" asdf:*central-registry* :test #'equal)

패키지 삭제하기:
;;; removes a package
(asdf-install:uninstall :package-name)

참고

happy hackin'

ps. 사용된 lisp 구현은 sbcl이다.

slime 설치, 한글 출력

함수형 언어/Lisp 2007. 3. 21. 14:53 by manywaypark
slime은 emacs를 환상적인 lisp IDE로 변신시켜준다.
cvs로 최신 소스를 받거나, 안정버전을 다운로드 한다(최신 개발 소스는 가끔 불안정한 경우도 있다).

~/.emacs에 추가할 내용(밑줄 부분은 자신의 설정에 맞게 변경):
;;; for slime
(setq inferior-lisp-program "/usr/bin/sbcl")
(add-to-list 'load-path "~/hacking/lisp/slime-2.0")
(require 'slime)
(slime-setup)
;;; for utf-8 coding system.
(set-language-environment "UTF-8")
(setq slime-net-coding-system 'utf-8-unix)
상단부는 slime 설정이고 하단부는 utf-8 관련 설정인데, utf-8 관련 설정을 하지 않으면 한글을 출력하려 할 때 다음과 같은 에러 메시지를 뿌리며 정상적인 실행이 안된다.
;;; (format t "한글")
Coding system iso-latin-1-unix not suitable for "00005a(:emacs-rex (swank:listener-eval \"(format t \\\"한글\\\")
\") \"COMMON-LISP-USER\" :repl-thread 8)

이제 남은 것은 M-x slime!!

happy hackin'

관련링크: slime home,cliki slime tips
1 2 
분류 전체보기 (306)
잡담 (20)
함수형 언어 (65)
emacs (16)
java (18)
tips & tricks (154)
사랑 (1)
가사 (0)
독서 (4)
mobile (6)
비함수형 언어 (2)

공지사항

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

03-29 01:16