diff options
author | kdx <kdx@42l.fr> | 2023-03-16 00:51:32 +0100 |
---|---|---|
committer | kdx <kdx@42l.fr> | 2023-03-16 00:51:32 +0100 |
commit | f3f52fafb74dd9f94f405f704b82b06a1a5efcf4 (patch) | |
tree | ce77316b3f560f2872668f0753f3c371ad5438d0 | |
parent | a0a2fb39c75525af379a18e418cadcfd347957f6 (diff) | |
download | golem-f3f52fafb74dd9f94f405f704b82b06a1a5efcf4.tar.gz |
identify local and global declarations
-rwxr-xr-x | build.sh | 2 | ||||
-rw-r--r-- | identify.c | 32 | ||||
-rw-r--r-- | identify.h | 4 | ||||
-rw-r--r-- | main.c | 4 |
4 files changed, 38 insertions, 4 deletions
@@ -1,2 +1,2 @@ #!/bin/sh -gcc -O0 -g -std=c99 -Wall -Wextra -o golem *.c +tcc -Os -std=c99 -Wall -Wextra -o golem *.c @@ -2,9 +2,27 @@ #include "token.h" #include <stddef.h> +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; + return 1; +} + void -identify(Token *list, int (*fun)(Token*), unsigned int type) +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; @@ -22,3 +40,15 @@ identify_function(Token *list) } return 1; } + +int +identify_global(Token *list) +{ + return identify_dec(list, TOK_KW_GLOBAL); +} + +int +identify_local(Token *list) +{ + return identify_dec(list, TOK_KW_LOCAL); +} @@ -1,5 +1,7 @@ #pragma once #include "token.h" -void identify(Token *list, int (*fun)(Token*), unsigned int type); +void identify(Token *list, int (*fun)(Token*), unsigned int type, int recurse); int identify_function(Token *list); +int identify_global(Token *list); +int identify_local(Token *list); @@ -37,7 +37,9 @@ main(int argc, char **argv) destroy_duplicates(&tokens, TOK_END); group_scope(&tokens); group_atom(&tokens); - identify(tokens, identify_function, GROUP_FUNCTION); + identify(tokens, identify_function, GROUP_FUNCTION, 0); + identify(tokens, identify_global, GROUP_DEC_GLOBAL, 0); + identify(tokens, identify_local, GROUP_DEC_LOCAL, 1); token_print(tokens, 1, 0); token_destroy(tokens); } |