aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorc+12023-11-18 23:14:24 -0500
committerc+12023-11-18 23:14:24 -0500
commit000658c8405f93bf8ce1e03a9055e6944e730534 (patch)
tree9d49aefb841a413351a3c0168497a93053062000 /src
parent2cc68205a1c0b746ad405607940e7183c4fb09b0 (diff)
I honestly don't remember. Parser stuff. Probably.
Diffstat (limited to 'src')
-rw-r--r--src/include/parser.h24
-rw-r--r--src/include/token.h46
-rw-r--r--src/include/tree.h31
-rw-r--r--src/main.c2
-rw-r--r--src/parser.c26
-rw-r--r--src/token.c2
-rw-r--r--src/tree.c50
-rw-r--r--src/util.c2
8 files changed, 125 insertions, 58 deletions
diff --git a/src/include/parser.h b/src/include/parser.h
index 79e1289..b39f481 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -24,6 +24,28 @@ parser_t* parser_init(token_t* token);
void parser_destroy(parser_t* parser);
/* Step the parser forward by 1 token. */
-void parser_nxt_token(parser_t* parser);
+int parser_nxt_token(parser_t* parser);
+
+/* Check whether the current token matches the given type. */
+int parser_match(parser_t* parser, token_type_t type);
+
+/*
+ parse lit: converting everything from strings to value in the tree
+ parse expr: (For now) call parse lit
+ parse blk: loop over all expressions until reaching an end block.
+
+*/
+
+/* Parse a single literal value. */
+void parser_parse_lit(parser_t* parser);
+
+/* Parse a single expression. */
+void parser_parse_expr(parser_t* parser);
+
+/* Parse a single block. */
+void parser_parse_blk(parser_t* parser);
+
+/* Parse the given tokens. */
+void parser_parse(parser_t* parser);
#endif
diff --git a/src/include/token.h b/src/include/token.h
index 08de829..f86c47f 100644
--- a/src/include/token.h
+++ b/src/include/token.h
@@ -4,35 +4,37 @@
#include "util.h"
#include "hlkt.h"
+typedef enum TOKEN_TYPE {
+ TOKEN_UNKNOWN,
+ TOKEN_CHAR,
+ TOKEN_STR,
+ TOKEN_EXPR_END,
+ TOKEN_SET,
+ TOKEN_LGROUP,
+ TOKEN_RGROUP,
+ TOKEN_APPLY,
+ TOKEN_LIST_DELIM,
+ TOKEN_TAG,
+ TOKEN_NAMESPACE_DELIM,
+ TOKEN_LBLOCK,
+ TOKEN_RBLOCK,
+ TOKEN_RLIST,
+ TOKEN_LLIST,
+ TOKEN_ESC,
+ TOKEN_KWD,
+ TOKEN_INT
+} token_type_t;
+
/* Token struct. */
-typedef struct TOKEN_STRUC {
+typedef struct TOKEN {
/* Token type. */
- enum TOKEN_TYPE {
- TOKEN_UNKNOWN,
- TOKEN_CHAR,
- TOKEN_STR,
- TOKEN_EXPR_END,
- TOKEN_SET,
- TOKEN_LGROUP,
- TOKEN_RGROUP,
- TOKEN_APPLY,
- TOKEN_LIST_DELIM,
- TOKEN_TAG,
- TOKEN_NAMESPACE_DELIM,
- TOKEN_LBLOCK,
- TOKEN_RBLOCK,
- TOKEN_RLIST,
- TOKEN_LLIST,
- TOKEN_ESC,
- TOKEN_KWD,
- TOKEN_INT
- } type;
+ token_type_t type;
/* Token value. */
char* val;
/* Next token. */
- struct TOKEN_STRUC* nxt;
+ struct TOKEN* nxt;
} token_t;
/* Creates a token. */
diff --git a/src/include/tree.h b/src/include/tree.h
index b878852..3dfb5be 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -3,9 +3,7 @@
#include "util.h"
-/* The Abstract Syntax Tree (AST) structure. */
-typedef struct TREE {
- enum TREE_TYPE {
+typedef enum TREE_TYPE {
TREE_TYPE_BLOCK,
TREE_TYPE_LINT,
TREE_TYPE_LSTR,
@@ -14,14 +12,21 @@ typedef struct TREE {
TREE_TYPE_CARG,
TREE_TYPE_DEF,
TREE_TYPE_CAL,
- } type;
+} tree_type_t;
+
+/* The Abstract Syntax Tree (AST) structure. */
+typedef struct TREE {
+ tree_type_t type;
union TREE_DATA{
/* Block. */
struct TREE_DATA_BLOCK {
/* The first expression in the block. */
struct TREE* val;
- /* The next expression in the block. If it's'nt a block, end the block. */
+ /*
+ The next block in the linked list.
+ - If it's `NULL`, it's the end of the block.
+ */
struct TREE* nxt;
} block;
@@ -75,19 +80,7 @@ tree_t* tree_init(int type);
/* Destroy the AST. */
void tree_destroy(tree_t* tree);
-/* TODO: Implement a better target organization structure that's better for searching. */
-typedef struct TREE_TARG {
- tree_t* tree;
- struct TREE_TARG* nxt;
-} tree_targ_t;
-
-/* Create a new target. */
-tree_targ_t* tree_targ_init(tree_t* tree);
-/*
- Destroy a target.
- - Frees all subsequent targets in `nxt`.
- - Does not free the `tree`.
-*/
-void tree_targ_destroy(tree_targ_t* targ);
+/* Print a tree. */
+void tree_print(tree_t* tree, int nest);
#endif
diff --git a/src/main.c b/src/main.c
index 2284444..8a60315 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,11 +7,13 @@
#include "include/token.h"
#include "include/pp.h"
#include "include/lexer.h"
+#include "include/parser.h"
int main(int argc, char* argv[]) {
char* src; /* the source "code" */
pp_t* pp;
lexer_t* lexer;
+ parser_t* parser;
/* get source */
src = source_get(argv[1]);
diff --git a/src/parser.c b/src/parser.c
index 300f3de..0b2fa37 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1,4 +1,5 @@
#include "include/parser.h"
+#include "include/token.h"
parser_t* parser_init(token_t* token) {
parser_t* parser;
@@ -13,3 +14,28 @@ parser_t* parser_init(token_t* token) {
void parser_destroy(parser_t* parser) {
free(parser);
}
+
+int parser_nxt_token(parser_t* parser) {
+ if (parser->token->nxt) {
+ parser->token = parser->token->nxt;
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+int parser_match(parser_t* parser, token_type_t type) {
+ return parser->token->type == type;
+}
+
+void parser_parse_lit(parser_t* parser) {
+}
+
+void parser_parser_expr(parser_t* parser) {
+}
+
+void parser_parse_blk(parser_t* parser) {
+}
+
+void parser_parse(parser_t* parser) {
+}
diff --git a/src/token.c b/src/token.c
index 33a2545..df71d17 100644
--- a/src/token.c
+++ b/src/token.c
@@ -3,7 +3,7 @@
token_t* token_init(int type, char val) {
token_t* token;
- token = emalloc(sizeof(struct TOKEN_STRUC));
+ token = emalloc(sizeof(token_t));
token->type = type;
token->val = emalloc(2);
*token->val = val;
diff --git a/src/tree.c b/src/tree.c
index f06f8b0..7c31956 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -8,6 +8,10 @@ tree_t* tree_init(int type) {
tree->type = type;
switch (tree->type) {
+ case TREE_TYPE_BLOCK:
+ tree->data.block.val = NULL;
+ tree->data.block.nxt = NULL;
+ break;
case TREE_TYPE_LINT:
tree->data.lint.val = 0;
break;
@@ -44,21 +48,41 @@ void tree_destroy(tree_t* tree) {
free(tree);
}
-tree_targ_t* tree_targ_init(tree_t* tree) {
- tree_targ_t* targ;
+void tree_print(tree_t* tree, int nest) {
+ char* spaces;
+ int i;
- targ = emalloc(sizeof(tree_targ_t));
- targ->tree = tree;
- targ->nxt = NULL;
+ for (spaces = ecalloc(nest + 1, sizeof(char)), i = 0; i < nest; spaces[i++] = ' ');
+ spaces[i] = '\0';
- return targ;
-}
+#define NEST(TEXT) log_raw("%s" TEXT "\n", spaces);
-void tree_targ_destroy(tree_targ_t* targ) {
- if (targ->nxt) {
- tree_targ_destroy(targ->nxt);
- targ->nxt = NULL;
+ if (!tree) {
+ NEST("NULL");
+ return;
}
- free(targ);
-}
+ switch (tree->type) {
+ case TREE_TYPE_BLOCK:
+ NEST("[block]");
+ NEST(" - val:");
+ tree_print(tree->data.block.val, nest + 3);
+ NEST(" - nxt:");
+ tree_print(tree->data.block.nxt, nest + 3);
+ break;
+ case TREE_TYPE_LINT:
+ log_raw("%s[lint]: %d\n", spaces, tree->data.lint.val);
+ break;
+ case TREE_TYPE_LSTR:
+ log_raw("%s[lstr]: %s\n", spaces, tree->data.lstr.val);
+ break;
+ case TREE_TYPE_TAG:
+ case TREE_TYPE_DARG:
+ case TREE_TYPE_CARG:
+ case TREE_TYPE_DEF:
+ case TREE_TYPE_CAL:
+ NEST("??");
+ break;
+ }
+
+}
diff --git a/src/util.c b/src/util.c
index 80a452a..73eff5b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -30,8 +30,6 @@ void log_raw(const char* fmt, ...) {
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
-
- fprintf(stderr, "\n");
}
void log_war(const char* fmt, ...) {