@@ -725,7 +725,7 @@ The following import files are currently bundled.
725
725
- [`char/unicode_derived_core.peg`](import/char/unicode_derived_core.peg) :
726
726
This provides various rules to match a Unicode character belonging to a specific [derived core property](https://www.unicode.org/reports/tr44/#DerivedCoreProperties.txt).
727
727
- **Utility Codes**
728
- - [`code/pcc_ast.peg`](import/code/pcc_ast.peg) :
728
+ - [`code/pcc_ast.v3. peg`](import/code/pcc_ast.v3 .peg) :
729
729
This provides codes to make it easier to build an AST (abstract syntax tree).
730
730
731
731
For details, see [here](import).
@@ -1049,7 +1049,7 @@ primary <- < [0-9]+ > { $$ = calc_ast_node__create_0(); $$->custom
1049
1049
_ <- [ \t]*
1050
1050
EOL <- '\n' / '\r\n' / '\r' / ';'
1051
1051
1052
- %import "code/pcc_ast.peg" # <-- provides AST build functions
1052
+ %import "code/pcc_ast.v3. peg" # <-- provides AST build functions
1053
1053
1054
1054
%%
1055
1055
void calc_ast_node_custom_data__initialize(calc_ast_manager_t *mgr, calc_ast_node_custom_data_t *obj) {
@@ -1062,37 +1062,22 @@ void calc_ast_node_custom_data__finalize(calc_ast_manager_t *mgr, calc_ast_node_
1062
1062
1063
1063
static void dump_ast(const calc_ast_node_t *obj, int depth) {
1064
1064
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);
1086
1072
{
1087
1073
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);
1090
1076
}
1091
1077
}
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);
1096
1081
}
1097
1082
}
1098
1083
else {
@@ -1117,9 +1102,9 @@ int main(int argc, char **argv) {
1117
1102
}
1118
1103
```
1119
1104
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).
1123
1108
1124
1109
An execution example is as follows.
1125
1110
0 commit comments