- Timestamp:
- 05/17/08 17:24:24 (5 years ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 6 modified
-
core/c_compiler.cpp (modified) (3 diffs)
-
core/inner.h (modified) (2 diffs)
-
core/m_basic.cpp (modified) (2 diffs)
-
core/sexp.cpp (modified) (3 diffs)
-
core/v_vm.cpp (modified) (2 diffs)
-
inc/sexp.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/core/c_compiler.cpp
r11676 r11750 276 276 277 277 //============================================================================= 278 // �}�N�� 279 280 static SExp gen_macro_from_sexp(SExp vars, SExp body) { 281 SExp var_min_max = get_var_min_max(vars); 282 sint minnarg = s2int(car(var_min_max)); 283 sint maxnarg = s2int(cdr(var_min_max)); 284 SExp code = cons(intern("lambda"), cons(vars, body)); 285 return gen_macro(code, minnarg, maxnarg); 286 } 287 288 static void compile_defmacro(SExp name, SExp vars, SExp body) { 289 define_global(name, gen_macro_from_sexp(vars, body)); 290 } 291 292 static bool macrop(SExp fn) { 293 if (type_of(fn) == tProc) { 294 Procedure* p = (Procedure*)fn.ptr; 295 if (p->get_proc_type() == Procedure::Macro) { 296 return true; 297 } 298 } 299 return false; 300 } 301 302 static SExp quote(SExp s) { return list(2, intern("quote"), s); } 303 304 static SExp gen_macro_call(Procedure* p, SExp args) { 305 SExp qargs = mapcar(quote, args); 306 return cons(p->u.cell.s, qargs); 307 } 308 309 static SExp compile_macroexpand_1(SExp m, SExp args) { 310 Procedure* p = (Procedure*)m.ptr; 311 if (p->get_func_type() == Procedure::Builtin) { 312 return p->u.mfunc(args); 313 } 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); 320 } 321 322 //============================================================================= 278 323 279 324 /// ����void init_compile(void) { … … 305 350 } else { 306 351 return compile_pair_loop(cdr(args), compile(car(args), e, s, list(2, ARGUMENT, c)), e, s, next); 352 } 353 } 354 355 // �K�p����p�C�� 356 static SExp compile_apply(SExp x, SExp e, SExp s, SExp next) { 357 SExp fn = car(x); 358 SExp args = cdr(x); 359 SExp gval = symbolp(fn) ? refer_global(fn) : nil; 360 if (macrop(gval)) { 361 return compile(transform_macro(gval, args), e, s, next); 362 } else { 363 int argnum = length(args); 364 SExp apply = list(2, APPLY, int2s(argnum)); 365 SExp nx = tailp(next) ? list(3, SHIFT, int2s(argnum), apply) : apply; 366 SExp c = compile(car(x), e, s, nx); 367 return compile_pair_loop(args, c, e, s, next); 307 368 } 308 369 } … … 364 425 SExp body = cddr(x); 365 426 return compile_define(var, body, e, s, next); 366 } else { 367 SExp args = cdr(x); 368 int argnum = length(args); 369 SExp apply = list(2, APPLY, int2s(argnum)); 370 SExp nx = tailp(next) ? list(3, SHIFT, int2s(argnum), apply) : apply; 371 SExp c = compile(car(x), e, s, nx); 372 return compile_pair_loop(args, c, e, s, next); 427 } else if (eq(op, intern("defmacro"))) { 428 SExp name = cadr(x); 429 SExp vars = caddr(x); 430 SExp body = cdddr(x); 431 compile_defmacro(name, vars, body); 432 return next; 433 } else { 434 return compile_apply(x, e, s, next); 373 435 } 374 436 } else { -
lang/c/misc/mlisp/core/inner.h
r11715 r11750 69 69 union { 70 70 SCFunc cfunc; 71 SCFuncM mfunc; 71 72 struct { 72 73 SExp s; ///< �� … … 77 78 78 79 int get_func_type() const { return flag & FuncType; } 80 int get_proc_type() const { return flag & ProcType; } 79 81 }; 80 82 -
lang/c/misc/mlisp/core/m_basic.cpp
r11617 r11750 50 50 rplacd(a, d); 51 51 return nil; 52 } 53 54 static SExp builtin_list(int s) { 55 sint n = get_arg_num(s); 56 SExp ls = nil; 57 for (int i=n; --i>=0; ) { 58 SExp a = get_arg(s, i); 59 ls = cons(a, ls); 60 } 61 return ls; 52 62 } 53 63 … … 199 209 const char* name; 200 210 SCFunc func; 201 int b_macro;202 211 int minarg; 203 212 int maxarg; 204 213 } static const tbl[] = { 205 { "cons", builtin_cons, FALSE, 2, 2, }, 206 { "car", builtin_car, FALSE, 1, 1, }, 207 { "cdr", builtin_cdr, FALSE, 1, 1, }, 208 { "pair?", builtin_consp, FALSE, 1, 1, }, 209 { "eq?", builtin_eq, FALSE, 2, 2, }, 210 { "set-car!", builtin_rplaca, FALSE, 2, 2, }, 211 { "set-cdr!", builtin_rplacd, FALSE, 2, 2, }, 212 { "+", builtin_plus, FALSE, 0, -1, }, 213 { "-", builtin_difference, FALSE, 1, -1, }, 214 { "*", builtin_times, FALSE, 0, -1, }, 215 { "/", builtin_quotient, FALSE, 1, -1, }, 216 { "=", builtin_numeq, FALSE, 1, -1, }, 217 { "<", builtin_lt, FALSE, 1, -1, }, 218 { ">", builtin_gt, FALSE, 1, -1, }, 219 { "<=", builtin_le, FALSE, 1, -1, }, 220 { ">=", builtin_ge, FALSE, 1, -1, }, 221 222 { "read", builtin_read, FALSE, 0, 0, }, 223 { "print", builtin_print, FALSE, 1, 1, }, 224 { "eval", builtin_eval, FALSE, 1, 1, }, 225 226 { "load", builtin_load, FALSE, 1, 1, }, 227 { "compile", builtin_compile, FALSE, 1, 1, }, 214 { "cons", builtin_cons, 2, 2, }, 215 { "car", builtin_car, 1, 1, }, 216 { "cdr", builtin_cdr, 1, 1, }, 217 { "pair?", builtin_consp, 1, 1, }, 218 { "eq?", builtin_eq, 2, 2, }, 219 { "set-car!", builtin_rplaca, 2, 2, }, 220 { "set-cdr!", builtin_rplacd, 2, 2, }, 221 { "list", builtin_list, 0, -1, }, 222 223 { "+", builtin_plus, 0, -1, }, 224 { "-", builtin_difference, 1, -1, }, 225 { "*", builtin_times, 0, -1, }, 226 { "/", builtin_quotient, 1, -1, }, 227 { "=", builtin_numeq, 1, -1, }, 228 { "<", builtin_lt, 1, -1, }, 229 { ">", builtin_gt, 1, -1, }, 230 { "<=", builtin_le, 1, -1, }, 231 { ">=", builtin_ge, 1, -1, }, 232 233 { "read", builtin_read, 0, 0, }, 234 { "print", builtin_print, 1, 1, }, 235 { "eval", builtin_eval, 1, 1, }, 236 237 { "load", builtin_load, 1, 1, }, 238 { "compile", builtin_compile, 1, 1, }, 228 239 }; 229 240 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 230 241 SExp sym = intern(tbl[i].name); 231 SExp fn = gen_cfunc(tbl[i].func, tbl[i]. b_macro, tbl[i].minarg, tbl[i].maxarg);242 SExp fn = gen_cfunc(tbl[i].func, tbl[i].minarg, tbl[i].maxarg); 232 243 define_global(sym, fn); 233 244 } -
lang/c/misc/mlisp/core/sexp.cpp
r11676 r11750 248 248 249 249 250 SExp gen_cfunc(SCFunc cfunc, int b_macro, int minnarg, int maxnarg) { 250 SExp gen_cfunc(SCFunc cfunc, int minnarg, int maxnarg) { 251 const bool b_macro = false; 251 252 Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 252 253 p->type = tProc; … … 262 263 } 263 264 264 265 SExp gen_closure(SExp body, SExp env, int minarg, int maxarg) { 266 bool b_macro = false; 265 SExp gen_cmacro(SCFuncM mfunc, int minnarg, int maxnarg) { 266 const bool b_macro = true; 267 Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 268 p->type = tProc; 269 p->flag = Procedure::Builtin | (b_macro ? Procedure::Macro : Procedure::Lambda); 270 p->u.mfunc = mfunc; 271 p->minnarg = minnarg; 272 p->maxnarg = maxnarg; 273 274 SExp s; 275 s.ptr = (SExpExtU*)p; 276 277 return s; 278 } 279 280 281 SExp gen_closure(SExp body, SExp env, int minnarg, int maxnarg) { 282 const bool b_macro = false; 267 283 Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 268 284 p->type = tProc; … … 270 286 p->u.cell.s = body; 271 287 p->u.cell.e = env; 272 p->minnarg = minarg; 273 p->maxnarg = maxarg; 288 p->minnarg = minnarg; 289 p->maxnarg = maxnarg; 290 291 SExp s; 292 s.ptr = (SExpExtU*)p; 293 294 return s; 295 } 296 297 298 SExp gen_macro(SExp body, int minnarg, int maxnarg) { 299 const bool b_macro = true; 300 Procedure* p = (Procedure*)salloc(sizeof(Procedure), FALSE); 301 p->type = tProc; 302 p->flag = Procedure::Cell | (b_macro ? Procedure::Macro : Procedure::Lambda); 303 p->u.cell.s = body; 304 p->u.cell.e = nil; 305 p->minnarg = minnarg; 306 p->maxnarg = maxnarg; 274 307 275 308 SExp s; -
lang/c/misc/mlisp/core/v_vm.cpp
r11715 r11750 212 212 int r = 0; 213 213 if (type_of(fn) == tProc) { 214 int ss = push(argnum, s);215 214 int n = s2int(argnum); 216 215 Procedure* proc = (Procedure*)fn.ptr; 217 216 if (check_arg_num(proc, n)) { 217 int ss = push(argnum, s); 218 218 switch (proc->get_func_type()) { 219 219 case Procedure::Builtin: … … 238 238 } 239 239 } else { 240 runtime_error("can't call");240 runtime_error("can't apply"); 241 241 r = -1; 242 242 } -
lang/c/misc/mlisp/inc/sexp.h
r11676 r11750 48 48 /// �g���݊��̌^ 49 49 typedef SExp (*SCFunc)(int stack); 50 typedef SExp (*SCFuncM)(SExp args); 50 51 51 52 … … 104 105 SExp list(int n, ...); 105 106 106 SExp gen_cfunc(SCFunc cfunc, int b_macro, int minnarg, int maxnarg); 107 SExp gen_cfunc(SCFunc cfunc, int minnarg, int maxnarg); 108 SExp gen_cmacro(SCFuncM mfunc, int minnarg, int maxnarg); 107 109 108 SExp gen_closure(SExp body, SExp env, int minarg, int maxarg); 110 SExp gen_closure(SExp body, SExp env, int minnarg, int maxnarg); 111 112 SExp gen_macro(SExp body, int minnarg, int maxnarg); 109 113 110 114 /// s���𐮐��ɕϊ�
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)