grammar case part 1 motherfucker

This commit is contained in:
AntoHesse 2017-02-22 01:43:25 +01:00
parent 49b79603b3
commit 5976d0d281
10 changed files with 37 additions and 23 deletions

View file

@ -1,8 +0,0 @@
case $rental in
("car") echo "For $rental rental is Rs.20 per k/m.";;
("van") echo "For $rental rental is Rs.10 per k/m.";;
("jeep") echo "For $rental rental is Rs.5 per k/m.";;
("bicycle") echo "For $rental rental 20 paisa per k/m.";;
("enfield") echo "For $rental rental Rs.3 per k/m.";;
("thunderbird") echo "For $rental rental Rs.5 per k/m.";;
esac

View file

@ -128,7 +128,7 @@ void set_exitstatus(int status, int override);
void ast_free(void *data, size_t content_size); void ast_free(void *data, size_t content_size);
int loop_exec(t_list *list_ast);
int loop_del(t_list *list_ast);
#endif #endif

View file

@ -1,8 +1,8 @@
case $rental in case $rental in
("car") echo "For $rental rental is Rs.20 per k/m.";; "cat") echo "For $rental rental is Rs.20 per k/m.";;
("van") echo "For $rental rental is Rs.10 per k/m.";; "van") echo "For $rental rental is Rs.10 per k/m.";;
("jeep") echo "For $rental rental is Rs.5 per k/m.";; "jeep") echo "For $rental rental is Rs.5 per k/m.";;
("bicycle") echo "For $rental rental 20 paisa per k/m.";; "bicycle") echo "For $rental rental 20 paisa per k/m.";;
("enfield") echo "For $rental rental Rs.3 per k/m.";; "enfield") echo "For $rental rental Rs.3 per k/m.";;
("thunderbird") echo "For $rental rental Rs.5 per k/m.";; "thunderbird") echo "For $rental rental Rs.5 per k/m.";;
esac esac

View file

@ -16,10 +16,12 @@ int get_reserved_words(t_list **alst)
{ {
t_token *token; t_token *token;
t_token *previous_token; t_token *previous_token;
t_token *ante_token;
t_list *temp; t_list *temp;
temp = *alst; temp = *alst;
previous_token = NULL; previous_token = NULL;
ante_token = NULL;
while (temp) while (temp)
{ {
token = temp->content; token = temp->content;
@ -45,8 +47,14 @@ int get_reserved_words(t_list **alst)
token->type = TK_ELSE; token->type = TK_ELSE;
else if (ft_strncmp(token->data, "until", 5) == 0) else if (ft_strncmp(token->data, "until", 5) == 0)
token->type = TK_UNTIL; token->type = TK_UNTIL;
else if (ft_strncmp(token->data, "case", 4) == 0)
token->type = TK_CASE;
} }
} }
if (ante_token && ante_token->type == TK_CASE
&& ft_strncmp(token->data, "in", 2) == 0)
token->type = TK_IN;
ante_token = previous_token;
previous_token = token; previous_token = token;
temp = temp->next; temp = temp->next;
} }

View file

@ -30,6 +30,7 @@ int handle_instruction(int fd)
DG("START: state=%i", parser.state); DG("START: state=%i", parser.state);
while (1) while (1)
{ {
DG("get input");
if ((ret = readline(fd, get_lexer_stack(lexer), &str))) if ((ret = readline(fd, get_lexer_stack(lexer), &str)))
{ {
if (ret == -1) if (ret == -1)
@ -47,21 +48,20 @@ int handle_instruction(int fd)
if (get_lexer_stack(lexer)) if (get_lexer_stack(lexer))
continue ; continue ;
lexer.state = DEFAULT; lexer.state = DEFAULT;
token_print(token);
if (get_reserved_words(&token)) if (get_reserved_words(&token))
return (1); return (1);
if (insert_newline(&token)) if (insert_newline(&token))
return (1); return (1);
if (ft_parse(&ast, &token, &parser)) if (ft_parse(&ast, &token, &parser))
continue ; continue ;
token = NULL;
DG("AFTER PARSING: state=%i", parser.state);
if (parser.state == SUCCESS) if (parser.state == SUCCESS)
break ; break ;
else if (parser.state == ERROR) else if (parser.state == ERROR)
return (error_syntax(&token)); return (error_syntax(&token));
} }
DG("succesful parsing:"); DG("succesful parsing:");
btree_print(STDBUG, ast, &ft_putast); // btree_print(STDBUG, ast, &ft_putast);
/* if (ft_exec(&ast)) */ /* if (ft_exec(&ast)) */
/* return (1); */ /* return (1); */
ft_add_str_in_history(lexer.str); ft_add_str_in_history(lexer.str);

View file

@ -57,6 +57,7 @@ t_aggrematch g_aggrematch[] =
{LINEBREAK, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST}, {LINEBREAK, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST},
{NEWLINE_LIST, CMD_NAME, CMD_SUPERIOR, CMD_NAME}, {NEWLINE_LIST, CMD_NAME, CMD_SUPERIOR, CMD_NAME},
{NEWLINE_LIST, TK_DO, TK_DO, TK_DO}, {NEWLINE_LIST, TK_DO, TK_DO, TK_DO},
{NEWLINE_LIST, TK_IN, TK_IN, TK_IN},
{NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN}, {NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN},
{NEWLINE_LIST, TK_IF, TK_IF, TK_IF}, {NEWLINE_LIST, TK_IF, TK_IF, TK_IF},
{NEWLINE_LIST, TK_ELIF, TK_ELIF, TK_ELIF}, {NEWLINE_LIST, TK_ELIF, TK_ELIF, TK_ELIF},

View file

@ -203,6 +203,7 @@ t_stackmatch g_stackmatch[] =
{TK_CASE, NEWLINE_LIST}, {TK_CASE, NEWLINE_LIST},
{TK_CASE, SEPARATOR_OP}, {TK_CASE, SEPARATOR_OP},
{TK_CASE, PIPE_SEMI_SEQUENCE}, {TK_CASE, PIPE_SEMI_SEQUENCE},
{TK_IN, TK_WORD},
{TK_ESAC, CASE_LIST}, {TK_ESAC, CASE_LIST},
{TK_ESAC, CASE_LIST_NS}, {TK_ESAC, CASE_LIST_NS},
{TK_ESAC, LINEBREAK}, {TK_ESAC, LINEBREAK},
@ -304,6 +305,7 @@ t_stackmatch g_stackmatch[] =
{LINEBREAK, COMPOUND_LIST}, {LINEBREAK, COMPOUND_LIST},
{LINEBREAK, PROGRAM}, {LINEBREAK, PROGRAM},
{NEWLINE_LIST, TK_DO}, {NEWLINE_LIST, TK_DO},
{NEWLINE_LIST, TK_IN},
{NEWLINE_LIST, TK_WHILE}, {NEWLINE_LIST, TK_WHILE},
{NEWLINE_LIST, TK_UNTIL}, {NEWLINE_LIST, TK_UNTIL},
{NEWLINE_LIST, TK_IF}, {NEWLINE_LIST, TK_IF},
@ -497,6 +499,7 @@ t_stackmatch g_stackmatch[] =
{PATTERN, TK_PAREN_OPEN}, {PATTERN, TK_PAREN_OPEN},
{PATTERN, CASE_LIST}, {PATTERN, CASE_LIST},
{PATTERN, TK_IN},
{PATTERN_CASE, CASE_LIST}, {PATTERN_CASE, CASE_LIST},
{CASE_ITEM, CASE_LIST}, {CASE_ITEM, CASE_LIST},
{CASE_ITEM, LINEBREAK}, {CASE_ITEM, LINEBREAK},

View file

@ -28,6 +28,7 @@ static int end_instruction(t_sym sym)
int ft_parse(t_btree **ast, t_list **token, t_parser *parser) int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
{ {
(void)ast;
while (*token) while (*token)
{ {
produce_sym(*parser->stack, parser->new_sym, token); produce_sym(*parser->stack, parser->new_sym, token);
@ -45,8 +46,8 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
parser->state = SUCCESS; parser->state = SUCCESS;
else else
parser->state = UNDEFINED; parser->state = UNDEFINED;
build_tree(ast, token); // build_tree(ast, token);
btree_print(STDBUG, *ast, &ft_putast); // btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*parser->stack) && !(*token)->next)) if ((end_instruction(*parser->stack) && !(*token)->next))
/* || *parser->stack == PROGRAM) */ /* || *parser->stack == PROGRAM) */
insert_linebreak(token); insert_linebreak(token);

View file

@ -43,7 +43,7 @@ t_prodmatch g_prodmatch[] =
{TK_WORD, TK_BANG, CMD_NAME}, {TK_WORD, TK_BANG, CMD_NAME},
{TK_WORD, PIPE_SEMI_SEQUENCE, CMD_NAME}, {TK_WORD, PIPE_SEMI_SEQUENCE, CMD_NAME},
{TK_WORD, SEPARATOR_OP, CMD_NAME}, {TK_WORD, SEPARATOR_OP, CMD_NAME},
{TK_WORD, IN, WORDLIST}, {TK_WORD, TK_IN, PATTERN},
{TK_WORD, CASE_LIST, PATTERN}, {TK_WORD, CASE_LIST, PATTERN},
{TK_WORD, TK_PAREN_OPEN, PATTERN}, {TK_WORD, TK_PAREN_OPEN, PATTERN},
{TK_ASSIGNEMENT_WORD, LINEBREAK, CMD_PREFIX}, {TK_ASSIGNEMENT_WORD, LINEBREAK, CMD_PREFIX},
@ -56,6 +56,7 @@ t_prodmatch g_prodmatch[] =
{TK_NAME, NEWLINE_LIST, FNAME}, {TK_NAME, NEWLINE_LIST, FNAME},
{TK_NAME, TK_FOR, NAME}, {TK_NAME, TK_FOR, NAME},
{TK_NEWLINE, TK_DO, NEWLINE_LIST}, {TK_NEWLINE, TK_DO, NEWLINE_LIST},
{TK_NEWLINE, TK_IN, NEWLINE_LIST},
{TK_NEWLINE, TK_WHILE, NEWLINE_LIST}, {TK_NEWLINE, TK_WHILE, NEWLINE_LIST},
{TK_NEWLINE, TK_UNTIL, NEWLINE_LIST}, {TK_NEWLINE, TK_UNTIL, NEWLINE_LIST},
{TK_NEWLINE, TK_IF, NEWLINE_LIST}, {TK_NEWLINE, TK_IF, NEWLINE_LIST},

View file

@ -16,6 +16,14 @@ char *read_state(t_sym current)
{ {
if (current == 0) if (current == 0)
return ("NULL"); return ("NULL");
if (current == PATTERN)
return ("PATTERN");
if (current == TK_CASE)
return ("TK_CASE");
if (current == TK_IN)
return ("TK_IN");
if (current == TK_ESAC)
return ("TK_ESAC");
if (current == UNTIL_CLAUSE) if (current == UNTIL_CLAUSE)
return ("UNTIL_CLAUSE"); return ("UNTIL_CLAUSE");
if (current == TK_UNTIL) if (current == TK_UNTIL)