Fix local variable name.
[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 ((stream tram-stream))
48   (intern (format "%s-error" (luna-class-name stream))))
49
50 (luna-define-method tram-stream-error ((stream tram-stream) error)
51   (throw (tram-stream-error-name stream) 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 (stream)
59      (let ((next
60             ,(if (functionp left)
61                  `(closure-call #',left stream)
62                `(closure-call ',left stream))))
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 (stream)
70      (let (next error)
71        (setq error
72              (catch (tram-stream-error-name stream)
73                (setq next
74                      ,(if (functionp left)
75                            `(closure-call #',left stream)
76                          `(closure-call ',left stream)))
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 from 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       (or combinator
107           (error "Unknown operator"))
108       (tram-fold-left
109        `(lambda (accu c)
110           (funcall
111            #',combinator accu
112            (if (listp c)
113                (tram-compose-transaction c)
114              c)))
115        (if (listp (car tram-transaction))
116            (tram-compose-transaction (pop tram-transaction))
117          (pop tram-transaction))
118        tram-transaction))))
119
120 (provide 'tram)
121
122 ;;; tram.el ends here