- Timestamp:
- 05/22/08 08:41:22 (6 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 9 modified
-
core/c_compiler.cpp (modified) (4 diffs)
-
core/c_compiler.h (modified) (1 diff)
-
core/inner.h (modified) (1 diff)
-
core/m_basic.cpp (modified) (5 diffs)
-
core/mlisp.cpp (modified) (1 diff)
-
core/s_util.cpp (modified) (1 diff)
-
core/s_util.h (modified) (1 diff)
-
proto/stackbased.scm (modified) (5 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/core/c_compiler.cpp
r12093 r12180 310 310 } 311 311 312 static SExp quote(SExp s ) { return list(2, intern("quote"), s); }312 static SExp quote(SExp s, void*) { return list(2, intern("quote"), s); } 313 313 314 314 static SExp gen_macro_call(Procedure* p, SExp args) { 315 SExp qargs = mapcar(quote, args );315 SExp qargs = mapcar(quote, args, NULL); 316 316 return cons(p->u.cell.s, qargs); 317 317 } … … 323 323 } else { 324 324 return evaluate(gen_macro_call(p, args)); 325 } 326 } 327 328 329 /// �}�N����ēW�J 330 static SExp compile_macroexpand_all_compound(int n, SExp x, SExp e); 331 static SExp compile_macroexpand_all(SExp x, SExp e) { 332 if (consp(x)) { 333 SExp op = car(x); 334 if (eq(op, intern("quote"))) { 335 return x; 336 } else if (eq(op, intern("lambda"))) { 337 SExp vars = cadr(x); 338 SExp newe = append2(dotted2proper(vars), e); 339 return compile_macroexpand_all_compound(2, x, newe); 340 } else if (eq(op, intern("if"))) { 341 return compile_macroexpand_all_compound(1, x, e); 342 } else if (eq(op, intern("set!"))) { 343 return compile_macroexpand_all_compound(2, x, e); 344 } else if (eq(op, intern("call/cc"))) { 345 return compile_macroexpand_all_compound(1, x, e); 346 } else if (eq(op, intern("define"))) { 347 SExp var_body = cdr(x); 348 for (;;) { 349 SExp var = car(var_body); 350 if (consp(var)) { 351 SExp body = cdr(var_body); 352 var_body = list(2, car(var), cons(intern("lambda"), cons(cdr(var), body))); 353 } else { 354 return compile_macroexpand_all_compound(2, cons(intern("define"), var_body), e); 355 } 356 } 357 } else if (eq(op, intern("defmacro"))) { 358 return x; 359 } else { 360 SExp fn = car(x); 361 SExp args = cdr(x); 362 SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 363 if (macrop(gval)) { 364 return compile_macroexpand_all(transform_macro(gval, args), e); 365 } else { 366 return compile_macroexpand_all_compound(0, x, e); 367 } 368 } 369 } else { 370 return x; 371 } 372 } 373 374 static SExp compile_macroexpand_all_compound(int n, SExp x, SExp e) { 375 if (n > 0) { 376 return cons(car(x), compile_macroexpand_all_compound(n-1, cdr(x), e)); 377 } else { 378 struct Local { 379 SExp e; 380 static SExp cb(SExp x, void* param) { 381 Local* l = (Local*)param; 382 return compile_macroexpand_all(x, l->e); 383 } 384 }; 385 Local l = { e }; 386 return mapcar(Local::cb, x, &l); 325 387 } 326 388 } … … 359 421 } 360 422 423 // �}�N����i�W�J 424 SExp macroexpand_1(SExp x) { 425 SExp fn = car(x); 426 SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 427 if (macrop(gval)) { 428 SExp args = cdr(x); 429 return transform_macro(gval, args); 430 } else { 431 return x; 432 } 433 } 434 361 435 // �K�p����p�C�� 362 436 static SExp compile_apply(SExp x, SExp e, SExp s, SExp next) { 363 SExp fn = car(x); 364 SExp args = cdr(x); 365 SExp gval = (symbolp(fn) && exist_global(fn)) ? refer_global(fn) : nil; 366 if (macrop(gval)) { 367 return compile(transform_macro(gval, args), e, s, next); 368 } else { 437 SExp expanded = macroexpand_1(x); 438 if (!eq(expanded, x)) { 439 return compile(expanded, e, s, next); 440 } else { 441 SExp args = cdr(x); 369 442 int argnum = length(args); 370 443 SExp apply = list(2, APPLY, int2s(argnum)); … … 445 518 return nil; 446 519 } 520 521 522 /// �g�b�v���x���ŃR���p�C�� 523 SExp compile_ontop(SExp code) { 524 SExp halt_code = cons(HALT, nil); 525 SExp expanded = compile_macroexpand_all(code, nil); 526 return compile(expanded, list(1, nil), nil, halt_code); 527 } -
lang/c/misc/mlisp/core/c_compiler.h
r11550 r12180 15 15 16 16 SExp is_macro(SExp name); 17 SExp macroexpand_1(SExp m, SExp x);18 17 19 18 #ifdef __cplusplus -
lang/c/misc/mlisp/core/inner.h
r11756 r12180 111 111 sint get_arg_num(void); 112 112 SExp get_arg(int idx); 113 114 SExp macroexpand_1(SExp x); -
lang/c/misc/mlisp/core/m_basic.cpp
r12094 r12180 210 210 } 211 211 212 static SExp builtin_macroexpand_1() { 213 SExp a = get_arg(0); 214 return macroexpand_1(a); 215 } 216 212 217 static SExp builtin_load() { 213 218 SExp a = get_arg(0); … … 244 249 //============================================================================= 245 250 246 static SExp transform_quasiquote_loop(SExp x ) {251 static SExp transform_quasiquote_loop(SExp x, void*) { 247 252 if (!consp(x)) 248 253 return list(2, intern("quote"), list(1, x)); … … 252 257 return cadr(x); 253 258 else { 254 SExp sub = mapcar(transform_quasiquote_loop, x );259 SExp sub = mapcar(transform_quasiquote_loop, x, NULL); 255 260 return list(2, intern("list"), cons(intern("append"), sub)); 256 261 } … … 258 263 259 264 static SExp builtin_quasiquote(SExp args) { 260 SExp res = transform_quasiquote_loop(car(args) );265 SExp res = transform_quasiquote_loop(car(args), NULL); 261 266 SExp h = car(res); 262 267 if (eq(h, intern("list"))) … … 321 326 { "print", builtin_print, 1, 1, }, 322 327 { "eval", builtin_eval, 1, 1, }, 328 { "macroexpand-1", builtin_macroexpand_1, 1, 1, }, 323 329 324 330 { "load", builtin_load, 1, 1, }, -
lang/c/misc/mlisp/core/mlisp.cpp
r11789 r12180 14 14 SExp run(SExp c) { 15 15 return vm(nil, c, 0, nil); 16 }17 18 /// �g�b�v���x���ŃR���p�C��19 SExp compile_ontop(SExp code) {20 SExp halt_code = cons(HALT, nil);21 return compile(code, list(1, nil), nil, halt_code);22 16 } 23 17 -
lang/c/misc/mlisp/core/s_util.cpp
r11715 r12180 63 63 } 64 64 65 SExp mapcar(SExp (*fn)(SExp ), SExp ls) {65 SExp mapcar(SExp (*fn)(SExp, void*), SExp ls, void* param) { 66 66 SExp acc = nil; 67 67 for (; !nilp(ls); ls = cdr(ls)) { 68 acc = cons(fn(car(ls) ), acc);68 acc = cons(fn(car(ls), param), acc); 69 69 } 70 70 return nreverse(acc); -
lang/c/misc/mlisp/core/s_util.h
r12094 r12180 30 30 31 31 /// ���X�g�̊e�v�f�Ɋ���p�i���X�g�͂P��� 32 SExp mapcar(SExp (*fn)(SExp ), SExp ls);32 SExp mapcar(SExp (*fn)(SExp, void*), SExp ls, void* param); 33 33 34 34 /// alist ((key . value) ...) ������ -
lang/c/misc/mlisp/proto/stackbased.scm
r12093 r12180 62 62 ;; マクロ 63 63 64 65 64 (define (compile-defmacro name vars body) 66 65 (define-global name (gen-macro vars body))) … … 89 88 90 89 90 ;; マクロの全展開、文法に従って修正する必要あり 91 (define (compile-macroexpand-all x e) 92 ; ドット対も処理する map 93 (define (mapd fn ls) 94 (cond ((pair? ls) (cons (fn (car ls)) (mapd fn (cdr ls)))) 95 ((null? ls) ls) 96 (else (fn ls)))) 97 (define (expand-compound n x e) 98 (cond ((null? x) x) 99 ; 頭の n 個は展開せず、 100 ((> n 0) (cons (car x) 101 (expand-compound (-1+ n) (cdr x) e))) 102 ; n 個以降は展開 103 (else (mapd (lambda (s) (compile-macroexpand-all s e)) x)))) 104 (cond 105 ((pair? x) 106 (case x 107 ((quote) x) 108 ((lambda) (let* ((vars (cadr x)) 109 (newe (append (dotted->proper vars) e))) 110 (expand-compound 2 x newe))) 111 ((if) (expand-compound 1 x e)) 112 ((set!) (expand-compound 2 x e)) 113 ((call/cc) (expand-compound 1 x e)) 114 ((define) (recur loop ((var-body (cdr x))) 115 (let ((var (car var-body)) 116 (body (cdr var-body))) 117 (if (pair? var) 118 (loop `(,(car var) (lambda ,(cdr var) ,@body))) 119 (expand-compound 2 `(define ,@var-body) e))))) 120 ((defmacro) x) 121 (else 122 (let* ((fn (car x)) 123 (args (cdr x)) 124 (gval (and (symbol? fn) 125 (exist-global? fn) 126 (refer-global fn)))) 127 (if (macro? gval) 128 (compile-macroexpand-all (transform-macro gval args) e) 129 (expand-compound 0 x e)))))) 130 (else x))) 131 132 91 133 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 92 134 ;; compiler … … 94 136 ;; トップレベルでコンパイル 95 137 (define (compile-top x) 96 (compile x '(()) '() '(HALT))) 138 (compile (compile-macroexpand-all x '()) 139 '(()) 140 '() 141 '(HALT))) 97 142 98 143 ;; 複文をコンパイル … … 355 400 (define VM 356 401 (lambda (a x f c) 357 (write/ss (list (car x) (dump-stack *sp*)))358 (newline)402 ; (write/ss (list (car x) (dump-stack *sp*))) 403 ; (newline) 359 404 (record-case x 360 405 (HALT () a) … … 609 654 610 655 656 657 658 659 660 611 661 (define (dump-stack s) 612 662 (subseq *stack* 0 s)) -
lang/c/misc/mlisp/readme.txt
r12094 r12180 7 7 8 8 * �m�[�g 9 - repl ���� quit�v�Ƒł����ނƔ�����- �V���{���̑啶���Ə�������ʂ���9 - repl ����(exit)�v�Ƒł����ނƔ�����- �V���{���̑啶���Ə�������ʂ��� 10 10 - ������ 11 11 -- ���� … … 52 52 - ��s���ɕϐ�������������Ƃ��̎����� 53 53 - �������� 54 - �}�N�����g�����Ƃ��́A���R�ϐ���t!�̎g�p�̃`�F�b�N54 -[v] �}�N�����g�����Ƃ��́A���R�ϐ���t!�̎g�p�̃`�F�b�N 55 55 56 56
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)