aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/parser.h33
-rw-r--r--src/include/tree.h7
-rw-r--r--src/main.c9
-rw-r--r--src/parser.c72
-rw-r--r--src/tree.c8
5 files changed, 111 insertions, 18 deletions
diff --git a/src/include/parser.h b/src/include/parser.h
index b39f481..59bbbb5 100644
--- a/src/include/parser.h
+++ b/src/include/parser.h
@@ -11,6 +11,8 @@ typedef struct PARSER {
/* The AST being produced. */
tree_t* tree;
+
+ /* Pointer to the part of the tree the parser is currently working on. */
} parser_t;
/* Creates a new parser. */
@@ -26,26 +28,31 @@ void parser_destroy(parser_t* parser);
/* Step the parser forward by 1 token. */
int parser_nxt_token(parser_t* parser);
-/* Check whether the current token matches the given type. */
+/*
+ Check whether the current token matches the given type.
+ - If it doesn't, return 0 and throw error.
+*/
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.
+/* Steps the parser forward by one token, then check whether the new token matches the given type. */
+int parser_nxt_token_match(parser_t* parser, token_type_t type);
+/*
+ Parse a single literal value.
+ - Only does integers for now.
*/
+tree_t* parser_parse_lit(parser_t* parser);
+
+/* Return a tree for a single semicolon-separated expression.*/
+tree_t* parser_parse_expr(parser_t* parser);
-/* Parse a single literal value. */
-void parser_parse_lit(parser_t* parser);
+/* Return a tree for a single curly-brace-separated expression. */
+tree_t* parser_parse_block(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);
+tree_t* parser_parse(parser_t* parser);
-/* Parse the given tokens. */
-void parser_parse(parser_t* parser);
+/* Parse with the given parser. */
+void parser_run(parser_t* parser);
#endif
diff --git a/src/include/tree.h b/src/include/tree.h
index 3dfb5be..ba1f5a5 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -5,6 +5,7 @@
typedef enum TREE_TYPE {
TREE_TYPE_BLOCK,
+ TREE_TYPE_EXPR,
TREE_TYPE_LINT,
TREE_TYPE_LSTR,
TREE_TYPE_TAG,
@@ -30,6 +31,12 @@ typedef struct TREE {
struct TREE* nxt;
} block;
+ /* Expression. */
+ struct TREE_DATA_EXPR {
+ /* The contents of the expression. */
+ struct TREE* val;
+ } expr;
+
/* Literal integer. */
struct TREE_DATA_LINT {
int val;
diff --git a/src/main.c b/src/main.c
index 8a60315..773134e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,6 +46,15 @@ int main(int argc, char* argv[]) {
lexer_run(lexer);
log_dbg("lexer ran");
+ /* Create the parser from the lexer's tokens. */
+ parser = parser_init(lexer->tokenl);
+ parser_run(parser);
+ tree_print(parser->tree, 0);
+
+ /* Clean up parser stuff. */
+ tree_destroy(parser->tree);
+ parser_destroy(parser);
+
/* clean up lexer stuff */
if (lexer->tokenl) { token_destroy(lexer->tokenl); } /* temp until parser eats tokens */
lexer_destroy(lexer);
diff --git a/src/parser.c b/src/parser.c
index 0b2fa37..fb8a4aa 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -25,17 +25,79 @@ int parser_nxt_token(parser_t* parser) {
}
int parser_match(parser_t* parser, token_type_t type) {
- return parser->token->type == type;
+ if (parser->token->type == type) {
+ return 1;
+ } else {
+ log_err("Unexpected token.");
+ return 0;
+ }
}
-void parser_parse_lit(parser_t* parser) {
+int parser_nxt_token_match(parser_t* parser, token_type_t type) {
+ parser_nxt_token(parser);
+ return parser_match(parser, type);
+}
+
+tree_t* parser_parse_lit(parser_t* parser) {
+ tree_t* lint;
+
+ lint = tree_init(TREE_TYPE_LINT);
+ lint->data.lint.val = atoi(parser->token->val);
+
+ return lint;
+}
+
+tree_t* parser_parse_expr(parser_t* parser) {
+ tree_t* expr;
+
+ expr = tree_init(TREE_TYPE_EXPR);
+
+ /* For now this is the only type of expression. */
+ expr->data.expr.val = parser_parse_lit(parser);
+
+ return expr;
}
-void parser_parser_expr(parser_t* parser) {
+tree_t* parser_parse_block(parser_t* parser) {
+ tree_t* block;
+
+ block = tree_init(TREE_TYPE_BLOCK);
+
+ if (parser->token->type == TOKEN_EXPR_END) {
+ if (parser_nxt_token(parser)) {
+ block->data.block.val = parser_parse_expr(parser);
+ } else {
+ block->data.block.val = NULL;
+ }
+
+ if (parser_nxt_token(parser)) {
+ block->data.block.nxt = parser_parse_block(parser);
+ } else {
+ block->data.block.nxt = NULL;
+ }
+
+ return block;
+ }
+
+ if (parser->token) {
+ block->data.block.val = parser_parse_expr(parser);
+ } else {
+ block->data.block.val = NULL;
+ }
+
+ if (parser_nxt_token(parser)) {
+ block->data.block.nxt = parser_parse_block(parser);
+ } else {
+ block->data.block.nxt = NULL;
+ }
+
+ return block;
}
-void parser_parse_blk(parser_t* parser) {
+tree_t* parser_parse(parser_t* parser) {
+ return parser_parse_block(parser);
}
-void parser_parse(parser_t* parser) {
+void parser_run(parser_t* parser) {
+ parser->tree = parser_parse(parser);
}
diff --git a/src/tree.c b/src/tree.c
index 7c31956..3dfa761 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -12,6 +12,9 @@ tree_t* tree_init(int type) {
tree->data.block.val = NULL;
tree->data.block.nxt = NULL;
break;
+ case TREE_TYPE_EXPR:
+ tree->data.expr.val = NULL;
+ break;
case TREE_TYPE_LINT:
tree->data.lint.val = 0;
break;
@@ -70,6 +73,11 @@ void tree_print(tree_t* tree, int nest) {
NEST(" - nxt:");
tree_print(tree->data.block.nxt, nest + 3);
break;
+ case TREE_TYPE_EXPR:
+ NEST("[expression]");
+ NEST(" - val:");
+ tree_print(tree->data.expr.val, nest + 3);
+ break;
case TREE_TYPE_LINT:
log_raw("%s[lint]: %d\n", spaces, tree->data.lint.val);
break;