summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx@42l.fr>2023-03-16 00:51:32 +0100
committerkdx <kdx@42l.fr>2023-03-16 00:51:32 +0100
commitf3f52fafb74dd9f94f405f704b82b06a1a5efcf4 (patch)
treece77316b3f560f2872668f0753f3c371ad5438d0
parenta0a2fb39c75525af379a18e418cadcfd347957f6 (diff)
downloadgolem-f3f52fafb74dd9f94f405f704b82b06a1a5efcf4.tar.gz
identify local and global declarations
-rwxr-xr-xbuild.sh2
-rw-r--r--identify.c32
-rw-r--r--identify.h4
-rw-r--r--main.c4
4 files changed, 38 insertions, 4 deletions
diff --git a/build.sh b/build.sh
index eb8f5ab..ed7e3e1 100755
--- a/build.sh
+++ b/build.sh
@@ -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
diff --git a/identify.c b/identify.c
index c9f2dac..def4438 100644
--- a/identify.c
+++ b/identify.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);
+}
diff --git a/identify.h b/identify.h
index 1f58cb6..1986469 100644
--- a/identify.h
+++ b/identify.h
@@ -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);
diff --git a/main.c b/main.c
index 098b895..a8ad2e2 100644
--- a/main.c
+++ b/main.c
@@ -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);
}