summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-03-16 04:56:29 +0100
committerkdx <kikoodx@paranoici.org>2023-03-16 04:56:29 +0100
commitaa926bd6a868320a767d7428fffeff1b0f087dee (patch)
treed5e4fe31b74c5c467fb03216413cb9e4b7b452ce
parentfd20e3aaa63c2b79b4be8924208e5601e35e9ee7 (diff)
downloadgolem-aa926bd6a868320a767d7428fffeff1b0f087dee.tar.gz
identify assignements
-rw-r--r--group.c14
-rw-r--r--group.h17
-rw-r--r--identify.c11
-rw-r--r--identify.h1
-rw-r--r--main.c3
-rw-r--r--token.c11
-rw-r--r--token.h1
7 files changed, 51 insertions, 7 deletions
diff --git a/group.c b/group.c
index 87d4145..4f774bc 100644
--- a/group.c
+++ b/group.c
@@ -68,6 +68,20 @@ group_scope(Token **list)
}
int
+group_funcall(Token **list)
+{
+ return group_enclosed(list, TOK_OPEN_PAREN, TOK_CLOS_PAREN,
+ GROUP_FUNCALL);
+}
+
+int
+group_deref(Token **list)
+{
+ return group_enclosed(list, TOK_OPEN_SQUAR, TOK_CLOS_SQUAR,
+ GROUP_DEREF);
+}
+
+int
group_atom(Token **list)
{
// Recurse in child GROUP_SCOPEs.
diff --git a/group.h b/group.h
index edfc7bd..0961a8a 100644
--- a/group.h
+++ b/group.h
@@ -1,13 +1,14 @@
#pragma once
enum {
- GROUP_NONE = 0,
- GROUP_SCOPE = 's',
- GROUP_ATOM = 'a',
- GROUP_FUNCTION = 'f',
- GROUP_ASSIGN = '=',
- GROUP_LET = 'l',
- GROUP_DEREF = 'd',
+ GROUP_NONE = 0,
+ GROUP_SCOPE = 's',
+ GROUP_ATOM = 'a',
+ GROUP_FUNCTION = 'f',
+ GROUP_FUNCALL = 'e',
+ GROUP_ASSIGN = '=',
+ GROUP_LET = 'l',
+ GROUP_DEREF = 'd',
};
struct Token;
@@ -18,4 +19,6 @@ typedef struct {
struct Token *group_create(struct Token **l, struct Token *b, struct Token *e);
int group_scope(struct Token **list);
+int group_funcall(struct Token **list);
+int group_deref(struct Token **list);
int group_atom(struct Token **list);
diff --git a/identify.c b/identify.c
index 5489d72..06a864a 100644
--- a/identify.c
+++ b/identify.c
@@ -48,3 +48,14 @@ identify_let(Token **list)
{
return identify_dec(list, TOK_KW_LET);
}
+
+int
+identify_assign(Token **list)
+{
+ if (token_len(*list) != 3)
+ return 0;
+ if ((*list)->next->type != TOK_ASSIGN)
+ return 0;
+ token_delete(list, (*list)->next);
+ return 1;
+}
diff --git a/identify.h b/identify.h
index 86bd422..d1e1b04 100644
--- a/identify.h
+++ b/identify.h
@@ -4,3 +4,4 @@
void identify(Token *list, int (*fun)(Token**), unsigned int type, int recurse);
int identify_function(Token **list);
int identify_let(Token **list);
+int identify_assign(Token **list);
diff --git a/main.c b/main.c
index f1c8a6c..19aca62 100644
--- a/main.c
+++ b/main.c
@@ -37,8 +37,11 @@ main(int argc, char **argv)
destroy_duplicates(&tokens, TOK_END);
group_scope(&tokens);
group_atom(&tokens);
+ group_funcall(&tokens);
+ group_deref(&tokens);
identify(tokens, identify_function, GROUP_FUNCTION, 0);
identify(tokens, identify_let, GROUP_LET, 1);
+ identify(tokens, identify_assign, GROUP_ASSIGN, 1);
token_print(tokens, 1, 0);
token_destroy(tokens);
}
diff --git a/token.c b/token.c
index b419908..05808f1 100644
--- a/token.c
+++ b/token.c
@@ -114,3 +114,14 @@ token_isgroup(Token *token, unsigned int type)
{
return (token->type == TOK_GROUP && token->group.type == type);
}
+
+int
+token_len(Token *token)
+{
+ int len = 0;
+ while (token != NULL) {
+ len += 1;
+ token = token->next;
+ }
+ return len;
+}
diff --git a/token.h b/token.h
index 9c4b699..d74c1a0 100644
--- a/token.h
+++ b/token.h
@@ -43,3 +43,4 @@ Token *token_search(Token *list, unsigned int type);
Token *token_search_closing(Token *list, unsigned int type, unsigned int opn);
Token *token_print(Token *token, int recurse, int depth);
int token_isgroup(Token *token, unsigned int type);
+int token_len(Token *token);