Changeset 11715 for lang/c

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

Cからグローバルの関数を呼びだせるようにした

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

Legend:

Unmodified
Added
Removed
  • lang/c/misc/mlisp/Rakefile

    r11617 r11715  
    1111 
    1212 
    13 OBJ_PATH = "objs" 
     13OBJ_PATH = "obj" 
    1414OBJ_EXT = ".o" 
    1515 
  • lang/c/misc/mlisp/core/inner.h

    r11617 r11715  
    66 
    77#include "sexp.h" 
     8#include "mlisp.h" 
    89 
    910#ifndef FALSE 
  • lang/c/misc/mlisp/core/s_util.cpp

    r11550 r11715  
    2626                } 
    2727        } 
     28        va_end(ap); 
    2829        return head; 
    2930} 
  • lang/c/misc/mlisp/core/v_vm.cpp

    r11676 r11715  
    178178 
    179179static SExp continuation(int s) { 
    180         SExp body = list(3, REFER_LOCAL, int2s(0), list(3, NUATE, save_stack(s), list(2, RETURN, int2s(0)))); 
     180        SExp body = list(3, REFER_LOCAL, int2s(0), list(3, NUATE, save_stack(s), list(1, RETURN))); 
    181181        return closure(1, 1, body, 0, 0); 
    182182} 
     
    206206        *px = index(ss, 0);     *pf = s2int(index(ss, 1));      *pc = index(ss, 2); 
    207207        return ss - 3; 
     208} 
     209 
     210 
     211static int apply(SExp fn, SExp argnum, int s, SExp* pa, SExp* px, sint* pf, SExp* pc, int* ps) { 
     212        int r = 0; 
     213        if (type_of(fn) == tProc) { 
     214                int ss = push(argnum, s); 
     215                int n = s2int(argnum); 
     216                Procedure* proc = (Procedure*)fn.ptr; 
     217                if (check_arg_num(proc, n)) { 
     218                        switch (proc->get_func_type()) { 
     219                        case Procedure::Builtin: 
     220                                { 
     221                                        SCFunc cfunc = proc->u.cfunc; 
     222                                        SExp res = (*cfunc)(ss); 
     223                                        *pa = res; 
     224                                        *ps = vm_return(px, pf, pc, ss); 
     225                                } 
     226                                break; 
     227                        case Procedure::Cell: 
     228                                { 
     229                                        modify_args(proc, n, s); 
     230                                        *px = closure_body(fn); *pf = ss;       *pc = fn;       *ps = ss; 
     231                                } 
     232                                r = 1; 
     233                                break; 
     234                        } 
     235                } else { 
     236                        runtime_error("wrong number of argument"); 
     237                        r = -1; 
     238                } 
     239        } else { 
     240                runtime_error("can't call"); 
     241                r = -1; 
     242        } 
     243        return r; 
     244} 
     245 
     246#include <malloc.h>             //<alloca.h> 
     247#include <stdarg.h> 
     248SExp vm_apply(SExp fn, int narg, ...) { 
     249        int s = 0; 
     250        SExp c = nil; 
     251        sint f = 0; 
     252        SExp ret = list(1, HALT); 
     253 
     254        // FRAME 
     255        s = push(ret, push(int2s(f), push(c, s))); 
     256 
     257        // argments 
     258        SExp* buf = (SExp*)alloca(sizeof(SExp) * narg); 
     259        va_list ap; 
     260        va_start(ap, narg); 
     261        for (int i=0; i<narg; ++i) { 
     262                buf[i] = va_arg(ap, SExp); 
     263        } 
     264        va_end(ap); 
     265        for (int i=narg; --i>=0; ) { 
     266                s = push(buf[i], s); 
     267        } 
     268 
     269        SExp a, x; 
     270        if (apply(fn, int2s(narg), s, &a, &x, &f, &c, &s) == 1) { 
     271                a = vm(a, x, f, c, s); 
     272        } 
     273        return a; 
    208274} 
    209275 
     
    273339                        x = xx; 
    274340                } else if (op == APPLY) { 
    275                         if (type_of(a) == tProc) { 
    276                                 SExp argnum = cadr(x); 
    277                                 int ss = push(argnum, s); 
    278                                 int n = s2int(argnum); 
    279                                 Procedure* proc = (Procedure*)a.ptr; 
    280                                 if (check_arg_num(proc, n)) { 
    281                                         modify_args(proc, n, s); 
    282                                         switch (proc->get_func_type()) { 
    283                                         case Procedure::Builtin: 
    284                                                 { 
    285                                                         SCFunc cfunc = proc->u.cfunc; 
    286                                                         SExp res = (*cfunc)(ss); 
    287                                                         a = res; 
    288                                                         s = vm_return(&x, &f, &c, ss); 
    289                                                 } 
    290                                                 break; 
    291                                         case Procedure::Cell: 
    292                                                 { 
    293                                                         x = closure_body(a);    f = ss; c = a;  s = ss; 
    294                                                 } 
    295                                                 break; 
    296                                         } 
    297                                 } else { 
    298                                         runtime_error("wrong number of argument"); 
    299                                 } 
    300                         } else { 
    301                                 runtime_error("can't call"); 
    302                                 break; 
    303                         } 
     341                        SExp argnum = cadr(x); 
     342                        apply(a, argnum, s, &a, &x, &f, &c, &s); 
    304343                } else if (op == RETURN) { 
    305344                        s = vm_return(&x, &f, &c, s); 
  • lang/c/misc/mlisp/core/v_vm.h

    r11550 r11715  
    1414SExp vm(SExp a, SExp x, sint f, SExp c, int s); 
    1515 
    16 void define_global(SExp sym, SExp val); 
    17  
    1816#ifdef __cplusplus 
    1917} //extern "C" 
  • lang/c/misc/mlisp/inc/mlisp.h

    r11551 r11715  
    2727 
    2828 
     29 
     30 
     31void define_global(SExp sym, SExp val); 
     32SExp refer_global(SExp sym); 
     33 
     34SExp vm_apply(SExp fn, int narg, ...); 
     35 
     36 
    2937#ifdef __cplusplus 
    3038} //extern "C" 
  • lang/c/misc/mlisp/mlisp.vcproj

    r11551 r11715  
    143143                                RelativePath=".\core\sexp.cpp"> 
    144144                        </File> 
    145                         <File 
    146                                 RelativePath=".\core\sexp.h"> 
    147                         </File> 
    148145                        <Filter 
    149146                                Name="compiler" 
     
    193190                        </File> 
    194191                </Filter> 
     192                <Filter 
     193                        Name="inc" 
     194                        Filter=""> 
     195                        <File 
     196                                RelativePath=".\inc\mlisp.h"> 
     197                        </File> 
     198                        <File 
     199                                RelativePath=".\inc\sexp.h"> 
     200                        </File> 
     201                </Filter> 
    195202        </Files> 
    196203        <Globals> 
  • lang/c/misc/mlisp/readme.txt

    r11676 r11715  
    4646-[v] �X�^�b�N�x�[�X�ɒu��������-[v] �C�ӌ‚̈��󂯎� 
    4747-[x] �}�N����� 
    48 --[x] macroexpand ��- C �������[�o���̊֐���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
     48--[x] macroexpand ��-[v] C �������[�o���̊֐���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
    4949- ��s���ɕϐ�������������Ƃ��̎����� 
    5050- �������� 
     
    5656--- �֐����ǂ����H 
    5757--- ����������Ă邩�H�i��s���� 
     58 
     59- �n�b�V����� 
    5860 
    5961- SDL �Ɨ��߂ĂȂ񂩍��i�e�g���X�j 
  • lang/c/misc/mlisp/test/main.cpp

    r11549 r11715  
    4646 
    4747 
    48 int main(int argc, char* argv[]) 
    49 { 
     48void test_call() { 
     49        SExp src = read_from_string( 
     50                        "(define fib" 
     51                        "  (lambda (n)" 
     52                        "    (if (< n 2)" 
     53                        "        n" 
     54                        "      (+ (fib (- n 1))" 
     55                        "         (fib (- n 2))))))" 
     56                ); 
     57 
     58        evaluate(src); 
     59 
     60        SExp r = vm_apply(refer_global(intern("fib")), 1, int2s(6)); 
     61        print(r); 
     62} 
     63 
     64 
     65int main(int argc, char* argv[]) { 
    5066        mlisp_new(&vtbl); 
     67#if 1 
    5168        if (argc >= 2) { 
    5269                if (setjmp(s_env) == 0) { 
     
    5673                repl(); 
    5774        } 
     75#else 
     76        test_call(); 
     77#endif 
    5878        mlisp_delete(); 
    5979