#include "identify.h" #include "token.h" #include static int identify_dec(Token **list, unsigned int type) { if (*list == NULL || (*list)->next == NULL) return 0; if ((*list)->type != type) return 0; for (Token *e = (*list)->next; e != NULL; e = e->next) if (e->type != TOK_WORD) return 0; // Delete redundant first element (always the type keyword). token_delete(list, *list); return 1; } static int identify_control(Token **list, unsigned int type) { if (token_len(*list) != 3) return 0; if ((*list)->type != type) return 0; // TODO: Add checks for condition type. if (!token_isgroup((*list)->next->next, GROUP_SCOPE)) return 0; token_delete(list, *list); return 1; } void identify(Token *list, int (*fun)(Token**), unsigned int type, int recurse) { if (recurse) for (Token *e = list; e != NULL; e = e->next) if (e->type == TOK_GROUP) identify(e->group.tokens, fun, type, 1); for (Token *e = list; e != NULL; e = e->next) if (token_isgroup(e, GROUP_ATOM) && fun(&e->group.tokens)) e->group.type = type; } int identify_function(Token **list) { for (Token *e = *list; e != NULL; e = e->next) { if (e->next == NULL && !token_isgroup(e, GROUP_SCOPE)) return 0; if (e->next != NULL && e->type != TOK_WORD) return 0; } return 1; } int identify_let(Token **list) { return identify_dec(list, TOK_KW_LET); } int identify_return(Token **list) { if (token_len(*list) != 2) return 0; if ((*list)->type != TOK_KW_RETURN) return 1; // TODO: Add checks for value type. token_delete(list, *list); return 1; } int identify_assign(Token **list) { if (token_len(*list) != 3) return 0; if ((*list)->next->type != TOK_ASSIGN) return 0; // TODO: Add checks for LH and RH value types. token_delete(list, (*list)->next); return 1; } int identify_if(Token **list) { return identify_control(list, TOK_KW_IF); } int identify_else(Token **list) { return identify_control(list, TOK_KW_ELSE); } int identify_while(Token **list) { return identify_control(list, TOK_KW_WHILE); } int identify_expression(Token **list) { if (token_len(*list) != 1 || !token_isgroup(*list, GROUP_FUNCALL)) return 0; return 1; }