X-Git-Url: https://pd.if.org/git/?p=pdutils;a=blobdiff_plain;f=utils%2Fsh%2Fmain.c;fp=utils%2Fsh%2Fmain.c;h=c956e10c0be6ef89cc278c00ac9f2169df2cf153;hp=0000000000000000000000000000000000000000;hb=dce1707cec7cd4268fe73371c7488052e5058181;hpb=5cd80baaa2b300ac0e2bded7bf9dfc7b74022ab8 diff --git a/utils/sh/main.c b/utils/sh/main.c new file mode 100644 index 0000000..c956e10 --- /dev/null +++ b/utils/sh/main.c @@ -0,0 +1,101 @@ +#include +#include +#include + +#include "gram.h" +#include "tok.h" +#include "lemon.h" +#include "parser.h" + +struct reserved { + char word[16]; + int token; +}; + + /* not quite reserved, but will fallback to WORD in the grammar */ +static struct reserved rwords[] = { + { "case", TOKEN_Case } + ,{ "esac", TOKEN_Esac } + ,{ "in", TOKEN_In } + ,{ "if", TOKEN_If } + ,{ "then", TOKEN_Then } + ,{ "else", TOKEN_Else } + ,{ "elif", TOKEN_Elif } + ,{ "fi", TOKEN_Fi } + ,{ "do", TOKEN_Do } + ,{ "done", TOKEN_Done } + ,{ "while", TOKEN_While } + ,{ "until", TOKEN_Until } + ,{ "for", TOKEN_For } + ,{ "!", TOKEN_Bang } + ,{ "}", TOKEN_Rbrace } + ,{ "{", TOKEN_Lbrace } + ,{ "",0 } +}; + +static int is_name(char *word) { + int i; + if (isdigit(word[0])) { + return 0; + } + for (i=0;word[i];i++) { + if (word[i] != '_' && !isalnum(word[i])) { + return 0; + } + } + return 1; +} + +static int is_reserved(char *word) { + int i; + for (i=0;rwords[i].token;i++) { + fprintf(stderr, "checking rword %s ?= %s\n", word, rwords[i].word); + if (!strcmp(word, rwords[i].word)) { + fprintf(stderr, "matched\n"); + return rwords[i].token; + } + } + return 0; +} + +int main(void) { + void* parser; + struct token_state ts; + struct token token; + int type, rtype; + + struct parser_state pstate; + pstate.casewords = 0; + pstate.incase = 0; + + parser = ParseAlloc(malloc); + ts_init(&ts, stdin); + ParseTrace(stderr, "par: "); + while(type = get_token(&ts, &token)) { + rtype = 0; + if (type == EOF) break; + if (token.type == TOKEN_WORD) { + rtype = is_reserved(token.text); + if (!rtype && is_name(token.text) && 0) { + rtype = TOKEN_NAME; + } + } + if (rtype) { + token.rtype = rtype; + } else { + token.rtype = token.type; + } + + fprintf(stderr, "feeding type %d\n", token.rtype); + + Parse(parser, token.rtype, &token, &pstate); + } + Parse(parser, 0, &token, &pstate); + + /* First input: + 15 / 5 + */ + ParseFree(parser, free ); + + return 0; +}