aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/parser.c43
2 files changed, 31 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index b034809..b6e0252 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,5 @@
exec = halk.out
sources := $(wildcard src/*.c)
-sources := $(filter-out src/parser.c, $(sources)) # exclude the incomplete parser for now.
objects = $(sources:.c=.o)
flags = -g
diff --git a/src/parser.c b/src/parser.c
index 2cc0d39..4da693b 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1,12 +1,13 @@
-#include "include/parser.h"
-#include "include/token.h"
-
-
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include "include/log.h"
+#include "include/parser.h"
+#include "include/token.h"
+
+
// initialize a parser
parser_t* parser_init(lexer_t* lexer) {
parser_t* parser = calloc(1, sizeof(struct PARSER_STRUC));
@@ -22,9 +23,7 @@ void parser_check_expect(parser_t* parser, int token_type) {
if (parser->token->type == token_type) {
parser->token = lexer_get_next_token(parser->lexer);
} else {
- fprintf(stderr, "[ERRR] [%d]\n\tUnexpected token '%s' with type '%d'.",
- parser->lexer->i, parser->token->value, parser->token->type);
-
+ log_err("Unexpected token");
exit(1);
}
}
@@ -56,6 +55,7 @@ tree_t* parser_parse_chunk(parser_t* parser) {
}
}
+// parse a list of chunks
tree_t* parser_parse_chunks(parser_t* parser) {
tree_t* subtree = tree_init(TREE_SUBTREE);
subtree->data.subtree.val = calloc(1, sizeof(struct TREE_STRUC));
@@ -89,10 +89,23 @@ 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(parser_t* parser) {
+ char* token_val = parser->token->value;
+
+ parser_check_expect(parser, TOKEN_KEYWORD); // var name or fn name
+
+ // check if function
+ if (parser->token->type == TOKEN_FN_APPLY) {
+ return parser_parse_fn_call(parser);
+ }
+
+ tree_t* var = tree_init(TREE_VAR);
+ var->data.var.name = token_val;
+ return var;
+};
tree_t* parser_parse_var_def(parser_t* parser) {
- int var_is_const = 0;
+ int var_is_const;
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
@@ -101,13 +114,14 @@ tree_t* parser_parse_var_def(parser_t* parser) {
var_is_const = 1;
parser_check_expect(parser, TOKEN_DEFINE_CONST);
} else {
+ var_is_const = 0;
parser_check_expect(parser, TOKEN_DEFINE_MUT);
}
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* var_def = tree_init(TREE_VAR_DEF); // create the var, as a subtree
- var_def->data.var_def.name = var_name;
+ 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;
@@ -122,7 +136,12 @@ tree_t* parser_parse_keyword(parser_t* parser) {
} else if (strcmp(parser->token->value, "fn")) {
return parser_parse_fn_def(parser);
} else {
- return parser_parse_var(parser);
+ return parser_parse_var(parser); // assume attempting to call
+ // variable/function;
+ // TODO: differentiate b/w the
+ // two on call; may be a syntax
+ // change :P just to make the
+ // interpreter easier
}
}