Changeset 11789 for lang/c

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

fix bug on 'find-free' and 'find-sets', lambda's syntax for compound expression.

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

Legend:

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

    r11756 r11789  
    7878                } else if (eq(op, intern("lambda"))) { 
    7979                        SExp vars = cadr(x); 
    80                         SExp body = caddr(x); 
    81                         return find_sets(body, set_minus(v, vars)); 
     80                        SExp body = cddr(x); 
     81                        SExp sets = set_minus(v, vars); 
     82                        for (SExp p = body; !nilp(p); p = cdr(p)) { 
     83                                SExp x = car(p); 
     84                                sets = find_sets(x, sets); 
     85                        } 
     86                        return sets; 
    8287                } else if (eq(op, intern("if"))) { 
    8388                        SExp test = cadr(x); 
     
    190195                } else if (eq(op, intern("lambda"))) { 
    191196                        SExp vars = cadr(x); 
    192                         SExp body = caddr(x); 
    193                         return find_free(body, set_union(vars, b)); 
     197                        SExp body = cddr(x); 
     198                        SExp free = set_union(vars, b); 
     199                        for (SExp p = body; !nilp(p); p = cdr(p)) { 
     200                                SExp x = car(p); 
     201                                free = find_free(x, free); 
     202                        } 
     203                        return free; 
    194204                } else if (eq(op, intern("if"))) { 
    195205                        SExp test = cadr(x); 
     
    307317} 
    308318 
    309 static SExp compile_macroexpand_1(SExp m, SExp args) { 
     319static SExp transform_macro(SExp m, SExp args) { 
    310320        Procedure* p = (Procedure*)m.ptr; 
    311321        if (p->get_func_type() == Procedure::Builtin) { 
    312322                return p->u.mfunc(args); 
    313323        } else { 
    314                 return gen_macro_call(p, args); 
    315         } 
    316 } 
    317  
    318 static SExp transform_macro(SExp m, SExp args) { 
    319         return compile_macroexpand_1(m, args); 
     324                return evaluate(gen_macro_call(p, args)); 
     325        } 
    320326} 
    321327 
     
    415421                        SExp xx = cadr(x); 
    416422                        SExp apply = list(2, APPLY, int2s(1)); 
    417                         SExp nx = is_tail ? list(3, SHIFT, 1, apply) : apply; 
     423                        SExp nx = is_tail ? list(3, SHIFT, int2s(1), apply) : apply; 
    418424                        SExp c = list(2, CONTI, list(2, ARGUMENT, compile(xx, e, s, nx))); 
    419425                        if (is_tail) 
  • lang/c/misc/mlisp/core/m_basic.cpp

    r11756 r11789  
    6060        } 
    6161        return ls; 
     62} 
     63 
     64static SExp builtin_append() { 
     65        sint n = get_arg_num(); 
     66        if (n == 0) { 
     67                return nil; 
     68        } else { 
     69                SExp ls = get_arg(n - 1); 
     70                for (int i=n-1; --i>=0; ) { 
     71                        ls = append2(get_arg(i), ls); 
     72                } 
     73                return ls; 
     74        } 
    6275} 
    6376 
     
    205218//============================================================================= 
    206219 
     220static SExp transform_quasiquote_loop(SExp x) { 
     221        if (!consp(x)) 
     222                return list(2, intern("quote"), list(1, x)); 
     223        else if (eq(car(x), intern("unquote"))) 
     224                return list(2, intern("list"), cadr(x)); 
     225        else if (eq(car(x), intern("unquote-splicing"))) 
     226                return cadr(x); 
     227        else { 
     228                SExp sub = mapcar(transform_quasiquote_loop, x); 
     229                return list(2, intern("list"), cons(intern("append"), sub)); 
     230        } 
     231} 
     232 
     233static SExp builtin_quasiquote(SExp args) { 
     234        SExp res = transform_quasiquote_loop(car(args)); 
     235        SExp h = car(res); 
     236        if (eq(h, intern("list"))) 
     237                return cadr(res); 
     238        else if (eq(h, intern("quote"))) 
     239                return list(2, intern("quote"), list(1, car(cadr(res)))); 
     240        else { 
     241                error(ERR_UNEXPECTED); 
     242                return nil; 
     243        } 
     244} 
     245 
     246 
     247//============================================================================= 
     248 
     249static void define_basic_const() { 
     250        struct { 
     251                const char* name; 
     252        } static const tbl[] = { 
     253                "t", 
     254                "nil", 
     255        }; 
     256        for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
     257                SExp sym = intern(tbl[i].name); 
     258                define_global(sym, sym); 
     259        } 
     260} 
     261 
     262 
    207263static void define_basic_proc() { 
    208264        struct { 
     
    220276                {       "set-cdr!",     builtin_rplacd,         2,      2,      }, 
    221277                {       "list",         builtin_list,           0,      -1,     }, 
     278                {       "append",       builtin_append,         0,      -1,     }, 
    222279 
    223280                {       "+",            builtin_plus,           0,      -1,     }, 
     
    246303 
    247304 
    248 static void define_basic_const() { 
     305static void define_basic_macro() { 
    249306        struct { 
    250307                const char* name; 
     308                SCFuncM func; 
     309                int minarg; 
     310                int maxarg; 
    251311        } static const tbl[] = { 
    252                 "t", 
    253                 "nil", 
     312                {       "quasiquote",   builtin_quasiquote,             1,      1,      }, 
    254313        }; 
    255314        for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
    256315                SExp sym = intern(tbl[i].name); 
    257                 define_global(sym, sym); 
     316                SExp fn = gen_cmacro(tbl[i].func, tbl[i].minarg, tbl[i].maxarg); 
     317                define_global(sym, fn); 
    258318        } 
    259319} 
     
    263323 
    264324void define_basic_lib(void) { 
     325        define_basic_const(); 
    265326        define_basic_proc(); 
    266         define_basic_const(); 
    267 } 
     327        define_basic_macro(); 
     328} 
  • lang/c/misc/mlisp/core/mlisp.cpp

    r11756 r11789  
    2525SExp evaluate(SExp code) { 
    2626        SExp c = compile_ontop(code); 
    27 print(c); 
     27//print(c); 
    2828        return run(c); 
    2929} 
     
    3737                return nil; 
    3838        } else { 
    39                 SExp res; 
    4039                for (;;) { 
    4140                        SExp sexp = read_from_file(fp); 
    4241                        if (nilp(sexp)) break;          // @todo: EOF�̈��� 
    43                         res = run(compile_ontop(sexp)); 
     42                        run(compile_ontop(sexp)); 
    4443                } 
    4544                fclose(fp); 
    46                 return res; 
     45                return t; 
    4746        } 
    4847} 
  • lang/c/misc/mlisp/core/v_vm.cpp

    r11756 r11789  
    197197 
    198198static int restore_stack(SExp v) { 
     199        clear_stack(); 
    199200        int s = vector_length(v); 
    200         clear_stack(); 
    201         for (int i=0; i<s; ) { 
     201        for (int i=0; i<s; ++i) { 
    202202                push(vector_ref(v, i)); 
    203203        } 
     
    257257                        case Procedure::Cell: 
    258258                                { 
    259                                         modify_args(proc, n, g_sp); 
     259                                        modify_args(proc, n, ss - 1); 
    260260                                        *px = closure_body(fn); *pf = ss;       *pc = fn; 
    261261                                } 
     
    318318                SExp op = car(x); 
    319319 
    320                 dump_stack(g_sp); 
    321                 print(op); 
     320//              dump_stack(g_sp); 
     321//              print(op); 
    322322 
    323323                if (op == HALT) { 
     
    396396 
    397397                        restore_stack(stack); 
    398                         push(int2s(0)); 
    399398                        x = xx; 
    400399                } else if (op == DEFINE) { 
  • lang/c/misc/mlisp/proto/stackbased.scm

    r11756 r11789  
    8282        (map (lambda (s) (list 'quote s)) args)))  ; 引数を全てクォートでくくる 
    8383 
    84 (define (compile-macroexpand-1 m args) 
     84(define (transform-macro m args) 
    8585  (let ((body (slot-ref m 'body))) 
    8686    (if (procedure? body) 
    8787        (apply body args) 
    88       (gen-macro-call m args)))) 
    89  
    90 (define (transform-macro m args) 
    91   (evaluate (compile-macroexpand-1 m args))) 
     88      (evaluate (gen-macro-call m args))))) 
    9289 
    9390 
     
    247244      (record-case x 
    248245                   (quote (obj) '()) 
    249                    (lambda (vars body) 
    250                      (find-free body (set-union vars b))) 
     246                   (lambda (vars) 
     247                     (let ((body (cddr x))) 
     248                       (fold (lambda (x r) (find-free x r)) 
     249                             (set-union vars b) 
     250                             body))) 
    251251                   (if (test then else) 
    252252                       (set-union (find-free test b) 
     
    272272      (record-case x 
    273273                   (quote (obj) '()) 
    274                    (lambda (vars body) 
    275                      (find-sets body (set-minus v vars))) 
     274                   (lambda (vars) 
     275                     (let ((body (cddr x))) 
     276                       (fold (lambda (x r) (find-sets x r)) 
     277                             (set-minus v vars) 
     278                             body))) 
    276279                   (if (test then else) 
    277280                       (set-union (find-sets test v) 
     
    314317(define *sp* 0) 
    315318 
     319(define (clear-stack) 
     320  (set! *sp* 0)) 
     321 
    316322(define (init-stack) 
    317323  (set! *stack* (make-vector 1000)) 
    318   (set! *sp* 0)) 
     324  (clear-stack)) 
    319325 
    320326(define push 
     
    379385                        (begin 
    380386                          (restore-stack stack) 
    381                           (push 0) 
    382387                          (VM a x f c))) 
    383388                 (FRAME (x ret) 
     
    406411                                        (VM res ret f c)) 
    407412                                    (begin 
    408                                       (modify-args a n *sp*) 
     413                                      (modify-args a n (- ss 1)) 
    409414                                      (VM a body ss a)))) 
    410415                              (error "wrong number of argument")) 
     
    449454  (lambda (s) 
    450455    (closure 
    451      '(cc) 
     456     '(1 . 1) 
    452457     (list 'REFER-LOCAL 0 (list 'NUATE (save-stack s) '(RETURN))) 
    453458     0 
     
    465470(define restore-stack 
    466471  (lambda (v) 
     472    (clear-stack) 
    467473    (let ((s (vector-length v))) 
    468474      (recur copy ((i 0)) 
    469475             (unless (= i s) 
    470                (vector-set! *stack* i (vector-ref v i)) 
     476               (push (vector-ref v i)) 
    471477               (copy (+ i 1)))) 
    472478      s))) 
     
    707713                     'proc 2 2 
    708714                     (lambda () (< (get-arg 0) (get-arg 1))))) 
     715              (cons '> 
     716                    (gen-builtin 
     717                     'proc 2 2 
     718                     (lambda () (> (get-arg 0) (get-arg 1))))) 
     719              (cons '= 
     720                    (gen-builtin 
     721                     'proc 2 2 
     722                     (lambda () (= (get-arg 0) (get-arg 1))))) 
    709723              (cons 'read 
    710724                    (gen-builtin 
     
    727741                     'proc 1 1 
    728742                     (lambda () (load-file (get-arg 0))))) 
     743              (cons 'macroexpand-1 
     744                    (gen-builtin 
     745                     'proc 1 1 
     746                     (lambda () 
     747                       (let ((sexp (get-arg 0))) 
     748                         (if (pair? sexp) 
     749                             (let* ((fn (car sexp)) 
     750                                    (m (and (exist-global? fn) 
     751                                            (refer-global fn)))) 
     752                               (if (macro? m) 
     753                                   (transform-macro m (cdr sexp)) 
     754                                 sexp)) 
     755                           sexp))))) 
    729756               
    730757               
     
    778805    (let ((srcfn (cadr args))) 
    779806      (load-file srcfn)))) 
     807 
     808 
     809 
     810#| 
     811(load "./stackbased.scm") 
     812(repl) 
     813 
     814 
     815 
     816(load "test_macro.scm") 
     817 
     818 
     819 
     820 
     821(macroexpand-1 '(let ((x 1) (y 2)) (+ x y))) 
     822 
     823 
     824 
     825(defmacro test (a . b) (list 'list a b)) 
     826 
     827 
     828(test 1 2) 
     829 
     830 
     831 
     832(evaluate '((lambda (a b . c) (list a b c)) 1 2 3 4)) 
     833 
     834 
     835 
     836 
     837(compile-top '((lambda (a . b) (list 'list a b)) '1 '2)) 
     838 
     839 
     840(let ((x 1) (y 2)) (cons x y)) 
     841 
     842 
     843 
     844 
     845 
     846 
     847 
     848 
     849|#