diff options
author | c+1 | 2023-11-21 16:12:58 -0500 |
---|---|---|
committer | c+1 | 2023-11-21 16:12:58 -0500 |
commit | 930ca8d3b760b33123ba877514a49eca5af35a6a (patch) | |
tree | 181b03509727a10ff0b41d76393ae0fa476c36fc | |
parent | a2a5e863dc9beb4bc7a73a7361d8e208599134c9 (diff) |
It is possible to parse strings.
-rw-r--r-- | src/include/parser.h | 16 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/parser.c | 26 | ||||
-rw-r--r-- | src/tree.c | 6 |
4 files changed, 39 insertions, 11 deletions
diff --git a/src/include/parser.h b/src/include/parser.h index 59bbbb5..a349992 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -37,19 +37,19 @@ int parser_match(parser_t* parser, token_type_t type); /* 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 the tree for an integer. */ +tree_t* parser_parse_lint(parser_t* parser); -/* Return a tree for a single semicolon-separated expression.*/ +/* 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 a tree for a single curly-brace-separated expression. */ +/* Return the tree for an expression. */ tree_t* parser_parse_block(parser_t* parser); - +/* Parse. */ tree_t* parser_parse(parser_t* parser); /* Parse with the given parser. */ @@ -54,8 +54,8 @@ int main(int argc, char* argv[]) { /* Clean up parser stuff. */ token_destroy(lexer->tokenl); tree_destroy(parser->tree); - parser_destroy(parser); lexer_destroy(lexer); + parser_destroy(parser); free(src); HLKT_LOG(); diff --git a/src/parser.c b/src/parser.c index fb8a4aa..b086703 100644 --- a/src/parser.c +++ b/src/parser.c @@ -38,7 +38,7 @@ int parser_nxt_token_match(parser_t* parser, token_type_t type) { return parser_match(parser, type); } -tree_t* parser_parse_lit(parser_t* parser) { +tree_t* parser_parse_lint(parser_t* parser) { tree_t* lint; lint = tree_init(TREE_TYPE_LINT); @@ -47,13 +47,35 @@ tree_t* parser_parse_lit(parser_t* parser) { return lint; } +tree_t* parser_parse_lstr(parser_t* parser) { + tree_t* lstr; + + lstr = tree_init(TREE_TYPE_LSTR); + lstr->data.lstr.len = strlen(parser->token->val); + lstr->data.lstr.val = ecalloc(lstr->data.lstr.len + 1, sizeof(char)); + lstr->data.lstr.val[lstr->data.lstr.len] = '\0'; + strcpy(lstr->data.lstr.val, parser->token->val); + + return lstr; +} + 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); + + switch (parser->token->type) { + case TOKEN_INT: + expr->data.expr.val = parser_parse_lint(parser); + break; + case TOKEN_STR: + expr->data.expr.val = parser_parse_lstr(parser); + break; + default: + return expr; + } return expr; } @@ -123,6 +123,12 @@ void tree_print(tree_t* tree, int nest) { log_raw("%s %d\n", spaces, tree->data.lint.val); break; case TREE_TYPE_LSTR: + NEST("[lstr]"); + NEST("val:"); + log_raw("%s %s\n", spaces, tree->data.lstr.val); + NEST("len:"); + log_raw("%s %d\n", spaces, tree->data.lstr.len); + break; case TREE_TYPE_TAG: case TREE_TYPE_DARG: case TREE_TYPE_CARG: |