Skip to content

Commit 02be20b

Browse files
committed
Add: amb-loop
1 parent 9cc76d7 commit 02be20b

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"src/storage.c",
3636
"src/strbldr.c",
3737
//
38-
"-O1", // Optimization Level. Tail Call Optimization seem to need >= 2
38+
"-O2", // Optimization Level. Tail Call Optimization seem to need >= 2
3939
"-std=c17", // 'ISO C 2017' standard
4040
"-o",
4141
"${workspaceFolder}/bin/sicp", // output e.g., "${fileDirname}/sicp",
@@ -75,4 +75,4 @@
7575
],
7676
},
7777
]
78-
}
78+
}

src/ambeval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void init(obj execution_environment)
241241
ambenv);
242242
}
243243

244-
static obj ambeval(obj exp, obj exenv, obj succeed, obj fail)
244+
obj ambeval(obj exp, obj exenv, obj succeed, obj fail)
245245
{
246246
static bool haveinit = false;
247247

src/ambeval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "obj.h"
66

7+
obj ambeval(obj exp, obj env, obj succeed, obj fail);
78
obj ambeval2(obj exp, obj env);
89

910
#endif

src/custom.c

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <ctype.h>
44
#include <stdlib.h>
5+
#include "ambeval.h"
56
#include "bitmap.h"
67
#include "environment.h"
78
#include "error.h"
@@ -27,11 +28,20 @@ static obj evalstr(char *e, obj env)
2728

2829
static obj eval_p(obj args)
2930
{
30-
if (is_err(chkarity("repl", 2, args)))
31+
if (is_err(args = chkarity("repl", 2, args))) {
3132
return args;
33+
}
3234
return eval(car(args), cadr(args));
3335
}
3436

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+
3545
static obj add_extras(int ex, obj env)
3646
{
3747
// Implementation specific, (not in book):
@@ -52,14 +62,14 @@ static obj add_extras(int ex, obj env)
5262
env);
5363

5464
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);
5767

5868
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);
6070

6171
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);
6373

6474
evalstr("(define (square x) (* x x))", env);
6575
evalstr("(define (cube x) (* x x x))", env);
@@ -311,13 +321,55 @@ static obj add_extras(int ex, obj env)
311321
evalstr("(define driver-loop repl)", env);
312322
}
313323
if (ex >= 435) {
324+
define_variable(of_identifier("ambeval"),
325+
of_function(ambeval_p), env);
314326
evalstr("(define (require p) (if (not p) (amb)))", env);
315327
evalstr("(define (distinct? items)"
316328
" (cond ((null? items) true)"
317329
" ((null? (cdr items)) true)"
318330
" ((member (car items) (cdr items)) false)"
319331
" (else (distinct? (cdr items)))))",
320332
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);
321373
}
322374
return unspecified;
323375
}

0 commit comments

Comments
 (0)