1.1 --- a/alt.h Fri Feb 11 02:15:04 2011 +0100
1.2 +++ b/alt.h Fri Feb 11 10:25:46 2011 +0100
1.3 @@ -4,7 +4,7 @@
1.4
1.5 /* tree */
1.6 #define TREE_DEPTH 32
1.7 -#define CHF(x) (x==' '?'s':x=='\t'?'t':x=='\n'?'n':x=='\r'?'r':x)
1.8 +#define CHF(x) (x=='\t'?'t':x=='\n'?'n':x=='\r'?'r':(x>'~'||x<' ')?'?':x)
1.9
1.10 typedef enum {
1.11 KEY_BLOCK, // {
1.12 @@ -71,7 +71,7 @@
1.13 int parse_file(AltState *st, const char *file);
1.14 int parse_is_operator(char ch);
1.15
1.16 -void alt_tree(AltState *st, int debug);
1.17 +void alt_tree(AltState *st);
1.18 void alt_tree_walk(AltState *st);
1.19 AltNode* alt_tree_resolve(AltState *st, const char *name);
1.20 void alt_tree_walk(AltState *st);
2.1 --- a/main.c Fri Feb 11 02:15:04 2011 +0100
2.2 +++ b/main.c Fri Feb 11 10:25:46 2011 +0100
2.3 @@ -7,22 +7,37 @@
2.4
2.5 void cb_level(AltState *st, int delta, char ch) {
2.6 PRINTLEVEL(st->level);
2.7 - if (delta>0) printf("{\n");
2.8 - else printf("}\n");
2.9 + printf (delta>0? "{\n": "}\n");
2.10 +}
2.11 +
2.12 +static void strfilter (char *str, const char *src, int len) {
2.13 + int i = 0;
2.14 + for (i=0; i<len && *src; i++, src++, str++) {
2.15 + int ch = CHF (*src);
2.16 + if (ch != *src || ch == '\\') {
2.17 + *str = '\\';
2.18 + str++;
2.19 + }
2.20 + *str = ch;
2.21 + }
2.22 + *str = 0;
2.23 }
2.24
2.25 void cb_word(AltState *st, char ch) {
2.26 - PRINTLEVEL(st->level);
2.27 - printf ("(%s) '%c'\n", st->str, CHF (ch));
2.28 + char str[1024];
2.29 + PRINTLEVEL (st->level);
2.30 + strfilter (str, st->str, sizeof (str));
2.31 + printf ("(%s) '%c'\n", str, CHF (ch));
2.32 }
2.33
2.34 int cb_error(AltState *st, const char *fmt) {
2.35 - printf ("ERROR: %s\n", fmt);
2.36 + fprintf (stderr, "ERROR: %s\n", fmt);
2.37 return 0;
2.38 }
2.39
2.40 static int _help(const char *arg0) {
2.41 - printf ("Usage: %s [-tph] [file ...]\n"
2.42 + printf ("Usage: %s [-rtph] [file ...]\n"
2.43 + " -d show debug\n"
2.44 " -r run script\n"
2.45 " -p show parse tree\n"
2.46 " -t show node tree\n"
2.47 @@ -35,21 +50,20 @@
2.48 int mode = 'p';
2.49 AltState st;
2.50
2.51 + memset (&st, 0, sizeof (AltState));
2.52 while (idx<argc) {
2.53 if (argv[idx][0] != '-')
2.54 break;
2.55 mode = argv[idx][1];
2.56 - if (mode == 'h')
2.57 - return _help (argv[0]);
2.58 + if (mode == 'd') st.debug = 1; else
2.59 + if (mode == 'h') return _help (argv[0]);
2.60 idx++;
2.61 }
2.62 - memset (&st, 0, sizeof (AltState));
2.63 - st.debug=1;
2.64 st.cb_word = cb_word;
2.65 st.cb_level = cb_level;
2.66 st.cb_error = cb_error;
2.67 if (mode == 'r' || mode == 't')
2.68 - alt_tree (&st, 0);
2.69 + alt_tree (&st);
2.70 if (idx>=argc)
2.71 ret = parse_fd (&st, 0);
2.72 else for (i=idx; i<argc; i++) {
3.1 --- a/parser.c Fri Feb 11 02:15:04 2011 +0100
3.2 +++ b/parser.c Fri Feb 11 10:25:46 2011 +0100
3.3 @@ -173,7 +173,7 @@
3.4 char buf[1024];
3.5 do {
3.6 ret = read (fd, buf, 1024);
3.7 - for (i=0;i<ret;i++)
3.8 + for (i=0; i<ret; i++)
3.9 if (!parse_char (st, buf[i]))
3.10 return 0;
3.11 } while (ret>0);
4.1 --- a/script.c Fri Feb 11 02:15:04 2011 +0100
4.2 +++ b/script.c Fri Feb 11 10:25:46 2011 +0100
4.3 @@ -10,7 +10,7 @@
4.4 }
4.5
4.6 int alt_word_is_assign(const char *str) {
4.7 - return (!strcmp(str, "=") || !strcmp(str, "+=") || !strcmp(str, "-="));
4.8 + return (!strcmp (str, "=") || !strcmp (str, "+=") || !strcmp (str, "-="));
4.9 }
4.10
4.11 int alt_word_is_op(const char *str) {
4.12 @@ -42,18 +42,18 @@
4.13 // variable
4.14 if (node->down && node->down->down && alt_word_is_assign(node->down->str)) {
4.15 //printf("ASSIGN '%s' = '%s'\n", node->str, node->down->down->str);
4.16 - setenv(node->str, node->down->down->str, 1);
4.17 + setenv (node->str, node->down->down->str, 1);
4.18 onode = node->down->down;
4.19 }
4.20 } else {
4.21 - if (!strcmp(node->str, "say")) {
4.22 + if (!strcmp (node->str, "say")) {
4.23 node = alt_tree_child (node);
4.24 while (node) {
4.25 puts (node->str);
4.26 node = node->down;
4.27 }
4.28 } else
4.29 - if (!strcmp(node->str, "system")) {
4.30 + if (!strcmp (node->str, "system")) {
4.31 if (!node->right) {
4.32 onode = node->down;
4.33 node = node->down;
4.34 @@ -69,24 +69,22 @@
4.35 node = node->down;
4.36 }
4.37 } else
4.38 - if (!strcmp(node->str, "exit")) {
4.39 + if (!strcmp (node->str, "exit")) {
4.40 node = alt_tree_child (node);
4.41 - if (node) exit (atoi(node->str));
4.42 - } else printf ("UNKNOWN (%s)\n", node->str);
4.43 + if (node) exit (atoi (node->str));
4.44 + } else fprintf (stderr, "UNKNOWN (%s)\n", node->str);
4.45 }
4.46 return alt_script_run (st, onode->down);
4.47 }
4.48
4.49 int alt_script(AltState *st) {
4.50 + AltNode *node;
4.51 AltTree *at = st->user;
4.52 - AltNode *node;
4.53 if (at == NULL)
4.54 return st->cb_error (st, "No tree found.");
4.55 -
4.56 node = alt_tree_resolve (st, "main");
4.57 if (node == NULL)
4.58 return st->cb_error (st, "Cannot find 'main'.");
4.59 -
4.60 //alt_tree_walk(st);
4.61 node = alt_tree_child (node);
4.62 return alt_script_run (st, node);
5.1 --- a/t/ops.alt Fri Feb 11 02:15:04 2011 +0100
5.2 +++ b/t/ops.alt Fri Feb 11 10:25:46 2011 +0100
5.3 @@ -1,4 +1,5 @@
5.4 main {
5.5 + a+=3;
5.6 foo= 3
5.7 jeje/*
5.8 bar =4
6.1 --- a/tree.c Fri Feb 11 02:15:04 2011 +0100
6.2 +++ b/tree.c Fri Feb 11 10:25:46 2011 +0100
6.3 @@ -7,11 +7,11 @@
6.4
6.5 static inline AltNode* node_alloc(AltTree *at) {
6.6 if (at->ncount >= ALLOC_POOL_SIZE) {
6.7 - if (++at->npool >= ALLOC_POOL_COUNT ) {
6.8 + if (++at->npool >= ALLOC_POOL_COUNT) {
6.9 fprintf (stderr, "FAIL: Cannot allocate more memory in the pool\n");
6.10 - exit(1);
6.11 + exit (1);
6.12 }
6.13 - at->nodes[at->npool] = malloc (sizeof(AltNode)*ALLOC_POOL_SIZE);
6.14 + at->nodes[at->npool] = malloc (sizeof (AltNode)*ALLOC_POOL_SIZE);
6.15 at->ncount = 0;
6.16 }
6.17 return &at->nodes[at->npool][at->ncount++];
6.18 @@ -52,12 +52,10 @@
6.19 }
6.20
6.21 static void engine_cb_word(AltState *st, char ch) {
6.22 + AltNode *node;
6.23 AltTree *at = (AltTree *) st->user;
6.24 - AltNode *node;
6.25 -
6.26 if (at->laststr && !*st->str && !*at->laststr)
6.27 return;
6.28 -
6.29 node = alt_node_new (st->user);
6.30 node->type = _word_type (st);
6.31 node->str = strdup (st->str);
6.32 @@ -71,7 +69,7 @@
6.33 node->left = at->cur;
6.34 } else {
6.35 /* add node at same nest level */
6.36 - //if (node != at->root) // do not infinite loop when only one node ???
6.37 + if (node != at->root) // do not infinite loop when only one node
6.38 at->cur->down = node;
6.39 node->up = at->cur;
6.40 }
6.41 @@ -128,26 +126,25 @@
6.42 return NULL;
6.43 }
6.44
6.45 -void alt_tree(AltState *st, int debug) {
6.46 - AltTree *at = (AltTree*) malloc (sizeof(AltTree));
6.47 +void alt_tree_free(AltState *st) {
6.48 + int i;
6.49 + AltTree *at;
6.50 + if (st != NULL && st->user != NULL) {
6.51 + at = (AltTree*) st->user;
6.52 + for (i=0; i<at->npool; i++)
6.53 + free (at->nodes[i]);
6.54 + free (at);
6.55 + st->user = NULL;
6.56 + }
6.57 +}
6.58 +
6.59 +void alt_tree(AltState *st) {
6.60 + AltTree *at = (AltTree*) malloc (sizeof (AltTree));
6.61 at->depth[0] = at->cur = at->root = 0;
6.62 at->npool = -1;
6.63 at->ncount = ALLOC_POOL_SIZE;
6.64 st->user = (void *) at;
6.65 /* set tree callbacks */
6.66 - st->debug = debug;
6.67 st->cb_word = engine_cb_word;
6.68 st->cb_level = engine_cb_level;
6.69 }
6.70 -
6.71 -void alt_tree_free(AltState *st) {
6.72 - int i;
6.73 - AltTree *at;
6.74 - if (st != NULL && st->user != NULL) {
6.75 - at = (AltTree*) st->user;
6.76 - for(i=0; i<at->npool; i++)
6.77 - free (at->nodes[i]);
6.78 - free(at);
6.79 - st->user = NULL;
6.80 - }
6.81 -}