Add negative?, positive?, zero?, 1+ 1-.
[mes.git] / quasiquote.c
1 /* -*-comment-start: "//";comment-end:""-*-
2  * Mes --- Maxwell Equations of Software
3  * Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
4  *
5  * This file is part of Mes.
6  *
7  * Mes is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or (at
10  * your option) any later version.
11  *
12  * Mes is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Mes.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #if QUASIQUOTE
22 SCM
23 unquote (SCM x) ///((no-environment))
24 {
25   return cons (cell_symbol_unquote, x);
26 }
27
28 SCM
29 unquote_splicing (SCM x) ///((no-environment))
30 {
31   return cons (cell_symbol_unquote_splicing, x);
32 }
33
34 SCM
35 eval_quasiquote (SCM e, SCM a)
36 {
37   return vm_call (vm_eval_quasiquote, e, cell_undefined, a);
38 }
39
40 SCM
41 vm_eval_quasiquote ()
42 {
43   if (r1 == cell_nil) return r1;
44   else if (atom_p (r1) == cell_t) return r1;
45   else if (eq_p (car (r1), cell_symbol_unquote) == cell_t)
46     return eval_env (cadr (r1), r0);
47   else if (TYPE (r1) == PAIR && TYPE (car (r1)) == PAIR
48            && eq_p (caar (r1), cell_symbol_unquote_splicing) == cell_t)
49     {
50       r2 = eval_env (cadar (r1), r0);
51       return append2 (r2, eval_quasiquote (cdr (r1), r0));
52     }
53   r2 = eval_quasiquote (car (r1), r0);
54   return cons (r2, eval_quasiquote (cdr (r1), r0));
55 }
56
57 SCM
58 add_unquoters (SCM a)
59 {
60   SCM q = assq_ref_cache (cell_symbol_the_unquoters, a);
61   return append2 (q, a);
62 }
63 #else // !QUASIQUOTE
64
65 SCM add_unquoters (SCM a){}
66 SCM eval_quasiquote (SCM e, SCM a){}
67
68 SCM unquote (SCM x){}
69 SCM unquote_splicing (SCM x){}
70 SCM vm_eval_quasiquote () {}
71
72 #endif // QUASIQUOTE
73
74 #if QUASISYNTAX
75 SCM
76 syntax (SCM x)
77 {
78   return cons (cell_symbol_syntax, x);
79 }
80
81 SCM
82 unsyntax (SCM x) ///((no-environment))
83 {
84   return cons (cell_symbol_unsyntax, x);
85 }
86
87 SCM
88 unsyntax_splicing (SCM x) ///((no-environment))
89 {
90   return cons (cell_symbol_unsyntax_splicing, x);
91 }
92
93 SCM
94 eval_quasisyntax (SCM e, SCM a)
95 {
96   return vm_call (vm_eval_quasisyntax, e, cell_undefined, a);
97 }
98
99 SCM
100 vm_eval_quasisyntax ()
101 {
102   if (r1 == cell_nil) return r1;
103   else if (atom_p (r1) == cell_t) return r1;
104   else if (eq_p (car (r1), cell_symbol_unsyntax) == cell_t)
105     return eval_env (cadr (r1), r0);
106   else if (TYPE (r1) == PAIR && TYPE (car (r1)) == PAIR
107            && eq_p (caar (r1), cell_symbol_unsyntax_splicing) == cell_t)
108     {
109       r2 = eval_env (cadar (r1), r0);
110       return append2 (r2, eval_quasisyntax (cdr (r1), r0));
111     }
112   r2 = eval_quasisyntax (car (r1), r0);
113   return cons (r2, eval_quasisyntax (cdr (r1), r0));
114 }
115
116 SCM
117 add_unsyntaxers (SCM a)
118 {
119   SCM q = assq_ref_cache (cell_symbol_the_unsyntaxers, a);
120   return append2 (q, a);
121 }
122
123 #else // !QUASISYNTAX
124 SCM syntax (SCM x){}
125 SCM unsyntax (SCM x){}
126 SCM unsyntax_splicing (SCM x){}
127 SCM add_unsyntaxers (SCM a){}
128 SCM eval_quasisyntax (SCM e, SCM a){}
129 SCM vm_eval_quasisyntax () {}
130
131 #endif // !QUASISYNTAX