@@ -725,7 +725,7 @@ The following import files are currently bundled.
725725 - [`char/unicode_derived_core.peg`](import/char/unicode_derived_core.peg) :
726726 This provides various rules to match a Unicode character belonging to a specific [derived core property](https://www.unicode.org/reports/tr44/#DerivedCoreProperties.txt).
727727- **Utility Codes**
728- - [`code/pcc_ast.peg`](import/code/pcc_ast.peg) :
728+ - [`code/pcc_ast.v3. peg`](import/code/pcc_ast.v3 .peg) :
729729 This provides codes to make it easier to build an AST (abstract syntax tree).
730730
731731For details, see [here](import).
@@ -1049,50 +1049,35 @@ primary <- < [0-9]+ > { $$ = calc_ast_node__create_0(); $$->custom
10491049_ <- [ \t]*
10501050EOL <- '\n' / '\r\n' / '\r' / ';'
10511051
1052- %import "code/pcc_ast.peg" # <-- provides AST build functions
1052+ %import "code/pcc_ast.v3. peg" # <-- provides AST build functions
10531053
10541054%%
1055- void calc_ast_node_custom_data__initialize(calc_ast_node_custom_data_t *obj) { /* <-- must be implemented when enabling node custom data */
1055+ void calc_ast_node_custom_data__initialize(calc_ast_manager_t *mgr, calc_ast_node_custom_data_t *obj) {
10561056 obj->text = NULL;
1057- }
1057+ } /* <-- must be implemented when enabling node custom data */
10581058
1059- void calc_ast_node_custom_data__finalize(calc_ast_node_custom_data_t *obj) { /* <-- must be implemented when enabling node custom data */
1059+ void calc_ast_node_custom_data__finalize(calc_ast_manager_t *mgr, calc_ast_node_custom_data_t *obj) {
10601060 free(obj->text);
1061- }
1061+ } /* <-- must be implemented when enabling node custom data */
10621062
10631063static void dump_ast(const calc_ast_node_t *obj, int depth) {
10641064 if (obj) {
1065- switch (obj->type) {
1066- case CALC_AST_NODE_TYPE_NULLARY:
1067- printf("%*s%s: \"%s\"\n", 2 * depth, "", "nullary", obj->custom.text);
1068- break;
1069- case CALC_AST_NODE_TYPE_UNARY:
1070- printf("%*s%s: \"%s\"\n", 2 * depth, "", "unary", obj->custom.text);
1071- dump_ast(obj->data.unary.node, depth + 1);
1072- break;
1073- case CALC_AST_NODE_TYPE_BINARY:
1074- printf("%*s%s: \"%s\"\n", 2 * depth, "", "binary", obj->custom.text);
1075- dump_ast(obj->data.binary.node[0], depth + 1);
1076- dump_ast(obj->data.binary.node[1], depth + 1);
1077- break;
1078- case CALC_AST_NODE_TYPE_TERNARY:
1079- printf("%*s%s: \"%s\"\n", 2 * depth, "", "ternary", obj->custom.text);
1080- dump_ast(obj->data.ternary.node[0], depth + 1);
1081- dump_ast(obj->data.ternary.node[1], depth + 1);
1082- dump_ast(obj->data.ternary.node[2], depth + 1);
1083- break;
1084- case CALC_AST_NODE_TYPE_VARIADIC:
1085- printf("%*s%s: \"%s\"\n", 2 * depth, "", "variadic", obj->custom.text);
1065+ const size_t n = calc_ast_node__get_child_count(obj);
1066+ const calc_ast_node_t *const *const p = calc_ast_node__get_child_array(obj);
1067+ const calc_ast_node_custom_data_t *const d = &(obj->custom);
1068+ const int b = calc_ast_node__is_variadic(obj);
1069+ if (b || n <= 3) {
1070+ static const char *const arity_name[] = { "nullary", "unary", "binary", "ternary" };
1071+ printf("%*s%s: \"%s\"\n", 2 * depth, "", b ? "variadic" : arity_name[n], d->text);
10861072 {
10871073 size_t i;
1088- for (i = 0; i < obj->data.variadic.len ; i++) {
1089- dump_ast(obj->data.variadic.node [i], depth + 1);
1074+ for (i = 0; i < n ; i++) {
1075+ dump_ast(p [i], depth + 1);
10901076 }
10911077 }
1092- break;
1093- default:
1094- printf("%*s%s: \"%s\"\n", 2 * depth, "", "(unknown)", obj->custom.text);
1095- break;
1078+ }
1079+ else {
1080+ printf("%*s%s: \"%s\"\n", 2 * depth, "", "(unknown)", d->text);
10961081 }
10971082 }
10981083 else {
@@ -1108,7 +1093,7 @@ int main(int argc, char **argv) {
11081093 calc_ast_node_t *ast = NULL;
11091094 while (calc_parse(ctx, &ast)) {
11101095 dump_ast(ast, 0);
1111- calc_ast_node__destroy(ast);
1096+ calc_ast_node__destroy(&mgr, ast);
11121097 }
11131098 calc_destroy(ctx);
11141099 }
@@ -1117,9 +1102,9 @@ int main(int argc, char **argv) {
11171102}
11181103```
11191104
1120- The key point is the line `%import "code/pcc_ast.peg"`.
1121- The import file [`code/pcc_ast.peg`](import/code) makes it easier to build ASTs.
1122- For more details, see [here](import/code/README .md).
1105+ The key point is the line `%import "code/pcc_ast.v3. peg"`.
1106+ The import file [`code/pcc_ast.v3. peg`](import/code) makes it easier to build ASTs.
1107+ For more details, see [here](import/code/pcc_ast.v3 .md).
11231108
11241109An execution example is as follows.
11251110
0 commit comments