- Timestamp:
- 05/04/08 08:57:54 (7 months ago)
- Location:
- lang/c/misc/mlisp
- Files:
-
- 8 modified
-
mlisp.vcproj (modified) (1 diff)
-
readme.txt (modified) (1 diff)
-
sexp/sexp.cpp (modified) (1 diff)
-
sexp/sexp.h (modified) (4 diffs)
-
sexp/sutil.h (modified) (1 diff)
-
test/compiler.cpp (modified) (5 diffs)
-
test/main.cpp (modified) (12 diffs)
-
test/vm.cpp (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/misc/mlisp/mlisp.vcproj
r11057 r11058 81 81 Name="VCLinkerTool" 82 82 AdditionalDependencies="boehmgc.lib libcpmt.lib" 83 OutputFile=" $(OutDir)/mlisp.exe"83 OutputFile="mlisp.exe" 84 84 LinkIncremental="1" 85 85 GenerateDebugInformation="TRUE" -
lang/c/misc/mlisp/readme.txt
r11057 r11058 8 8 -BoehmGC ��p 9 9 -�q�[�v�x�[�X�̎�� 10 -repl ����quit�v�Ƒł����ނƔ����� 10 -repl ����quit�v�Ƒł����ނƔ�����-�unil�v�ut�v���V���{���Ȃ̂ŁA�R���p�C�������V���{���Q�ƂɂȂ�Ă��܂� 11 -�V���{���̑啶���Ə�������ʂ��� -
lang/c/misc/mlisp/sexp/sexp.cpp
r11057 r11058 90 90 } 91 91 92 const char* s2str(SExp s) { 93 type_check(s, tStr); 94 String* str = (String*)s.ptr; 95 return str->str; 96 } 97 92 98 93 99 //============================================================================= -
lang/c/misc/mlisp/sexp/sexp.h
r11057 r11058 90 90 void rplacd(SExp s, SExp d); // cdr ���u������ 91 91 92 /// ������Ή��������{����� 92 93 SExp intern(const char* str); 93 94 … … 113 114 SExp int2s(sint x); 114 115 116 /// S������ϊ� 117 const char* s2str(SExp s); 118 115 119 116 120 … … 124 128 //============================================================================= 125 129 // �C�����C�� 126 127 __inline int consp(SExp s) { return type_of(s) == tCell; }128 129 __inline int symbolp(SExp s) { return type_of(s) == tSymb; }130 131 130 132 131 __inline SExp int2s(sint x) { … … 145 144 146 145 147 148 __inline SExp cadr(SExp s) { return car(cdr(s)); }149 __inline SExp cddr(SExp s) { return cdr(cdr(s)); }150 __inline SExp caddr(SExp s) { return car(cdr(cdr(s))); }151 __inline SExp cdddr(SExp s) { return cdr(cdr(cdr(s))); }152 __inline SExp cadddr(SExp s) { return car(cdr(cdr(cdr(s)))); }153 __inline SExp cddddr(SExp s) { return cdr(cdr(cdr(cdr(s)))); }154 155 156 157 146 #ifdef __cplusplus 158 147 inline bool SExp::operator==(const SExp s) const { return eq(*this, s) != 0; } -
lang/c/misc/mlisp/sexp/sutil.h
r11057 r11058 36 36 37 37 38 39 __inline int consp(SExp s) { return type_of(s) == tCell; } 40 41 __inline int symbolp(SExp s) { return type_of(s) == tSymb; } 42 43 __inline SExp cadr(SExp s) { return car(cdr(s)); } 44 __inline SExp cddr(SExp s) { return cdr(cdr(s)); } 45 __inline SExp caddr(SExp s) { return car(cddr(s)); } 46 __inline SExp cdddr(SExp s) { return cdr(cddr(s)); } 47 __inline SExp cadddr(SExp s) { return car(cdddr(s)); } 48 __inline SExp cddddr(SExp s) { return cdr(cdddr(s)); } 49 50 38 51 #ifdef __cplusplus 39 52 } // extern "C" -
lang/c/misc/mlisp/test/compiler.cpp
r11057 r11058 43 43 } 44 44 45 // ���[�J�����ɕϐ���^ 46 SExp compile_define_global(SExp env, SExp var) { 47 return cons(int2s(length(env) - 1), compile_define_var(last_pair(env), var)); 48 } 49 45 50 46 51 47 52 48 53 static SExp compile_lookup(SExp var, SExp e) { 54 #if 0 49 55 int rib = 0; 50 56 nxtrib:; 51 if (nilp(e)) { 52 return nil; 53 } 57 if (nilp(e)) return nil; 54 58 SExp vars = car(e); 55 59 int elt = 0; … … 58 62 if (eq(car(vars), var)) return cons(int2s(rib), int2s(elt)); 59 63 else { vars = cdr(vars); ++elt; goto nxtelt; } 64 #else 65 int rib = 0; 66 for (;; e = cdr(e), ++rib) { 67 if (nilp(e)) return nil; 68 SExp vars = car(e); 69 int elt = 0; 70 for (;; vars = cdr(vars), ++elt) { 71 if (nilp(vars)) break; 72 if (eq(car(vars), var)) return cons(int2s(rib), int2s(elt)); 73 } 74 } 75 #endif 60 76 } 61 77 … … 128 144 if (symbolp(x)) { 129 145 SExp access = compile_lookup(x, e); 146 if (nilp(access)) { 147 access = compile_define_global(e, x); 148 } 130 149 return list(3, REFER, access, next); 131 150 } else if (consp(x)) { … … 155 174 SExp var = cadr(x); 156 175 SExp body = cddr(x); 157 compile_define(var, body, e, next);176 return compile_define(var, body, e, next); 158 177 } else if (eq(op, intern("call/cc"))) { 159 178 SExp xx = cadr(x); … … 164 183 return list(3, FRAME, c, next); 165 184 } else { 166 compile_pair_loop(cdr(x), compile(car(x), e, cons(APPLY, nil)), e, next);185 return compile_pair_loop(cdr(x), compile(car(x), e, cons(APPLY, nil)), e, next); 167 186 } 168 187 } else { 169 list(3, CONSTANT, x, next);188 return list(3, CONSTANT, x, next); 170 189 } 171 190 } -
lang/c/misc/mlisp/test/main.cpp
r11057 r11058 33 33 SExp comp_env, run_env; 34 34 35 int load(const char* fn); 35 36 36 37 … … 38 39 #include "op.h" 39 40 #include "inner.h" 41 42 SExp run(SExp c) { 43 return vm(nil, c, run_env, nil, nil); 44 } 40 45 41 46 SExp evaluate(SExp code) { 42 47 SExp halt_code = cons(HALT, nil); 43 48 SExp c = compile(code, comp_env, halt_code); 44 SExp r = vm(nil, c, run_env, nil, nil); 45 return r; 46 } 47 48 49 static bool check_number(SExp a) 50 { 49 return run(c); 50 } 51 52 53 static bool check_number(SExp a) { 51 54 switch (type_of(a)) { 52 55 default: … … 85 88 } 86 89 87 static SExp builtin_plus(SExp arg) 88 { 90 static SExp builtin_plus(SExp arg) { 89 91 sint x = 0; 90 92 for (SExp p = arg; consp(p); p = cdr(p)) { … … 96 98 } 97 99 98 static SExp builtin_difference(SExp arg) 99 { 100 static SExp builtin_difference(SExp arg) { 100 101 SExp a = car(arg); 101 102 check_number(a); … … 115 116 } 116 117 117 static SExp builtin_lt(SExp arg) 118 { 118 static SExp builtin_times(SExp arg) { 119 sint x = 1; 120 for (SExp p = arg; consp(p); p = cdr(p)) { 121 SExp a = car(p); 122 check_number(a); 123 x *= s2int(a); 124 } 125 return int2s(x); 126 } 127 128 static SExp builtin_quotient(SExp arg) { 129 SExp a = car(arg); 130 check_number(a); 131 sint x = s2int(a); 132 133 SExp p = cdr(arg); 134 if (!consp(p)) { 135 x = -x; 136 } else { 137 for (; consp(p); p = cdr(p)) { 138 SExp a = car(p); 139 check_number(a); 140 sint d = s2int(a); 141 if (d == 0) { 142 // error(ERR_ZERO_DIVIDE); 143 assert(!"zero divide"); 144 } else { 145 x /= d; 146 } 147 } 148 } 149 return int2s(x); 150 } 151 152 static SExp builtin_lt(SExp arg) { 119 153 SExp a = car(arg); 120 154 check_number(a); … … 131 165 } 132 166 133 static SExp builtin_read(SExp arg) 134 { 167 static SExp builtin_gt(SExp arg) { 168 SExp a = car(arg); 169 check_number(a); 170 sint x = s2int(a); 171 172 for (SExp p = cdr(arg); consp(p); p = cdr(p)) { 173 SExp b = car(p); 174 check_number(a); 175 sint y = s2int(b); 176 if (x <= y) return nil; 177 x = y; 178 } 179 return t; 180 } 181 182 static SExp builtin_read(SExp arg) { 135 183 return read_from_file(stdin); 136 184 } 137 185 138 static SExp builtin_print(SExp arg) 139 { 186 static SExp builtin_print(SExp arg) { 140 187 SExp a = car(arg); 141 188 print(a); … … 143 190 } 144 191 145 static SExp builtin_eval(SExp arg) 146 { 192 static SExp builtin_eval(SExp arg) { 147 193 SExp code = car(arg); 148 194 149 195 return evaluate(code); 196 } 197 198 static SExp builtin_load(SExp arg) { 199 SExp a = car(arg); 200 const char* fn = s2str(a); 201 if (fn != NULL) { 202 if (load(fn)) { 203 return t; 204 } 205 } 206 return nil; 207 } 208 209 static SExp builtin_compile(SExp arg) { 210 SExp code = car(arg); 211 SExp halt_code = cons(HALT, nil); 212 return compile(code, comp_env, halt_code); 150 213 } 151 214 … … 168 231 { "+", builtin_plus, FALSE, 0, -1, }, 169 232 { "-", builtin_difference, FALSE, 1, -1, }, 233 { "*", builtin_times, FALSE, 0, -1, }, 234 { "/", builtin_quotient, FALSE, 1, -1, }, 170 235 { "<", builtin_lt, FALSE, 1, -1, }, 236 { ">", builtin_gt, FALSE, 1, -1, }, 171 237 172 238 … … 174 240 { "print", builtin_print, FALSE, 1, 1, }, 175 241 { "eval", builtin_eval, FALSE, 1, 1, }, 242 243 { "load", builtin_load, FALSE, 1, 1, }, 244 { "compile", builtin_compile, FALSE, 1, 1, }, 176 245 }; 177 246 for (int i=0; i<sizeof(tbl)/sizeof(*tbl); ++i) { … … 218 287 219 288 220 void loop() { 289 SExp compile_file(const char* fn) { 290 FILE* fp = fopen(fn, "r"); 291 SExp code = nil; 292 if (fp != NULL) { 293 SExp acc = nil; 294 for (;;) { 295 SExp s = read_from_file(fp); 296 if (nilp(s)) break; 297 acc = cons(s, acc); 298 } 299 SExp halt_code = cons(HALT, nil); 300 code = compile_block(nreverse(acc), comp_env, halt_code); 301 302 fclose(fp); 303 } 304 return code; 305 } 306 307 308 int load(const char* fn) { 309 SExp code = compile_file(fn); 310 if (!nilp(code)) { 311 SExp r = run(code); 312 return TRUE; 313 } else { 314 return FALSE; 315 } 316 } 317 318 319 void repl() { 221 320 SExp halt_code = cons(HALT, nil); 222 321 … … 238 337 239 338 240 SExp load_file(const char* fn) {241 FILE* fp = fopen(fn, "r");242 SExp code = nil;243 if (fp != NULL) {244 SExp acc = nil;245 for (;;) {246 SExp s = read_from_file(fp);247 if (nilp(s)) break;248 acc = cons(s, acc);249 }250 code = nreverse(acc);251 252 fclose(fp);253 }254 return code;255 }256 257 258 SExp load(const char* fn) {259 SExp sexps = load_file(fn);260 261 SExp halt_code = cons(HALT, nil);262 SExp c = compile_block(sexps, comp_env, halt_code);263 SExp r = vm(nil, c, run_env, nil, nil);264 265 return r;266 }267 268 269 339 int main(int argc, char* argv[]) 270 340 { … … 279 349 load(argv[1]); 280 350 } else { 281 loop();351 repl(); 282 352 } 283 353 mlisp_delete(); -
lang/c/misc/mlisp/test/vm.cpp
r11057 r11058 9 9 #include "inner.h" 10 10 #include <assert.h> 11 12 static void runtime_error(const char* msg) { 13 assert(!msg); 14 } 15 11 16 12 17 … … 27 32 for (int elt = s2int(cdr(access)); ; --elt) { 28 33 if (nilp(r)) { 29 extend_run_env(car(e), elt + 1); 34 SExp le = car(e); 35 if (!nilp(le)) { 36 extend_run_env(car(e), elt + 1); 37 } else { 38 rplaca(e, replicate(elt + 1, nil)); 39 } 40 30 41 r = car(e); elt = s2int(cdr(access)); 31 42 } … … 165 176 } 166 177 } else { 167 assert(!"illegal opecode"); 178 runtime_error("illegal opecode"); 179 return nil; 168 180 } 169 181 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)