Skip to content

Commit 06480ab

Browse files
committed
Add *_sizeof() function to all objects (#10)
1 parent dff5aef commit 06480ab

File tree

8 files changed

+32
-5
lines changed

8 files changed

+32
-5
lines changed

src/include/ast.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ERBX_AST_H
22
#define ERBX_AST_H
33

4+
#include <stdlib.h>
5+
46
#include "array.h"
57

68
typedef struct AST_STRUCT {
@@ -26,4 +28,6 @@ typedef struct AST_STRUCT {
2628

2729
AST_T* ast_init(int type);
2830

31+
size_t ast_sizeof(void);
32+
2933
#endif

src/include/buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct BUFFER_STRUCT {
1010
} buffer_T;
1111

1212
void buffer_init(buffer_T* buffer);
13+
1314
void buffer_append(buffer_T* buffer, const char* text);
1415
void buffer_prepend(buffer_T* buffer, const char* text);
1516
void buffer_concat(buffer_T* destination, buffer_T* source);

src/include/lexer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define ERBX_LEXER_H
33

44
#include "token.h"
5-
#include <stdio.h>
5+
#include <stdlib.h>
66

77
typedef struct LEXER_STRUCT {
88
char* source;
@@ -44,4 +44,6 @@ token_T* lexer_parse_single_quoted_id(lexer_T* lexer);
4444
token_T* lexer_parse_tag_name(lexer_T* lexer);
4545
token_T* lexer_parse_whitespace(lexer_T* lexer);
4646

47+
size_t lexer_sizeof(void);
48+
4749
#endif

src/include/parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ERBX_PARSER_H
22
#define ERBX_PARSER_H
33

4+
#include <stdlib.h>
5+
46
#include "lexer.h"
57
#include "ast.h"
68

@@ -13,4 +15,6 @@ parser_T* parser_init(lexer_T* lexer);
1315
token_T* parser_consume(parser_T* parser, int type);
1416
AST_T* parser_parse(parser_T* parser);
1517

18+
size_t parser_sizeof(void);
19+
1620
#endif

src/include/token.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef ERBX_TOKEN_H
22
#define ERBX_TOKEN_H
33

4+
#include <stdlib.h>
5+
46
typedef struct TOKEN_STRUCT {
57
char* value;
68
enum {
@@ -29,4 +31,6 @@ token_T* token_init(char* value, int type);
2931
char* token_to_string(token_T* token);
3032
const char* token_type_to_string(int type);
3133

34+
size_t token_sizeof(void);
35+
3236
#endif

src/lexer.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#include "include/macros.h"
33

44
#include <string.h>
5-
#include <stdlib.h>
5+
#include <stdio.h>
66
#include <ctype.h>
77

8+
size_t lexer_sizeof(void) {
9+
return sizeof(struct LEXER_STRUCT);
10+
}
11+
812
lexer_T* lexer_init(char* source) {
9-
lexer_T* lexer = calloc(1, sizeof(struct LEXER_STRUCT));
13+
lexer_T* lexer = calloc(1, lexer_sizeof());
1014

1115
lexer->state = STATE_NONE;
1216
lexer->source = source;

src/parser.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
#include <stdlib.h>
66
#include <string.h>
77

8+
size_t parser_sizeof(void) {
9+
return sizeof(struct PARSER_STRUCT);
10+
}
11+
812
parser_T* parser_init(lexer_T* lexer) {
9-
parser_T * parser = calloc(1, sizeof(struct PARSER_STRUCT));
13+
parser_T * parser = calloc(1, parser_sizeof());
1014
parser->lexer = lexer;
1115
parser->token = lexer_next_token(lexer);
1216

src/token.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <string.h>
55
#include <stdio.h>
66

7+
size_t token_sizeof(void) {
8+
return sizeof(struct TOKEN_STRUCT);
9+
}
10+
711
token_T* token_init(char* value, int type) {
8-
token_T* token = calloc(1, sizeof(struct TOKEN_STRUCT));
12+
token_T* token = calloc(1, token_sizeof());
913
token->value = value;
1014
token->type = type;
1115

0 commit comments

Comments
 (0)