- Timestamp:
- 05/06/08 23:51:23 (7 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 7 modified
-
compiler/compiler.cpp (modified) (2 diffs)
-
readme.txt (modified) (1 diff)
-
sexp/sprint.cpp (modified) (3 diffs)
-
sexp/sutil.cpp (modified) (1 diff)
-
sexp/sutil.h (modified) (2 diffs)
-
test/main.cpp (modified) (7 diffs)
-
vm/vm.cpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/compiler/compiler.cpp
r11144 r11218 181 181 else if (eq(hd, intern("quote"))) return list(2, intern("quote"), car(cadr(res))); 182 182 else { assert(!"unexpected"); return nil; } 183 } 184 185 /// �h�b�g�y�A�̈ʒu��� 186 static int dotted_pos(SExp ls) { 187 for (int pos = 0; ; ++pos, ls = cdr(ls)) { 188 if (nilp(ls)) return -1; 189 if (!consp(ls)) return pos; 190 } 191 } 192 193 /// REST �p�����[�^����H 194 static bool has_rest_param(SExp* prest, SExp* pmodvars, SExp vars) { 195 int pos = dotted_pos(vars); 196 if (pos >= 0) { 197 if (pos == 0) { 198 *pmodvars = cons(vars, nil); 199 } else { 200 SExp copied = list_copy(vars); 201 SExp last = last_pair(copied); 202 rplacd(last, cons(cdr(last), nil)); 203 *pmodvars = copied; 204 } 205 *prest = int2s(pos); 206 return true; 207 } else { 208 return false; 209 } 210 } 211 212 /// lambda ���̃R���p�C�� 213 static SExp compile_lambda(SExp rest, SExp vars, SExp body, SExp e, SExp next) { 214 return list(3, CLOSE, cons(rest, compile_block(body, extend(e, vars), list(1, RETURN))), next); 183 215 } 184 216 … … 217 249 SExp vars = cadr(x); 218 250 SExp body = cddr(x); 219 return list(3, CLOSE, compile_block(body, extend(e, vars), cons(RETURN, nil)), next); 251 SExp rest, modified_vars; 252 if (has_rest_param(&rest, &modified_vars, vars)) { 253 return compile_lambda(rest, modified_vars, body, e, next); 254 } else { 255 return compile_lambda(nil, vars, body, e, next); 256 } 220 257 } else if (eq(op, intern("if"))) { 221 258 SExp test = cadr(x); -
lang/c/misc/mlisp/readme.txt
r11185 r11218 53 53 54 54 55 56 * �o�O 57 - �}�N�����A�錾���ɓo�^�����Ȃ��̂ŁA�������g��яo���Ă��N�������s����-- cond 58 55 59 * changelog 56 60 -
lang/c/misc/mlisp/sexp/sprint.cpp
r11144 r11218 4 4 static void print_rec(SExp s); 5 5 6 static void print_cell(SExp p) 7 { 6 static void print_cell(SExp p) { 8 7 bool b_first = true; 9 8 for (; type_of(p) == tCell; ) { … … 21 20 22 21 23 static void print_rec(SExp s) 24 { 22 static void print_rec(SExp s) { 25 23 switch (type_of(s)) { 26 24 default: assert(FALSE); break; … … 75 73 76 74 77 void print(SExp s) 78 { 75 void print(SExp s) { 79 76 print_rec(s); 80 77 printf("\n"); -
lang/c/misc/mlisp/sexp/sutil.cpp
r11144 r11218 78 78 } 79 79 80 SExp list_copy(SExp ls) { 81 SExp cp = nil; 82 for (; consp(ls); ls = cdr(ls)) { 83 cp = cons(car(ls), cp); 84 } 85 cp = nreverse(cp); 86 if (!nilp(ls)) { 87 if (!nilp(cp)) rplacd(last_pair(cp), ls); 88 else cp = ls; 89 } 90 return cp; 91 } 92 93 SExp list_tail(SExp ls, int k) { 94 while (k > 0) { 95 ls = cdr(ls); 96 } 97 return ls; 98 } 99 80 100 81 101 -
lang/c/misc/mlisp/sexp/sutil.h
r11144 r11218 35 35 SExp assoc(SExp sym, SExp alist); 36 36 37 /// ���X�g�̃R�s�[ 38 SExp list_copy(SExp ls); 39 40 /// ���X�g��k �Ԗڂ�cdr ��� 41 SExp list_tail(SExp ls, int k); 42 37 43 38 44 /// ���X�g�ŗv�f���ЂƂ����H … … 45 51 46 52 __inline int consp(SExp s) { return type_of(s) == tCell; } 47 48 53 __inline int symbolp(SExp s) { return type_of(s) == tSymb; } 49 54 __inline int zerop(SExp s) { return eq(s, int2s(0)); } 50 55 __inline SExp cadr(SExp s) { return car(cdr(s)); } 51 56 __inline SExp cddr(SExp s) { return cdr(cdr(s)); } -
lang/c/misc/mlisp/test/main.cpp
r11144 r11218 33 33 SExp g_comp_env, g_run_env; 34 34 35 int load (const char* fn);35 int load_file(const char* fn); 36 36 37 37 … … 164 164 } 165 165 166 static SExp builtin_numeq(SExp arg) { 167 SExp a = car(arg); 168 check_number(a); 169 sint x = s2int(a); 170 171 for (SExp p = cdr(arg); consp(p); p = cdr(p)) { 172 SExp b = car(p); 173 check_number(a); 174 sint y = s2int(b); 175 if (x != y) return nil; 176 } 177 return t; 178 } 179 166 180 static SExp builtin_lt(SExp arg) { 167 181 SExp a = car(arg); … … 194 208 } 195 209 210 static SExp builtin_le(SExp arg) { 211 SExp a = car(arg); 212 check_number(a); 213 sint x = s2int(a); 214 215 for (SExp p = cdr(arg); consp(p); p = cdr(p)) { 216 SExp b = car(p); 217 check_number(a); 218 sint y = s2int(b); 219 if (x > y) return nil; 220 x = y; 221 } 222 return t; 223 } 224 225 static SExp builtin_ge(SExp arg) { 226 SExp a = car(arg); 227 check_number(a); 228 sint x = s2int(a); 229 230 for (SExp p = cdr(arg); consp(p); p = cdr(p)) { 231 SExp b = car(p); 232 check_number(a); 233 sint y = s2int(b); 234 if (x < y) return nil; 235 x = y; 236 } 237 return t; 238 } 239 196 240 static SExp builtin_read(SExp arg) { 197 241 return read_from_file(stdin); … … 214 258 const char* fn = s2str(a); 215 259 if (fn != NULL) { 216 if (load (fn)) {260 if (load_file(fn)) { 217 261 return t; 218 262 } … … 291 335 { "*", builtin_times, FALSE, 0, -1, }, 292 336 { "/", builtin_quotient, FALSE, 1, -1, }, 337 { "=", builtin_numeq, FALSE, 1, -1, }, 293 338 { "<", builtin_lt, FALSE, 1, -1, }, 294 339 { ">", builtin_gt, FALSE, 1, -1, }, 340 { "<=", builtin_le, FALSE, 1, -1, }, 341 { ">=", builtin_ge, FALSE, 1, -1, }, 295 342 296 343 … … 353 400 354 401 355 SExp compile_file(const char* fn) { 402 // �\�[�X�̃��[�h�i�ǂݍ��݁A�R���p�C���A��s�j 403 int load_file(const char* fn) { 356 404 FILE* fp = fopen(fn, "r"); 357 SExp code = nil;358 405 if (fp == NULL) { 359 // runtime_error();360 } else { 361 SExp acc = nil;406 return FALSE; 407 } else { 408 SExp halt_code = cons(HALT, nil); 362 409 for (;;) { 363 SExp s = read_from_file(fp);364 if (nilp(s )) break;365 acc = cons(s, acc);410 SExp sexp = read_from_file(fp); 411 if (nilp(sexp)) break; 412 run(compile(sexp, g_comp_env, halt_code)); 366 413 } 367 SExp halt_code = cons(HALT, nil);368 code = compile_block(nreverse(acc), g_comp_env, halt_code);369 370 414 fclose(fp); 371 }372 return code;373 }374 375 376 int load(const char* fn) {377 SExp code = compile_file(fn);378 if (!nilp(code)) {379 SExp r = run(code);380 415 return TRUE; 381 } else {382 return FALSE;383 416 } 384 417 } … … 416 449 init_env(); 417 450 if (argc >= 2) { 418 load (argv[1]);451 load_file(argv[1]); 419 452 } else { 420 453 repl(); -
lang/c/misc/mlisp/vm/vm.cpp
r11144 r11218 66 66 67 67 static SExp continuation(SExp s) { 68 return closure( list(3, NUATE, s, cons(int2s(0), int2s(0))), nil);68 return closure(cons(nil, list(3, NUATE, s, cons(int2s(0), int2s(0)))), nil); 69 69 } 70 70 … … 78 78 } 79 79 80 static SExp modify_args(SExp pos, SExp r) { 81 if (!nilp(pos)) { 82 if (zerop(pos)) { 83 r = cons(r, nil); 84 } else { 85 SExp last = list_tail(r, s2int(pos) - 1); 86 rplacd(last, cons(cdr(last), nil)); 87 } 88 } 89 return r; 90 } 80 91 81 92 SExp vm(SExp a, SExp x, SExp e, SExp r, SExp s) { 82 SExp ret_code = cons(RETURN, nil);83 93 for (;;) { 84 94 SExp op = car(x); … … 127 137 { 128 138 SExp xx = cadr(x); 129 a = continuation(s); x = xx; ;139 a = continuation(s); x = xx; 130 140 } 131 141 } else if (op == NUATE) { 132 142 { 133 SExp s = cadr(x);143 SExp ss = cadr(x); 134 144 SExp var = caddr(x); 135 a = car(lookup(var, e, false)); x = ret_code;145 a = car(lookup(var, e, false)); x = cons(RETURN, nil); s = ss; 136 146 } 137 147 } else if (op == FRAME) { … … 156 166 SCFunc cfunc = proc->u.cfunc; 157 167 SExp res = (*cfunc)(r); 158 a = res; x = ret_code; r = nil;168 a = res; x = cons(RETURN, nil); r = nil; 159 169 } 160 170 break; … … 163 173 SExp body = proc->u.cell.s; 164 174 SExp ee = proc->u.cell.e; 165 x = body; e = extend(ee, r); r = nil;175 x = cdr(body); e = extend(ee, modify_args(car(body), r)); r = nil; 166 176 } 167 177 break; … … 182 192 SExp rr = caddr(s); 183 193 SExp ss = cadddr(s); 184 x = xx; e = ee; r = rr; s = ss; ;;194 x = xx; e = ee; r = rr; s = ss; 185 195 } 186 196 } else {
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)