Changeset 11617 for lang/c

Show
Ignore:
Timestamp:
05/15/08 08:34:39 (6 months ago)
Author:
mokehehe
Message:
 
Location:
lang/c/misc/mlisp
Files:
7 modified

Legend:

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

    r11185 r11617  
    1414OBJ_EXT = ".o" 
    1515 
    16 INC_PATHS = %w(sexp compiler vm) 
     16INC_PATHS = %w(inc) 
    1717 
    1818INCLUDES = INC_PATHS.map {|x| "-I#{x}"}.join(" ") 
  • lang/c/misc/mlisp/core/inner.h

    r11550 r11617  
    106106 
    107107/// ��s���F�X�^�b�N�Q�� 
    108 SExp refer_stack(int s, int i); 
     108sint get_arg_num(int s); 
     109SExp get_arg(int s, int idx); 
  • lang/c/misc/mlisp/core/m_basic.cpp

    r11550 r11617  
    66#include "v_vm.h" 
    77#include "mlisp.h" 
     8#include "s_util.h" 
    89#include "inner.h" 
    910 
     
    1112//============================================================================= 
    1213static SExp builtin_cons(int s) { 
    13         SExp a = refer_stack(s, 0); 
    14         SExp d = refer_stack(s, 1); 
     14        SExp a = get_arg(s, 0); 
     15        SExp d = get_arg(s, 1); 
    1516        return cons(a, d); 
    1617} 
    1718 
    1819static SExp builtin_car(int s) { 
    19         SExp a = refer_stack(s, 0); 
     20        SExp a = get_arg(s, 0); 
    2021        return car(a); 
    2122} 
    2223 
    2324static SExp builtin_cdr(int s) { 
    24         SExp a = refer_stack(s, 0); 
     25        SExp a = get_arg(s, 0); 
    2526        return cdr(a); 
    2627} 
    2728 
    2829static SExp builtin_consp(int s) { 
    29         SExp v = refer_stack(s, 0); 
     30        SExp v = get_arg(s, 0); 
    3031        return consp(v) ? t : nil; 
    3132} 
    3233 
    3334static SExp builtin_eq(int s) { 
    34         SExp a = refer_stack(s, 0); 
    35         SExp d = refer_stack(s, 1); 
     35        SExp a = get_arg(s, 0); 
     36        SExp d = get_arg(s, 1); 
    3637        return eq(a, d) ? t : nil; 
    3738} 
    3839 
    3940static SExp builtin_rplaca(int s) { 
    40         SExp a = refer_stack(s, 0); 
    41         SExp d = refer_stack(s, 1); 
     41        SExp a = get_arg(s, 0); 
     42        SExp d = get_arg(s, 1); 
    4243        rplaca(a, d); 
    4344        return nil; 
     
    4546 
    4647static SExp builtin_rplacd(int s) { 
    47         SExp a = refer_stack(s, 0); 
    48         SExp d = refer_stack(s, 1); 
     48        SExp a = get_arg(s, 0); 
     49        SExp d = get_arg(s, 1); 
    4950        rplacd(a, d); 
    5051        return nil; 
     
    5253 
    5354static SExp builtin_plus(int s) { 
    54         sint n = s2int(refer_stack(s, -1)); 
     55        sint n = get_arg_num(s); 
    5556        sint x = 0; 
    5657        for (int i=0; i<n; ++i) { 
    57                 SExp a = refer_stack(s, i); 
     58                SExp a = get_arg(s, i); 
    5859                type_check(a, tInt); 
    5960                x += s2int(a); 
     
    6364 
    6465static SExp builtin_difference(int s) { 
    65         sint n = s2int(refer_stack(s, -1)); 
    66         SExp a = refer_stack(s, 0); 
     66        sint n = get_arg_num(s); 
     67        SExp a = get_arg(s, 0); 
    6768        type_check(a, tInt); 
    6869        sint x = s2int(a); 
     
    7172        } else { 
    7273                for (int i=1; i<n; ++i) { 
    73                         SExp a = refer_stack(s, i); 
     74                        SExp a = get_arg(s, i); 
    7475                        type_check(a, tInt); 
    7576                        x -= s2int(a); 
     
    8081 
    8182static SExp builtin_times(int s) { 
    82         sint n = s2int(refer_stack(s, -1)); 
     83        sint n = get_arg_num(s); 
    8384        sint x = 1; 
    84         for (int i=0; i<n; ++i) { 
    85                 SExp a = refer_stack(s, i); 
     85        for (int i=1; i<n; ++i) { 
     86                SExp a = get_arg(s, i); 
    8687                type_check(a, tInt); 
    8788                x *= s2int(a); 
     
    9192 
    9293static SExp builtin_quotient(int s) { 
    93         sint n = s2int(refer_stack(s, -1)); 
    94         SExp a = refer_stack(s, 0); 
     94        sint n = get_arg_num(s); 
     95        SExp a = get_arg(s, 0); 
    9596        type_check(a, tInt); 
    9697        sint x = s2int(a); 
     
    99100        } else { 
    100101                for (int i=1; i<n; ++i) { 
    101                         SExp a = refer_stack(s, i); 
     102                        SExp a = get_arg(s, i); 
    102103                        type_check(a, tInt); 
    103104                        sint d = s2int(a); 
     
    114115 
    115116static SExp num_predicate(int s, int n, bool (*p)(sint, sint)) { 
    116         SExp a = refer_stack(s, 0); 
     117        SExp a = get_arg(s, 0); 
    117118        type_check(a, tInt); 
    118119        sint x = s2int(a); 
    119120 
    120121        for (int i=1; i<n; ++i) { 
    121                 SExp b = refer_stack(s, i); 
     122                SExp b = get_arg(s, i); 
    122123                type_check(b, tInt); 
    123124                sint y = s2int(b); 
     
    130131static bool numeq(sint a, sint b)       { return a == b; } 
    131132static SExp builtin_numeq(int s) { 
    132         sint n = s2int(refer_stack(s, -1)); 
     133        sint n = get_arg_num(s); 
    133134        return num_predicate(s, n, numeq); 
    134135} 
     
    136137static bool numlt(sint a, sint b)       { return a < b; } 
    137138static SExp builtin_lt(int s) { 
    138         sint n = s2int(refer_stack(s, -1)); 
     139        sint n = get_arg_num(s); 
    139140        return num_predicate(s, n, numlt); 
    140141} 
     
    142143static bool numgt(sint a, sint b)       { return a > b; } 
    143144static SExp builtin_gt(int s) { 
    144         sint n = s2int(refer_stack(s, -1)); 
     145        sint n = get_arg_num(s); 
    145146        return num_predicate(s, n, numgt); 
    146147} 
     
    148149static bool numle(sint a, sint b)       { return a <= b; } 
    149150static SExp builtin_le(int s) { 
    150         sint n = s2int(refer_stack(s, -1)); 
     151        sint n = get_arg_num(s); 
    151152        return num_predicate(s, n, numle); 
    152153} 
     
    154155static bool numge(sint a, sint b)       { return a >= b; } 
    155156static SExp builtin_ge(int s) { 
    156         sint n = s2int(refer_stack(s, -1)); 
     157        sint n = get_arg_num(s); 
    157158        return num_predicate(s, n, numge); 
    158159} 
     
    164165 
    165166static SExp builtin_print(int s) { 
    166         SExp a = refer_stack(s, 0); 
     167        SExp a = get_arg(s, 0); 
    167168        print(a); 
    168169        return nil; 
     
    170171 
    171172static SExp builtin_eval(int s) { 
    172         SExp code = refer_stack(s, 0); 
     173        SExp code = get_arg(s, 0); 
    173174 
    174175        return evaluate(code); 
     
    176177 
    177178static SExp builtin_load(int s) { 
    178         SExp a = refer_stack(s, 0); 
     179        SExp a = get_arg(s, 0); 
    179180        const char* fn = s2str(a); 
    180181        if (fn != NULL) { 
     
    186187 
    187188static SExp builtin_compile(int s) { 
    188         SExp code = refer_stack(s, 0); 
     189        SExp code = get_arg(s, 0); 
    189190        return compile_ontop(code); 
    190191} 
  • lang/c/misc/mlisp/core/mlisp.cpp

    r11550 r11617  
    2525SExp evaluate(SExp code) { 
    2626        SExp c = compile_ontop(code); 
    27 //print(c); 
     27print(c); 
    2828        return run(c); 
    2929} 
  • lang/c/misc/mlisp/core/s_print.cpp

    r11550 r11617  
     1//============================================================================= 
     2///     S���̏o�� 
     3//============================================================================= 
     4 
    15#include "inner.h" 
    26#include "s_util.h" 
  • lang/c/misc/mlisp/core/s_vect.cpp

    r11550 r11617  
    11//============================================================================= 
    2 ///     S ������/** 
    3         mallloc �Ŋm�ۂ����A�h���X�͂S�̔{���Ɖ���*/ 
     2///     �x�N�^ 
    43//============================================================================= 
    54 
  • lang/c/misc/mlisp/core/v_vm.cpp

    r11550 r11617  
    2323static SExp g_stack[STACK_SIZE]; 
    2424 
    25  
    2625inline static SExp stack_ref(int idx) { 
    2726        assert(0 <= idx && idx < STACK_SIZE); 
     
    3130 
    3231inline static SExp index(int s, int i) { 
    33         return g_stack[s - i - 2]; 
     32        return g_stack[s - i - 1]; 
    3433} 
    3534 
    3635inline static void index_set(int s, int i, SExp v) { 
    37         g_stack[s - i - 2] = v; 
     36        g_stack[s - i - 1] = v; 
    3837} 
    3938 
     
    5150static int shift_args(int n, int m, int s) { 
    5251        for (int i=n-1; --i > -2; ) { 
    53                 index_set(s, i + m + 1, index(s, i)); 
     52                index_set(s, i + m + 2, index(s, i + 1)); 
    5453        } 
    5554        return s - m - 1; 
     
    5958 
    6059 
    61 SExp refer_stack(int s, int i) { 
    62         return index(s, i); 
     60sint get_arg_num(int s) { 
     61        return s2int(index(s, 0)); 
     62} 
     63 
     64SExp get_arg(int s, int i) { 
     65        return index(s, i + 1); 
    6366} 
    6467 
     
    6770        printf("#%d[", s); 
    6871        for (int i=0; i<s; ++i) { 
    69                 print_rec(refer_stack(s, i-1)); 
    70                 printf(" "); 
     72                if (i != 0)     printf(","); 
     73                print_rec(g_stack[i]); 
    7174        } 
    7275        printf("]\n"); 
     
    176179 
    177180static int vm_return(SExp* px, sint* pf, SExp* pc, int s) { 
    178         int n = s2int(index(s, -1)); 
    179         int ss = s - n; 
     181        int n = s2int(index(s, 0)); 
     182        int ss = s - n - 1; 
    180183        *px = index(ss, 0);     *pf = s2int(index(ss, 1));      *pc = index(ss, 2); 
    181         return ss - (3 + 1); 
     184        return ss - 3; 
    182185} 
    183186 
     
    194197                SExp op = car(x); 
    195198 
    196 //              dump_stack(s); 
    197 //              print(op); 
     199                dump_stack(s); 
     200                print(op); 
    198201 
    199202                if (op == HALT) { 
     
    213216                        sint n = s2int(cadr(x)); 
    214217                        SExp xx = caddr(x); 
    215                         a = index(f, n);        x = xx; 
     218                        a = index(f, n + 1);    x = xx; 
    216219                } else if (op == REFER_FREE) { 
    217220                        sint n = s2int(cadr(x)); 
     
    255258                } else if (op == SHIFT) { 
    256259                        sint n = s2int(cadr(x)); 
    257                         sint m = s2int(index(s, n - 1)); 
     260                        sint m = s2int(index(s, n)); 
    258261                        SExp xx = caddr(x); 
    259262                        x = xx; s = shift_args(n, m, s); 
     
    264267                        SExp stack = cadr(x); 
    265268                        SExp xx = caddr(x); 
    266                         x = xx; s = restore_stack(stack); 
     269                        x = xx; s = push(int2s(0), restore_stack(stack)); 
    267270                } else if (op == DEFINE) { 
    268271                        SExp var = cadr(x);