Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
93 lines (81 loc) · 1.96 KB
/
Copy pathmain.c
File metadata and controls
93 lines (81 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* alt - abstract language tree // pancake<at>nopcode.org */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "alt.h"
void cb_level(AltState *st, int delta, char ch) {
PRINTLEVEL (st->level);
printf (delta>0? "%c\n": "%c\n", ch);
}
static void strfilter (char *str, const char *src, int len) {
int i;
for (i=0; i<len && *src; i++, src++, str++) {
int ch = CHF (*src);
if (ch != *src || ch == '\\')
*str++ = '\\';
*str = ch;
}
*str = 0;
}
void cb_comment(AltState *st, char ch) {
PRINTLEVEL (st->level);
printf ("/*%s*/ '%c'\n", st->str, CHF (ch));
}
void cb_word(AltState *st, char ch) {
char str[1024];
PRINTLEVEL (st->level);
strfilter (str, st->str, sizeof (str));
printf ("(%s) '%c'\n", str, CHF (ch));
}
int cb_error(AltState *st, const char *fmt) {
fprintf (stderr, "ERROR: %s\n", fmt);
return 0;
}
static int _version(const char *arg0) {
printf (ALT_VERSION"\n");
return 0;
}
static int _help(const char *arg0) {
printf ("Usage: %s [-rtph] [file ...]\n"
" -d show debug\n"
" -r run script\n"
" -p show parse tree\n"
" -t show node tree\n"
" -v show version\n"
" -h this help\n", arg0);
return 0;
}
int main(int argc, char **argv) {
int i, ret, idx = 1;
AltState st = {0};
int mode = 'p';
while (idx<argc) {
if (argv[idx][0] != '-')
break;
mode = argv[idx][1];
if (mode == 'v') return _version(argv[0]);
if (mode == 'd') st.debug = 1; else
if (mode == 'h') return _help (argv[0]);
idx++;
}
st.cb_word = cb_word;
st.cb_level = cb_level;
st.cb_error = cb_error;
st.cb_comment = cb_comment;
if (mode == 'r' || mode == 't')
alt_tree (&st);
if (idx>=argc)
ret = parse_fd (&st, 0);
else for (i=idx; i<argc; i++) {
if (parse_file (&st, argv[i])) {
fprintf (stderr, "Cannot open file '%s'\n", argv[i]);
return 1;
}
}
if (mode == 'r')
alt_script (&st);
else if (mode == 't')
alt_tree_walk (&st);
alt_tree_free (&st);
return 0;
}