Changeset 12180 for lang/c

Show
Ignore:
Timestamp:
05/22/08 08:41:22 (6 months ago)
Author:
mokehehe
Message:

Expand all macro at begining of compilation.
Add 'macroexpand-1'.

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

Legend:

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

    r12093 r12180  
    310310} 
    311311 
    312 static SExp quote(SExp s)       { return list(2, intern("quote"), s); } 
     312static SExp quote(SExp s, void*)        { return list(2, intern("quote"), s); } 
    313313 
    314314static SExp gen_macro_call(Procedure* p, SExp args) { 
    315         SExp qargs = mapcar(quote, args); 
     315        SExp qargs = mapcar(quote, args, NULL); 
    316316        return cons(p->u.cell.s, qargs); 
    317317} 
     
    323323        } else { 
    324324                return evaluate(gen_macro_call(p, args)); 
     325        } 
     326} 
     327 
     328 
     329/// �}�N����ēW�J 
     330static SExp compile_macroexpand_all_compound(int n, SExp x, SExp e); 
     331static SExp compile_macroexpand_all(SExp x, SExp e) { 
     332        if (consp(x)) { 
     333                SExp op = car(x); 
     334                if (eq(op, intern("quote"))) { 
     335                        return x; 
     336                } else if (eq(op, intern("lambda"))) { 
     337                        SExp vars = cadr(x); 
     338                        SExp newe = append2(dotted2proper(vars), e); 
     339                        return compile_macroexpand_all_compound(2, x, newe); 
     340                } else if (eq(op, intern("if"))) { 
     341                        return compile_macroexpand_all_compound(1, x, e); 
     342                } else if (eq(op, intern("set!"))) { 
     343                        return compile_macroexpand_all_compound(2, x, e); 
     344                } else if (eq(op, intern("call/cc"))) { 
     345                        return compile_macroexpand_all_compound(1, x, e); 
     346                } else if (eq(op, intern("define"))) { 
     347                        SExp var_body = cdr(x); 
     348                        for (;;) { 
     349                                SExp var = car(var_body); 
     350                                if (consp(var)) { 
     351                                        SExp body = cdr(var_body); 
     352                                        var_body = list(2, car(var), cons(intern("lambda"), cons(cdr(var), body))); 
     353                                } else { 
     354                                        return compile_macroexpand_all_compound(2, cons(intern("define"), var_body), e); 
     355                                } 
     356                        } 
     357                } else if (eq(op, intern("defmacro"))) { 
     358                        return x; 
     359                } else { 
     360                        SExp fn = car(x); 
     361                        SExp args = cdr(x); 
     362                        SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 
     363                        if (macrop(gval)) { 
     364                                return compile_macroexpand_all(transform_macro(gval, args), e); 
     365                        } else { 
     366                                return compile_macroexpand_all_compound(0, x, e); 
     367                        } 
     368                } 
     369        } else { 
     370                return x; 
     371        } 
     372} 
     373 
     374static SExp compile_macroexpand_all_compound(int n, SExp x, SExp e) { 
     375        if (n > 0) { 
     376                return cons(car(x), compile_macroexpand_all_compound(n-1, cdr(x), e)); 
     377        } else { 
     378                struct Local { 
     379                        SExp e; 
     380                        static SExp cb(SExp x, void* param) { 
     381                                Local* l = (Local*)param; 
     382                                return compile_macroexpand_all(x, l->e); 
     383                        } 
     384                }; 
     385                Local l = { e }; 
     386                return mapcar(Local::cb, x, &l); 
    325387        } 
    326388} 
     
    359421} 
    360422 
     423// �}�N����i�W�J 
     424SExp macroexpand_1(SExp x) { 
     425        SExp fn = car(x); 
     426        SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 
     427        if (macrop(gval)) { 
     428                SExp args = cdr(x); 
     429                return transform_macro(gval, args); 
     430        } else { 
     431                return x; 
     432        } 
     433} 
     434 
    361435// �K�p����p�C�� 
    362436static SExp compile_apply(SExp x, SExp e, SExp s, SExp next) { 
    363         SExp fn = car(x); 
    364         SExp args = cdr(x); 
    365         SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 
    366         if (macrop(gval)) { 
    367                 return compile(transform_macro(gval, args), e, s, next); 
    368         } else { 
     437        SExp expanded = macroexpand_1(x); 
     438        if (!eq(expanded, x)) { 
     439                return compile(expanded, e, s, next); 
     440        } else { 
     441                SExp args = cdr(x); 
    369442                int argnum = length(args); 
    370443                SExp apply = list(2, APPLY, int2s(argnum)); 
     
    445518        return nil; 
    446519} 
     520 
     521 
     522/// �g�b�v���x���ŃR���p�C�� 
     523SExp compile_ontop(SExp code) { 
     524        SExp halt_code = cons(HALT, nil); 
     525        SExp expanded = compile_macroexpand_all(code, nil); 
     526        return compile(expanded, list(1, nil), nil, halt_code); 
     527} 
  • lang/c/misc/mlisp/core/c_compiler.h

    r11550 r12180  
    1515 
    1616SExp is_macro(SExp name); 
    17 SExp macroexpand_1(SExp m, SExp x); 
    1817 
    1918#ifdef __cplusplus 
  • lang/c/misc/mlisp/core/inner.h

    r11756 r12180  
    111111sint get_arg_num(void); 
    112112SExp get_arg(int idx); 
     113 
     114SExp macroexpand_1(SExp x); 
  • lang/c/misc/mlisp/core/m_basic.cpp

    r12094 r12180  
    210210} 
    211211 
     212static SExp builtin_macroexpand_1() { 
     213        SExp a = get_arg(0); 
     214        return macroexpand_1(a); 
     215} 
     216 
    212217static SExp builtin_load() { 
    213218        SExp a = get_arg(0); 
     
    244249//============================================================================= 
    245250 
    246 static SExp transform_quasiquote_loop(SExp x) { 
     251static SExp transform_quasiquote_loop(SExp x, void*) { 
    247252        if (!consp(x)) 
    248253                return list(2, intern("quote"), list(1, x)); 
     
    252257                return cadr(x); 
    253258        else { 
    254                 SExp sub = mapcar(transform_quasiquote_loop, x); 
     259                SExp sub = mapcar(transform_quasiquote_loop, x, NULL); 
    255260                return list(2, intern("list"), cons(intern("append"), sub)); 
    256261        } 
     
    258263 
    259264static SExp builtin_quasiquote(SExp args) { 
    260         SExp res = transform_quasiquote_loop(car(args)); 
     265        SExp res = transform_quasiquote_loop(car(args), NULL); 
    261266        SExp h = car(res); 
    262267        if (eq(h, intern("list"))) 
     
    321326                {       "print",        builtin_print,          1,      1,      }, 
    322327                {       "eval",         builtin_eval,           1,      1,      }, 
     328                {       "macroexpand-1",        builtin_macroexpand_1,          1,      1,      }, 
    323329 
    324330                {       "load",         builtin_load,           1,      1,      }, 
  • lang/c/misc/mlisp/core/mlisp.cpp

    r11789 r12180  
    1414SExp run(SExp c) { 
    1515        return vm(nil, c, 0, nil); 
    16 } 
    17  
    18 /// �g�b�v���x���ŃR���p�C�� 
    19 SExp compile_ontop(SExp code) { 
    20         SExp halt_code = cons(HALT, nil); 
    21         return compile(code, list(1, nil), nil, halt_code); 
    2216} 
    2317 
  • lang/c/misc/mlisp/core/s_util.cpp

    r11715 r12180  
    6363} 
    6464 
    65 SExp mapcar(SExp (*fn)(SExp), SExp ls) { 
     65SExp mapcar(SExp (*fn)(SExp, void*), SExp ls, void* param) { 
    6666        SExp acc = nil; 
    6767        for (; !nilp(ls); ls = cdr(ls)) { 
    68                 acc = cons(fn(car(ls)), acc); 
     68                acc = cons(fn(car(ls), param), acc); 
    6969        } 
    7070        return nreverse(acc); 
  • lang/c/misc/mlisp/core/s_util.h

    r12094 r12180  
    3030 
    3131/// ���X�g�̊e�v�f�Ɋ֐���p�i���X�g�͂P�Œ�� 
    32 SExp mapcar(SExp (*fn)(SExp), SExp ls); 
     32SExp mapcar(SExp (*fn)(SExp, void*), SExp ls, void* param); 
    3333 
    3434/// alist ((key . value) ...) ������ 
  • lang/c/misc/mlisp/proto/stackbased.scm

    r12093 r12180  
    6262;; マクロ 
    6363 
    64  
    6564(define (compile-defmacro name vars body) 
    6665  (define-global name (gen-macro vars body))) 
     
    8988 
    9089 
     90;; マクロの全展開、文法に従って修正する必要あり 
     91(define (compile-macroexpand-all x e) 
     92  ; ドット対も処理する map 
     93  (define (mapd fn ls) 
     94    (cond ((pair? ls) (cons (fn (car ls)) (mapd fn (cdr ls)))) 
     95          ((null? ls) ls) 
     96          (else (fn ls)))) 
     97  (define (expand-compound n x e) 
     98    (cond ((null? x) x) 
     99          ; 頭の n 個は展開せず、 
     100          ((> n 0) (cons (car x) 
     101                         (expand-compound (-1+ n) (cdr x) e))) 
     102          ; n 個以降は展開 
     103          (else (mapd (lambda (s) (compile-macroexpand-all s e)) x)))) 
     104  (cond 
     105   ((pair? x) 
     106    (case x 
     107      ((quote) x) 
     108      ((lambda) (let* ((vars (cadr x)) 
     109                       (newe (append (dotted->proper vars) e))) 
     110                  (expand-compound 2 x newe))) 
     111      ((if) (expand-compound 1 x e)) 
     112      ((set!) (expand-compound 2 x e)) 
     113      ((call/cc) (expand-compound 1 x e)) 
     114      ((define) (recur loop ((var-body (cdr x))) 
     115                       (let ((var (car var-body)) 
     116                             (body (cdr var-body))) 
     117                         (if (pair? var) 
     118                             (loop `(,(car var) (lambda ,(cdr var) ,@body))) 
     119                           (expand-compound 2 `(define ,@var-body) e))))) 
     120      ((defmacro) x) 
     121      (else 
     122       (let* ((fn (car x)) 
     123              (args (cdr x)) 
     124              (gval (and (symbol? fn) 
     125                         (exist-global? fn) 
     126                         (refer-global fn)))) 
     127         (if (macro? gval) 
     128             (compile-macroexpand-all (transform-macro gval args) e) 
     129           (expand-compound 0 x e)))))) 
     130   (else x))) 
     131 
     132 
    91133;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
    92134;; compiler 
     
    94136;; トップレベルでコンパイル 
    95137(define (compile-top x) 
    96   (compile x '(()) '() '(HALT))) 
     138  (compile (compile-macroexpand-all x '()) 
     139           '(()) 
     140           '() 
     141           '(HALT))) 
    97142 
    98143;; 複文をコンパイル 
     
    355400(define VM 
    356401  (lambda (a x f c) 
    357     (write/ss (list (car x) (dump-stack *sp*))) 
    358     (newline) 
     402;    (write/ss (list (car x) (dump-stack *sp*))) 
     403;    (newline) 
    359404    (record-case x 
    360405                 (HALT () a) 
     
    609654 
    610655 
     656 
     657 
     658 
     659 
     660 
    611661(define (dump-stack s) 
    612662  (subseq *stack* 0 s)) 
  • lang/c/misc/mlisp/readme.txt

    r12094 r12180  
    77 
    88* �m�[�g 
    9 - repl ����quit�v�Ƒł����ނƔ�����- �V���{���̑啶���Ə�������ʂ��� 
     9- repl ����(exit)�v�Ƒł����ނƔ�����- �V���{���̑啶���Ə�������ʂ��� 
    1010- ������ 
    1111-- ���� 
     
    5252- ��s���ɕϐ�������������Ƃ��̎����� 
    5353- �������� 
    54 - �}�N�����g�����Ƃ��́A���R�ϐ���t!�̎g�p�̃`�F�b�N 
     54-[v] �}�N�����g�����Ƃ��́A���R�ϐ���t!�̎g�p�̃`�F�b�N 
    5555 
    5656