Skip to content

Commit 842dc8e

Browse files
committed
Make YA_NULL use a singleton, so that YA_NULL can use a pointer, which is required by YA_LIST and YA_BRANCH.
1 parent ea728a6 commit 842dc8e

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

yyast/leaf.c

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <yyast/utils.h>
4040
#include <yyast/error.h>
4141

42+
ya_t ya_null_singleton;
43+
4244
ya_t ya_literal(const char * restrict name, ya_type_t type, const void * restrict buf, size_t buf_size)
4345
{
4446
size_t aligned_buf_size = ya_align64(buf_size);

yyast/leaf.h

+16-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
#include <yyast/types.h>
2929
#include <yyast/utils.h>
3030

31+
/** Ya_null is a singleton, so that it can be
32+
* referenced by pointer by the YA_NULL macro.
33+
*
34+
* The singleton is initialized in ya_main().
35+
* The node functions will not free this, or any, ya_null leafs.
36+
*/
37+
extern ya_t ya_null_singleton;
38+
39+
/** Null node, unused child entry.
40+
*/
41+
ya_t ya_null(void);
42+
43+
/** Null node, unused child entry.
44+
*/
45+
#define YA_NULL (&ya_null_singleton)
46+
3147
/** Create an literal node.
3248
*
3349
* @param name name of the node.
@@ -117,12 +133,4 @@ ya_t ya_leaf(const char * restrict name);
117133
*/
118134
#define YA_LEAF(name) ya_leaf(name)
119135

120-
/** Null node, unused child entry.
121-
*/
122-
ya_t ya_null(void);
123-
124-
/** Null node, unused child entry.
125-
*/
126-
#define YA_NULL ya_null()
127-
128136
#endif

yyast/main.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
#include <yyast/utils.h>
3131
#include <yyast/node.h>
3232
#include <yyast/count.h>
33+
#include <yyast/leaf.h>
3334

3435
extern FILE *yyin;
3536
int yyparse();
3637

3738
char *ya_output_filename = NULL;
3839
char *ya_input_filename = NULL;
3940

40-
void ya_usage(int exit_code)
41+
void ya_usage(char *application, int exit_code)
4142
{
4243
fprintf(stderr, "Usage:\n");
43-
fprintf(stderr, " application -h\n");
44-
fprintf(stderr, " application [-c] [-o output file] input file\n");
44+
fprintf(stderr, " %s -h\n", application);
45+
fprintf(stderr, " %s [-c] [-o output file] input file\n", application);
4546
fprintf(stderr, "\n");
4647
fprintf(stderr, "Options:\n");
4748
fprintf(stderr, " -h Show help message\n");
@@ -69,29 +70,29 @@ void ya_parse_options(int argc, char *argv[], char *extension)
6970
break;
7071
case 'h':
7172
// Help, just shows usage without error.
72-
ya_usage(0);
73+
ya_usage(argv[0], 0);
7374
case 'c':
7475
// Compile, which is the only mode it supports.
7576
break;
7677
case 0:
7778
break;
7879
case ':':
7980
fprintf(stderr, "Missing option argument.");
80-
ya_usage(2);
81+
ya_usage(argv[0], 2);
8182
case '?':
8283
fprintf(stderr, "Unknown option.");
83-
ya_usage(2);
84+
ya_usage(argv[0], 2);
8485
default:
8586
fprintf(stderr, "Unknown error in getopt_long().");
86-
ya_usage(2);
87+
ya_usage(argv[0], 2);
8788
}
8889
}
8990
argc -= optind;
9091
argv += optind;
9192

9293
if (argc != 1) {
9394
fprintf(stderr, "Expecting a single filename.\n");
94-
ya_usage(2);
95+
ya_usage(argv[0], 2);
9596
}
9697

9798
ya_input_filename = argv[0];
@@ -107,6 +108,9 @@ int ya_main(int argc, char *argv[], char *extension)
107108
FILE *out;
108109
char *reposition_s;
109110

111+
// Initialize singletons.
112+
ya_null_singleton = ya_null();
113+
110114
ya_parse_options(argc, argv, extension);
111115

112116
if (strcmp(ya_input_filename, "-") == 0) {

yyast/node.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ ya_t ya_generic_nodev(const char * restrict name, ya_type_t type, va_list ap)
123123
}
124124

125125
// Now that the child node is copied in self, we should free() it.
126-
free(item->node);
126+
// The singleton YA_NULL, should not be free-ed.
127+
if (item->type != YA_NODE_TYPE_NULL) {
128+
free(item->node);
129+
}
127130
}
128131

129132
va_end(ap2);

0 commit comments

Comments
 (0)