History

#t and #f as booleans

Sigrid Haflínudóttir committed 6 years ago

Commit d5ad0421 parent e4c2fc82 files · +20 −8Browse files

Unified · Split

2 files
  1. aa.c +15 −8
  2. aa.h +5 −0
aa.c+15−8
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 #define CDR(x) ( (x)->cdr.o )
88 #define TAG(x) ( (x)->car.c )
99 #define ISNIL(x) ( (x) == &Nil )
10+#define ISTRUE(x) ( (x) == &True )
1011 #define TYPE(x) ( TAG(x) & 0x1 ? TAG(x) >> 2 : AA_TPAIR )
1112 #define SETTYPE(x,t) ( TAG(x) = (t) << 2 | 1 )
1213 #define NUMBER(x) ( (x)->cdr.n )
@@ -51,12 +52,14 @@ static const char *primnames[P_MAX] = { · 38 unmodified lines
5152 "<=", "+", "-", "*", "/",
5253 };
5354
54−static const char *typenames[] = {
55+static const char *typenames[AA_TMAX] = {
5556 "pair", "free", "nil", "number", "symbol", "string",
56− "func", "macro", "prim", "cfunc", "ptr"
57+ "func", "macro", "prim", "cfunc", "ptr", "bool",
5758 };
5859
5960 aaObject Nil = {{ (void*)(AA_TNIL << 2 | 1) }, { nil }};
61+aaObject False = {{ (void*)(AA_TBOOL << 2 | 1) }, { nil }};
62+aaObject True = {{ (void*)(AA_TBOOL << 2 | 1) }, { nil }};
6063 static aaObject rparen;
6164
6265 static aaObject *eval(aaContext *ctx, aaObject *obj, aaObject *env, aaObject **bind);
@@ -295,7 +298,8 @@ aa_cons(aaContext *ctx, aaObject *car, aaObject *cdr) · 232 unmodified lines
295298 aaObject *
296299 aa_bool(aaContext *ctx, int b)
297300 {
298− return b ? ctx->t : &Nil;
301+ USED(ctx);
302+ return b ? &True : &False;
299303 }
300304
301305 aaObject *
@@ -463,6 +467,10 @@ aa_write(aaContext *ctx, aaObject *obj, aaWriteFn fn, void *udata, int qt) · 161 unmodified lines
463467 fn(ctx, udata, '"');
464468 break;
465469
470+ case AA_TBOOL:
471+ writestr(ctx, fn, udata, ISTRUE(obj) ? "#t" : "#f");
472+ break;
473+
466474 default:
467475 sprint(buf, "[%s %p]", typenames[TYPE(obj)], (void*)obj);
468476 writestr(ctx, fn, udata, buf);
@@ -814,7 +822,8 @@ eval(aaContext *ctx, aaObject *obj, aaObject *env, aaObject **newenv) · 345 unmodified lines
814822 break;
815823
816824 case P_NOT:
817− res = aa_bool(ctx, ISNIL(evalarg()));
825+ va = checktype(ctx, evalarg(), AA_TBOOL);
826+ res = aa_bool(ctx, !ISTRUE(va));
818827 break;
819828
820829 case P_EQ:
@@ -913,10 +922,6 @@ aa_open(void *ptr, int size, int flags) · 92 unmodified lines
913922 ctx->freelist = v;
914923 }
915924
916− /* init objects */
917− ctx->t = aa_symbol(ctx, "t");
918− aa_set(ctx, ctx->t, ctx->t);
919−
920925 /* register built in primitives */
921926 save = aa_savegc(ctx);
922927 for(i = 0; i < P_MAX; i++){
@@ -926,6 +931,8 @@ aa_open(void *ptr, int size, int flags) · 3 unmodified lines
926931 aa_set(ctx, aa_symbol(ctx, primnames[i]), v);
927932 aa_restoregc(ctx, save);
928933 }
934+ aa_set(ctx, aa_symbol(ctx, "#t"), &True);
935+ aa_set(ctx, aa_symbol(ctx, "#f"), &False);
929936
930937 if((flags & AA_NOSTDIO) == 0){
931938 Binit(&ctx->fd[0], 0, OREAD);
aa.h+5−0
@@ -58,11 +58,16 @@ enum { · 57 unmodified lines
5858 AA_TPRIM,
5959 AA_TCFUNC,
6060 AA_TPTR,
61+ AA_TBOOL,
62+
63+ AA_TMAX,
6164
6265 AA_NOSTDIO = 1<<0,
6366 };
6467
6568 extern aaObject Nil;
69+extern aaObject False;
70+extern aaObject True;
6671
6772 aaContext *aa_open(void *ptr, int size, int flags);
6873 void aa_close(aaContext *ctx);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t