|
Revision 5993, 1.6 kB
(checked in by mokehehe, 5 years ago)
|
|
builtin を自動に組込みに
コマンドラインオプションでファイルを実行できるように
|
| Line | |
|---|
| 1 | #include <stdio.h>
|
|---|
| 2 | #include <string.h>
|
|---|
| 3 | #include "mlisp.h"
|
|---|
| 4 |
|
|---|
| 5 | #ifndef FALSE
|
|---|
| 6 | #define FALSE (0)
|
|---|
| 7 | #define TRUE (1)
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | /// �Θb��
|
|---|
| 11 | void interact(MLContext* ml, FILE* fp, FILE* ofp)
|
|---|
| 12 | {
|
|---|
| 13 | int b_print_read = FALSE;
|
|---|
| 14 |
|
|---|
| 15 | for (;;) {
|
|---|
| 16 | SExp r, e = cNL;
|
|---|
| 17 | const char* str;
|
|---|
| 18 |
|
|---|
| 19 | fprintf(ofp, "> ");
|
|---|
| 20 |
|
|---|
| 21 | for (;;) {
|
|---|
| 22 | r = ml_read(ml, fp);
|
|---|
| 23 | if (r == cEOF) goto L_quit;
|
|---|
| 24 | if (r == cNL) break;
|
|---|
| 25 |
|
|---|
| 26 | str = ml_get_string(ml, r);
|
|---|
| 27 | if (str != NULL) {
|
|---|
| 28 | if (strcmp(str, ":quit") == 0) goto L_quit;
|
|---|
| 29 | if (strcmp(str, ":debug") == 0) {
|
|---|
| 30 | b_print_read = !b_print_read;
|
|---|
| 31 | ml_set_flag(ml, MLF_PRINT_FUNC_ARG, b_print_read);
|
|---|
| 32 | ml_set_flag(ml, MLF_PRINT_MACRO_REPLACE, b_print_read);
|
|---|
| 33 | fprintf(ofp, "debug: %s\n", b_print_read ? "ON" : "OFF");
|
|---|
| 34 | continue;
|
|---|
| 35 | }
|
|---|
| 36 | if (strcmp(str, ":dump") == 0) {
|
|---|
| 37 | ml_dump(ml);
|
|---|
| 38 | continue;
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | if (b_print_read) {
|
|---|
| 42 | ml_print(ml, ofp, r);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | e = ml_eval(ml, r);
|
|---|
| 46 | }
|
|---|
| 47 | if (e != cNL) {
|
|---|
| 48 | ml_print(ml, ofp, e);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | L_quit:;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /// �Θb��
|
|---|
| 55 | int eval_file(MLContext* ml, const char* fn)
|
|---|
| 56 | {
|
|---|
| 57 | FILE* fp = fopen(fn, "r");
|
|---|
| 58 | if (fp == NULL) {
|
|---|
| 59 | return FALSE;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | for (;;) {
|
|---|
| 63 | SExp r = ml_read(ml, fp);
|
|---|
| 64 | if (r == cEOF) break;
|
|---|
| 65 | if (r == cNL) continue;
|
|---|
| 66 |
|
|---|
| 67 | ml_eval(ml, r);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | return TRUE;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /// �G���g���|�C���g
|
|---|
| 74 | int main(int argc, char* argv[])
|
|---|
| 75 | {
|
|---|
| 76 | MLContext* ml = ml_new();
|
|---|
| 77 |
|
|---|
| 78 | if (argc < 2) {
|
|---|
| 79 | interact(ml, stdin, stdout);
|
|---|
| 80 | } else {
|
|---|
| 81 | int i;
|
|---|
| 82 | for (i=0; ++i<argc; ) {
|
|---|
| 83 | const char* fn = argv[i];
|
|---|
| 84 | eval_file(ml, fn);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | ml_delete(ml);
|
|---|
| 89 |
|
|---|
| 90 | return 0;
|
|---|
| 91 | }
|
|---|