Changeset 11549 for lang/c

Show
Ignore:
Timestamp:
05/14/08 08:15:48 (6 months ago)
Author:
mokehehe
Message:
 
Location:
lang/c/misc/mlisp
Files:
4 added
2 removed
6 modified
11 moved

Legend:

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

    r11547 r11549  
    120120                        </File> 
    121121                        <File 
     122                                RelativePath=".\sexp\s_hash.h"> 
     123                        </File> 
     124                        <File 
     125                                RelativePath=".\sexp\s_print.cpp"> 
     126                        </File> 
     127                        <File 
     128                                RelativePath=".\sexp\s_read.cpp"> 
     129                        </File> 
     130                        <File 
     131                                RelativePath=".\sexp\s_util.cpp"> 
     132                        </File> 
     133                        <File 
     134                                RelativePath=".\sexp\s_util.h"> 
     135                        </File> 
     136                        <File 
     137                                RelativePath=".\sexp\s_vect.cpp"> 
     138                        </File> 
     139                        <File 
     140                                RelativePath=".\sexp\s_vect.h"> 
     141                        </File> 
     142                        <File 
    122143                                RelativePath=".\sexp\sexp.cpp"> 
    123144                        </File> 
     
    125146                                RelativePath=".\sexp\sexp.h"> 
    126147                        </File> 
    127                         <File 
    128                                 RelativePath=".\sexp\shash.h"> 
    129                         </File> 
    130                         <File 
    131                                 RelativePath=".\sexp\sprint.cpp"> 
    132                         </File> 
    133                         <File 
    134                                 RelativePath=".\sexp\sread.cpp"> 
    135                         </File> 
    136                         <File 
    137                                 RelativePath=".\sexp\sutil.cpp"> 
    138                         </File> 
    139                         <File 
    140                                 RelativePath=".\sexp\sutil.h"> 
    141                         </File> 
    142                         <File 
    143                                 RelativePath=".\sexp\svect.cpp"> 
    144                         </File> 
    145                         <File 
    146                                 RelativePath=".\sexp\svect.h"> 
    147                         </File> 
    148148                        <Filter 
    149149                                Name="compiler" 
    150150                                Filter=""> 
    151151                                <File 
    152                                         RelativePath=".\sexp\compiler.cpp"> 
     152                                        RelativePath=".\sexp\c_compiler.cpp"> 
    153153                                </File> 
    154154                                <File 
    155                                         RelativePath=".\sexp\compiler.h"> 
     155                                        RelativePath=".\sexp\c_compiler.h"> 
    156156                                </File> 
    157157                        </Filter> 
     
    163163                                </File> 
    164164                                <File 
    165                                         RelativePath=".\sexp\vm.cpp"> 
     165                                        RelativePath=".\sexp\v_vm.cpp"> 
    166166                                </File> 
    167167                                <File 
    168                                         RelativePath=".\sexp\vm.h"> 
     168                                        RelativePath=".\sexp\v_vm.h"> 
     169                                </File> 
     170                        </Filter> 
     171                        <Filter 
     172                                Name="modules" 
     173                                Filter=""> 
     174                                <File 
     175                                        RelativePath=".\sexp\m_basic.cpp"> 
     176                                </File> 
     177                                <File 
     178                                        RelativePath=".\sexp\m_basic.h"> 
     179                                </File> 
     180                                <File 
     181                                        RelativePath=".\sexp\mlisp.cpp"> 
     182                                </File> 
     183                                <File 
     184                                        RelativePath=".\sexp\mlisp.h"> 
    169185                                </File> 
    170186                        </Filter> 
  • lang/c/misc/mlisp/readme.txt

    r11387 r11549  
    2424* �\�[�X�\�� 
    2525 
    26         sexp 
    27                 �r������        compiler 
    28                 �R���p�C�� 
    29         vm 
    30                 �o�[�`�����}�V�� 
     26        core 
     27                ����s_ ���‚��� 
     28                        �r������                ����c_ ���‚��� 
     29                        �R���p�C���֘A 
     30                ����v_ ���‚��� 
     31                        �o�[�`�����}�V���֘A 
     32                ����m_ ���‚��� 
     33                        ���W���[���֘A 
     34 
    3135        test 
    3236                �e�X�g�p�G���g�� 
     
    4044 
    4145* ToDo 
    42 -[v] �X�^�b�N�x�[�X�ɒu��������-[x] �C�ӌ‚̈��󂯎� 
     46-[v] �X�^�b�N�x�[�X�ɒu��������-[v] �C�ӌ‚̈��󂯎� 
    4347-[x] �}�N����� 
    4448--[x] macroexpand ��- C �������[�o���̊֐���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 
     
    6468* changelog 
    6569 
     70** 080513 
     71- �C�ӌ‚̈����� 
     72 
    6673** 080511 
    6774- �X�^�b�N�x�[�X�ɒu������ 
  • lang/c/misc/mlisp/sexp/c_compiler.cpp

    r11547 r11549  
    66//============================================================================= 
    77 
    8 #include "compiler.h" 
     8#include "c_compiler.h" 
    99#include "sexp.h" 
    10 #include "sutil.h" 
     10#include "s_util.h" 
    1111#include "op.h" 
    1212#include "inner.h" 
  • lang/c/misc/mlisp/sexp/inner.h

    r11387 r11549  
    11//============================================================================= 
     2///     ���� 
    23//============================================================================= 
    34 
     
    103104void print_vector(SExp v); 
    104105 
     106 
     107/// ��s���F�X�^�b�N�Q�� 
    105108SExp refer_stack(int s, int i); 
  • lang/c/misc/mlisp/sexp/s_print.cpp

    r11387 r11549  
    11#include "inner.h" 
    2 #include "sutil.h" 
     2#include "s_util.h" 
    33 
    44static void print_cell(SExp p) { 
  • lang/c/misc/mlisp/sexp/s_util.cpp

    r11387 r11549  
    66//============================================================================= 
    77 
    8 #include "sutil.h" 
     8#include "s_util.h" 
    99#include <stdarg.h> 
    1010 
  • lang/c/misc/mlisp/sexp/s_vect.cpp

    r11387 r11549  
    44//============================================================================= 
    55 
    6 #include "svect.h" 
     6#include "s_vect.h" 
    77#include "inner.h" 
    88 
  • lang/c/misc/mlisp/sexp/sexp.cpp

    r11387 r11549  
    55 
    66#include "sexp.h" 
    7 #include "shash.h" 
     7#include "s_hash.h" 
    88#include "inner.h" 
    99#include <stdio.h> 
     
    283283//============================================================================= 
    284284 
    285 void mlisp_new(const SVTable* vtbl) { 
     285void sexp_new(const SVTable* vtbl) { 
    286286        init(vtbl); 
    287287} 
    288288 
    289 void mlisp_delete(void) { 
     289void sexp_delete(void) { 
    290290        close(); 
    291291} 
  • lang/c/misc/mlisp/sexp/sexp.h

    r11544 r11549  
    7272// �֐��� 
    7373 
    74 /// �R���e�L�X�g�̐��� 
    75 /** 
     74/// �‹��̏���/** 
    7675        @param  f               GC�‚��������A���P�[�^�ւ̊֐��|�C���^ 
    7776*/ 
    78 void mlisp_new(const SVTable* vtbl); 
     77void sexp_new(const SVTable* vtbl); 
    7978 
    8079/// ���� 
    81 void mlisp_delete(void); 
     80void sexp_delete(void); 
    8281 
    8382// Lisp�n�֐� 
  • lang/c/misc/mlisp/sexp/v_vm.cpp

    r11547 r11549  
    66//============================================================================= 
    77 
    8 #include "vm.h" 
     8#include "v_vm.h" 
    99#include "sexp.h" 
    10 #include "sutil.h" 
    11 #include "svect.h" 
     10#include "s_util.h" 
     11#include "s_vect.h" 
    1212#include "op.h" 
    1313#include "inner.h" 
  • lang/c/misc/mlisp/test/main.cpp

    r11544 r11549  
    33//============================================================================= 
    44 
    5 #include "sexp.h" 
    6 #include "sutil.h" 
    7 #include "compiler.h" 
    8 #include "vm.h" 
     5#include "mlisp.h" 
    96#include <gc/gc.h> 
    107#include <setjmp.h> 
    11  
    12 #ifndef FALSE 
    13 #define FALSE   (0) 
    14 #define TRUE    (1) 
    15 #endif 
    168 
    179static jmp_buf s_env; 
     
    3022} 
    3123 
    32  
    33 int load_file(const char* fn); 
    34  
    35  
    36  
    37 #include "op.h" 
    38 #include "inner.h" 
    39  
    40 /// ��s 
    41 SExp run(SExp c) { 
    42         return vm(nil, c, 0, nil, 0); 
    43 } 
    44  
    45 /// �g�b�v���x���ŃR���p�C�� 
    46 SExp compile_ontop(SExp code) { 
    47         SExp halt_code = cons(HALT, nil); 
    48         return compile(code, list(1, nil), nil, halt_code); 
    49 } 
    50  
    51 /// �]�� 
    52 SExp evaluate(SExp code) { 
    53         SExp c = compile_ontop(code); 
    54 //print(c); 
    55         return run(c); 
    56 } 
    57  
    58  
    59 static bool check_number(SExp a) { 
    60         switch (type_of(a)) { 
    61         default: 
    62                 error(ERR_TYPE_REQUIRED, "integer"); 
    63                 return false; 
    64         case tInt: 
    65                 return true; 
    66         } 
    67 } 
    68  
    69  
    70 //============================================================================= 
    71 static SExp builtin_cons(int s) { 
    72         SExp a = refer_stack(s, 0); 
    73         SExp d = refer_stack(s, 1); 
    74         return cons(a, d); 
    75 } 
    76  
    77 static SExp builtin_car(int s) { 
    78         SExp a = refer_stack(s, 0); 
    79         return car(a); 
    80 } 
    81  
    82 static SExp builtin_cdr(int s) { 
    83         SExp a = refer_stack(s, 0); 
    84         return cdr(a); 
    85 } 
    86  
    87 static SExp builtin_consp(int s) { 
    88         SExp v = refer_stack(s, 0); 
    89         return consp(v) ? t : nil; 
    90 } 
    91  
    92 static SExp builtin_eq(int s) { 
    93         SExp a = refer_stack(s, 0); 
    94         SExp d = refer_stack(s, 1); 
    95         return eq(a, d) ? t : nil; 
    96 } 
    97  
    98 static SExp builtin_rplaca(int s) { 
    99         SExp a = refer_stack(s, 0); 
    100         SExp d = refer_stack(s, 1); 
    101         rplaca(a, d); 
    102         return nil; 
    103 } 
    104  
    105 static SExp builtin_rplacd(int s) { 
    106         SExp a = refer_stack(s, 0); 
    107         SExp d = refer_stack(s, 1); 
    108         rplacd(a, d); 
    109         return nil; 
    110 } 
    111  
    112 static SExp builtin_plus(int s) { 
    113         sint n = s2int(refer_stack(s, -1)); 
    114         sint x = 0; 
    115         for (int i=0; i<n; ++i) { 
    116                 SExp a = refer_stack(s, i); 
    117                 check_number(a); 
    118                 x += s2int(a); 
    119         } 
    120         return int2s(x); 
    121 } 
    122  
    123 static SExp builtin_difference(int s) { 
    124         sint n = s2int(refer_stack(s, -1)); 
    125         SExp a = refer_stack(s, 0); 
    126         check_number(a); 
    127         sint x = s2int(a); 
    128         if (n == 1) { 
    129                 x = -x; 
    130         } else { 
    131                 for (int i=1; i<n; ++i) { 
    132                         SExp a = refer_stack(s, i); 
    133                         check_number(a); 
    134                         x -= s2int(a); 
    135                 } 
    136         } 
    137         return int2s(x); 
    138 } 
    139  
    140 static SExp builtin_times(int s) { 
    141         sint n = s2int(refer_stack(s, -1)); 
    142         sint x = 1; 
    143         for (int i=0; i<n; ++i) { 
    144                 SExp a = refer_stack(s, i); 
    145                 check_number(a); 
    146                 x *= s2int(a); 
    147         } 
    148         return int2s(x); 
    149 } 
    150  
    151 static SExp builtin_quotient(int s) { 
    152         sint n = s2int(refer_stack(s, -1)); 
    153         SExp a = refer_stack(s, 0); 
    154         check_number(a); 
    155         sint x = s2int(a); 
    156         if (n == 1) { 
    157                 x = 1 / x; 
    158         } else { 
    159                 for (int i=1; i<n; ++i) { 
    160                         SExp a = refer_stack(s, i); 
    161                         check_number(a); 
    162                         sint d = s2int(a); 
    163                         if (d == 0) { 
    164 //                              error(ERR_ZERO_DIVIDE); 
    165                                 assert(!"zero divide"); 
    166                         } else { 
    167                                 x /= d; 
    168                         } 
    169                 } 
    170         } 
    171         return int2s(x); 
    172 } 
    173  
    174 static SExp num_predicate(int s, int n, bool (*p)(sint, sint)) { 
    175         SExp a = refer_stack(s, 0); 
    176         check_number(a); 
    177         sint x = s2int(a); 
    178  
    179         for (int i=1; i<n; ++i) { 
    180                 SExp b = refer_stack(s, i); 
    181                 check_number(b); 
    182                 sint y = s2int(b); 
    183                 if (!p(x, y))   return nil; 
    184                 x = y; 
    185         } 
    186         return t; 
    187 } 
    188  
    189 static bool numeq(sint a, sint b)       { return a == b; } 
    190 static SExp builtin_numeq(int s) { 
    191         sint n = s2int(refer_stack(s, -1)); 
    192         return num_predicate(s, n, numeq); 
    193 } 
    194  
    195 static bool numlt(sint a, sint b)       { return a < b; } 
    196 static SExp builtin_lt(int s) { 
    197         sint n = s2int(refer_stack(s, -1)); 
    198         return num_predicate(s, n, numlt); 
    199 } 
    200  
    201 static bool numgt(sint a, sint b)       { return a > b; } 
    202 static SExp builtin_gt(int s) { 
    203         sint n = s2int(refer_stack(s, -1)); 
    204         return num_predicate(s, n, numgt); 
    205 } 
    206  
    207 static bool numle(sint a, sint b)       { return a <= b; } 
    208 static SExp builtin_le(int s) { 
    209         sint n = s2int(refer_stack(s, -1)); 
    210         return num_predicate(s, n, numle); 
    211 } 
    212  
    213 static bool numge(sint a, sint b)       { return a >= b; } 
    214 static SExp builtin_ge(int s) { 
    215         sint n = s2int(refer_stack(s, -1)); 
    216         return num_predicate(s, n, numge); 
    217 } 
    218  
    219  
    220 static SExp builtin_read(int s) { 
    221         return read_from_file(stdin); 
    222 } 
    223  
    224 static SExp builtin_print(int s) { 
    225         SExp a = refer_stack(s, 0); 
    226         print(a); 
    227         return nil; 
    228 } 
    229  
    230 static SExp builtin_eval(int s) { 
    231         SExp code = refer_stack(s, 0); 
    232  
    233         return evaluate(code); 
    234 } 
    235  
    236 static SExp builtin_load(int s) { 
    237         SExp a = refer_stack(s, 0); 
    238         const char* fn = s2str(a); 
    239         if (fn != NULL) { 
    240                 if (load_file(fn)) { 
    241                         return t; 
    242                 } 
    243         } 
    244         return nil; 
    245 } 
    246  
    247 static SExp builtin_compile(int s) { 
    248         SExp code = refer_stack(s, 0); 
    249         SExp halt_code = cons(HALT, nil); 
    250         return compile_ontop(code); 
    251 } 
    252  
    253  
    254 //============================================================================= 
    255  
    256  
    257  
    258 static void add_proctbl() { 
    259         struct { 
    260                 const char* name; 
    261                 SCFunc func; 
    262                 int b_macro; 
    263                 int minarg; 
    264                 int maxarg; 
    265         } static const tbl[] = { 
    266                 {       "cons",         builtin_cons,           FALSE,  2,      2,      }, 
    267                 {       "car",          builtin_car,            FALSE,  1,      1,      }, 
    268                 {       "cdr",          builtin_cdr,            FALSE,  1,      1,      }, 
    269                 {       "pair?",        builtin_consp,          FALSE,  1,      1,      }, 
    270                 {       "eq?",          builtin_eq,                     FALSE,  2,      2,      }, 
    271                 {       "set-car!",     builtin_rplaca,         FALSE,  2,      2,      }, 
    272                 {       "set-cdr!",     builtin_rplacd,         FALSE,  2,      2,      }, 
    273                 {       "+",            builtin_plus,           FALSE,  0,      -1,     }, 
    274                 {       "-",            builtin_difference,     FALSE,  1, -1,  }, 
    275                 {       "*",            builtin_times,          FALSE,  0,      -1,     }, 
    276                 {       "/",            builtin_quotient,       FALSE,  1, -1,  }, 
    277                 {       "=",            builtin_numeq,          FALSE,  1, -1,  }, 
    278                 {       "<",            builtin_lt,                     FALSE,  1, -1,  }, 
    279                 {       ">",            builtin_gt,                     FALSE,  1, -1,  }, 
    280                 {       "<=",           builtin_le,                     FALSE,  1, -1,  }, 
    281                 {       ">=",           builtin_ge,                     FALSE,  1, -1,  }, 
    282  
    283                 {       "read",         builtin_read,           FALSE,  0,      0,      }, 
    284                 {       "print",        builtin_print,          FALSE,  1,      1,      }, 
    285                 {       "eval",         builtin_eval,           FALSE,  1,      1,      }, 
    286  
    287                 {       "load",         builtin_load,           FALSE,  1,      1,      }, 
    288                 {       "compile",      builtin_compile,        FALSE,  1,      1,      }, 
    289         }; 
    290         for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
    291                 SExp sym = intern(tbl[i].name); 
    292                 SExp fn = gen_cfunc(tbl[i].func, tbl[i].b_macro, tbl[i].minarg, tbl[i].maxarg); 
    293                 define_global(sym, fn); 
    294         } 
    295 } 
    296  
    297 static void add_consttbl() { 
    298         struct { 
    299                 const char* name; 
    300         } static const tbl[] = { 
    301                 "t", 
    302                 "nil", 
    303         }; 
    304         for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 
    305                 SExp sym = intern(tbl[i].name); 
    306                 define_global(sym, sym); 
    307         } 
    308 } 
    309  
    310 static void init_env() { 
    311         add_proctbl(); 
    312         add_consttbl(); 
    313 } 
    314  
    315  
    316 // �\�[�X�̃��[�h�i�ǂݍ��݁A�R���p�C���A��s�j 
    317 int load_file(const char* fn) { 
    318         FILE* fp = fopen(fn, "r"); 
    319         if (fp == NULL) { 
    320                 return FALSE; 
    321         } else { 
    322                 SExp halt_code = cons(HALT, nil); 
    323                 for (;;) { 
    324                         SExp sexp = read_from_file(fp); 
    325                         if (nilp(sexp)) break; 
    326                         run(compile(sexp, nil, nil, halt_code)); 
    327                 } 
    328                 fclose(fp); 
    329                 return TRUE; 
    330         } 
    331 } 
     24static const SVTable vtbl = { 
     25        my_gc_malloc, 
     26        my_error, 
     27}; 
    33228 
    33329 
    33430void repl() { 
    335         SExp halt_code = cons(HALT, nil); 
    336  
    33731        for (;;) { 
    33832                if (setjmp(s_env) == 0) { 
     
    35448int main(int argc, char* argv[]) 
    35549{ 
    356         static const SVTable vtbl = { 
    357                 my_gc_malloc, 
    358                 my_error, 
    359         }; 
    360  
    36150        mlisp_new(&vtbl);