Changeset 11750 for lang/c

Show
Ignore:
Timestamp:
05/17/08 17:24:24 (5 years ago)
Author:
mokehehe
Message:

マクロ組込み

Location:
lang/c/misc/mlisp
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • lang/c/misc/mlisp/core/c_compiler.cpp

    r11676 r11750  
    276276 
    277277//============================================================================= 
     278// �}�N�� 
     279 
     280static SExp gen_macro_from_sexp(SExp vars, SExp body) { 
     281        SExp var_min_max = get_var_min_max(vars); 
     282        sint minnarg = s2int(car(var_min_max)); 
     283        sint maxnarg = s2int(cdr(var_min_max)); 
     284        SExp code = cons(intern("lambda"), cons(vars, body)); 
     285        return gen_macro(code, minnarg, maxnarg); 
     286} 
     287 
     288static void compile_defmacro(SExp name, SExp vars, SExp body) { 
     289        define_global(name, gen_macro_from_sexp(vars, body)); 
     290} 
     291 
     292static bool macrop(SExp fn) { 
     293        if (type_of(fn) == tProc) { 
     294                Procedure* p = (Procedure*)fn.ptr; 
     295                if (p->get_proc_type() == Procedure::Macro) { 
     296                        return true; 
     297                } 
     298        } 
     299        return false; 
     300} 
     301 
     302static SExp quote(SExp s)       { return list(2, intern("quote"), s); } 
     303 
     304static SExp gen_macro_call(Procedure* p, SExp args) { 
     305        SExp qargs = mapcar(quote, args); 
     306        return cons(p->u.cell.s, qargs); 
     307} 
     308 
     309static SExp compile_macroexpand_1(SExp m, SExp args) { 
     310        Procedure* p = (Procedure*)m.ptr; 
     311        if (p->get_func_type() == Procedure::Builtin) { 
     312                return p->u.mfunc(args); 
     313        } else { 
     314                return gen_macro_call(p, args); 
     315        } 
     316} 
     317 
     318static SExp transform_macro(SExp m, SExp args) { 
     319        return compile_macroexpand_1(m, args); 
     320} 
     321 
     322//============================================================================= 
    278323 
    279324/// ����void init_compile(void) { 
     
    305350        } else { 
    306351                return compile_pair_loop(cdr(args), compile(car(args), e, s, list(2, ARGUMENT, c)), e, s, next); 
     352        } 
     353} 
     354 
     355// �K�p����p�C�� 
     356static SExp compile_apply(SExp x, SExp e, SExp s, SExp next) { 
     357        SExp fn = car(x); 
     358        SExp args = cdr(x); 
     359        SExp gval = symbolp(fn) ? refer_global(fn) : nil; 
     360        if (macrop(gval)) { 
     361                return compile(transform_macro(gval, args), e, s, next); 
     362        } else { 
     363                int argnum = length(args); 
     364                SExp apply = list(2, APPLY, int2s(argnum)); 
     365                SExp nx = tailp(next) ? list(3, SHIFT, int2s(argnum), apply) : apply; 
     366                SExp c = compile(car(x), e, s, nx); 
     367                return compile_pair_loop(args, c, e, s, next); 
    307368        } 
    308369} 
     
    364425                        SExp body = cddr(x); 
    365426                        return compile_define(var, body, e, s, next); 
    366                 } else { 
    367                         SExp args = cdr(x); 
    368                         int argnum = length(args); 
    369                         SExp apply = list(2, APPLY, int2s(argnum)); 
    370                         SExp nx = tailp(next) ? list(3, SHIFT, int2s(argnum), apply) : apply; 
    371                         SExp c = compile(car(x), e, s, nx); 
    372                         return compile_pair_loop(args, c, e, s, next); 
     427                } else if (eq(op, intern("defmacro"))) { 
     428                        SExp name = cadr(x); 
     429                        SExp vars = caddr(x); 
     430                        SExp body = cdddr(x); 
     431                        compile_defmacro(name, vars, body); 
     432                        return next; 
     433                } else { 
     434                        return compile_apply(x, e, s, next); 
    373435                } 
    374436        } else { 
  • lang/c/misc/mlisp/core/inner.h

    r11715 r11750  
    6969        union { 
    7070                SCFunc cfunc; 
     71                SCFuncM mfunc; 
    7172                struct { 
    7273                        SExp s;                 ///< �� 
     
    7778 
    7879        int get_func_type() const               { return flag & FuncType; } 
     80        int get_proc_type() const               { return flag & ProcType; } 
    7981}; 
    8082 
  • lang/c/misc/mlisp/core/m_basic.cpp

    r11617 r11750  
    5050        rplacd(a, d); 
    5151        return nil; 
     52} 
     53 
     54static SExp builtin_list(int s) { 
     55        sint n = get_arg_num(s); 
     56        SExp ls = nil; 
     57        for (int i=n; --i>=0; ) { 
     58                SExp a = get_arg(s, i); 
     59                ls = cons(a, ls); 
     60        } 
     61        return ls; 
    5262} 
    5363 
     
    199209                const char* name; 
    200210                SCFunc func; 
    201                 int b_macro; 
    202211                int minarg; 
    203212                int maxarg; 
    204213        } static const tbl[] = { 
    205                 {       "cons",         builtin_cons,           FALSE,  2,      2,      }, 
    206                 {       "car",          builtin_car,            FALSE,  1,      1,      }, 
    207                 {       "cdr",          builtin_cdr,            FALSE,  1,      1,      }, 
    208                 {       "pair?",        builtin_consp,          FALSE,  1,      1,      }, 
    209                 {       "eq?",          builtin_eq,                     FALSE,  2,      2,      }, 
    210                 {       "set-car!",     builtin_rplaca,         FALSE,  2,      2,      }, 
    211                 {       "set-cdr!",     builtin_rplacd,         FALSE,  2,      2,      }, 
    212                 {       "+",            builtin_plus,           FALSE,  0,      -1,     }, 
    213                 {       "-",            builtin_difference,     FALSE,  1, -1,  }, 
    214                 {       "*",            builtin_times,          FALSE,  0,      -1,     }, 
    215                 {       "/",            builtin_quotient,       FALSE,  1, -1,  }, 
    216                 {       "=",            builtin_numeq,          FALSE,  1, -1,  }, 
    217                 {       "<",            builtin_lt,                     FALSE,  1, -1,  }, 
    218                 {       ">",            builtin_gt,                     FALSE,  1, -1,  }, 
    219                 {       "<=",           builtin_le,                     FALSE,  1, -1,  }, 
    220                 {       ">=",           builtin_ge,                     FALSE,  1, -1,  }, 
    221  
    222                 {       "read",         builtin_read,           FALSE,  0,      0,      }, 
    223                 {       "print",        builtin_print,          FALSE,  1,      1,      }, 
    224                 {       "eval",         builtin_eval,           FALSE,  1,      1,      }, 
    225  
    226                 {       "load",         builtin_load,           FALSE,  1,      1,      }, 
    227                 {       "compile",      builtin_compile,        FALSE,  1,      1,      }, 
     214                {       "cons",         builtin_cons,           2,      2,      }, 
     215                {       "car",          builtin_car,            1,      1,      }, 
     216                {       "cdr",          builtin_cdr,            1,      1,      }, 
     217                {       "pair?",        builtin_consp,          1,      1,      }, 
     218                {       "eq?",          builtin_eq,                     2,      2,      }, 
     219                {       "set-car!",     builtin_rplaca,         2,      2,      }, 
     220                {       "set-cdr!",     builtin_rplacd,         2,      2,      }, 
     221                {       "list",         builtin_list,           0,      -1,     }, 
     222 
     223                {       "+",            builtin_plus,           0,      -1,     }, 
     224                {       "-",            builtin_difference,     1, -1,  }, 
     225                {       "*",            builtin_times,          0,      -1,     }, 
     226                {       "/",            builtin_quotient,       1, -1,  }, 
     227                {       "=",            builtin_numeq,          1, -1,  }, 
     228                {       "<",            builtin_lt,                     1, -1,  }, 
     229                {       ">",            builtin_gt,                     1, -1,  }, 
     230                {       "<=",           builtin_le,                     1, -1,  }, 
     231                {       ">=",           builtin_ge,                     1, -1,  }, 
     232 
     233                {       "read",         builtin_read,           0,      0,      }, 
     234                {       "print",        builtin_print,          1,      1,      }, 
     235                {       "eval",         builtin_eval,           1,      1,      }, 
     236 
     237                {       "load",         builtin_load,           1,      1,      }, 
     238                {       "compile",      builtin_compile,        1,      1,      }, 
    228239        }; 
    229240        for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
    230241                SExp sym = intern(tbl[i].name); 
    231                 SExp fn = gen_cfunc(tbl[i].func, tbl[i].b_macro, tbl[i].minarg, tbl[i].maxarg); 
     242                SExp fn = gen_cfunc(tbl[i].func, tbl[i].minarg, tbl[i].maxarg); 
    232243                define_global(sym, fn); 
    233244        } 
  • lang/c/misc/mlisp/core/sexp.cpp

    r11676 r11750  
    248248 
    249249 
    250 SExp gen_cfunc(SCFunc cfunc, int b_macro, int minnarg, int maxnarg) { 
     250SExp gen_cfunc(SCFunc cfunc, int minnarg, int maxnarg) { 
     251        const bool b_macro = false; 
    251252        Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 
    252253        p->type = tProc; 
     
    262263} 
    263264 
    264  
    265 SExp gen_closure(SExp body, SExp env, int minarg, int maxarg) { 
    266         bool b_macro = false; 
     265SExp gen_cmacro(SCFuncM mfunc, int minnarg, int maxnarg) { 
     266        const bool b_macro = true; 
     267        Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 
     268        p->type = tProc; 
     269        p->flag = Procedure::Builtin | (b_macro ? Procedure::Macro : Procedure::Lambda); 
     270        p->u.mfunc = mfunc; 
     271        p->minnarg = minnarg; 
     272        p->maxnarg = maxnarg; 
     273 
     274        SExp s; 
     275        s.ptr = (SExpExtU*)p; 
     276 
     277        return s; 
     278} 
     279 
     280 
     281SExp gen_closure(SExp body, SExp env, int minnarg, int maxnarg) { 
     282        const bool b_macro = false; 
    267283        Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 
    268284        p->type = tProc; 
     
    270286        p->u.cell.s = body; 
    271287        p->u.cell.e = env; 
    272         p->minnarg = minarg; 
    273         p->maxnarg = maxarg; 
     288        p->minnarg = minnarg; 
     289        p->maxnarg = maxnarg; 
     290 
     291        SExp s; 
     292        s.ptr = (SExpExtU*)p; 
     293 
     294        return s; 
     295} 
     296 
     297 
     298SExp gen_macro(SExp body, int minnarg, int maxnarg) { 
     299        const bool b_macro = true; 
     300        Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 
     301        p->type = tProc; 
     302        p->flag = Procedure::Cell | (b_macro ? Procedure::Macro : Procedure::Lambda); 
     303        p->u.cell.s = body; 
     304        p->u.cell.e = nil; 
     305        p->minnarg = minnarg; 
     306        p->maxnarg = maxnarg; 
    274307 
    275308        SExp s; 
  • lang/c/misc/mlisp/core/v_vm.cpp

    r11715 r11750  
    212212        int r = 0; 
    213213        if (type_of(fn) == tProc) { 
    214                 int ss = push(argnum, s); 
    215214                int n = s2int(argnum); 
    216215                Procedure* proc = (Procedure*)fn.ptr; 
    217216                if (check_arg_num(proc, n)) { 
     217                        int ss = push(argnum, s); 
    218218                        switch (proc->get_func_type()) { 
    219219                        case Procedure::Builtin: 
     
    238238                } 
    239239        } else { 
    240                 runtime_error("can't call"); 
     240                runtime_error("can't apply"); 
    241241                r = -1; 
    242242        } 
  • lang/c/misc/mlisp/inc/sexp.h

    r11676 r11750  
    4848/// �g���݊֐��̌^ 
    4949typedef SExp (*SCFunc)(int stack); 
     50typedef SExp (*SCFuncM)(SExp args); 
    5051 
    5152 
     
    104105SExp list(int n, ...); 
    105106 
    106 SExp gen_cfunc(SCFunc cfunc, int b_macro, int minnarg, int maxnarg); 
     107SExp gen_cfunc(SCFunc cfunc, int minnarg, int maxnarg); 
     108SExp gen_cmacro(SCFuncM mfunc, int minnarg, int maxnarg); 
    107109 
    108 SExp gen_closure(SExp body, SExp env, int minarg, int maxarg); 
     110SExp gen_closure(SExp body, SExp env, int minnarg, int maxnarg); 
     111 
     112SExp gen_macro(SExp body, int minnarg, int maxnarg); 
    109113 
    110114/// s���𐮐��ɕϊ�