2
2
3
3
#include <ctype.h>
4
4
#include <stdlib.h>
5
+ #include "ambeval.h"
5
6
#include "bitmap.h"
6
7
#include "environment.h"
7
8
#include "error.h"
@@ -27,11 +28,20 @@ static obj evalstr(char *e, obj env)
27
28
28
29
static obj eval_p (obj args )
29
30
{
30
- if (is_err (chkarity ("repl" , 2 , args )))
31
+ if (is_err (args = chkarity ("repl" , 2 , args ))) {
31
32
return args ;
33
+ }
32
34
return eval (car (args ), cadr (args ));
33
35
}
34
36
37
+ static obj ambeval_p (obj args )
38
+ {
39
+ if (is_err (args = chkarity ("amb-loop" , 4 , args ))) {
40
+ return args ;
41
+ }
42
+ return ambeval (car (args ), cadr (args ), caddr (args ), cadddr (args ));
43
+ }
44
+
35
45
static obj add_extras (int ex , obj env )
36
46
{
37
47
// Implementation specific, (not in book):
@@ -52,14 +62,14 @@ static obj add_extras(int ex, obj env)
52
62
env );
53
63
54
64
if (ex > 101 ) {
55
- // define_variable(of_identifier("abs"), of_function(absl), env);
56
- evalstr ("(define (abs x) (if (< x 0) (- x) x))" , env );
65
+ define_variable (of_identifier ("abs" ), of_function (absl ), env );
66
+ // evalstr("(define (abs x) (if (< x 0) (- x) x))", env);
57
67
58
68
define_variable (of_identifier ("<=" ), of_function (lte ), env );
59
- //evalstr("(define (<= x y) (not (> x y)))", env);
69
+ // evalstr("(define (<= x y) (not (> x y)))", env);
60
70
61
71
define_variable (of_identifier (">=" ), of_function (gte ), env );
62
- //evalstr("(define (>= x y) (not (< x y)))", env);
72
+ // evalstr("(define (>= x y) (not (< x y)))", env);
63
73
64
74
evalstr ("(define (square x) (* x x))" , env );
65
75
evalstr ("(define (cube x) (* x x x))" , env );
@@ -311,13 +321,55 @@ static obj add_extras(int ex, obj env)
311
321
evalstr ("(define driver-loop repl)" , env );
312
322
}
313
323
if (ex >= 435 ) {
324
+ define_variable (of_identifier ("ambeval" ),
325
+ of_function (ambeval_p ), env );
314
326
evalstr ("(define (require p) (if (not p) (amb)))" , env );
315
327
evalstr ("(define (distinct? items)"
316
328
" (cond ((null? items) true)"
317
329
" ((null? (cdr items)) true)"
318
330
" ((member (car items) (cdr items)) false)"
319
331
" (else (distinct? (cdr items)))))" ,
320
332
env );
333
+ evalstr ("(define (amb-loop)"
334
+ " (define input-prompt \";;; Amb-Eval input:\")"
335
+ " (define output-prompt \";;; Amb-Eval value:\")"
336
+ " (define (prompt-for-input string)"
337
+ " (newline) (newline) (display string) (newline))"
338
+ " (define (announce-output string)"
339
+ " (newline) (display string) (newline))"
340
+ " (define (user-print object)"
341
+ " (if (and (pair? object)"
342
+ " (eq? (car object) 'procedure))"
343
+ " (display (list 'compound-procedure"
344
+ " (procedure-parameters object)"
345
+ " (procedure-body object)"
346
+ " '<procedure-env>))"
347
+ " (display object)))"
348
+ " (define (internal-loop try-again)"
349
+ " (prompt-for-input input-prompt)"
350
+ " (let ((input (read)))"
351
+ " (if (eq? input 'try-again)"
352
+ " (try-again)"
353
+ " (begin"
354
+ " (newline)"
355
+ " (display \";;; Starting a new problem \")"
356
+ " (ambeval input"
357
+ " the-global-environment"
358
+ " (lambda (val next-alternative)"
359
+ " (announce-output output-prompt)"
360
+ " (user-print val)"
361
+ " (internal-loop next-alternative))"
362
+ " (lambda ()"
363
+ " (announce-output"
364
+ " \";;; There are no more values of\")"
365
+ " (user-print input)"
366
+ " (amb-loop)))))))"
367
+ " (internal-loop"
368
+ " (lambda ()"
369
+ " (newline)"
370
+ " (display \";;; There is no current problem\")"
371
+ " (amb-loop))))" ,
372
+ env );
321
373
}
322
374
return unspecified ;
323
375
}
0 commit comments