Changeset 11058 for lang/c

Show
Ignore:
Timestamp:
05/04/08 08:57:54 (7 months ago)
Author:
mokehehe
Message:

コンパイルできるようにした

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

Legend:

Unmodified
Added
Removed
  • lang/c/misc/mlisp/mlisp.vcproj

    r11057 r11058  
    8181                                Name="VCLinkerTool" 
    8282                                AdditionalDependencies="boehmgc.lib libcpmt.lib" 
    83                                 OutputFile="$(OutDir)/mlisp.exe" 
     83                                OutputFile="mlisp.exe" 
    8484                                LinkIncremental="1" 
    8585                                GenerateDebugInformation="TRUE" 
  • lang/c/misc/mlisp/readme.txt

    r11057 r11058  
    88-BoehmGC ��p 
    99-�q�[�v�x�[�X�̎�� 
    10 -repl ����quit�v�Ƒł����ނƔ����� 
     10-repl ����quit�v�Ƒł����ނƔ�����-�unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
     11-�V���{���̑啶���Ə�������ʂ��� 
  • lang/c/misc/mlisp/sexp/sexp.cpp

    r11057 r11058  
    9090} 
    9191 
     92const char* s2str(SExp s) { 
     93        type_check(s, tStr); 
     94        String* str = (String*)s.ptr; 
     95        return str->str; 
     96} 
     97 
    9298 
    9399//============================================================================= 
  • lang/c/misc/mlisp/sexp/sexp.h

    r11057 r11058  
    9090void rplacd(SExp s, SExp d);    // cdr ���u������ 
    9191 
     92/// ������Ή��������{����� 
    9293SExp intern(const char* str); 
    9394 
     
    113114SExp int2s(sint x); 
    114115 
     116/// S���𕶎���ϊ� 
     117const char* s2str(SExp s); 
     118 
    115119 
    116120 
     
    124128//============================================================================= 
    125129// �C�����C�� 
    126  
    127 __inline int consp(SExp s)              { return type_of(s) == tCell; } 
    128  
    129 __inline int symbolp(SExp s)    { return type_of(s) == tSymb; } 
    130  
    131130 
    132131__inline SExp int2s(sint x) { 
     
    145144 
    146145 
    147  
    148 __inline SExp cadr(SExp s)              { return car(cdr(s)); } 
    149 __inline SExp cddr(SExp s)              { return cdr(cdr(s)); } 
    150 __inline SExp caddr(SExp s)             { return car(cdr(cdr(s))); } 
    151 __inline SExp cdddr(SExp s)             { return cdr(cdr(cdr(s))); } 
    152 __inline SExp cadddr(SExp s)    { return car(cdr(cdr(cdr(s)))); } 
    153 __inline SExp cddddr(SExp s)    { return cdr(cdr(cdr(cdr(s)))); } 
    154  
    155  
    156  
    157146#ifdef __cplusplus 
    158147inline bool SExp::operator==(const SExp s) const { return eq(*this, s) != 0; } 
  • lang/c/misc/mlisp/sexp/sutil.h

    r11057 r11058  
    3636 
    3737 
     38 
     39__inline int consp(SExp s)              { return type_of(s) == tCell; } 
     40 
     41__inline int symbolp(SExp s)    { return type_of(s) == tSymb; } 
     42 
     43__inline SExp cadr(SExp s)              { return car(cdr(s)); } 
     44__inline SExp cddr(SExp s)              { return cdr(cdr(s)); } 
     45__inline SExp caddr(SExp s)             { return car(cddr(s)); } 
     46__inline SExp cdddr(SExp s)             { return cdr(cddr(s)); } 
     47__inline SExp cadddr(SExp s)    { return car(cdddr(s)); } 
     48__inline SExp cddddr(SExp s)    { return cdr(cdddr(s)); } 
     49 
     50 
    3851#ifdef __cplusplus 
    3952} // extern "C" 
  • lang/c/misc/mlisp/test/compiler.cpp

    r11057 r11058  
    4343} 
    4444 
     45// ���[�J���‹��ɕϐ���^ 
     46SExp compile_define_global(SExp env, SExp var) { 
     47        return cons(int2s(length(env) - 1), compile_define_var(last_pair(env), var)); 
     48} 
     49 
    4550 
    4651 
    4752 
    4853static SExp compile_lookup(SExp var, SExp e) { 
     54#if 0 
    4955        int rib = 0; 
    5056nxtrib:; 
    51                 if (nilp(e)) { 
    52                         return nil; 
    53                 } 
     57                if (nilp(e))    return nil; 
    5458                SExp vars = car(e); 
    5559                int elt = 0; 
     
    5862                        if (eq(car(vars), var)) return cons(int2s(rib), int2s(elt)); 
    5963                        else                                    { vars = cdr(vars); ++elt; goto nxtelt; } 
     64#else 
     65        int rib = 0; 
     66        for (;; e = cdr(e), ++rib) { 
     67                if (nilp(e))    return nil; 
     68                SExp vars = car(e); 
     69                int elt = 0; 
     70                for (;; vars = cdr(vars), ++elt) { 
     71                        if (nilp(vars))                 break; 
     72                        if (eq(car(vars), var)) return cons(int2s(rib), int2s(elt)); 
     73                } 
     74        } 
     75#endif 
    6076} 
    6177 
     
    128144        if (symbolp(x)) { 
    129145                SExp access = compile_lookup(x, e); 
     146                if (nilp(access)) { 
     147                        access = compile_define_global(e, x); 
     148                } 
    130149                return list(3, REFER, access, next); 
    131150        } else if (consp(x)) { 
     
    155174                        SExp var = cadr(x); 
    156175                        SExp body = cddr(x); 
    157                         compile_define(var, body, e, next); 
     176                        return compile_define(var, body, e, next); 
    158177                } else if (eq(op, intern("call/cc"))) { 
    159178                        SExp xx = cadr(x); 
     
    164183                                return list(3, FRAME, c, next); 
    165184                } else { 
    166                         compile_pair_loop(cdr(x), compile(car(x), e, cons(APPLY, nil)), e, next); 
     185                        return compile_pair_loop(cdr(x), compile(car(x), e, cons(APPLY, nil)), e, next); 
    167186                } 
    168187        } else { 
    169                 list(3, CONSTANT, x, next); 
     188                return list(3, CONSTANT, x, next); 
    170189        } 
    171190} 
  • lang/c/misc/mlisp/test/main.cpp

    r11057 r11058  
    3333SExp comp_env, run_env; 
    3434 
     35int load(const char* fn); 
    3536 
    3637 
     
    3839#include "op.h" 
    3940#include "inner.h" 
     41 
     42SExp run(SExp c) { 
     43        return vm(nil, c, run_env, nil, nil); 
     44} 
    4045 
    4146SExp evaluate(SExp code) { 
    4247        SExp halt_code = cons(HALT, nil); 
    4348        SExp c = compile(code, comp_env, halt_code); 
    44         SExp r = vm(nil, c,  run_env, nil, nil); 
    45         return r; 
    46 } 
    47  
    48  
    49 static bool check_number(SExp a) 
    50 { 
     49        return run(c); 
     50} 
     51 
     52 
     53static bool check_number(SExp a) { 
    5154        switch (type_of(a)) { 
    5255        default: 
     
    8588} 
    8689 
    87 static SExp builtin_plus(SExp arg) 
    88 { 
     90static SExp builtin_plus(SExp arg) { 
    8991        sint x = 0; 
    9092        for (SExp p = arg; consp(p); p = cdr(p)) { 
     
    9698} 
    9799 
    98 static SExp builtin_difference(SExp arg) 
    99 { 
     100static SExp builtin_difference(SExp arg) { 
    100101        SExp a = car(arg); 
    101102        check_number(a); 
     
    115116} 
    116117 
    117 static SExp builtin_lt(SExp arg) 
    118 { 
     118static SExp builtin_times(SExp arg) { 
     119        sint x = 1; 
     120        for (SExp p = arg; consp(p); p = cdr(p)) { 
     121                SExp a = car(p); 
     122                check_number(a); 
     123                x *= s2int(a); 
     124        } 
     125        return int2s(x); 
     126} 
     127 
     128static SExp builtin_quotient(SExp arg) { 
     129        SExp a = car(arg); 
     130        check_number(a); 
     131        sint x = s2int(a); 
     132 
     133        SExp p = cdr(arg); 
     134        if (!consp(p)) { 
     135                x = -x; 
     136        } else { 
     137                for (; consp(p); p = cdr(p)) { 
     138                        SExp a = car(p); 
     139                        check_number(a); 
     140                        sint d = s2int(a); 
     141                        if (d == 0) { 
     142//                              error(ERR_ZERO_DIVIDE); 
     143                                assert(!"zero divide"); 
     144                        } else { 
     145                                x /= d; 
     146                        } 
     147                } 
     148        } 
     149        return int2s(x); 
     150} 
     151 
     152static SExp builtin_lt(SExp arg) { 
    119153        SExp a = car(arg); 
    120154        check_number(a); 
     
    131165} 
    132166 
    133 static SExp builtin_read(SExp arg) 
    134 { 
     167static SExp builtin_gt(SExp arg) { 
     168        SExp a = car(arg); 
     169        check_number(a); 
     170        sint x = s2int(a); 
     171 
     172        for (SExp p = cdr(arg); consp(p); p = cdr(p)) { 
     173                SExp b = car(p); 
     174                check_number(a); 
     175                sint y = s2int(b); 
     176                if (x <= y)     return nil; 
     177                x = y; 
     178        } 
     179        return t; 
     180} 
     181 
     182static SExp builtin_read(SExp arg) { 
    135183        return read_from_file(stdin); 
    136184} 
    137185 
    138 static SExp builtin_print(SExp arg) 
    139 { 
     186static SExp builtin_print(SExp arg) { 
    140187        SExp a = car(arg); 
    141188        print(a); 
     
    143190} 
    144191 
    145 static SExp builtin_eval(SExp arg) 
    146 { 
     192static SExp builtin_eval(SExp arg) { 
    147193        SExp code = car(arg); 
    148194 
    149195        return evaluate(code); 
     196} 
     197 
     198static SExp builtin_load(SExp arg) { 
     199        SExp a = car(arg); 
     200        const char* fn = s2str(a); 
     201        if (fn != NULL) { 
     202                if (load(fn)) { 
     203                        return t; 
     204                } 
     205        } 
     206        return nil; 
     207} 
     208 
     209static SExp builtin_compile(SExp arg) { 
     210        SExp code = car(arg); 
     211        SExp halt_code = cons(HALT, nil); 
     212        return compile(code, comp_env, halt_code); 
    150213} 
    151214 
     
    168231                {       "+",            builtin_plus,           FALSE,  0,      -1,     }, 
    169232                {       "-",            builtin_difference,     FALSE,  1, -1,  }, 
     233                {       "*",            builtin_times,          FALSE,  0,      -1,     }, 
     234                {       "/",            builtin_quotient,       FALSE,  1, -1,  }, 
    170235                {       "<",            builtin_lt,                     FALSE,  1, -1,  }, 
     236                {       ">",            builtin_gt,                     FALSE,  1, -1,  }, 
    171237 
    172238 
     
    174240                {       "print",        builtin_print,          FALSE,  1,      1,      }, 
    175241                {       "eval",         builtin_eval,           FALSE,  1,      1,      }, 
     242 
     243                {       "load",         builtin_load,           FALSE,  1,      1,      }, 
     244                {       "compile",      builtin_compile,        FALSE,  1,      1,      }, 
    176245        }; 
    177246        for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
     
    218287 
    219288 
    220 void loop() { 
     289SExp compile_file(const char* fn) { 
     290        FILE* fp = fopen(fn, "r"); 
     291        SExp code = nil; 
     292        if (fp != NULL) { 
     293                SExp acc = nil; 
     294                for (;;) { 
     295                        SExp s = read_from_file(fp); 
     296                        if (nilp(s))    break; 
     297                        acc = cons(s, acc); 
     298                } 
     299                SExp halt_code = cons(HALT, nil); 
     300                code = compile_block(nreverse(acc), comp_env, halt_code); 
     301 
     302                fclose(fp); 
     303        } 
     304        return code; 
     305} 
     306 
     307 
     308int load(const char* fn) { 
     309        SExp code = compile_file(fn); 
     310        if (!nilp(code)) { 
     311                SExp r = run(code); 
     312                return TRUE; 
     313        } else { 
     314                return FALSE; 
     315        } 
     316} 
     317 
     318 
     319void repl() { 
    221320        SExp halt_code = cons(HALT, nil); 
    222321 
     
    238337 
    239338 
    240 SExp load_file(const char* fn) { 
    241         FILE* fp = fopen(fn, "r"); 
    242         SExp code = nil; 
    243         if (fp != NULL) { 
    244                 SExp acc = nil; 
    245                 for (;;) { 
    246                         SExp s = read_from_file(fp); 
    247                         if (nilp(s))    break; 
    248                         acc = cons(s, acc); 
    249                 } 
    250                 code = nreverse(acc); 
    251  
    252                 fclose(fp); 
    253         } 
    254         return code; 
    255 } 
    256  
    257  
    258 SExp load(const char* fn) { 
    259         SExp sexps = load_file(fn); 
    260  
    261         SExp halt_code = cons(HALT, nil); 
    262         SExp c = compile_block(sexps, comp_env, halt_code); 
    263         SExp r = vm(nil, c,  run_env, nil, nil); 
    264  
    265         return r; 
    266 } 
    267  
    268  
    269339int main(int argc, char* argv[]) 
    270340{ 
     
    279349                load(argv[1]); 
    280350        } else { 
    281                 loop(); 
     351                repl(); 
    282352        } 
    283353        mlisp_delete(); 
  • lang/c/misc/mlisp/test/vm.cpp

    r11057 r11058  
    99#include "inner.h" 
    1010#include <assert.h> 
     11 
     12static void runtime_error(const char* msg) { 
     13        assert(!msg); 
     14} 
     15 
    1116 
    1217 
     
    2732        for (int elt = s2int(cdr(access)); ; --elt) { 
    2833                if (nilp(r)) { 
    29                         extend_run_env(car(e), elt + 1); 
     34                        SExp le = car(e); 
     35                        if (!nilp(le)) { 
     36                                extend_run_env(car(e), elt + 1); 
     37                        } else { 
     38                                rplaca(e, replicate(elt + 1, nil)); 
     39                        } 
     40 
    3041                        r = car(e);     elt = s2int(cdr(access)); 
    3142                } 
     
    165176                        } 
    166177                } else { 
    167                         assert(!"illegal opecode"); 
     178                        runtime_error("illegal opecode"); 
     179                        return nil; 
    168180                } 
    169181        }