| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* backq.c - backquote mechanism |
| 6 |
* |
| 7 |
* Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 8 |
* Copyright (c) 1990, Giuseppe Attardi. |
| 9 |
* Copyright (c) 2001, Juan Jose Garcia Ripoll. |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <ecl/ecl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
|
| 18 |
/******************************* ------- ******************************/ |
| 19 |
|
| 20 |
#define QUOTE 1 |
| 21 |
#define EVAL 2 |
| 22 |
#define LIST 3 |
| 23 |
#define LISTX 4 |
| 24 |
#define APPEND 5 |
| 25 |
#define NCONC 6 |
| 26 |
|
| 27 |
extern int _cl_backq_car(cl_object *px); |
| 28 |
static cl_object backq(cl_object x); |
| 29 |
|
| 30 |
static cl_object |
| 31 |
kwote(cl_object x) |
| 32 |
{ |
| 33 |
cl_type t = ecl_t_of(x); |
| 34 |
if ((t == t_symbol && !Null(x) && !ecl_keywordp(x)) || |
| 35 |
t == t_list || t == t_vector) |
| 36 |
x = CONS(@'quote', ecl_list1(x)); |
| 37 |
return x; |
| 38 |
} |
| 39 |
|
| 40 |
/* |
| 41 |
_cl_backq_cdr(&x) puts result into x and returns one of |
| 42 |
|
| 43 |
QUOTE the form should be quoted |
| 44 |
EVAL the form should be evaluated |
| 45 |
LIST the form should be applied to LIST |
| 46 |
LISTX the form should be applied to LIST* |
| 47 |
APPEND the form should be applied to APPEND |
| 48 |
NCONC the form should be applied to NCONC |
| 49 |
*/ |
| 50 |
static int |
| 51 |
_cl_backq_cdr(cl_object *px) |
| 52 |
{ |
| 53 |
cl_object x = *px, ax, dx; |
| 54 |
int a, d, out; |
| 55 |
|
| 56 |
if (ECL_ATOM(x)) |
| 57 |
return(QUOTE); |
| 58 |
if (CAR(x) == @'si::unquote') { |
| 59 |
*px = CADR(x); |
| 60 |
return(EVAL); |
| 61 |
} |
| 62 |
if (CAR(x) == @'si::unquote-splice' || CAR(x) == @'si::unquote-nsplice') |
| 63 |
FEerror(",@@ or ,. has appeared in an illegal position.", 0); |
| 64 |
|
| 65 |
ax = CAR(x); dx = CDR(x); |
| 66 |
a = _cl_backq_car(&ax); |
| 67 |
d = _cl_backq_cdr(&dx); |
| 68 |
if (d == QUOTE) { |
| 69 |
switch (a) { |
| 70 |
case QUOTE: |
| 71 |
return(QUOTE); |
| 72 |
|
| 73 |
case EVAL: |
| 74 |
if (Null(dx)) { |
| 75 |
out = LIST; |
| 76 |
} else if (CONSP(dx) && Null(CDR(dx))) { |
| 77 |
dx = ecl_list1(kwote(CAR(dx))); |
| 78 |
out = LIST; |
| 79 |
} else { |
| 80 |
dx = ecl_list1(kwote(dx)); |
| 81 |
out = LISTX; |
| 82 |
} |
| 83 |
break; |
| 84 |
case APPEND: |
| 85 |
case NCONC: |
| 86 |
if (Null(dx)) { |
| 87 |
*px = ax; |
| 88 |
return EVAL; |
| 89 |
} else { |
| 90 |
dx = ecl_list1(kwote(dx)); |
| 91 |
out = a; |
| 92 |
} |
| 93 |
break; |
| 94 |
default: |
| 95 |
ecl_internal_error("backquote botch"); |
| 96 |
} |
| 97 |
} else if (d == EVAL) { |
| 98 |
switch (a) { |
| 99 |
case QUOTE: |
| 100 |
ax = kwote(ax); |
| 101 |
dx = ecl_list1(dx); |
| 102 |
out = LISTX; |
| 103 |
break; |
| 104 |
case EVAL: |
| 105 |
dx = ecl_list1(dx); |
| 106 |
out = LISTX; |
| 107 |
break; |
| 108 |
case APPEND: |
| 109 |
case NCONC: |
| 110 |
dx = ecl_list1(dx); |
| 111 |
out = a; |
| 112 |
break; |
| 113 |
default: |
| 114 |
ecl_internal_error("backquote botch"); |
| 115 |
} |
| 116 |
} else if (d == a) { |
| 117 |
out = d; |
| 118 |
} else { |
| 119 |
switch (d) { |
| 120 |
case LIST: |
| 121 |
if (a == QUOTE) { |
| 122 |
ax = kwote(ax); |
| 123 |
out = LIST; |
| 124 |
goto OUTPUT; |
| 125 |
} else if (a == EVAL) { |
| 126 |
out = LIST; |
| 127 |
goto OUTPUT; |
| 128 |
} |
| 129 |
dx = CONS(@'list', dx); |
| 130 |
break; |
| 131 |
case LISTX: |
| 132 |
if (a == QUOTE) { |
| 133 |
ax = kwote(ax); |
| 134 |
out = LISTX; |
| 135 |
goto OUTPUT; |
| 136 |
} else if (a == EVAL) { |
| 137 |
out = LISTX; |
| 138 |
goto OUTPUT; |
| 139 |
} |
| 140 |
dx = CONS(@'list*', dx); |
| 141 |
break; |
| 142 |
case APPEND: |
| 143 |
dx = CONS(@'append', dx); |
| 144 |
break; |
| 145 |
case NCONC: |
| 146 |
dx = CONS(@'nconc', dx); |
| 147 |
break; |
| 148 |
default: |
| 149 |
ecl_internal_error("backquote botch"); |
| 150 |
} |
| 151 |
switch (a) { |
| 152 |
case QUOTE: |
| 153 |
ax = kwote(ax); |
| 154 |
dx = ecl_list1(dx); |
| 155 |
out = LISTX; |
| 156 |
break; |
| 157 |
case EVAL: |
| 158 |
dx = ecl_list1(dx); |
| 159 |
out = LISTX; |
| 160 |
break; |
| 161 |
case APPEND: |
| 162 |
case NCONC: |
| 163 |
dx = ecl_list1(dx); |
| 164 |
out = a; |
| 165 |
break; |
| 166 |
default: |
| 167 |
ecl_internal_error("backquote botch"); |
| 168 |
} |
| 169 |
} |
| 170 |
OUTPUT: |
| 171 |
*px = CONS(ax, dx); |
| 172 |
return out; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
_cl_backq_car(&x) puts result into x and returns one of |
| 177 |
|
| 178 |
QUOTE the form should be quoted |
| 179 |
EVAL the form should be evaluated |
| 180 |
APPEND the form should be appended |
| 181 |
into the outer form |
| 182 |
NCONC the form should be nconc'ed |
| 183 |
into the outer form |
| 184 |
*/ |
| 185 |
int |
| 186 |
_cl_backq_car(cl_object *px) |
| 187 |
{ |
| 188 |
cl_object x = *px; |
| 189 |
int d; |
| 190 |
AGAIN: |
| 191 |
if (ECL_ATOM(x)) |
| 192 |
return(QUOTE); |
| 193 |
if (CAR(x) == @'si::quasiquote') { |
| 194 |
x = *px = backq(CADR(x)); |
| 195 |
goto AGAIN; |
| 196 |
} |
| 197 |
if (CAR(x) == @'si::unquote') { |
| 198 |
*px = CADR(x); |
| 199 |
return EVAL; |
| 200 |
} |
| 201 |
if (CAR(x) == @'si::unquote-splice') { |
| 202 |
*px = CADR(x); |
| 203 |
return APPEND; |
| 204 |
} |
| 205 |
if (CAR(x) == @'si::unquote-nsplice') { |
| 206 |
*px = CADR(x); |
| 207 |
return NCONC; |
| 208 |
} |
| 209 |
d = _cl_backq_cdr(px); |
| 210 |
switch (d) { |
| 211 |
case QUOTE: |
| 212 |
case EVAL: |
| 213 |
return(d); |
| 214 |
|
| 215 |
case LIST: |
| 216 |
*px = CONS(@'list', *px); |
| 217 |
break; |
| 218 |
|
| 219 |
case LISTX: |
| 220 |
*px = CONS(@'list*', *px); |
| 221 |
break; |
| 222 |
|
| 223 |
case APPEND: |
| 224 |
*px = CONS(@'append', *px); |
| 225 |
break; |
| 226 |
|
| 227 |
case NCONC: |
| 228 |
*px = CONS(@'nconc', *px); |
| 229 |
break; |
| 230 |
|
| 231 |
default: |
| 232 |
ecl_internal_error("backquote botch"); |
| 233 |
} |
| 234 |
return(EVAL); |
| 235 |
} |
| 236 |
|
| 237 |
static cl_object |
| 238 |
backq(cl_object x) |
| 239 |
{ |
| 240 |
int a; |
| 241 |
|
| 242 |
a = _cl_backq_car(&x); |
| 243 |
if (a == APPEND || a == NCONC) |
| 244 |
FEerror(",@@ or ,. has appeared in an illegal position.", 0); |
| 245 |
if (a == QUOTE) |
| 246 |
return(kwote(x)); |
| 247 |
return(x); |
| 248 |
} |
| 249 |
|
| 250 |
static cl_object |
| 251 |
quasiquote_macro(cl_object whole, cl_object env) |
| 252 |
{ |
| 253 |
if (ecl_length(whole) != 2) { |
| 254 |
FEprogram_error("Syntax error: ~S.", 1, whole); |
| 255 |
} |
| 256 |
@(return backq(CADR(whole))); |
| 257 |
} |
| 258 |
|
| 259 |
void |
| 260 |
init_backq(void) |
| 261 |
{ |
| 262 |
ecl_def_c_macro(@'si::quasiquote', (cl_objectfn_fixed)quasiquote_macro, 2); |
| 263 |
} |