From 930ca8d3b760b33123ba877514a49eca5af35a6a Mon Sep 17 00:00:00 2001 From: c+1 Date: Tue, 21 Nov 2023 16:12:58 -0500 Subject: It is possible to parse strings. --- src/include/parser.h | 16 ++++++++-------- src/main.c | 2 +- src/parser.c | 26 ++++++++++++++++++++++++-- src/tree.c | 6 ++++++ 4 files changed, 39 insertions(+), 11 deletions(-) (limited to 'src') 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. */ diff --git a/src/main.c b/src/main.c index 710cbe0..11ab70a 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } diff --git a/src/tree.c b/src/tree.c index 83f36bc..8c32809 100644 --- a/src/tree.c +++ b/src/tree.c @@ -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: -- cgit v1.2.3