aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/parser.h9
-rw-r--r--src/lexer.c2
-rw-r--r--src/parser.c74
3 files changed, 55 insertions, 30 deletions
diff --git a/src/include/parser.h b/src/include/parser.h
index 5ff8366..c77fd29 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -20,7 +20,6 @@ void parser_check_expect(parser_t* parser, int token_type);
// creates the abstract syntax tree
tree_t* parser_parse(parser_t* parser);
-
tree_t* parser_parse_token_id(parser_t* parser);
// parse a single chunk
@@ -29,18 +28,16 @@ tree_t* parser_parse_chunk(parser_t* parser);
// parse all in list of chunks
tree_t* parser_parse_chunks(parser_t* parser);
+// leaves of the tree
tree_t* parser_parse_expr(parser_t* parser);
-
tree_t* parser_parse_fac(parser_t* parser);
-
tree_t* parser_parse_term(parser_t* parser);
-
tree_t* parser_parse_fn_call(parser_t* parser);
-
+tree_t* parser_parse_fn_def(parser_t* parser);
tree_t* parser_parse_var(parser_t* parser);
tree_t* parser_parse_var_def(parser_t* parser);
-
tree_t* parser_parse_str(parser_t* parser);
+tree_t* parser_parse_keyword(parser_t* parser);
#endif
diff --git a/src/lexer.c b/src/lexer.c
index 557fd1f..b61f750 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -119,7 +119,7 @@ token_t* lexer_get_next_token(lexer_t* lexer) {
lexer_get_c_as_string(lexer)
)
); break;
- case '\0': return token_init(TOKEN_END, lexer_get_c_as_string(lexer)); break;
+ case '\0': return token_init(TOKEN_EOF, lexer_get_c_as_string(lexer)); break;
default:
fputs("Unrecognized token.", stderr);
exit(1);
diff --git a/src/parser.c b/src/parser.c
index 0a437a6..a7bf1b5 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1,4 +1,5 @@
#include "include/parser.h"
+#include "include/token.h"
#include <stdlib.h>
@@ -45,50 +46,77 @@ tree_t* parser_parse_token_id(parser_t* parser) {
tree_t* parser_parse_chunk(parser_t* parser) {
switch (parser->token->type) {
case TOKEN_KEYWORD: {
- return parser_parse_token_id(parser);
+ return parser_parse_keyword(parser);
}
}
}
-// parse all in list of chunks
tree_t* parser_parse_chunks(parser_t* parser) {
- // initialize the subtree to return
tree_t* subtree = tree_init(TREE_SUBTREE);
+ subtree->data.subtree.val = calloc(1, sizeof(struct TREE_STRUC));
- // initialize the subtree_val with unit size
- subtree->data.subtree.size = calloc(1, sizeof(struct TREE_STRUC));
-
- tree_t* tree_chunk = parser_parse_chunk(parser);
-
- subtree->data.subtree.val[0] = tree_chunk;
+ tree_t* chunk = parser_parse_chunk(parser);
+ subtree->data.subtree.val[0] = chunk;
while (parser->token->type == TOKEN_END) {
- // expect semicolon
parser_check_expect(parser, TOKEN_END);
- // make room for new subtree
- subtree->data.subtree.size ++;
+ tree_t* chunk = parser_parse_chunk(parser);
+ subtree->data.subtree.size += 1;
subtree->data.subtree.val = realloc(
- subtree->data.subtree.val,
- subtree->data.subtree.size * sizeof(struct TREE_STRUC)
+ subtree->data.subtree.val,
+ subtree->data.subtree.size * sizeof(struct TREE_STRUC)
);
-
- // add to end of list
- subtree->data.subtree.val[subtree->data.subtree.size - 1] = tree_chunk;
+ subtree->data.subtree.val[subtree->data.subtree.size - 1] = chunk;
+
}
return subtree;
}
-tree_t* parser_parse_expr(parser_t* parser);
+tree_t* parser_parse_expr(parser_t* parser) {};
+
+tree_t* parser_parse_fac(parser_t* parser) {};
+
+tree_t* parser_parse_term(parser_t* parser) {};
+
+tree_t* parser_parse_fn_call(parser_t* parser) {};
+
+tree_t* parser_parse_fn_def(parser_t* parser) {};
+
+tree_t* parser_parse_var(parser_t* parser) {};
-tree_t* parser_parse_fac(parser_t* parser);
+tree_t* parser_parse_var_def(parser_t* parser) {
+ int var_is_const = 0;
+ parser_check_expect(parser, TOKEN_KEYWORD); // let
+ char* var_name = parser->token->value; // set variable name
+ parser_check_expect(parser, TOKEN_KEYWORD); // expect variable name & advance
+ if (parser->token->type == TOKEN_DEFINE_CONST) { // check either constant or mutable variable
+ var_is_const = 1;
+ parser_check_expect(parser, TOKEN_DEFINE_CONST);
+ } else {
+ parser_check_expect(parser, TOKEN_DEFINE_MUT);
+ }
-tree_t* parser_parse_term(parser_t* parser);
+ tree_t* var_val = parser_parse_expr(parser); // set the value
+ tree_t* var_def = tree_init(TREE_VAR_DEF); // create the var
-tree_t* parser_parse_fn_call(parser_t* parser);
+ var_def->data.var_def.name = var_name;
+ var_def->data.var_def.val = var_val;
+ var_def->data.var_def.is_const = &var_is_const;
-tree_t* parser_parse_var(parser_t* parser);
+ return var_def;
+};
-tree_t* parser_parse_str(parser_t* parser);
+tree_t* parser_parse_str(parser_t* parser) {};
+
+tree_t* parser_parse_keyword(parser_t* parser) {
+ if (strcmp(parser->token->value, "let")) {
+ return parser_parse_var_def(parser);
+ } else if (strcmp(parser->token->value, "fn")) {
+ return parser_parse_fn_def(parser);
+ } else {
+ return parser_parse_var(parser);
+ }
+}