Changeset 5972 for lang/c/misc

Show
Ignore:
Timestamp:
02/01/08 00:46:41 (5 years ago)
Author:
mokehehe
Message:

文字列実装
シンボルのfindとmakeを統合
print修正

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

Legend:

Unmodified
Added
Removed
  • lang/c/misc/mlisp/readme.txt

    r5924 r5972  
    5151* todo 
    5252 
    53 - �����Ƃ��������� ���L�V�J���X�R�[�v�A�N���[�W�� 
     53- ���L�V�J���X�R�[�v�A�N���[�W�� 
    5454- �K�x�R�� 
    5555- assert �Ŏ��ȂȂ��悤�� 
     
    6262- &optional, &rest 
    6363 
    64 -[v] �}�N�����Łu`�v�u,�v�u,@�v��������� 
     64-[v] �����Ƃ���������[v] �}�N�����Łu`�v�u,�v�u,@�v��������� 
    6565-- �u`�v�u,�v����ς� 
    6666-- �u,@�v����ς� 
  • lang/c/misc/mlisp/src/main.c

    r5814 r5972  
    4141                        } 
    4242                        if (b_print_read) { 
    43                                 ml_print(ml, ofp, r, FALSE); 
     43                                ml_print(ml, ofp, r); 
    4444                        } 
    4545 
     
    4747                } 
    4848                if (e != cNL) { 
    49                         ml_print(ml, ofp, e, FALSE); 
     49                        ml_print(ml, ofp, e); 
    5050                } 
    5151        } 
  • lang/c/misc/mlisp/src/ml_builtin.c

    r5924 r5972  
    5252                        SExp b; 
    5353                        if (SEXP_CONSP(ca)) { 
    54                                 SExp unq = ml_make_symbol(ml, "unquote", NULL); 
    55                                 SExp unqa = ml_make_symbol(ml, "unquote-splicing", NULL); 
    5654                                SExp caa = ml_car(ml, ca); 
    57                                 if (caa == unq) { 
     55                                if (caa == ml_find_symbol(ml, "unquote", NULL, TRUE)) { 
    5856                                        b = ml_eval(ml, ml_car(ml, ml_cdr(ml, ca))); 
    59                                 } else if (caa == unqa) { 
     57                                } else if (caa == ml_find_symbol(ml, "unquote-splicing", NULL, TRUE)) { 
    6058                                        b = ml_eval(ml, ml_car(ml, ml_cdr(ml, ca))); 
    6159                                        ml_rplacd(ml, tail, b); 
     
    291289{ 
    292290        FILE* fp = stdout; 
    293         ml_print(ml, fp, a, TRUE); 
    294         return cNil; 
     291        SExp ca = ml_car(ml, a); 
     292        ml_print(ml, fp, ca); 
     293        return ca; 
    295294} 
    296295 
  • lang/c/misc/mlisp/src/mlisp.cpp

    r5924 r5972  
    122122        SExp read(FILE* fp); 
    123123        SExp eval(SExp a); 
    124         void print(FILE* fp, SExp a, bool b_cell); 
     124        void print(FILE* fp, SExp a); 
    125125        SExp apply(SExp ca, SExp cd, bool b_eval); 
    126126 
     
    142142        void runtime_error(SExp a, const char* msg); 
    143143 
    144         SExp make_symbol(const char* str, const char* end); 
     144        SExp find_symbol(const char* str, const char* end, bool b_make); 
    145145 
    146146private: 
     
    158158        SExp alloc_symbol(); 
    159159        Symbol* get_symbol_ptr(SExp a); 
    160         SExp find_symbol(const char* str, const char* end); 
    161160 
    162161        SExp eval_cell(SExp a);                                                 ///< �Z���������car �̊֐��Ɏc�������ČĂяo���j 
     
    296295        if (a != cNil) { 
    297296                fprintf(fp, " :"); 
    298                 print(fp, a, false); 
     297                print(fp, a); 
    299298        } else { 
    300299                fprintf(fp, "\n"); 
     
    434433Symbol* MLContext::get_symbol_ptr(SExp a) 
    435434{ 
    436         assert(SEXP_TYPE(a) == tSymb); 
     435        assert(SEXP_TYPE(a) == tSymb || SEXP_TYPE(a) == tStr); 
    437436        return &g_symbol_buf[SEXP_UVAL(a)]; 
    438437} 
    439438 
    440 SExp MLContext::find_symbol(const char* str, const char* end) 
     439SExp MLContext::find_symbol(const char* str, const char* end, bool b_make) 
    441440{ 
    442441        int len; 
     
    454453                } 
    455454        } 
    456         return cFail; 
    457 } 
    458  
    459 SExp MLContext::make_symbol(const char* str, const char* end) 
    460 { 
    461         SExp a; 
    462         a = find_symbol(str, end); 
    463         if (a == cFail) { 
    464                 a = alloc_symbol(); 
     455 
     456 
     457        if (b_make) { 
     458                SExp a = alloc_symbol(); 
    465459                Symbol* p = get_symbol_ptr(a); 
    466460                p->sexp = cUndef; 
     
    474468                        p->str[len] = '\0'; 
    475469                } 
    476         } 
    477         return a; 
     470                return a; 
     471        } 
     472 
     473        return cFail; 
    478474} 
    479475 
     
    492488        SExp fn = MAKE_SEXP(tFunc, SEXP_UVAL(cell)); 
    493489 
    494         SExp symb = make_symbol(word, NULL); 
     490        SExp symb = find_symbol(word, NULL, true); 
    495491        setq(symb, fn); 
    496492        return symb; 
     
    535531        *q = '\0'; 
    536532 
    537         SExp a = make_symbol(str, q); 
    538         return cons(find_symbol("quote", NULL), cons(a, cNil)); 
     533        SExp a = find_symbol(str, q, true); 
     534        return MAKE_SEXP(tStr, SEXP_UVAL(a)); 
    539535} 
    540536 
     
    621617                { 
    622618                        SExp a = read_rec(fp); 
    623                         return cons(make_symbol("quote", NULL), cons(a, cNil)); 
     619                        return cons(find_symbol("quote", NULL, true), cons(a, cNil)); 
    624620                } 
    625621        case '`': 
    626622                { 
    627623                        SExp a = read_rec(fp); 
    628                         return cons(make_symbol("quasiquote", NULL), cons(a, cNil)); 
     624                        return cons(find_symbol("quasiquote", NULL, true), cons(a, cNil)); 
    629625                } 
    630626        case ',': 
     
    634630                        if (c != '@') { 
    635631                                ungetc(c, fp); 
    636                                 fn = make_symbol("unquote", NULL); 
     632                                fn = find_symbol("unquote", NULL, true); 
    637633                        } else { 
    638                                 fn = make_symbol("unquote-splicing", NULL); 
     634                                fn = find_symbol("unquote-splicing", NULL, true); 
    639635                        } 
    640636                        SExp a = read_rec(fp); 
     
    665661                        SExp a = reserved(symb, q-symb); 
    666662                        if (a == cFail) { 
    667                                 a = make_symbol(symb, q); 
     663                                a = find_symbol(symb, q, true); 
    668664                        } 
    669665                        return a; 
     
    699695        case tOther: 
    700696        case tFunc: 
     697        case tStr: 
    701698                return a; 
    702699        case tSymb: 
     
    752749 
    753750                        if (flags[MLF_PRINT_FUNC_ARG]) { 
    754                                 print(stdout, param, false); 
     751                                print(stdout, param); 
    755752                        } 
    756753                        bind_arg(arg, param); 
     
    759756                                SExp replace = eval(body); 
    760757                                if (flags[MLF_PRINT_MACRO_REPLACE]) { 
    761                                         print(stdout, replace, false); 
     758                                        print(stdout, replace); 
    762759                                } 
    763760                                body = replace; 
     
    817814                print_rec(stdout, symb); 
    818815                printf(":"); 
    819                 print(stdout, val, false); 
     816                print(stdout, val); 
    820817        } 
    821818} 
     
    879876                        Symbol* p = get_symbol_ptr(a); 
    880877                        fprintf(fp, "%s", p->str); 
     878                } 
     879                break; 
     880        case tStr: 
     881                { 
     882                        Symbol* p = get_symbol_ptr(a); 
     883                        fprintf(fp, "\"%s\"", p->str); 
    881884                } 
    882885                break; 
     
    911914} 
    912915 
    913 void MLContext::print(FILE* fp, SExp a, bool b_cell) 
    914 { 
    915         if (!b_cell) { 
    916                 print_rec(fp, a); 
    917         } else { 
    918                 print_cell(fp, a); 
    919         } 
     916void MLContext::print(FILE* fp, SExp a) 
     917{ 
     918        print_rec(fp, a); 
    920919        printf("\n"); 
    921920} 
     
    934933SExp ml_read(MLContext* ml, FILE* fp)                                                           { return ml->read(fp); } 
    935934SExp ml_eval(MLContext* ml, SExp a)                                                                     { return ml->eval(a); } 
    936 void ml_print(MLContext* ml, FILE* fp, SExp a, int b_cell)                      { ml->print(fp, a, b_cell != FALSE); } 
     935void ml_print(MLContext* ml, FILE* fp, SExp a)                                          { ml->print(fp, a); } 
    937936SExp ml_apply(MLContext* ml, SExp ca, SExp cd)                                          { return ml->apply(ca, cd, false); } 
    938937 
     
    950949 
    951950void ml_setq(MLContext* ml, SExp symb, SExp arg_body)                           { ml->setq(symb, arg_body); } 
    952 SExp ml_make_symbol(MLContext* ml, const char* str, const char* end)    { return ml->make_symbol(str, end); } 
     951SExp ml_find_symbol(MLContext* ml, const char* str, const char* end, int b_make)        { return ml->find_symbol(str, end, b_make != FALSE); } 
    953952 
    954953const char* ml_get_symbol_string(MLContext* ml, SExp symb)                      { return ml->get_symbol_string(symb); } 
  • lang/c/misc/mlisp/src/mlisp.h

    r5814 r5972  
    4545SExp ml_read(MLContext* ml, FILE* fp); 
    4646SExp ml_eval(MLContext* ml, SExp a); 
    47 void ml_print(MLContext* ml, FILE* fp, SExp a, int b_cell); 
     47void ml_print(MLContext* ml, FILE* fp, SExp a); 
    4848SExp ml_apply(MLContext* ml, SExp ca, SExp cd); 
    4949 
     
    6565 
    6666void ml_setq(MLContext* ml, SExp symb, SExp arg_body);          ///< setq 
    67 SExp ml_make_symbol(MLContext* ml, const char* str, const char* end);           ///< �V���{���쐬 
     67SExp ml_find_symbol(MLContext* ml, const char* str, const char* end, int b_make);               ///< �V���{���쐬 
    6868 
    6969const char* ml_get_symbol_string(MLContext* ml, SExp symb);     ///< symbol string 
     
    8383#define tOther                          (10)    ///< other (nil, t, eof) 
    8484#define tFunc                           (14)    ///< function (lambda or macro) 
     85#define tStr                            (18)    ///< string 
    8586 
    8687/// macros