* smtp.el (smtp-via-smtp): Don't compile transaction-function.
[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
60             ,(if (functionp left)
61                  `(closure-call #',left trans)
62                `(closure-call ',left trans))))
63        ,(if (functionp right)
64             `(closure-call #',right next)
65           `(closure-call ',right next)))))
66
67 (defun tram-compose-|| (left right)
68   "Additive combinator which composes LEFT and RIGHT operations."
69   `(lambda (trans)
70      (let (next error)
71        (setq error
72              (catch (tram-stream-error-name trans)
73                (setq next
74                      ,(if (functionp left)
75                            `(closure-call #',left trans)
76                          `(closure-call ',left trans)))
77                nil))
78        (if error
79            ,(if (functionp right)
80                 `(closure-call #',right next)
81               `(closure-call ',right next))
82          next))))
83
84 (defun tram-fold-left (function accu sequence)
85   "Apply FUNCTION to ACCU while folding SEQUENCE left to right."
86   (if (null sequence)
87       accu
88     (tram-fold-left
89      function (funcall function accu (car sequence))
90      (cdr sequence))))
91
92 ;;;###autoload
93 (defmacro tram-define-transaction (name tram-transaction &optional doc)
94   "Set NAME the compiled code of TRAM-TRANSACTION."
95   `(let ((transaction
96           ,(tram-compose-transaction (eval tram-transaction))))
97      (defconst ,name transaction ,doc)))
98
99 ;;;###autoload
100 (defun tram-compose-transaction (tram-transaction)
101   "Compose transaction-function from TRAM-TRANSACTION."
102   (if (not (symbolp (car tram-transaction)))
103       tram-transaction
104     (let ((combinator
105            (get (pop tram-transaction) 'tram-compose-function))
106           accu)
107       (if (null combinator)
108           (error "Unknown operator")
109         (setq accu
110               (tram-fold-left
111                `(lambda (accu c)
112                   (funcall
113                    #',combinator accu
114                    (if (listp c)
115                        (tram-compose-transaction c)
116                      c)))
117                (if (listp (car tram-transaction))
118                    (tram-compose-transaction (pop tram-transaction))
119                  (pop tram-transaction))
120                tram-transaction))
121         (if (and (listp accu) (eq (car accu) 'lambda))
122             (byte-compile accu)
123           accu)))))
124
125 (provide 'tram)
126
127 ;;; tram.el ends here