Changeset 12093 for lang/c

Show
Ignore:
Timestamp:
05/21/08 08:27:12 (6 months ago)
Author:
mokehehe
Message:

fix a bug of continuation

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

Legend:

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

    r11789 r12093  
    422422                        SExp apply = list(2, APPLY, int2s(1)); 
    423423                        SExp nx = is_tail ? list(3, SHIFT, int2s(1), apply) : apply; 
    424                         SExp c = list(2, CONTI, list(2, ARGUMENT, compile(xx, e, s, nx))); 
     424                        SExp c = list(3, CONTI, is_tail ? t : nil, list(2, ARGUMENT, compile(xx, e, s, nx))); 
    425425                        if (is_tail) 
    426426                                return c; 
  • lang/c/misc/mlisp/core/m_basic.cpp

    r11789 r12093  
    88#include "s_util.h" 
    99#include "inner.h" 
     10#include <stdlib.h>     // for exit() 
    1011 
    1112 
     
    214215} 
    215216 
     217static SExp builtin_exit() { 
     218        sint n = get_arg_num(); 
     219        int ret = 0; 
     220        if (n >= 1) { 
     221                SExp s = get_arg(0); 
     222                if (type_of(s) == tInt) { 
     223                        ret = s2int(s); 
     224                } else { 
     225                        ret = -1; 
     226                } 
     227        } 
     228        exit(ret); 
     229        return nil; 
     230} 
     231 
    216232 
    217233 
     
    294310                {       "load",         builtin_load,           1,      1,      }, 
    295311                {       "compile",      builtin_compile,        1,      1,      }, 
     312                {       "exit",         builtin_exit,           0,      1,      }, 
    296313        }; 
    297314        for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
  • lang/c/misc/mlisp/core/v_vm.cpp

    r11789 r12093  
    205205} 
    206206 
    207 static SExp continuation(int s) { 
    208         SExp body = list(3, REFER_LOCAL, int2s(0), list(3, NUATE, save_stack(s), list(1, RETURN))); 
     207static SExp continuation(int s, SExp tt) { 
     208        SExp body = list(3, REFER_LOCAL, int2s(0), list(4, NUATE, save_stack(s), tt, list(1, RETURN))); 
    209209        return closure(1, 1, body, 0, 0); 
    210210} 
     
    389389                        x = xx; 
    390390                } else if (op == CONTI) { 
    391                         SExp xx = cadr(x); 
    392                         a = continuation(g_sp); x = xx; 
     391                        SExp tt = cadr(x); 
     392                        SExp xx = caddr(x); 
     393                        a = continuation(g_sp, tt);     x = xx; 
    393394                } else if (op == NUATE) { 
    394395                        SExp stack = cadr(x); 
    395                         SExp xx = caddr(x); 
     396                        SExp tt = caddr(x); 
     397                        SExp xx = cadddr(x); 
    396398 
    397399                        restore_stack(stack); 
     400                        if (nilp(tt))   push(int2s(0)); 
    398401                        x = xx; 
    399402                } else if (op == DEFINE) { 
  • lang/c/misc/mlisp/inc/mlisp.h

    r11756 r12093  
    2323SExp evaluate(SExp code); 
    2424 
    25 // �\�[�X�̓ǂݍ��݁A�]�� 
     25/// �\�[�X�̓ǂݍ��݁A�]�� 
    2626SExp load_eval(const char* fn); 
    2727 
    2828 
     29/// �O���[�o���Ȓl�̒� 
     30void define_global(SExp sym, SExp val); 
    2931 
     32/// �O���[�o���̎Q�� 
     33SExp refer_global(SExp sym); 
    3034 
    31 void define_global(SExp sym, SExp val); 
    32 SExp refer_global(SExp sym); 
     35/// �O���[�o���ɒl�����݂��邩�H 
    3336int exist_global(SExp sym); 
    3437 
     38/// �Ăяo�� 
    3539SExp vm_apply(SExp fn, int narg, ...); 
    3640 
  • lang/c/misc/mlisp/inc/sexp.h

    r11756 r12093  
    103103void reset_error(void); 
    104104 
    105 SExp list(int n, ...); 
    106  
    107105SExp gen_cfunc(SCFunc cfunc, int minnarg, int maxnarg); 
    108106SExp gen_cmacro(SCFuncM mfunc, int minnarg, int maxnarg); 
  • lang/c/misc/mlisp/proto/stackbased.scm

    r11789 r12093  
    184184                                           (compile xx e s (list 'ASSIGN-GLOBAL var next))))) 
    185185                   (call/cc (x) 
    186                             (let ((c `(CONTI 
    187                                        (ARGUMENT 
    188                                         ,(compile x e s 
    189                                                   (if (tail? next) 
    190                                                       (list 'SHIFT 
    191                                                             1 
    192                                                             '(APPLY 1)) 
    193                                                     '(APPLY 1))))))) 
    194                               (if (tail? next) 
     186                            (let* ((t? (tail? next)) 
     187                                   (c `(CONTI 
     188                                        ,t? 
     189                                        (ARGUMENT 
     190                                         ,(compile x e s 
     191                                                   (if t? 
     192                                                       `(SHIFT 
     193                                                         1 
     194                                                         (APPLY 1)) 
     195                                                     '(APPLY 1))))))) 
     196                              (if t? 
    195197                                  c 
    196                                 (list 'FRAME c next)))) 
     198                                `(FRAME ,c ,next)))) 
    197199                   (define (var) 
    198200                     (let ((body (cddr x))) 
     
    353355(define VM 
    354356  (lambda (a x f c) 
    355 ;    (write/ss (list (car x) a (dump-stack *sp*))) 
    356 ;    (newline) 
     357    (write/ss (list (car x) (dump-stack *sp*))) 
     358    (newline) 
    357359    (record-case x 
    358360                 (HALT () a) 
     
    380382                              (set-box! (index-closure c n) a) 
    381383                              (VM a x f c)) 
    382                  (CONTI (x) 
    383                         (VM (continuation *sp*) x f c)) 
    384                  (NUATE (stack x) 
     384                 (CONTI (t? x) 
     385                        (VM (continuation *sp* t?) x f c)) 
     386                 (NUATE (stack t? x) 
    385387                        (begin 
    386388                          (restore-stack stack) 
     389                          (if (not t?) (push 0)) 
    387390                          (VM a x f c))) 
    388391                 (FRAME (x ret) 
     
    452455 
    453456(define continuation 
    454   (lambda (s) 
     457  (lambda (s t?) 
    455458    (closure 
    456459     '(1 . 1) 
    457      (list 'REFER-LOCAL 0 (list 'NUATE (save-stack s) '(RETURN))) 
     460     (list 'REFER-LOCAL 0 (list 'NUATE (save-stack s) t? '(RETURN))) 
    458461     0 
    459462     0))) 
     
    463466    (let ((v (make-vector s))) 
    464467      (recur copy ((i 0)) 
    465              (unless (= i s) 
     468             (unless (>= i s) 
    466469               (vector-set! v i (vector-ref *stack* i)) 
    467470               (copy (+ i 1)))) 
     
    811814(load "./stackbased.scm") 
    812815(repl) 
    813  
     816quit 
    814817 
    815818 
     
    841844 
    842845 
    843  
     846(call/cc (lambda (cc) (cc 1234))) 
     847 
     848 
     8491234 
     850 
     851(load "test_nondet.scm") 
    844852 
    845853 
  • lang/c/misc/mlisp/readme.txt

    r11756 r12093  
    4141                Cygwin �p Rakefile 
    4242 
     43        proto 
     44                Gauche �ɂ��v���g�^�C�v�� 
     45 
    4346 
    4447 
     
    4649-[v] �X�^�b�N�x�[�X�ɒu��������-[v] �C�ӌ‚̈��󂯎� 
    4750-[v] �}�N����� 
    48 --[x] macroexpand ��-[v] C �������[�o���̊֐���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
     51--[v] macroexpand ��-[v] C �������[�o���̊֐���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
    4952- ��s���ɕϐ�������������Ƃ��̎����� 
    5053- �������� 
  • lang/c/misc/mlisp/test/main.cpp

    r11715 r12093  
    66#include <gc/gc.h> 
    77#include <setjmp.h> 
     8 
     9#define MESSAGE         "To quit, type '(exit)'\n" 
     10 
    811 
    912static jmp_buf s_env; 
     
    2932 
    3033void repl() { 
     34        printf("%s", MESSAGE); 
    3135        for (;;) { 
    3236                if (setjmp(s_env) == 0) { 
     
    3438                                printf("> "); 
    3539                                SExp s = read_from_file(stdin); 
    36                                 if (eq(s, intern("quit"))) { 
    37                                         return; 
    38                                 } 
    3940 
    4041                                SExp r = evaluate(s);