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
7#define CDR(x) ( (x)->cdr.o )7#define CDR(x) ( (x)->cdr.o )
8#define TAG(x) ( (x)->car.c )8#define TAG(x) ( (x)->car.c )
9#define ISNIL(x) ( (x) == &Nil )9#define ISNIL(x) ( (x) == &Nil )
10#define ISTRUE(x) ( (x) == &True )
10#define TYPE(x) ( TAG(x) & 0x1 ? TAG(x) >> 2 : AA_TPAIR )11#define TYPE(x) ( TAG(x) & 0x1 ? TAG(x) >> 2 : AA_TPAIR )
11#define SETTYPE(x,t) ( TAG(x) = (t) << 2 | 1 )12#define SETTYPE(x,t) ( TAG(x) = (t) << 2 | 1 )
12#define NUMBER(x) ( (x)->cdr.n )13#define NUMBER(x) ( (x)->cdr.n )
@@ -51,12 +52,14 @@ static const char *primnames[P_MAX] = { · 38 unmodified lines
51 "<=", "+", "-", "*", "/",52 "<=", "+", "-", "*", "/",
52};53};
53 54
54static const char *typenames[] = {55static const char *typenames[AA_TMAX] = {
55 "pair", "free", "nil", "number", "symbol", "string",56 "pair", "free", "nil", "number", "symbol", "string",
56 "func", "macro", "prim", "cfunc", "ptr"57 "func", "macro", "prim", "cfunc", "ptr", "bool",
57};58};
58 59
59aaObject Nil = {{ (void*)(AA_TNIL << 2 | 1) }, { nil }};60aaObject Nil = {{ (void*)(AA_TNIL << 2 | 1) }, { nil }};
61aaObject False = {{ (void*)(AA_TBOOL << 2 | 1) }, { nil }};
62aaObject True = {{ (void*)(AA_TBOOL << 2 | 1) }, { nil }};
60static aaObject rparen;63static aaObject rparen;
61 64
62static aaObject *eval(aaContext *ctx, aaObject *obj, aaObject *env, aaObject **bind);65static 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
295aaObject *298aaObject *
296aa_bool(aaContext *ctx, int b)299aa_bool(aaContext *ctx, int b)
297{300{
298 return b ? ctx->t : &Nil;301 USED(ctx);
302 return b ? &True : &False;
299}303}
300 304
301aaObject *305aaObject *
@@ -463,6 +467,10 @@ aa_write(aaContext *ctx, aaObject *obj, aaWriteFn fn, void *udata, int qt) · 161 unmodified lines
463 fn(ctx, udata, '"');467 fn(ctx, udata, '"');
464 break;468 break;
465 469
470 case AA_TBOOL:
471 writestr(ctx, fn, udata, ISTRUE(obj) ? "#t" : "#f");
472 break;
473
466 default:474 default:
467 sprint(buf, "[%s %p]", typenames[TYPE(obj)], (void*)obj);475 sprint(buf, "[%s %p]", typenames[TYPE(obj)], (void*)obj);
468 writestr(ctx, fn, udata, buf);476 writestr(ctx, fn, udata, buf);
@@ -814,7 +822,8 @@ eval(aaContext *ctx, aaObject *obj, aaObject *env, aaObject **newenv) · 345 unmodified lines
814 break;822 break;
815 823
816 case P_NOT:824 case P_NOT:
817 res = aa_bool(ctx, ISNIL(evalarg()));825 va = checktype(ctx, evalarg(), AA_TBOOL);
826 res = aa_bool(ctx, !ISTRUE(va));
818 break;827 break;
819 828
820 case P_EQ:829 case P_EQ:
@@ -913,10 +922,6 @@ aa_open(void *ptr, int size, int flags) · 92 unmodified lines
913 ctx->freelist = v;922 ctx->freelist = v;
914 }923 }
915 924
916 /* init objects */
917 ctx->t = aa_symbol(ctx, "t");
918 aa_set(ctx, ctx->t, ctx->t);
919
920 /* register built in primitives */925 /* register built in primitives */
921 save = aa_savegc(ctx);926 save = aa_savegc(ctx);
922 for(i = 0; i < P_MAX; i++){927 for(i = 0; i < P_MAX; i++){
@@ -926,6 +931,8 @@ aa_open(void *ptr, int size, int flags) · 3 unmodified lines
926 aa_set(ctx, aa_symbol(ctx, primnames[i]), v);931 aa_set(ctx, aa_symbol(ctx, primnames[i]), v);
927 aa_restoregc(ctx, save);932 aa_restoregc(ctx, save);
928 }933 }
934 aa_set(ctx, aa_symbol(ctx, "#t"), &True);
935 aa_set(ctx, aa_symbol(ctx, "#f"), &False);
929 936
930 if((flags & AA_NOSTDIO) == 0){937 if((flags & AA_NOSTDIO) == 0){
931 Binit(&ctx->fd[0], 0, OREAD);938 Binit(&ctx->fd[0], 0, OREAD);
aa.h+5−0
@@ -58,11 +58,16 @@ enum { · 57 unmodified lines
58 AA_TPRIM,58 AA_TPRIM,
59 AA_TCFUNC,59 AA_TCFUNC,
60 AA_TPTR,60 AA_TPTR,
61 AA_TBOOL,
62
63 AA_TMAX,
61 64
62 AA_NOSTDIO = 1<<0,65 AA_NOSTDIO = 1<<0,
63};66};
64 67
65extern aaObject Nil;68extern aaObject Nil;
69extern aaObject False;
70extern aaObject True;
66 71
67aaContext *aa_open(void *ptr, int size, int flags);72aaContext *aa_open(void *ptr, int size, int flags);
68void aa_close(aaContext *ctx);73void 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