* tram.el: Rename from net-trans.el; require `closure'.
[elisp/flim.git] / tram.el
1 ;;; tram.el --- basic session framework for internet protocols
2
3 ;; Copyright (C) 2000 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Created: 2000/08/14
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
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.
14
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.
19
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.
24
25
26 ;;; Commentary:
27 ;; 
28
29 ;;; Code:
30
31 (require 'luna)
32 (require 'closure)
33
34 (eval-when-compile (require 'cl))
35
36 (eval-and-compile
37   (luna-define-class tram-stream ())
38
39   (luna-define-internal-accessors 'tram-stream))
40
41 (luna-define-generic tram-stream-error-name (stream)
42   "Return error symbol of the STREAM.")
43
44 (luna-define-generic tram-stream-error (stream error)
45   "Throw an ERROR of the STREAM.")
46
47 (luna-define-method tram-stream-error-name ((trans tram-stream))
48   (intern (format "%s-error" (luna-class-name trans))))
49
50 (luna-define-method tram-stream-error ((trans tram-stream) error)
51   (throw (tram-stream-error-name trans) error))
52
53 (put '&& 'tram-compose-function #'tram-compose-&&)
54 (put '|| 'tram-compose-function #'tram-compose-||)
55
56 (defun tram-compose-&& (left right)
57   "Multiplicative combinator which composes LEFT and RIGHT operations."
58   `(lambda (trans)
59      (let ((next (closure-call ',left trans)))
60        (closure-call ',right next))))
61
62 (defun tram-compose-|| (left right)
63   "Additive combinator which composes LEFT and RIGHT operations."
64   `(lambda (trans)
65      (let (next error)
66        (setq error
67              (catch (tram-stream-error-name trans)
68                (setq next (closure-call ',left trans))
69                nil))
70        (if error
71            (closure-call ',right trans)
72          next))))
73
74 (defun tram-fold-left (function accu sequence)
75   "Apply FUNCTION to ACCU while folding SEQUENCE left to right."
76   (if (null sequence)
77       accu
78     (tram-fold-left
79      function (funcall function accu (car sequence))
80      (cdr sequence))))
81
82 ;;;###autoload
83 (defmacro tram-define-transaction (name tram-transaction &optional doc)
84   "Set NAME the compiled code of TRAM-TRANSACTION."
85   `(let ((transaction
86           ,(tram-compose-transaction (eval tram-transaction))))
87      (defconst ,name transaction ,doc)))
88
89 ;;;###autoload
90 (defun tram-compose-transaction (tram-transaction)
91   "Compose transaction-function from TRAM-TRANSACTION."
92   (if (not (symbolp (car tram-transaction)))
93       tram-transaction
94     (let ((combinator
95            (get (pop tram-transaction) 'tram-compose-function))
96           accu)
97       (if (null combinator)
98           (error "Unknown operator")
99         (setq accu
100               (tram-fold-left
101                `(lambda (accu c)
102                   (funcall
103                    #',combinator accu
104                    (if (listp c)
105                        (tram-compose-transaction c)
106                      c)))
107                (if (listp (car tram-transaction))
108                    (tram-compose-transaction (pop tram-transaction))
109                  (pop tram-transaction))
110                tram-transaction))
111         (if (and (listp accu) (eq (car accu) 'lambda))
112             (byte-compile accu)
113           accu)))))
114
115 (provide 'tram)
116
117 ;;; tram.el ends here