- Timestamp:
- 05/14/08 00:49:49 (6 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 4 modified
-
compiler/compiler.cpp (modified) (3 diffs)
-
sexp/sexp.h (modified) (1 diff)
-
test/main.cpp (modified) (10 diffs)
-
vm/vm.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/compiler/compiler.cpp
r11387 r11544 280 280 SExp sets = find_sets(body, vars); 281 281 282 SExp boxes = make_boxes(sets, vars, compile(body, cons(vars, free), set_union(sets, set_intersect(s, free)), list( 2, RETURN, int2s(length(vars)))));282 SExp boxes = make_boxes(sets, vars, compile(body, cons(vars, free), set_union(sets, set_intersect(s, free)), list(1, RETURN))); 283 283 return collect_free(free, e, list(4, CLOSE, int2s(length(free)), boxes, next)); 284 284 } else if (eq(op, intern("if"))) { … … 306 306 int is_tail = tailp(next); 307 307 SExp xx = cadr(x); 308 SExp apply = list( 1, APPLY);309 SExp nx = is_tail ? list( 4, SHIFT, 1, cadr(next), apply) : apply;308 SExp apply = list(2, APPLY, int2s(1)); 309 SExp nx = is_tail ? list(3, SHIFT, 1, apply) : apply; 310 310 SExp c = list(2, CONTI, list(2, ARGUMENT, compile(xx, e, s, nx))); 311 311 if (is_tail) … … 319 319 } else { 320 320 SExp args = cdr(x); 321 SExp apply = list(1, APPLY); 322 SExp nx = tailp(next) ? list(4, SHIFT, int2s(length(cdr(x))), cadr(next), apply) : apply; 321 int argnum = length(args); 322 SExp apply = list(2, APPLY, int2s(argnum)); 323 SExp nx = tailp(next) ? list(3, SHIFT, int2s(argnum), apply) : apply; 323 324 SExp c = compile(car(x), e, s, nx); 324 325 return compile_pair_loop(args, c, e, s, next); -
lang/c/misc/mlisp/sexp/sexp.h
r11387 r11544 47 47 48 48 /// �g���݊��̌^ 49 typedef SExp (*SCFunc)(int );49 typedef SExp (*SCFunc)(int stack); 50 50 51 51 -
lang/c/misc/mlisp/test/main.cpp
r11387 r11544 31 31 32 32 33 SExp g_comp_env, g_run_env;34 35 33 int load_file(const char* fn); 36 34 … … 40 38 #include "inner.h" 41 39 40 /// ��s 42 41 SExp run(SExp c) { 43 42 return vm(nil, c, 0, nil, 0); 44 43 } 45 44 45 /// �g�b�v���x���ŃR���p�C�� 46 SExp compile_ontop(SExp code) { 47 SExp halt_code = cons(HALT, nil); 48 return compile(code, list(1, nil), nil, halt_code); 49 } 50 51 /// �]�� 46 52 SExp evaluate(SExp code) { 47 SExp halt_code = cons(HALT, nil);48 SExp c = compile(code, g_comp_env, nil, halt_code);53 SExp c = compile_ontop(code); 54 //print(c); 49 55 return run(c); 50 56 } … … 79 85 } 80 86 87 static SExp builtin_consp(int s) { 88 SExp v = refer_stack(s, 0); 89 return consp(v) ? t : nil; 90 } 91 81 92 static SExp builtin_eq(int s) { 82 93 SExp a = refer_stack(s, 0); … … 85 96 } 86 97 98 static SExp builtin_rplaca(int s) { 99 SExp a = refer_stack(s, 0); 100 SExp d = refer_stack(s, 1); 101 rplaca(a, d); 102 return nil; 103 } 104 105 static SExp builtin_rplacd(int s) { 106 SExp a = refer_stack(s, 0); 107 SExp d = refer_stack(s, 1); 108 rplacd(a, d); 109 return nil; 110 } 111 87 112 static SExp builtin_plus(int s) { 113 sint n = s2int(refer_stack(s, -1)); 88 114 sint x = 0; 89 for (int i=0; i< 2; ++i) {115 for (int i=0; i<n; ++i) { 90 116 SExp a = refer_stack(s, i); 91 117 check_number(a); … … 96 122 97 123 static SExp builtin_difference(int s) { 124 sint n = s2int(refer_stack(s, -1)); 98 125 SExp a = refer_stack(s, 0); 99 126 check_number(a); 100 127 sint x = s2int(a); 101 for (int i=1; i<2; ++i) { 128 if (n == 1) { 129 x = -x; 130 } else { 131 for (int i=1; i<n; ++i) { 132 SExp a = refer_stack(s, i); 133 check_number(a); 134 x -= s2int(a); 135 } 136 } 137 return int2s(x); 138 } 139 140 static SExp builtin_times(int s) { 141 sint n = s2int(refer_stack(s, -1)); 142 sint x = 1; 143 for (int i=0; i<n; ++i) { 102 144 SExp a = refer_stack(s, i); 103 145 check_number(a); 104 x -= s2int(a);146 x *= s2int(a); 105 147 } 106 148 return int2s(x); 107 149 } 108 150 151 static SExp builtin_quotient(int s) { 152 sint n = s2int(refer_stack(s, -1)); 153 SExp a = refer_stack(s, 0); 154 check_number(a); 155 sint x = s2int(a); 156 if (n == 1) { 157 x = 1 / x; 158 } else { 159 for (int i=1; i<n; ++i) { 160 SExp a = refer_stack(s, i); 161 check_number(a); 162 sint d = s2int(a); 163 if (d == 0) { 164 // error(ERR_ZERO_DIVIDE); 165 assert(!"zero divide"); 166 } else { 167 x /= d; 168 } 169 } 170 } 171 return int2s(x); 172 } 173 174 static SExp num_predicate(int s, int n, bool (*p)(sint, sint)) { 175 SExp a = refer_stack(s, 0); 176 check_number(a); 177 sint x = s2int(a); 178 179 for (int i=1; i<n; ++i) { 180 SExp b = refer_stack(s, i); 181 check_number(b); 182 sint y = s2int(b); 183 if (!p(x, y)) return nil; 184 x = y; 185 } 186 return t; 187 } 188 189 static bool numeq(sint a, sint b) { return a == b; } 190 static SExp builtin_numeq(int s) { 191 sint n = s2int(refer_stack(s, -1)); 192 return num_predicate(s, n, numeq); 193 } 194 195 static bool numlt(sint a, sint b) { return a < b; } 109 196 static SExp builtin_lt(int s) { 110 SExp a = refer_stack(s, 0); 111 check_number(a); 112 SExp d = refer_stack(s, 1); 113 check_number(d); 114 115 return s2int(a) < s2int(d) ? t : nil; 197 sint n = s2int(refer_stack(s, -1)); 198 return num_predicate(s, n, numlt); 199 } 200 201 static bool numgt(sint a, sint b) { return a > b; } 202 static SExp builtin_gt(int s) { 203 sint n = s2int(refer_stack(s, -1)); 204 return num_predicate(s, n, numgt); 205 } 206 207 static bool numle(sint a, sint b) { return a <= b; } 208 static SExp builtin_le(int s) { 209 sint n = s2int(refer_stack(s, -1)); 210 return num_predicate(s, n, numle); 211 } 212 213 static bool numge(sint a, sint b) { return a >= b; } 214 static SExp builtin_ge(int s) { 215 sint n = s2int(refer_stack(s, -1)); 216 return num_predicate(s, n, numge); 217 } 218 219 220 static SExp builtin_read(int s) { 221 return read_from_file(stdin); 116 222 } 117 223 … … 119 225 SExp a = refer_stack(s, 0); 120 226 print(a); 121 return a; 227 return nil; 228 } 229 230 static SExp builtin_eval(int s) { 231 SExp code = refer_stack(s, 0); 232 233 return evaluate(code); 234 } 235 236 static SExp builtin_load(int s) { 237 SExp a = refer_stack(s, 0); 238 const char* fn = s2str(a); 239 if (fn != NULL) { 240 if (load_file(fn)) { 241 return t; 242 } 243 } 244 return nil; 245 } 246 247 static SExp builtin_compile(int s) { 248 SExp code = refer_stack(s, 0); 249 SExp halt_code = cons(HALT, nil); 250 return compile_ontop(code); 122 251 } 123 252 … … 138 267 { "car", builtin_car, FALSE, 1, 1, }, 139 268 { "cdr", builtin_cdr, FALSE, 1, 1, }, 269 { "pair?", builtin_consp, FALSE, 1, 1, }, 140 270 { "eq?", builtin_eq, FALSE, 2, 2, }, 141 { "+", builtin_plus, FALSE, 2, 2, }, 142 { "-", builtin_difference, FALSE, 2, 2, }, 143 { "<", builtin_lt, FALSE, 2, 2, }, 271 { "set-car!", builtin_rplaca, FALSE, 2, 2, }, 272 { "set-cdr!", builtin_rplacd, FALSE, 2, 2, }, 273 { "+", builtin_plus, FALSE, 0, -1, }, 274 { "-", builtin_difference, FALSE, 1, -1, }, 275 { "*", builtin_times, FALSE, 0, -1, }, 276 { "/", builtin_quotient, FALSE, 1, -1, }, 277 { "=", builtin_numeq, FALSE, 1, -1, }, 278 { "<", builtin_lt, FALSE, 1, -1, }, 279 { ">", builtin_gt, FALSE, 1, -1, }, 280 { "<=", builtin_le, FALSE, 1, -1, }, 281 { ">=", builtin_ge, FALSE, 1, -1, }, 282 283 { "read", builtin_read, FALSE, 0, 0, }, 144 284 { "print", builtin_print, FALSE, 1, 1, }, 285 { "eval", builtin_eval, FALSE, 1, 1, }, 286 287 { "load", builtin_load, FALSE, 1, 1, }, 288 { "compile", builtin_compile, FALSE, 1, 1, }, 145 289 }; 146 290 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { … … 151 295 } 152 296 153 static void add_consttbl(SExp* pcenv, SExp* prenv) { 154 SExp cenv = *pcenv; 155 SExp renv = *prenv; 156 157 #if 0 297 static void add_consttbl() { 158 298 struct { 159 299 const char* name; … … 164 304 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { 165 305 SExp sym = intern(tbl[i].name); 166 cenv = cons(sym, cenv); 167 renv = cons(sym, renv); 168 } 169 #endif 170 171 *pcenv = cenv; 172 *prenv = renv; 306 define_global(sym, sym); 307 } 173 308 } 174 309 175 310 static void init_env() { 176 SExp cenv = nil;177 SExp renv = nil;178 179 311 add_proctbl(); 180 add_consttbl(&cenv, &renv); 181 182 g_comp_env = cons(cenv, nil); 183 g_run_env = cons(renv, nil); 312 add_consttbl(); 184 313 } 185 314 … … 195 324 SExp sexp = read_from_file(fp); 196 325 if (nilp(sexp)) break; 197 run(compile(sexp, g_comp_env, nil, halt_code));326 run(compile(sexp, nil, nil, halt_code)); 198 327 } 199 328 fclose(fp); -
lang/c/misc/mlisp/vm/vm.cpp
r11387 r11544 31 31 32 32 inline static SExp index(int s, int i) { 33 return g_stack[s - i - 1];33 return g_stack[s - i - 2]; 34 34 } 35 35 36 36 inline static void index_set(int s, int i, SExp v) { 37 g_stack[s - i - 1] = v;37 g_stack[s - i - 2] = v; 38 38 } 39 39 … … 50 50 51 51 static int shift_args(int n, int m, int s) { 52 for (int i=n ; --i >= 0; ) {53 index_set(s, i + m , index(s, i));54 } 55 return s - m ;52 for (int i=n-1; --i > -2; ) { 53 index_set(s, i + m + 1, index(s, i)); 54 } 55 return s - m - 1; 56 56 } 57 57 … … 67 67 printf("#%d[", s); 68 68 for (int i=0; i<s; ++i) { 69 print_rec(refer_stack(s, i ));69 print_rec(refer_stack(s, i-1)); 70 70 printf(" "); 71 71 } … … 175 175 //============================================================================= 176 176 177 static int vm_return(SExp* px, sint* pf, SExp* pc, int s, int n) { 177 static int vm_return(SExp* px, sint* pf, SExp* pc, int s) { 178 int n = s2int(index(s, -1)); 178 179 int ss = s - n; 179 180 *px = index(ss, 0); *pf = s2int(index(ss, 1)); *pc = index(ss, 2); 180 return ss - 3;181 return ss - (3 + 1); 181 182 } 182 183 … … 227 228 a = closure(body, n, s); x = xx; s -= n; 228 229 } else if (op == APPLY) { 230 SExp argnum = cadr(x); 231 int ss = push(argnum, s); 229 232 if (type_of(a) == tProc) { 230 233 Procedure* proc = (Procedure*)a.ptr; … … 233 236 { 234 237 SCFunc cfunc = proc->u.cfunc; 235 SExp res = (*cfunc)(s); 236 const int n = 2; // �����i���j 238 SExp res = (*cfunc)(ss); 237 239 a = res; 238 s = vm_return(&x, &f, &c, s , n);240 s = vm_return(&x, &f, &c, ss); 239 241 } 240 242 break; 241 243 case Procedure::Cell: 242 244 { 243 x = closure_body(a); f = s ; c = a;245 x = closure_body(a); f = ss; c = a; s = ss; 244 246 } 245 247 break; … … 250 252 } 251 253 } else if (op == RETURN) { 252 sint n = s2int(cadr(x)); 253 s = vm_return(&x, &f, &c, s, n); 254 s = vm_return(&x, &f, &c, s); 254 255 } else if (op == SHIFT) { 255 256 sint n = s2int(cadr(x)); 256 sint m = s2int( caddr(x));257 SExp xx = cadd dr(x);257 sint m = s2int(index(s, n - 1)); 258 SExp xx = caddr(x); 258 259 x = xx; s = shift_args(n, m, s); 259 260 } else if (op == CONTI) {
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)