From bf3767fa7f0f1687d930d0d137e76a0206545fd6 Mon Sep 17 00:00:00 2001 From: c Date: Sat, 25 Nov 2023 10:37:37 -0500 Subject: Functions with single arguments are possible. --- src/include/parser.h | 6 ++++++ src/include/tree.h | 6 +++--- src/parser.c | 37 ++++++++++++++++++++++++++++++++----- src/tree.c | 28 ++++++++++++++++++++-------- 4 files changed, 61 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/include/parser.h b/src/include/parser.h index a349992..acf6438 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -49,6 +49,12 @@ tree_t* parser_parse_expr(parser_t* parser); /* Return the tree for an expression. */ tree_t* parser_parse_block(parser_t* parser); +/* Return the tree for a call's arguments. */ +tree_t* parser_parse_carg(parser_t* parser); + +/* Return the tree for a call. */ +tree_t* parser_parse_call(parser_t* parser); + /* Parse. */ tree_t* parser_parse(parser_t* parser); diff --git a/src/include/tree.h b/src/include/tree.h index 3d3bcbd..8954ef8 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_CAL, + TREE_TYPE_CALL, } tree_type_t; /* The Abstract Syntax Tree (AST) structure. */ @@ -74,10 +74,10 @@ typedef struct TREE { } def; /* Calls. */ - struct TREE_DATA_CAL { + struct TREE_DATA_CALL { char* target; struct TREE* arg; /* CARG */ - } cal; + } call; } data; } tree_t; diff --git a/src/parser.c b/src/parser.c index b086703..c378d75 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,5 +1,6 @@ #include "include/parser.h" #include "include/token.h" +#include "include/tree.h" parser_t* parser_init(token_t* token) { parser_t* parser; @@ -51,10 +52,11 @@ 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); + /* Swap pointers to allow for future token destruction. */ + lstr->data.lstr.val = parser->token->val; + parser->token->val = NULL; return lstr; } @@ -64,8 +66,6 @@ tree_t* parser_parse_expr(parser_t* parser) { expr = tree_init(TREE_TYPE_EXPR); - /* For now this is the only type of expression. */ - switch (parser->token->type) { case TOKEN_INT: expr->data.expr.val = parser_parse_lint(parser); @@ -73,6 +73,9 @@ tree_t* parser_parse_expr(parser_t* parser) { case TOKEN_STR: expr->data.expr.val = parser_parse_lstr(parser); break; + case TOKEN_KWD: + expr->data.expr.val = parser_parse_call(parser); + break; default: return expr; } @@ -116,6 +119,30 @@ tree_t* parser_parse_block(parser_t* parser) { return block; } +tree_t* parser_parse_carg(parser_t* parser) { + tree_t* carg; + + carg = tree_init(TREE_TYPE_CARG); + + carg->data.carg.val = parser_parse_expr(parser); + carg->data.carg.nxt = NULL; + + return carg; +} + +tree_t* parser_parse_call(parser_t* parser) { + tree_t* call; + + call = tree_init(TREE_TYPE_CALL); + + call->data.call.target = parser->token->val; + parser->token->val = NULL; + parser_nxt_token_match(parser, TOKEN_APPLY) || (call->data.call.arg = NULL); + call->data.call.arg = (parser_nxt_token(parser) ? parser_parse_carg(parser) : NULL); + + return call; +} + tree_t* parser_parse(parser_t* parser) { return parser_parse_block(parser); } diff --git a/src/tree.c b/src/tree.c index 8c32809..4720ae6 100644 --- a/src/tree.c +++ b/src/tree.c @@ -38,9 +38,9 @@ tree_t* tree_init(int type) { tree->data.def.arg = NULL; tree->data.def.val = NULL; break; - case TREE_TYPE_CAL: - tree->data.cal.target = NULL; - tree->data.cal.arg = NULL; + case TREE_TYPE_CALL: + tree->data.call.target = NULL; + tree->data.call.arg = NULL; break; } @@ -80,9 +80,9 @@ void tree_destroy(tree_t* tree) { free(tree->data.def.arg); tree_destroy(tree->data.def.val); break; - case TREE_TYPE_CAL: - free(tree->data.cal.target); - tree_destroy(tree->data.cal.arg); + case TREE_TYPE_CALL: + free(tree->data.call.target); + tree_destroy(tree->data.call.arg); break; } @@ -129,11 +129,23 @@ void tree_print(tree_t* tree, int nest) { NEST("len:"); log_raw("%s %d\n", spaces, tree->data.lstr.len); break; + case TREE_TYPE_CALL: + NEST("[call]"); + NEST("target:"); + log_raw("%s %s\n", spaces, tree->data.call.target); + NEST("arg:") + tree_print(tree->data.call.arg, nest + 1); + break; + case TREE_TYPE_CARG: + NEST("[carg]"); + NEST("val:"); + tree_print(tree->data.carg.val, nest + 1); + NEST("nxt:"); + tree_print(tree->data.carg.nxt, nest + 1); + break; case TREE_TYPE_TAG: case TREE_TYPE_DARG: - case TREE_TYPE_CARG: case TREE_TYPE_DEF: - case TREE_TYPE_CAL: NEST("???"); break; } -- cgit v1.2.3