Skip to content

Commit 675e17b

Browse files
committed
More error checking
1 parent 4cb381a commit 675e17b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

lazy.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,12 @@ Expr* parse_manual_close(Stream* f, int expected_terminator) {
599599

600600

601601
static Expr* car(Expr* list) {
602+
check(1);
602603
return partial_apply(list, &cK);
603604
}
604605

605606
static Expr* cdr(Expr* list) {
607+
check(1);
606608
return partial_apply(list, &KI);
607609
}
608610

lazy2.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
// stream.
3232
//
3333

34-
#define DEBUG_COUNTERS 0
34+
#define DEBUG_COUNTERS 1
3535

3636
#include <assert.h>
3737
#include <stdio.h>
@@ -213,7 +213,7 @@ static void oom(int n) {
213213
gc();
214214
if (is_exhausted(n)) {
215215
fprintf(stderr, "out of memory!\n");
216-
exit(4);
216+
abort();
217217
}
218218
}
219219

@@ -339,21 +339,21 @@ static inline Expr *partial_eval_primitive_application(Expr *e, Expr **prev) {
339339
e->type = Num;
340340
e->numeric_arg1 = to_number(rhs_res) + 1;
341341
if (e->numeric_arg1 == 0) {
342-
fputs("Runtime error: invalid output format (attempted to apply inc to a non-number)\n", stderr);
343-
exit(3);
342+
fputs("Runtime error: invalid output format (attempted to apply inc to a non-number)\n",
343+
stderr);
344+
abort();
344345
}
345346
e->arg2 = 0;
346347
break;
347348
}
348349
case Num:
349350
fputs("Runtime error: invalid output format (attempted to apply a number)\n", stderr);
350-
exit(3);
351+
abort();
351352
default:
352353
fprintf(stderr,
353354
"INTERNAL ERROR: invalid type in partial_eval_primitive_application (%d)\n",
354355
e->arg1->type);
355356
abort();
356-
exit(4);
357357
}
358358

359359
return e;
@@ -433,7 +433,7 @@ Expr *parse_expr(FILE* f) {
433433
return &cI;
434434
default:
435435
printf("Invalid character!\n");
436-
exit(1);
436+
abort();
437437
}
438438
return 0;
439439
}
@@ -442,16 +442,18 @@ Expr *parse_expr_top(FILE* f) {
442442
Expr *e = parse_expr(f);
443443
if (fgetc(f) != '\n') {
444444
fprintf(stderr, "input program missing trailing newline\n");
445-
exit(1);
445+
abort();
446446
}
447447
return e;
448448
}
449449

450450
static Expr *car(Expr *list) {
451+
check(1);
451452
return partial_apply(list, &cK);
452453
}
453454

454455
static Expr *cdr(Expr *list) {
456+
check(1);
455457
return partial_apply(list, &KI);
456458
}
457459

@@ -462,7 +464,7 @@ static int church2int(Expr *church) {
462464
int result = to_number(partial_eval(e));
463465
if (result == -1) {
464466
fputs("Runtime error: invalid output format (result was not a number)\n", stderr);
465-
exit(3);
467+
abort();
466468
}
467469
*church2int_root = 0;
468470
return result;
@@ -480,7 +482,7 @@ int main(int argc, char** argv) {
480482
f = fopen(argv[1], "r");
481483
if (!f) {
482484
fprintf(stderr, "Unable to open the file \"%s\".\n", argv[1]);
483-
exit(1);
485+
return 1;
484486
}
485487
}
486488
Expr *e = parse_expr_top(f);

lazy3.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,12 @@ Expr *parse_expr_top(state *s) {
517517
}
518518

519519
Expr *car(state *s, Expr *list) {
520+
check(s, 1);
520521
return partial_apply(s, list, s->cK);
521522
}
522523

523524
Expr *cdr(state *s, Expr *list) {
525+
check(s, 1);
524526
return partial_apply(s, list, s->KI);
525527
}
526528

@@ -566,7 +568,7 @@ int main(int argc, char** argv) {
566568
return ch-256;
567569
}
568570
putchar(ch);
569-
fflush(stdout);
571+
570572
s->roots[0] = cdr(s, s->roots[0]);
571573
}
572574
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy