aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authors-over-42023-08-29 10:17:27 -0400
committers-over-42023-08-29 10:17:27 -0400
commit4176dea8503cc8eebfa24b14aaefc3a8dc643230 (patch)
treed2fd320fa4c83fcd397995ff009ae565b6a5e933 /src
parent497a3b690c3828775ad1d141f53484a748d5b61a (diff)
g
Diffstat (limited to 'src')
-rw-r--r--src/include/parser.h7
-rw-r--r--src/include/tree.h6
-rw-r--r--src/lexer.c16
-rw-r--r--src/parser.c43
-rw-r--r--src/token.c1
-rw-r--r--src/tree.c25
6 files changed, 51 insertions, 47 deletions
diff --git a/src/include/parser.h b/src/include/parser.h
index 0ba2038..deb6c7c 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -4,6 +4,8 @@
#include "lexer.h"
#include "tree.h"
+#include <stdarg.h>
+
typedef struct PARSER_STRUC {
lexer_t* lexer; // lexer used by the parser
token_t* token; // current token
@@ -12,9 +14,8 @@ typedef struct PARSER_STRUC {
parser_t* parser_init(lexer_t* lexer);
void parser_destroy(parser_t* parser);
-// expect token, or die
-void parser_token_expect(parser_t* parser, int token);
-void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*));
+// expect token(s), or die
+void parser_token_expect(parser_t* parser, int token, ...);
// do the parse
tree_t* parser_parse(parser_t* parser);
diff --git a/src/include/tree.h b/src/include/tree.h
index fde66ea..a2b71da 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -21,15 +21,17 @@ typedef struct TREE_STRUC {
struct { // === DEFINITIONS ===
char* type; // the definition type
char** tags; // the definition tags
+ size_t tags_size; // the number of tags
char* name; // the definition name
+ struct TREE_STRUC** args; // the arguments the definition will accept
+ size_t args_size; // the number of arguments
struct TREE_STRUC* value; // value of definition
- // :D lines line up :D
} def;
struct { // === CALLS ===
char* target; // name of definition being called
struct TREE_STRUC** args; // arguments passed to definition
- size_t args_size; // size of arguments
+ size_t args_size; // the number of arguments
} call;
// === TYPES ===
diff --git a/src/lexer.c b/src/lexer.c
index a5ed31b..09831bb 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -104,9 +104,9 @@ token_t* lexer_next_token(lexer_t* lexer, int token_type) {
return token;
}
-// get the current character as a string
+/* get the current character as a string */
char* lexer_get_c_as_string(lexer_t* lexer) {
- char* str; // the string to return
+ char* str; /* the string to return */
str = calloc(2, sizeof(char));
str[0] = lexer->c;
@@ -115,11 +115,13 @@ char* lexer_get_c_as_string(lexer_t* lexer) {
return str;
}
-// fskip: skip first char?
-// lskip: skip last char?
+/*
+ fskip: skip first char?
+ lskip: skip last char?
+*/
token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lskip, int type) {
- size_t len; // length of collected token so far
- char* token; // collected token so far
+ size_t len; /* length of collected token so far */
+ char* token; /* collected token so far */
len = 0;
token = calloc(len, sizeof(char));
@@ -144,7 +146,7 @@ token_t* lexer_collect(lexer_t* lexer, int (*end_char)(char), int fskip, int lsk
if (lskip) { lexer_next(lexer); }
- token[len] = '\0'; // null terminate
+ token[len] = '\0'; /* null terminate */
return token_init(type, token);
}
diff --git a/src/parser.c b/src/parser.c
index 4233138..c32202e 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -20,24 +20,19 @@ parser_t* parser_init(lexer_t* lexer) {
void parser_destroy(parser_t* parser) { free(parser); }
-void parser_token_expect(parser_t* parser, int token) {
- token == parser->token->type?
- parser->token = lexer_get_next_token(parser->lexer):
- die(
- "unexpected token\n\ttype: [%s]\n\tvalue: [%s]",
- token_get_type(parser->token->type),
- parser->token->value
- );
-}
-
-void parser_token_expectf(parser_t* parser, int (*expected_token)(token_t*)) {
- expected_token(parser->token)?
- parser->token = lexer_get_next_token(parser->lexer):
- die(
- "unexpected token\n\ttype: [%s]\n\tvalue: [%s]",
- token_get_type(parser->token->type),
- parser->token->value
- );
+void parser_token_expect(parser_t* parser, int tokens, ...) {
+ va_list ap;
+ va_start(ap, tokens);
+ for (int i = 0; i < tokens; ++i) {
+ va_arg(ap, int) == parser->token->type?
+ parser->token = lexer_get_next_token(parser->lexer):
+ die(
+ "unexpected token\n\ttype: [%s]\n\tvalue: [%s]",
+ token_get_type(parser->token->type),
+ parser->token->value
+ );
+ }
+ va_end(ap);
}
tree_t* parser_parse(parser_t* parser) { return parser_parse_exprs(parser); }
@@ -73,7 +68,7 @@ tree_t* parser_parse_exprs(parser_t* parser) {
parser_token_expect(parser, TOKEN_STMNT_END);
statement = parser_parse_expr(parser);
- comp->data.comp.size += 1;
+ comp->data.comp.size ++;
comp->data.comp.value = realloc(
comp->data.comp.value,
comp->data.comp.size * sizeof(struct TREE_STRUC)
@@ -97,12 +92,12 @@ tree_t* parser_parse_def(parser_t* parser) {
def->type = TREE_DEF;
- def->data.def.name = parser->token->value;
-
-// def->data.def.
-
while (parser->token->type == TOKEN_DEF_TAG) {
-
+ def->data.def.tags_size ++;
+ def->data.def.tags = realloc(
+ def->data.def.args,
+ def->data.def.args_size + sizeof(parser->token->value)
+ );
}
}
diff --git a/src/token.c b/src/token.c
index 41e7e0c..355a6dd 100644
--- a/src/token.c
+++ b/src/token.c
@@ -2,7 +2,6 @@
#include "include/token.h"
-// token constructor
token_t* token_init(int type, char* val) {
token_t* token = calloc(1, sizeof(struct TOKEN_STRUC));
token->type = type;
diff --git a/src/tree.c b/src/tree.c
index 0ed0697..235d0e6 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -8,23 +8,28 @@ tree_t* tree_init(int type) {
switch (type) {
case TREE_COMP:
- tree->data.comp.value = (void*) 0;
+ tree->data.comp.value = NULL;
tree->data.comp.size = 0;
break;
case TREE_DEF:
- tree->data.def.name = (void*) 0;
- tree->data.def.value = (void*) 0;
+ tree->data.def.type = NULL;
+ tree->data.def.tags = NULL;
+ tree->data.def.tags_size = 0;
+ tree->data.def.name = NULL;
+ tree->data.def.args = NULL;
+ tree->data.def.tags_size = 0;
+ tree->data.def.value = NULL;
break;
case TREE_CALL:
- tree->data.call.args = (void*) 0;
+ tree->data.call.target = NULL;
+ tree->data.call.args = NULL;
tree->data.call.args_size = 0;
- tree->data.call.target = (void*) 0;
break;
- case TREE_TYPE_STR:
- tree->data.type_str.value = (void*) 0;
- break;
- case TREE_TYPE_INT:
- tree->data.type_int.value = (void*) 0;
+ case TREE_TYPE_STR:
+ tree->data.type_str.value = NULL;
+ break;
+ case TREE_TYPE_INT:
+ tree->data.type_int.value = 0;
break;
}