1 ;;; tram.el --- basic session framework for internet protocols
3 ;; Copyright (C) 2000 Daiki Ueno
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
34 (eval-when-compile (require 'cl))
37 (luna-define-class tram-stream ())
39 (luna-define-internal-accessors 'tram-stream))
41 (luna-define-generic tram-stream-error-name (stream)
42 "Return error symbol of the STREAM.")
44 (luna-define-generic tram-stream-error (stream error)
45 "Throw an ERROR of the STREAM.")
47 (luna-define-method tram-stream-error-name ((trans tram-stream))
48 (intern (format "%s-error" (luna-class-name trans))))
50 (luna-define-method tram-stream-error ((trans tram-stream) error)
51 (throw (tram-stream-error-name trans) error))
53 (put '&& 'tram-compose-function #'tram-compose-&&)
54 (put '|| 'tram-compose-function #'tram-compose-||)
56 (defun tram-compose-&& (left right)
57 "Multiplicative combinator which composes LEFT and RIGHT operations."
59 (let ((next (closure-call ',left trans)))
60 (closure-call ',right next))))
62 (defun tram-compose-|| (left right)
63 "Additive combinator which composes LEFT and RIGHT operations."
67 (catch (tram-stream-error-name trans)
68 (setq next (closure-call ',left trans))
71 (closure-call ',right trans)
74 (defun tram-fold-left (function accu sequence)
75 "Apply FUNCTION to ACCU while folding SEQUENCE left to right."
79 function (funcall function accu (car sequence))
83 (defmacro tram-define-transaction (name tram-transaction &optional doc)
84 "Set NAME the compiled code of TRAM-TRANSACTION."
86 ,(tram-compose-transaction (eval tram-transaction))))
87 (defconst ,name transaction ,doc)))
90 (defun tram-compose-transaction (tram-transaction)
91 "Compose transaction-function from TRAM-TRANSACTION."
92 (if (not (symbolp (car tram-transaction)))
95 (get (pop tram-transaction) 'tram-compose-function))
98 (error "Unknown operator")
105 (tram-compose-transaction c)
107 (if (listp (car tram-transaction))
108 (tram-compose-transaction (pop tram-transaction))
109 (pop tram-transaction))
111 (if (and (listp accu) (eq (car accu) 'lambda))
117 ;;; tram.el ends here