- Timestamp:
- 05/24/08 22:42:00 (6 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 11 modified
-
core/inner.h (modified) (1 diff)
-
core/m_basic.cpp (modified) (1 diff)
-
core/m_vect.cpp (modified) (2 diffs)
-
core/s_print.cpp (modified) (8 diffs)
-
core/s_read.cpp (modified) (5 diffs)
-
core/sexp.cpp (modified) (3 diffs)
-
core/v_vm.cpp (modified) (9 diffs)
-
inc/sexp.h (modified) (2 diffs)
-
proto/stackbased.scm (modified) (7 diffs)
-
readme.txt (modified) (3 diffs)
-
test/main.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/core/inner.h
r12268 r12283 15 15 16 16 /// �G���[ 17 enum {18 17 // read ���̃G���[ 19 ERR_READ_ILLEGAL_CHAR = 1, 20 ERR_READ_STRING_NOT_TERMINATE, 21 ERR_READ_NO_CLOSE_PAREN, 22 ERR_READ_DOT_FIRST, 23 ERR_READ_EMPTY_AFTER_DOT, 24 ERR_READ_ILLEGAL_DOT_LIST, 25 18 #define ERR_READ_ILLEGAL_CHAR "illegal char: %s" 19 #define ERR_READ_STRING_NOT_TERMINATE "string not terminate" 20 #define ERR_READ_NO_CLOSE_PAREN "no close paren" 21 #define ERR_READ_DOT_FIRST "dot first" 22 #define ERR_READ_EMPTY_AFTER_DOT "empty after dot" 23 #define ERR_READ_ILLEGAL_DOT_LIST "illegal dot list" 26 24 // �R���p�C�����̃G���[ 27 ERR_TYPE_REQUIRED, 28 ERR_UNDEFINED_SYMBOL, 29 ERR_UNEXPECTED, 30 }; 25 #define ERR_TYPE_REQUIRED "type required: %s" 26 #define ERR_UNDEFINED_SYMBOL "undefined symbol" 27 #define ERR_UNEXPECTED "unexpected" 31 28 32 29 -
lang/c/misc/mlisp/core/m_basic.cpp
r12263 r12283 270 270 return list(2, intern("quote"), list(1, car(cadr(res)))); 271 271 else { 272 error(ERR_UNEXPECTED );272 error(ERR_UNEXPECTED, 0, NULL); 273 273 return nil; 274 274 } -
lang/c/misc/mlisp/core/m_vect.cpp
r12263 r12283 14 14 unsigned int size; 15 15 SExp buf[1]; // �o�b�t�@�̃T�C�Y���L�т�}; 16 17 18 void add_vector_module(void) {19 tVect = mlisp_alloc_new_type();20 }21 16 22 17 … … 73 68 printf(")"); 74 69 } 70 71 72 73 74 75 static SExp fn_make_vector() { 76 sint n = s2int(get_arg(0)); 77 return make_vector(n); 78 } 79 80 static SExp fn_vector_set() { 81 SExp v = get_arg(0); 82 sint i = s2int(get_arg(1)); 83 SExp val = get_arg(2); 84 vector_set(v, i, val); 85 return nil; 86 } 87 88 static SExp fn_vector_ref() { 89 SExp v = get_arg(0); 90 sint i = s2int(get_arg(1)); 91 return vector_ref(v, i); 92 } 93 94 95 void add_vector_module(void) { 96 tVect = mlisp_alloc_new_type(); 97 98 struct { 99 const char* name; 100 SCFunc func; 101 int minarg; 102 int maxarg; 103 } static const tbl[] = { 104 { "make-vector", fn_make_vector, 1, 1, }, 105 { "vector-set!", fn_vector_set, 3, 3, }, 106 { "vector-ref", fn_vector_ref, 2, 2, }, 107 }; 108 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 109 SExp sym = intern(tbl[i].name); 110 SExp fn = gen_cfunc(tbl[i].func, tbl[i].minarg, tbl[i].maxarg); 111 define_global(sym, fn); 112 } 113 } -
lang/c/misc/mlisp/core/s_print.cpp
r12263 r12283 6 6 #include "s_util.h" 7 7 8 static void print_cell(SExp p) { 8 9 class Printer { 10 void (*cb)(const char*, void*); 11 void* ua; 12 13 inline void out(const char* s) { 14 this->cb(s, ua); 15 } 16 17 void print_cell(SExp p); 18 19 public: 20 Printer(void (*cb_)(const char*, void*), void* ua_) 21 : cb(cb_), ua(ua_) 22 { 23 } 24 25 void print_rec(SExp s); 26 }; 27 28 void Printer::print_cell(SExp p) { 9 29 bool b_first = true; 10 30 for (; type_of(p) == tCell; ) { 11 31 Cell* cell = (Cell*)p.ptr; 12 if (!b_first) printf(" ");32 if (!b_first) out(" "); 13 33 print_rec(cell->car); 14 34 p = cell->cdr; … … 16 36 } 17 37 if (!eq(p, nil)) { 18 printf(" . ");38 out(" . "); 19 39 print_rec(p); 20 40 } … … 22 42 23 43 24 void print_rec(SExp s) {44 void Printer::print_rec(SExp s) { 25 45 switch (type_of(s)) { 26 46 default: assert(FALSE); break; 27 47 case tInt: 28 printf("%d", s2int(s)); 48 { 49 char buf[16]; 50 sprintf(buf, "%d", s2int(s)); 51 out(buf); 52 } 29 53 break; 30 54 case tCell: … … 32 56 SExp a = car(s); 33 57 if (eq(a, intern("quote")) && singlep(cdr(s))) { 34 printf("'");58 out("'"); 35 59 print_rec(car(cdr(s))); 36 60 } else { 37 printf("(");61 out("("); 38 62 print_cell(s); 39 printf(")");63 out(")"); 40 64 } 41 65 } … … 44 68 { 45 69 Symbol* p = (Symbol*)s.ptr; 46 printf("%s",p->str);70 out(p->str); 47 71 } 48 72 break; … … 50 74 { 51 75 String* p = (String*)s.ptr; 52 printf("\"%s\"", p->str); 76 out("\""); 77 out(p->str); 78 out("\""); 53 79 } 54 80 break; … … 68 94 case Procedure::Macro | Procedure::Cell: type = "macro"; break; 69 95 } 70 printf("#<%s: %s>", type, name); 96 out("#<"); 97 out(type); 98 out(": "); 99 out(name); 100 out(">"); 71 101 } 72 102 break; … … 78 108 79 109 110 void print_to(SExp s, void (*cb)(const char*, void*), void* ua) { 111 Printer pr(cb, ua); 112 pr.print_rec(s); 113 } 114 115 116 void print_rec(SExp s) { 117 struct Local { 118 static void cb(const char* s, void* ua) { printf("%s", s); } 119 }; 120 print_to(s, Local::cb, NULL); 121 } 122 80 123 void print(SExp s) { 81 124 print_rec(s); -
lang/c/misc/mlisp/core/s_read.cpp
r11794 r12283 125 125 for (; c = strm->getch(), c != '"'; ) { 126 126 if (c == EOF) { 127 error(ERR_READ_STRING_NOT_TERMINATE );127 error(ERR_READ_STRING_NOT_TERMINATE, 0, NULL); 128 128 break; 129 129 } … … 178 178 if (s.i == cFail || s.i == cEOF) { 179 179 // ERR: (x .) 180 error(ERR_READ_EMPTY_AFTER_DOT );180 error(ERR_READ_EMPTY_AFTER_DOT, 0, NULL); 181 181 } else { 182 182 bool err = false; 183 183 if (eq(top, nil)) { 184 184 // ERR: (. x) 185 error(ERR_READ_DOT_FIRST );185 error(ERR_READ_DOT_FIRST, 0, NULL); 186 186 err = true; 187 187 } else { … … 195 195 if (err2 && !err) { 196 196 // ERR: (x . y z) 197 error(ERR_READ_ILLEGAL_DOT_LIST );197 error(ERR_READ_ILLEGAL_DOT_LIST, 0, NULL); 198 198 } 199 199 } … … 239 239 if (c != ')') { 240 240 strm->ungetch(c); 241 error(ERR_READ_NO_CLOSE_PAREN );241 error(ERR_READ_NO_CLOSE_PAREN, 0, NULL); 242 242 } 243 243 return s; … … 317 317 SExp s = read_rec(strm); 318 318 if (s.i == cFail) { 319 int c = strm->getch(); // �ǂݍ��ݎ��s�ɂȂ��������ݎ̂Ă� error(ERR_READ_ILLEGAL_CHAR, c); 319 int c = strm->getch(); // �ǂݍ��ݎ��s�ɂȂ��������ݎ̂Ă� SExp args[] = { int2s(c), }; 320 error(ERR_READ_ILLEGAL_CHAR, sizeof(args)/sizeof(*args), args); 320 321 s = nil; 321 322 } -
lang/c/misc/mlisp/core/sexp.cpp
r12263 r12283 9 9 #include <stdio.h> 10 10 #include <string.h> 11 #include <stdarg.h>12 13 /// �G���[14 static const char* errmsg[] = {15 "illegal char: %c",16 "string not terminate",17 "no close paren",18 "dot first",19 "empty after dot",20 "illegal dot list",21 22 "type required: %s",23 "undefined symbol",24 "unexpected",25 };26 11 27 12 /// �^���������tatic const char* TypeStrTbl[] = { … … 150 135 } 151 136 152 void error(int errid, ...) { 153 va_list ap; 154 va_start(ap, errid); 155 char buf[256]; 156 vsprintf(buf, errmsg[errid-1], ap); 157 va_end(ap); 158 137 void error(const char* errmsg, int narg, SExp* args) { 159 138 if (vtbl->error != NULL) { 160 (*vtbl->error)( buf);139 (*vtbl->error)(errmsg, narg, args); 161 140 } else { 162 exit( errid);141 exit(-1); 163 142 } 164 143 } … … 169 148 int type_check(SExp s, int type) { 170 149 if (type_of(s) != type) { 171 error(ERR_TYPE_REQUIRED, TypeStrTbl[type]); 150 SExp args[] = { gen_str(TypeStrTbl[type]), }; 151 error(ERR_TYPE_REQUIRED, sizeof(args)/sizeof(*args), args); 172 152 return FALSE; 173 153 } else { -
lang/c/misc/mlisp/core/v_vm.cpp
r12268 r12283 14 14 #include <assert.h> 15 15 16 static void runtime_error(const char* msg) { 17 printf("%s\n", msg); 18 error(ERR_UNEXPECTED); 16 #define ERR_UNBOUND_VARIABLE "unbound variable: %s" 17 #define ERR_SYMBOL_NOT_DEFINED "symbol not defined: %s" 18 19 static void runtime_error(const char* msg, int narg, SExp* args) { 20 error(msg, narg, args); 19 21 } 20 22 … … 48 50 static void push(SExp x) { 49 51 if (g_sp >= STACK_SIZE) { 50 runtime_error("stack overflow" );52 runtime_error("stack overflow", 0, NULL); 51 53 return; 52 54 } … … 56 58 static SExp pop() { 57 59 if (g_sp <= 0) { 58 runtime_error("stack underflow" );60 runtime_error("stack underflow", 0, NULL); 59 61 return nil; 60 62 } … … 65 67 g_sp -= n; 66 68 if (g_sp < 0) { 67 runtime_error("stack underflow" );69 runtime_error("stack underflow", 0, NULL); 68 70 } 69 71 } … … 124 126 return it->second; 125 127 } else { 126 runtime_error("unbound variable:"); // sym 128 SExp args[] = { sym }; 129 runtime_error(ERR_UNBOUND_VARIABLE, sizeof(args)/sizeof(*args), args); // sym 127 130 return nil; 128 131 } … … 183 186 if (n == proc->minnarg) { // REST �p�����[�^�ɉ��������Ȃ�����ꍇ�A�X�^�b�N�t���[�����������̂Ŋg������ const int m = n - proc->minnarg + 1; 184 187 for (int i=0; i<n; ++i) { 185 index_set(s, i , index(s, i + m));188 index_set(s, i - m, index(s, i)); 186 189 } 187 190 push(int2s(n + m)); … … 275 278 } 276 279 } else { 277 runtime_error("wrong number of argument"); 280 SExp args[] = { int2s(proc->minnarg), argnum }; 281 runtime_error("wrong number of argument: %d to %d", sizeof(args)/sizeof(*args), args); 278 282 r = -1; 279 283 } 280 284 } else { 281 runtime_error("can't apply"); 285 SExp args[] = { fn }; 286 runtime_error("can't apply: %s", sizeof(args)/sizeof(*args), args); 282 287 r = -1; 283 288 } … … 372 377 unlink(n); 373 378 a = aa; x = xx; 374 } else if (op == BOX) {375 sint n = s2int(cadr(x));376 SExp xx = caddr(x);377 index_set(g_sp, n + 1, box(index(g_sp, n + 1)));378 x = xx;379 379 } else if (op == ASSIGN_LOCAL) { 380 380 sint n = s2int(cadr(x)); … … 423 423 SExp var = cadr(x); 424 424 SExp xx = caddr(x); 425 define_global(var, a); 426 a = var; x = xx; 425 if (exist_global(var)) { 426 define_global(var, a); 427 } else { 428 SExp args[] = { var }; 429 runtime_error(ERR_SYMBOL_NOT_DEFINED, sizeof(args)/sizeof(*args), args); 430 } 431 x = xx; 432 } else if (op == BOX) { 433 sint n = s2int(cadr(x)); 434 SExp xx = caddr(x); 435 index_set(g_sp, n + 1, box(index(g_sp, n + 1))); 436 x = xx; 437 } else if (op == INDIRECT) { 438 SExp xx = cadr(x); 439 a = unbox(a); x = xx; 427 440 } else { 428 runtime_error("illegal opecode"); 441 SExp args[] = { op }; 442 runtime_error("illegal opecode: %s", sizeof(args)/sizeof(*args), args); 429 443 return nil; 430 444 } -
lang/c/misc/mlisp/inc/sexp.h
r12263 r12283 59 59 60 60 /// �G���[���ɌĂяo���� 61 void (*error)(const char* errmsg );61 void (*error)(const char* errmsg, int narg, SExp* args); 62 62 } SVTable; 63 63 … … 97 97 SExp read_from_string(const char* str); 98 98 void print(SExp s); 99 void print_to(SExp s, void (*cb)(const char*, void*), void* ua); 99 100 100 void error( int errid, ...);101 void error(const char* errmsg, int narg, SExp* arg); 101 102 void reset_error(void); 102 103 -
lang/c/misc/mlisp/proto/stackbased.scm
r12268 r12283 104 104 (cond 105 105 ((pair? x) 106 (case x106 (case (car x) 107 107 ((quote) x) 108 108 ((lambda) (let* ((vars (cadr x)) … … 461 461 (modify-args a n) 462 462 (VM a body *sp* a)))) 463 (error " wrong number of argument"))463 (error "stackbased: wrong number of argument")) 464 464 (error "can't apply"))) 465 465 (RETURN () … … 477 477 (ASSIGN-GLOBAL (var x) 478 478 (if (exist-global? var) 479 (VM (define-global var a) x f c) 479 (begin 480 (define-global var a) 481 (VM a x f c)) 480 482 (error "assign to undefined symbol:" var))) 481 483 (else … … 571 573 (define (expand-frame n m) 572 574 (dotimes (i n '()) 573 (index-set! s i (index s (+ i m))))575 (index-set! s (- i m) (index s i))) 574 576 (push (+ n m))) 575 577 (let* ((min (closure-min-arg-num c)) … … 583 585 (let ((m (1+ (- n min)))) 584 586 (expand-frame n m) 585 (inc! s (+ n m))))587 (inc! s m))) 586 588 (index-set! s min rest)) 587 589 (error "not implemented")))))) … … 856 858 857 859 (define (repl) 860 (init-stack) 858 861 (let ((s (read))) 859 862 (unless (eq? s 'quit) … … 878 881 879 882 883 880 884 (load "test_macro.scm") 881 885 882 886 883 884 885 (macroexpand-1 '(let ((x 1) (y 2)) (+ x y))) 886 887 888 889 (defmacro test (a . b) (list 'list a b)) 890 891 892 (test 1 2) 893 894 895 896 (evaluate '((lambda (a b . c) (list a b c)) 1 2 3 4)) 897 898 899 900 901 (compile-top '((lambda (a . b) (list 'list a b)) '1 '2)) 902 903 904 (let ((x 1) (y 2)) (cons x y)) 905 906 907 (call/cc (lambda (cc) (cc 1234))) 908 909 910 1234 911 912 (load "test_nondet.scm") 913 887 (macroexpand-1 888 '(letrec ((loop (lambda (n acc) 889 (if (> n 0) 890 (loop (- n 1) (+ acc n)) 891 acc)))) 892 (loop 10 0))) 893 894 895 letrec 896 897 898 899 900 901 (letrec ((loop (lambda (n acc) 902 (if (> n 0) 903 (loop (- n 1) (+ acc n)) 904 acc)))) 905 (loop 10 0)) 914 906 915 907 -
lang/c/misc/mlisp/readme.txt
r12268 r12283 47 47 48 48 * ToDo 49 -[v] � C�ӌ̈��O����Ɨ�����-�G���[���b�Z�[�W���Ƃ킩������49 -[v] �G���[���b�Z�[�W���Ƃ킩������ 50 50 -begin 51 51 -[v] �x�N�^��W���[���Ƃ��Ď������-[v] �X�^�b�N�x�[�X�ɒu��������-[v] �C�ӌ̈��� 52 52 -[v] �}�N����� 53 53 --[v] macroexpand ��-[v] C �������[�o���̊���яo�������ɂ���- �unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 54 - ��s���ɕϐ�������������Ƃ��̎�����54 -[v] ��s���ɕϐ�������������Ƃ��̎����� 55 55 - �������� 56 56 -[v] �}�N�����g�����Ƃ��́A���R�ϐ���t!�̎g�p�̃`�F�b�N … … 61 61 -- define �̈��V���{�����H 62 62 -- ���Ăяo���`�F�b�N 63 --- �����ǂ����H
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)