diff options
author | c | 2024-01-28 20:15:35 -0500 |
---|---|---|
committer | c | 2024-01-28 20:15:35 -0500 |
commit | 422606d8b3cc6fba74f97af46f0378fc274d60ad (patch) | |
tree | 34314628de5e3132cffc661901bd1e4c9b2aa0de /src | |
parent | 87e0d4b3d23a7eb38f0e196eac333c318fef27ea (diff) |
Fixed things.
- Fixed `parser_nxt_token()` assuming the existance of a parser's token.
- Fixed block parsing.
Diffstat (limited to 'src')
-rw-r--r-- | src/include/lexer.h | 2 | ||||
-rw-r--r-- | src/include/parser.h | 17 | ||||
-rw-r--r-- | src/include/pp.h | 2 | ||||
-rw-r--r-- | src/include/tree.h | 2 | ||||
-rw-r--r-- | src/parser.c | 86 |
5 files changed, 67 insertions, 42 deletions
diff --git a/src/include/lexer.h b/src/include/lexer.h index 02275cb..5235f3b 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -23,7 +23,7 @@ typedef struct LEXER_STRUC { /* Integer. */ LEXER_STATE_INT, /* Keyword. */ - LEXER_STATE_KWD, + LEXER_STATE_KWD } state; /* The linked list of tokens generated. */ diff --git a/src/include/parser.h b/src/include/parser.h index eae09d4..a442d60 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -14,7 +14,7 @@ typedef enum PARSER_STATE { PARSER_STATE_DARG, PARSER_STATE_CARG, PARSER_STATE_DEF, - PARSER_STATE_CALL, + PARSER_STATE_CALL } parser_state_t; typedef struct PARSER { @@ -41,6 +41,15 @@ void parser_destroy(parser_t* parser); /* Step the parser forward by 1 token. */ int parser_nxt_token(parser_t* parser); +/* Get tree for first (implied) block. */ +tree_t* parser_parse_init(parser_t* parser); + +/* Get tree for a block. */ +tree_t* parser_parse_block(parser_t* parser); + +/* Get tree for an expression.*/ +tree_t* parser_parse_expr(parser_t* parser); + /* Check whether the current token matches the given type. - If it doesn't, return 0 and throw error. @@ -56,12 +65,6 @@ tree_t* parser_parse_lint(parser_t* parser); /* Return the tree for a string. */ tree_t* parser_parse_lstr(parser_t* parser); -/* Return the tree for an expression.*/ -tree_t* parser_parse_expr(parser_t* parser); - -/* Return the tree for an block. */ -tree_t* parser_parse_block(parser_t* parser); - /* Return the tree for a definition's arguments. */ tree_t* parser_parse_darg(parser_t* parser); diff --git a/src/include/pp.h b/src/include/pp.h index 4186ef3..bbb901c 100644 --- a/src/include/pp.h +++ b/src/include/pp.h @@ -27,7 +27,7 @@ typedef struct PP_STRUC { PP_STATE_REG, /* Regular. */ PP_STATE_STR, /* String. */ PP_STATE_COM, /* Comment. */ - PP_STATE_ESC, /* Escaped character in string. */ + PP_STATE_ESC /* Escaped character in string. */ /* PP_STATE_MCO, */ /* Macro. */ } state; } pp_t; diff --git a/src/include/tree.h b/src/include/tree.h index 31b8f3f..2a62f26 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -12,7 +12,7 @@ typedef enum TREE_TYPE { TREE_TYPE_DARG, TREE_TYPE_CARG, TREE_TYPE_DEF, - TREE_TYPE_CALL, + TREE_TYPE_CALL } tree_type_t; /* The Abstract Syntax Tree (AST) structure. */ diff --git a/src/parser.c b/src/parser.c index 2cf9b2c..f94fe53 100644 --- a/src/parser.c +++ b/src/parser.c @@ -18,6 +18,7 @@ void parser_destroy(parser_t* parser) { /* TODO: What if the program begins with a ";"? */ int parser_nxt_token(parser_t* parser) { /* Preserve original token list, to be cleaned up by lexer. */ + if (!parser->token) { return 0; } parser->token = parser->token->nxt; if (parser->token && parser->token->type == TOKEN_TYPE_EXPR_END) { return parser_nxt_token(parser); @@ -25,6 +26,59 @@ int parser_nxt_token(parser_t* parser) { return parser->token ? 1 : 0; } +tree_t* parser_parse_init(parser_t* parser) { + /* There is nothing to do. */ + if (!parser->token || parser->token->type == TOKEN_TYPE_RBLOCK) { + return NULL; + } + tree_t* block; + block = tree_init(TREE_TYPE_BLOCK); + + block->data.block.val = parser_parse_expr(parser); + block->data.block.nxt = parser_parse_block(parser); + + return block; +} + +tree_t* parser_parse_block(parser_t* parser) { + /* There is nothing to do. */ + if (!parser->token || parser->token->type == TOKEN_TYPE_RBLOCK) { + parser_nxt_token(parser); /* Skip over closing bracket. */ + return NULL; + } + tree_t* block; + block = tree_init(TREE_TYPE_BLOCK); + + block->data.block.val = parser_parse_expr(parser); + block->data.block.nxt = parser_parse_block(parser); + + parser_nxt_token(parser); /* Skip over semicolon. */ + + return block; +} + +tree_t* parser_parse_expr(parser_t* parser) { + tree_t* expr = NULL; + + switch (parser->token->type) { + case TOKEN_TYPE_INT: + expr = parser_parse_lint(parser); + break; + case TOKEN_TYPE_STR: + expr = parser_parse_lstr(parser); + break; + case TOKEN_TYPE_LBLOCK: + parser_nxt_token(parser); /* Skip over opening curly bracket. */ + expr = parser_parse_block(parser); + break; + default: + log_war("%s: Unknown token type: %d", __func__, parser->token->type); + parser_nxt_token(parser); + } + + return expr; +} + /* I don't know if these need to exist. Guess I'll wait and seeā¦ */ int parser_match(parser_t* parser, token_type_t type) { return parser->token->type == type; @@ -59,38 +113,6 @@ tree_t* parser_parse_lstr(parser_t* parser) { return lstr; } -tree_t* parser_parse_expr(parser_t* parser) { - tree_t* expr = NULL; - - switch (parser->token->type) { - case TOKEN_TYPE_INT: - expr = parser_parse_lint(parser); - break; - case TOKEN_TYPE_STR: - expr = parser_parse_lstr(parser); - break; - default: - log_war("%s: Unknown token type: %d", __func__, parser->token->type); - parser_nxt_token(parser); - } - - return expr; -} - -tree_t* parser_parse_block(parser_t* parser) { - /* There is nothing to do. */ - if (!parser->token || parser->token->type == TOKEN_TYPE_RBLOCK) { - return NULL; - } - tree_t* block; - block = tree_init(TREE_TYPE_BLOCK); - - block->data.block.val = parser_parse_expr(parser); - block->data.block.nxt = parser_parse_block(parser); - - return block; -} - tree_t* parser_parse_tag(parser_t* parser) { if (parser->token->type != TOKEN_TYPE_TAG) { return NULL; } |