- Timestamp:
- 05/17/08 22:52:26 (6 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 5 modified
-
core/c_compiler.cpp (modified) (4 diffs)
-
core/m_basic.cpp (modified) (5 diffs)
-
core/mlisp.cpp (modified) (2 diffs)
-
core/v_vm.cpp (modified) (4 diffs)
-
proto/stackbased.scm (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/core/c_compiler.cpp
r11756 r11789 78 78 } else if (eq(op, intern("lambda"))) { 79 79 SExp vars = cadr(x); 80 SExp body = caddr(x); 81 return find_sets(body, set_minus(v, vars)); 80 SExp body = cddr(x); 81 SExp sets = set_minus(v, vars); 82 for (SExp p = body; !nilp(p); p = cdr(p)) { 83 SExp x = car(p); 84 sets = find_sets(x, sets); 85 } 86 return sets; 82 87 } else if (eq(op, intern("if"))) { 83 88 SExp test = cadr(x); … … 190 195 } else if (eq(op, intern("lambda"))) { 191 196 SExp vars = cadr(x); 192 SExp body = caddr(x); 193 return find_free(body, set_union(vars, b)); 197 SExp body = cddr(x); 198 SExp free = set_union(vars, b); 199 for (SExp p = body; !nilp(p); p = cdr(p)) { 200 SExp x = car(p); 201 free = find_free(x, free); 202 } 203 return free; 194 204 } else if (eq(op, intern("if"))) { 195 205 SExp test = cadr(x); … … 307 317 } 308 318 309 static SExp compile_macroexpand_1(SExp m, SExp args) {319 static SExp transform_macro(SExp m, SExp args) { 310 320 Procedure* p = (Procedure*)m.ptr; 311 321 if (p->get_func_type() == Procedure::Builtin) { 312 322 return p->u.mfunc(args); 313 323 } else { 314 return gen_macro_call(p, args); 315 } 316 } 317 318 static SExp transform_macro(SExp m, SExp args) { 319 return compile_macroexpand_1(m, args); 324 return evaluate(gen_macro_call(p, args)); 325 } 320 326 } 321 327 … … 415 421 SExp xx = cadr(x); 416 422 SExp apply = list(2, APPLY, int2s(1)); 417 SExp nx = is_tail ? list(3, SHIFT, 1, apply) : apply;423 SExp nx = is_tail ? list(3, SHIFT, int2s(1), apply) : apply; 418 424 SExp c = list(2, CONTI, list(2, ARGUMENT, compile(xx, e, s, nx))); 419 425 if (is_tail) -
lang/c/misc/mlisp/core/m_basic.cpp
r11756 r11789 60 60 } 61 61 return ls; 62 } 63 64 static SExp builtin_append() { 65 sint n = get_arg_num(); 66 if (n == 0) { 67 return nil; 68 } else { 69 SExp ls = get_arg(n - 1); 70 for (int i=n-1; --i>=0; ) { 71 ls = append2(get_arg(i), ls); 72 } 73 return ls; 74 } 62 75 } 63 76 … … 205 218 //============================================================================= 206 219 220 static SExp transform_quasiquote_loop(SExp x) { 221 if (!consp(x)) 222 return list(2, intern("quote"), list(1, x)); 223 else if (eq(car(x), intern("unquote"))) 224 return list(2, intern("list"), cadr(x)); 225 else if (eq(car(x), intern("unquote-splicing"))) 226 return cadr(x); 227 else { 228 SExp sub = mapcar(transform_quasiquote_loop, x); 229 return list(2, intern("list"), cons(intern("append"), sub)); 230 } 231 } 232 233 static SExp builtin_quasiquote(SExp args) { 234 SExp res = transform_quasiquote_loop(car(args)); 235 SExp h = car(res); 236 if (eq(h, intern("list"))) 237 return cadr(res); 238 else if (eq(h, intern("quote"))) 239 return list(2, intern("quote"), list(1, car(cadr(res)))); 240 else { 241 error(ERR_UNEXPECTED); 242 return nil; 243 } 244 } 245 246 247 //============================================================================= 248 249 static void define_basic_const() { 250 struct { 251 const char* name; 252 } static const tbl[] = { 253 "t", 254 "nil", 255 }; 256 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 257 SExp sym = intern(tbl[i].name); 258 define_global(sym, sym); 259 } 260 } 261 262 207 263 static void define_basic_proc() { 208 264 struct { … … 220 276 { "set-cdr!", builtin_rplacd, 2, 2, }, 221 277 { "list", builtin_list, 0, -1, }, 278 { "append", builtin_append, 0, -1, }, 222 279 223 280 { "+", builtin_plus, 0, -1, }, … … 246 303 247 304 248 static void define_basic_ const() {305 static void define_basic_macro() { 249 306 struct { 250 307 const char* name; 308 SCFuncM func; 309 int minarg; 310 int maxarg; 251 311 } static const tbl[] = { 252 "t", 253 "nil", 312 { "quasiquote", builtin_quasiquote, 1, 1, }, 254 313 }; 255 314 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 256 315 SExp sym = intern(tbl[i].name); 257 define_global(sym, sym); 316 SExp fn = gen_cmacro(tbl[i].func, tbl[i].minarg, tbl[i].maxarg); 317 define_global(sym, fn); 258 318 } 259 319 } … … 263 323 264 324 void define_basic_lib(void) { 325 define_basic_const(); 265 326 define_basic_proc(); 266 define_basic_ const();267 } 327 define_basic_macro(); 328 } -
lang/c/misc/mlisp/core/mlisp.cpp
r11756 r11789 25 25 SExp evaluate(SExp code) { 26 26 SExp c = compile_ontop(code); 27 print(c);27 //print(c); 28 28 return run(c); 29 29 } … … 37 37 return nil; 38 38 } else { 39 SExp res;40 39 for (;;) { 41 40 SExp sexp = read_from_file(fp); 42 41 if (nilp(sexp)) break; // @todo: EOF�̈��� 43 r es = run(compile_ontop(sexp));42 run(compile_ontop(sexp)); 44 43 } 45 44 fclose(fp); 46 return res;45 return t; 47 46 } 48 47 } -
lang/c/misc/mlisp/core/v_vm.cpp
r11756 r11789 197 197 198 198 static int restore_stack(SExp v) { 199 clear_stack(); 199 200 int s = vector_length(v); 200 clear_stack(); 201 for (int i=0; i<s; ) { 201 for (int i=0; i<s; ++i) { 202 202 push(vector_ref(v, i)); 203 203 } … … 257 257 case Procedure::Cell: 258 258 { 259 modify_args(proc, n, g_sp);259 modify_args(proc, n, ss - 1); 260 260 *px = closure_body(fn); *pf = ss; *pc = fn; 261 261 } … … 318 318 SExp op = car(x); 319 319 320 dump_stack(g_sp);321 print(op);320 // dump_stack(g_sp); 321 // print(op); 322 322 323 323 if (op == HALT) { … … 396 396 397 397 restore_stack(stack); 398 push(int2s(0));399 398 x = xx; 400 399 } else if (op == DEFINE) { -
lang/c/misc/mlisp/proto/stackbased.scm
r11756 r11789 82 82 (map (lambda (s) (list 'quote s)) args))) ; 引数を全てクォートでくくる 83 83 84 (define ( compile-macroexpand-1m args)84 (define (transform-macro m args) 85 85 (let ((body (slot-ref m 'body))) 86 86 (if (procedure? body) 87 87 (apply body args) 88 (gen-macro-call m args)))) 89 90 (define (transform-macro m args) 91 (evaluate (compile-macroexpand-1 m args))) 88 (evaluate (gen-macro-call m args))))) 92 89 93 90 … … 247 244 (record-case x 248 245 (quote (obj) '()) 249 (lambda (vars body) 250 (find-free body (set-union vars b))) 246 (lambda (vars) 247 (let ((body (cddr x))) 248 (fold (lambda (x r) (find-free x r)) 249 (set-union vars b) 250 body))) 251 251 (if (test then else) 252 252 (set-union (find-free test b) … … 272 272 (record-case x 273 273 (quote (obj) '()) 274 (lambda (vars body) 275 (find-sets body (set-minus v vars))) 274 (lambda (vars) 275 (let ((body (cddr x))) 276 (fold (lambda (x r) (find-sets x r)) 277 (set-minus v vars) 278 body))) 276 279 (if (test then else) 277 280 (set-union (find-sets test v) … … 314 317 (define *sp* 0) 315 318 319 (define (clear-stack) 320 (set! *sp* 0)) 321 316 322 (define (init-stack) 317 323 (set! *stack* (make-vector 1000)) 318 ( set! *sp* 0))324 (clear-stack)) 319 325 320 326 (define push … … 379 385 (begin 380 386 (restore-stack stack) 381 (push 0)382 387 (VM a x f c))) 383 388 (FRAME (x ret) … … 406 411 (VM res ret f c)) 407 412 (begin 408 (modify-args a n *sp*)413 (modify-args a n (- ss 1)) 409 414 (VM a body ss a)))) 410 415 (error "wrong number of argument")) … … 449 454 (lambda (s) 450 455 (closure 451 '( cc)456 '(1 . 1) 452 457 (list 'REFER-LOCAL 0 (list 'NUATE (save-stack s) '(RETURN))) 453 458 0 … … 465 470 (define restore-stack 466 471 (lambda (v) 472 (clear-stack) 467 473 (let ((s (vector-length v))) 468 474 (recur copy ((i 0)) 469 475 (unless (= i s) 470 ( vector-set! *stack* i(vector-ref v i))476 (push (vector-ref v i)) 471 477 (copy (+ i 1)))) 472 478 s))) … … 707 713 'proc 2 2 708 714 (lambda () (< (get-arg 0) (get-arg 1))))) 715 (cons '> 716 (gen-builtin 717 'proc 2 2 718 (lambda () (> (get-arg 0) (get-arg 1))))) 719 (cons '= 720 (gen-builtin 721 'proc 2 2 722 (lambda () (= (get-arg 0) (get-arg 1))))) 709 723 (cons 'read 710 724 (gen-builtin … … 727 741 'proc 1 1 728 742 (lambda () (load-file (get-arg 0))))) 743 (cons 'macroexpand-1 744 (gen-builtin 745 'proc 1 1 746 (lambda () 747 (let ((sexp (get-arg 0))) 748 (if (pair? sexp) 749 (let* ((fn (car sexp)) 750 (m (and (exist-global? fn) 751 (refer-global fn)))) 752 (if (macro? m) 753 (transform-macro m (cdr sexp)) 754 sexp)) 755 sexp))))) 729 756 730 757 … … 778 805 (let ((srcfn (cadr args))) 779 806 (load-file srcfn)))) 807 808 809 810 #| 811 (load "./stackbased.scm") 812 (repl) 813 814 815 816 (load "test_macro.scm") 817 818 819 820 821 (macroexpand-1 '(let ((x 1) (y 2)) (+ x y))) 822 823 824 825 (defmacro test (a . b) (list 'list a b)) 826 827 828 (test 1 2) 829 830 831 832 (evaluate '((lambda (a b . c) (list a b c)) 1 2 3 4)) 833 834 835 836 837 (compile-top '((lambda (a . b) (list 'list a b)) '1 '2)) 838 839 840 (let ((x 1) (y 2)) (cons x y)) 841 842 843 844 845 846 847 848 849 |#
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)