Skip to content

Commit 9cc76d7

Browse files
committed
Add: amb-fail
1 parent cfe2114 commit 9cc76d7

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

src/ambeval.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ static obj amb_choices_p(obj args)
2121
return cdar(args);
2222
}
2323

24+
static obj amb_fail_p(obj args)
25+
{
26+
(void)args;
27+
return error_amb(AREA, "No more choices");
28+
}
29+
2430
static obj evalstr(char *e, obj env)
2531
{
2632
return eceval(readp(openin_string(e)), env);
@@ -263,16 +269,13 @@ static obj ambeval(obj exp, obj exenv, obj succeed, obj fail)
263269

264270
obj ambeval2(obj exp, obj exenv)
265271
{
272+
define_variable(of_identifier("amb-fail"), of_function(amb_fail_p),
273+
exenv);
266274
obj value = of_identifier("value");
267275
obj fail_s = of_identifier("fail");
268276
obj succeed =
269277
make_procedure(list2(value, fail_s), list1(value), emptylst);
270278
obj fail = make_procedure(
271-
emptylst,
272-
list1( // list2(of_identifier("display"),
273-
// of_string(
274-
// "ERROR: (AMBEVAL) unexpected call to amb2's fail")),
275-
list2(quote, amb_fail)),
276-
emptylst);
279+
emptylst, list1(list1(of_identifier("amb-fail"))), exenv);
277280
return ambeval(exp, exenv, succeed, fail);
278281
}

src/error.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ enum errsbtyp {
1616
ERROR_IO, // 6
1717
ERROR_PARSER, // 7
1818
ERROR_UNBOUND_VARIABLE, // 8
19-
ERROR_USER // 9
19+
ERROR_AMB, // 9
20+
ERROR_USER // 10
2021
};
2122

2223
const char *errstr(obj dat)
@@ -127,6 +128,14 @@ obj error_unbound_variable(const char *area, const char *message, ...)
127128
message, args);
128129
}
129130

131+
obj error_amb(const char *area, const char *message, ...)
132+
{
133+
va_list args;
134+
va_start(args, message);
135+
return print_make_err(ERROR_AMB, "Amb failed", area,
136+
message, args);
137+
}
138+
130139
obj user_error(obj args)
131140
{
132141
obj dat;

src/error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ obj error_io(const char *area, const char *message, ...);
1818
obj error_parser(const char *area, const char *message, ...);
1919
obj error_syntax(const char *area, const char *message, ...);
2020
obj error_unbound_variable(const char *area, const char *message, ...);
21+
obj error_amb(const char *area, const char *message, ...);
2122

2223
obj user_error(const obj args);
24+
2325
#endif

src/obj.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ obj (*to_function(obj dat))(obj)
239239
// KEYWORDS
240240

241241
SYMBOL_VAR(amb)
242-
const obj amb_fail = SYMBOL("amb-fail");
243242
const obj and_s = SYMBOL("and");
244243
SYMBOL_VAR(uapply)
245244
const obj __ppapply = SYMBOL("__%%apply");

src/obj.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ obj (*to_function(obj dat))(obj);
120120
// KEYWORDS
121121

122122
extern const obj amb;
123-
extern const obj amb_fail;
124123
extern const obj and_s;
125124
extern const obj uapply;
126125
extern const obj __ppapply;

test/code/ch4/ex4.41/ex4.41-cbf.sicp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
(%ambeval)
2+
13
(define (multiple-dwelling)
24

35
(define floors (list 1 2 3 4 5))
@@ -50,4 +52,4 @@
5052

5153
(enum-fletchers floors))
5254

53-
(time (multiple-dwelling))
55+
(time (multiple-dwelling))

0 commit comments

Comments
 (0)