From 82e759959cda6cee81486a1f3f5fef0713587223 Mon Sep 17 00:00:00 2001 From: c Date: Mon, 27 Nov 2023 09:06:35 -0500 Subject: Definitions with no arguments can now be parsed. Definitions with arguments can also be parser, but they cannot store values. --- src/include/tree.h | 7 +++---- src/parser.c | 26 ++++++++++++++++++++++++-- src/tree.c | 30 +++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/include/tree.h b/src/include/tree.h index 1976773..20d97be 100644 --- a/src/include/tree.h +++ b/src/include/tree.h @@ -33,8 +33,7 @@ typedef struct TREE { /* Expression. */ struct TREE_DATA_EXPR { - /* The contents of the expression. */ - struct TREE* val; + struct TREE* val; /* ??? */ } expr; /* Literal integer. */ @@ -56,7 +55,7 @@ typedef struct TREE { /* Definition arguments. */ struct TREE_DATA_DARG { - struct TREE* val; /* DEF */ + struct TREE* tag; /* TAG */ struct TREE* nxt; /* DARG */ } darg; @@ -70,7 +69,7 @@ typedef struct TREE { struct TREE_DATA_DEF { struct TREE* tag; /* TAG */ struct TREE* arg; /* DARG */ - struct TREE* val; + struct TREE* val; /* EXPR */ } def; /* Calls. */ diff --git a/src/parser.c b/src/parser.c index 040ef0d..6eb1125 100644 --- a/src/parser.c +++ b/src/parser.c @@ -71,6 +71,8 @@ tree_t* parser_parse_expr(parser_t* parser) { case TOKEN_KWD: expr->data.expr.val = parser_parse_call(parser); break; + case TOKEN_TAG: + expr->data.expr.val = parser_parse_def(parser); default: return expr; } @@ -114,12 +116,29 @@ tree_t* parser_parse_block(parser_t* parser) { return block; } +tree_t* parser_parse_tag(parser_t* parser) { + tree_t* tag; + + tag = tree_init(TREE_TYPE_TAG); + + tag->data.tag.val = parser->token->val; + parser->token->val = NULL; + tag->data.tag.nxt = (parser_nxt_token_match(parser, TOKEN_TAG) ? parser_parse_tag(parser) : NULL); + + return tag; +} + tree_t* parser_parse_darg(parser_t* parser) { tree_t* darg; darg = tree_init(TREE_TYPE_DARG); - darg->data.darg.tag + darg->data.darg.tag = parser_parse_tag(parser); + /*darg->data.darg.nxt = (parser_nxt_token_match(parser, TOKEN_LIST_DELIM) && parser_nxt_token_match(parser, TOKEN_TAG) ? parser_parse_darg(parser) : NULL);*/ + + (! parser_nxt_token_match(parser, TOKEN_LIST_DELIM)) && (darg->data.darg.nxt = NULL); + (! parser_nxt_token_match(parser, TOKEN_TAG)) && (darg->data.darg.nxt = NULL); + return darg; } @@ -129,6 +148,10 @@ tree_t* parser_parse_def(parser_t* parser) { def = tree_init(TREE_TYPE_DEF); + def->data.def.tag = parser_parse_tag(parser); + def->data.def.arg = ((parser->token->type == TOKEN_APPLY) && (parser_nxt_token_match(parser, TOKEN_TAG))) ? parser_parse_darg(parser) : NULL; + def->data.def.val = ((parser->token->type == TOKEN_SET) && parser_nxt_token(parser)) ? parser_parse_expr(parser) : NULL; + return def; } @@ -138,7 +161,6 @@ tree_t* parser_parse_carg(parser_t* parser) { carg = tree_init(TREE_TYPE_CARG); carg->data.carg.val = parser_parse_expr(parser); - carg->data.carg.nxt = (parser_nxt_token_match(parser, TOKEN_LIST_DELIM) && parser_nxt_token(parser) ? parser_parse_carg(parser) : NULL); return carg; diff --git a/src/tree.c b/src/tree.c index cea6fcd..48c3acf 100644 --- a/src/tree.c +++ b/src/tree.c @@ -69,7 +69,7 @@ void tree_destroy(tree_t* tree) { tree_destroy(tree->data.tag.nxt); break; case TREE_TYPE_DARG: - tree_destroy(tree->data.darg.val); + tree_destroy(tree->data.darg.tag); tree_destroy(tree->data.darg.nxt); break; case TREE_TYPE_CARG: @@ -77,8 +77,8 @@ void tree_destroy(tree_t* tree) { tree_destroy(tree->data.carg.nxt); break; case TREE_TYPE_DEF: - free(tree->data.def.tag); - free(tree->data.def.arg); + tree_destroy(tree->data.def.tag); + tree_destroy(tree->data.def.arg); tree_destroy(tree->data.def.val); break; case TREE_TYPE_CALL: @@ -144,11 +144,31 @@ void tree_print(tree_t* tree, int nest) { NEST("nxt:"); tree_print(tree->data.carg.nxt, nest + 1); break; + case TREE_TYPE_DEF: + NEST("[def]"); + NEST("tag:"); + tree_print(tree->data.def.tag, nest + 1); + NEST("arg:"); + tree_print(tree->data.def.arg, nest + 1); + NEST("val:"); + tree_print(tree->data.def.val, nest + 1); + break; case TREE_TYPE_TAG: + NEST("[tag]"); + NEST("val:"); + log_raw("%s %s\n", spaces, tree->data.call.target); + NEST("nxt:"); + tree_print(tree->data.tag.nxt, nest + 1); + break; case TREE_TYPE_DARG: - case TREE_TYPE_DEF: - NEST("???"); + NEST("[darg]"); + NEST("tag:"); + tree_print(tree->data.darg.tag, nest + 1); + NEST("nxt:"); + tree_print(tree->data.darg.nxt, nest + 1); break; + default: + log_err("%d", __LINE__); } tree_print_exit: free(spaces); return; -- cgit v1.2.3