]> pd.if.org Git - pdutils/blob - posix/sh/main.c
implemented pwd
[pdutils] / posix / sh / main.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <ctype.h>
4
5 #include "gram.h"
6 #include "tok.h"
7 #include "lemon.h"
8 #include "parser.h"
9
10 struct reserved {
11         char word[16];
12         int token;
13 };
14
15         /* not quite reserved, but will fallback to WORD in the grammar */
16 static struct reserved rwords[] = {
17         { "case", TOKEN_Case }
18         ,{ "esac", TOKEN_Esac }
19         ,{ "in", TOKEN_In }
20         ,{ "if", TOKEN_If }
21         ,{ "then", TOKEN_Then }
22         ,{ "else", TOKEN_Else }
23         ,{ "elif", TOKEN_Elif }
24         ,{ "fi", TOKEN_Fi }
25         ,{ "do", TOKEN_Do }
26         ,{ "done", TOKEN_Done }
27         ,{ "while", TOKEN_While }
28         ,{ "until", TOKEN_Until }
29         ,{ "for", TOKEN_For }
30         ,{ "!", TOKEN_Bang }
31         ,{ "}", TOKEN_Rbrace }
32         ,{ "{", TOKEN_Lbrace }
33         ,{ "",0 }
34 };
35
36 static int is_name(char *word) {
37         int i;
38         if (isdigit(word[0])) {
39                 return 0;
40         }
41         for (i=0;word[i];i++) {
42                 if (word[i] != '_' && !isalnum(word[i])) {
43                         return 0;
44                 }
45         }
46         return 1;
47 }
48
49 static int is_reserved(char *word) {
50         int i;
51         for (i=0;rwords[i].token;i++) {
52                 fprintf(stderr, "checking rword %s ?= %s\n", word, rwords[i].word);
53                 if (!strcmp(word, rwords[i].word)) {
54                         fprintf(stderr, "matched\n");
55                         return rwords[i].token;
56                 }
57         }
58         return 0;
59 }
60
61 int main(void) {
62         void* parser;
63         struct token_state ts;
64         struct token token;
65         int type, rtype;
66
67         struct parser_state pstate;
68         pstate.casewords = 0;
69         pstate.incase = 0;
70
71         parser = ParseAlloc(malloc);
72         ts_init(&ts, stdin);
73         ParseTrace(stderr, "par: ");
74         while(type = get_token(&ts, &token)) {
75                 rtype = 0;
76                 if (type == EOF) break;
77                 if (token.type == TOKEN_WORD) {
78                         rtype = is_reserved(token.text);
79                         if (!rtype && is_name(token.text) && 0) {
80                                 rtype = TOKEN_NAME;
81                         }
82                 }
83                 if (rtype) {
84                         token.rtype = rtype;
85                 } else {
86                         token.rtype = token.type;
87                 }
88
89                 fprintf(stderr, "feeding type %d\n", token.rtype);
90                 
91                 Parse(parser, token.rtype, &token, &pstate);
92         }
93         Parse(parser, 0, &token, &pstate);
94
95         /* First input: 
96         15 / 5
97                                 */
98         ParseFree(parser, free );
99
100         return 0;
101 }