Skip to content

Commit 1f3430d

Browse files
committed
Update .c and .h files and add bin/format and bin/lint
1 parent 1f470a8 commit 1f3430d

File tree

28 files changed

+320
-295
lines changed

28 files changed

+320
-295
lines changed

bin/format

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#!/bin/bash
22

3-
# Format all C/C++ files in the repository
4-
find . \( -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.hpp' \) \
5-
-not -path "./build/*" \
6-
-not -path "./vendor/*" \
7-
-exec clang-format -i {} +
3+
set -e # Exit on error
4+
5+
clang-format -i src/*.c src/**/*.c src/*.h src/**/*.h ext/**/*.c ext/**/*.h
86

97
echo "Formatting complete!"

bin/lint

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e # Exit on error
4+
5+
clang-format --dry-run --Werror src/*.c src/**/*.c src/*.h src/**/*.h ext/**/*.c ext/**/*.h
6+
7+
echo "Linting complete!"

ext/erbx/extension.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@
44

55
#include "../../src/include/erbx.h"
66

7-
static const char *check_string(VALUE value) {
8-
if (NIL_P(value)) {
7+
static const char* check_string(VALUE value) {
8+
if(NIL_P(value)) {
99
return NULL;
1010
}
1111

12-
if (!RB_TYPE_P(value, T_STRING)) {
13-
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)",
14-
rb_obj_class(value));
12+
if(!RB_TYPE_P(value, T_STRING)) {
13+
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
1514
}
1615

1716
return RSTRING_PTR(value);
1817
}
1918

2019
VALUE rb_erbx_lex(VALUE self, VALUE source) {
21-
const char *string = check_string(source);
20+
const char* string = check_string(source);
2221

23-
erbx_lex((char *)string);
22+
erbx_lex((char*) string);
2423

2524
return Qnil;
2625
}

src/array.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,66 @@
11
#include "include/array.h"
22

3-
size_t array_sizeof(void) { return sizeof(array_T); }
3+
size_t array_sizeof(void) {
4+
return sizeof(array_T);
5+
}
46

5-
array_T *array_init(int capacity) {
6-
array_T *array = (array_T *)malloc(sizeof(array_T));
7+
array_T* array_init(int capacity) {
8+
array_T* array = (array_T*) malloc(sizeof(array_T));
79

810
array->size = 0;
911
array->capacity = capacity;
10-
array->items = (void **)malloc(sizeof(void *) * capacity);
12+
array->items = (void**) malloc(sizeof(void*) * capacity);
1113

1214
return array;
1315
}
1416

15-
void array_append(array_T *array, void *item) {
16-
if (array->size >= array->capacity) {
17+
void array_append(array_T* array, void* item) {
18+
if(array->size >= array->capacity) {
1719
array->capacity *= 2;
18-
array->items = (void **)realloc(array->items, sizeof(void *) * array->capacity);
20+
array->items = (void**) realloc(array->items, sizeof(void*) * array->capacity);
1921
}
2022

2123
array->items[array->size] = item;
2224
array->size++;
2325
}
2426

25-
void *array_get(array_T *array, int index) {
26-
if (index >= array->size || index < 0) {
27+
void* array_get(array_T* array, int index) {
28+
if(index >= array->size || index < 0) {
2729
return NULL;
2830
}
2931

3032
return array->items[index];
3133
}
3234

33-
void array_set(array_T *array, int index, void *item) {
34-
if (index >= array->size || index < 0) {
35+
void array_set(array_T* array, int index, void* item) {
36+
if(index >= array->size || index < 0) {
3537
return;
3638
}
3739

3840
array->items[index] = item;
3941
}
4042

41-
void array_remove(array_T *array, int index) {
42-
if (index >= array->size || index < 0) {
43+
void array_remove(array_T* array, int index) {
44+
if(index >= array->size || index < 0) {
4345
return;
4446
}
4547

46-
for (int i = index; i < array->size - 1; i++) {
48+
for(int i = index; i < array->size - 1; i++) {
4749
array->items[i] = array->items[i + 1];
4850
}
4951

5052
array->size--;
5153
}
5254

53-
size_t array_size(array_T *array) { return array->size; }
55+
size_t array_size(array_T* array) {
56+
return array->size;
57+
}
5458

55-
size_t array_capacity(array_T *array) { return array->capacity; }
59+
size_t array_capacity(array_T* array) {
60+
return array->capacity;
61+
}
5662

57-
void array_free(array_T *array) {
63+
void array_free(array_T* array) {
5864
free(array->items);
5965
free(array);
6066
}

src/ast.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include <stdlib.h>
44

5-
size_t ast_sizeof(void) { return sizeof(struct AST_STRUCT); }
5+
size_t ast_sizeof(void) {
6+
return sizeof(struct AST_STRUCT);
7+
}
68

7-
AST_T *ast_init(int type) {
8-
AST_T *ast = calloc(1, ast_sizeof());
9+
AST_T* ast_init(int type) {
10+
AST_T* ast = calloc(1, ast_sizeof());
911
ast->type = type;
1012
ast->children = array_init(ast_sizeof());
1113

src/buffer.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,42 @@
44

55
#include "include/buffer.h"
66

7-
bool buffer_init(buffer_T *buffer) {
7+
bool buffer_init(buffer_T* buffer) {
88
buffer->capacity = 1024;
99
buffer->length = 0;
1010
buffer->value = malloc(buffer->capacity * sizeof(char));
1111

12-
if (buffer->value) {
12+
if(buffer->value) {
1313
buffer->value[0] = '\0';
1414
}
1515

1616
return buffer != NULL;
1717
}
1818

19-
char *buffer_value(buffer_T *buffer) { return buffer->value; }
19+
char* buffer_value(buffer_T* buffer) {
20+
return buffer->value;
21+
}
2022

21-
size_t buffer_length(buffer_T *buffer) { return buffer->length; }
23+
size_t buffer_length(buffer_T* buffer) {
24+
return buffer->length;
25+
}
2226

23-
size_t buffer_capacity(buffer_T *buffer) { return buffer->capacity; }
27+
size_t buffer_capacity(buffer_T* buffer) {
28+
return buffer->capacity;
29+
}
2430

25-
size_t buffer_sizeof(void) { return sizeof(buffer_T); }
31+
size_t buffer_sizeof(void) {
32+
return sizeof(buffer_T);
33+
}
2634

27-
void buffer_append(buffer_T *buffer, const char *text) {
35+
void buffer_append(buffer_T* buffer, const char* text) {
2836
size_t text_length = strlen(text);
2937

30-
if (buffer->length + text_length >= buffer->capacity) {
38+
if(buffer->length + text_length >= buffer->capacity) {
3139
size_t new_capacity = (buffer->length + text_length) * 2;
32-
char *new_buffer = realloc(buffer->value, new_capacity);
40+
char* new_buffer = realloc(buffer->value, new_capacity);
3341

34-
if (new_buffer) {
42+
if(new_buffer) {
3543
buffer->value = new_buffer;
3644
buffer->capacity = new_capacity;
3745
} else {
@@ -44,14 +52,13 @@ void buffer_append(buffer_T *buffer, const char *text) {
4452
buffer->length += text_length;
4553
}
4654

47-
void buffer_prepend(buffer_T *buffer, const char *text) {
48-
if (text == NULL || text[0] == '\0')
49-
return;
55+
void buffer_prepend(buffer_T* buffer, const char* text) {
56+
if(text == NULL || text[0] == '\0') return;
5057

5158
size_t text_length = strlen(text);
5259
size_t new_length = buffer->length + text_length;
5360

54-
if (new_length >= buffer->capacity) {
61+
if(new_length >= buffer->capacity) {
5562
size_t new_capacity = new_length * 2;
5663
buffer->value = realloc(buffer->value, new_capacity);
5764
buffer->capacity = new_capacity;
@@ -64,13 +71,12 @@ void buffer_prepend(buffer_T *buffer, const char *text) {
6471
buffer->value[buffer->length] = '\0';
6572
}
6673

67-
void buffer_concat(buffer_T *destination, buffer_T *source) {
68-
if (source->length == 0)
69-
return;
74+
void buffer_concat(buffer_T* destination, buffer_T* source) {
75+
if(source->length == 0) return;
7076

7177
size_t new_length = destination->length + source->length;
7278

73-
if (new_length >= destination->capacity) {
79+
if(new_length >= destination->capacity) {
7480
size_t new_capacity = new_length * 2;
7581
destination->value = realloc(destination->value, new_capacity);
7682
destination->capacity = new_capacity;
@@ -81,7 +87,7 @@ void buffer_concat(buffer_T *destination, buffer_T *source) {
8187
destination->length = new_length;
8288
}
8389

84-
void buffer_free(buffer_T *buffer) {
90+
void buffer_free(buffer_T* buffer) {
8591
free(buffer->value);
8692
buffer->value = NULL;
8793
buffer->length = buffer->capacity = 0;

src/erbx.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
#include <stdlib.h>
1111

12-
array_T *erbx_lex(char *source) {
13-
lexer_T *lexer = lexer_init(source);
14-
token_T *token = 0;
12+
array_T* erbx_lex(char* source) {
13+
lexer_T* lexer = lexer_init(source);
14+
token_T* token = 0;
1515

16-
array_T *tokens = array_init(1);
16+
array_T* tokens = array_init(1);
1717

18-
while ((token = lexer_next_token(lexer))->type != TOKEN_EOF) {
18+
while((token = lexer_next_token(lexer))->type != TOKEN_EOF) {
1919
array_append(tokens, token);
2020
}
2121

@@ -24,20 +24,20 @@ array_T *erbx_lex(char *source) {
2424
return tokens;
2525
}
2626

27-
array_T *erbx_lex_file(const char *path) {
28-
char *source = erbx_read_file(path);
29-
array_T *tokens = erbx_lex(source);
27+
array_T* erbx_lex_file(const char* path) {
28+
char* source = erbx_read_file(path);
29+
array_T* tokens = erbx_lex(source);
3030

3131
free(source);
3232

3333
return tokens;
3434
}
3535

36-
void erbx_lex_to_buffer(char *source, buffer_T *output) {
37-
array_T *tokens = erbx_lex(source);
36+
void erbx_lex_to_buffer(char* source, buffer_T* output) {
37+
array_T* tokens = erbx_lex(source);
3838

39-
for (int i = 0; i < array_size(tokens); i++) {
40-
token_T *token = array_get(tokens, i);
39+
for(int i = 0; i < array_size(tokens); i++) {
40+
token_T* token = array_get(tokens, i);
4141
buffer_append(output, token_to_string(token));
4242
buffer_append(output, "\n");
4343
}
@@ -47,4 +47,6 @@ void erbx_lex_to_buffer(char *source, buffer_T *output) {
4747
// printf("%zu\n", root->children->size);
4848
}
4949

50-
const char *erbx_version(void) { return ERBX_VERSION; }
50+
const char* erbx_version(void) {
51+
return ERBX_VERSION;
52+
}

src/include/array.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
#include <stdlib.h>
55

66
typedef struct ARRAY_STRUCT {
7-
void **items;
7+
void** items;
88
size_t size;
99
size_t capacity;
1010
} array_T;
1111

12-
array_T *array_init(int capacity);
12+
array_T* array_init(int capacity);
1313

14-
void *array_get(array_T *array, int index);
14+
void* array_get(array_T* array, int index);
1515

16-
void array_append(array_T *array, void *item);
17-
void array_set(array_T *array, int index, void *item);
18-
void array_free(array_T *array);
19-
void array_remove(array_T *array, int index);
16+
void array_append(array_T* array, void* item);
17+
void array_set(array_T* array, int index, void* item);
18+
void array_free(array_T* array);
19+
void array_remove(array_T* array, int index);
2020

21-
size_t array_capacity(array_T *array);
22-
size_t array_size(array_T *array);
21+
size_t array_capacity(array_T* array);
22+
size_t array_size(array_T* array);
2323
size_t array_sizeof(void);
2424

2525
#endif

src/include/ast.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ typedef struct AST_STRUCT {
1919
AST_NOOP,
2020
} type;
2121

22-
array_T *children;
23-
char *name;
24-
struct AST_STRUCT *value;
22+
array_T* children;
23+
char* name;
24+
struct AST_STRUCT* value;
2525
int data_type;
2626
int int_value;
2727
} AST_T;
2828

29-
AST_T *ast_init(int type);
29+
AST_T* ast_init(int type);
3030

3131
size_t ast_sizeof(void);
3232

src/include/buffer.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@
55
#include <stdlib.h>
66

77
typedef struct BUFFER_STRUCT {
8-
char *value;
8+
char* value;
99
size_t length;
1010
size_t capacity;
1111
} buffer_T;
1212

13-
bool buffer_init(buffer_T *buffer);
13+
bool buffer_init(buffer_T* buffer);
1414

15-
void buffer_append(buffer_T *buffer, const char *text);
16-
void buffer_prepend(buffer_T *buffer, const char *text);
17-
void buffer_concat(buffer_T *destination, buffer_T *source);
18-
void buffer_free(buffer_T *buffer);
15+
void buffer_append(buffer_T* buffer, const char* text);
16+
void buffer_prepend(buffer_T* buffer, const char* text);
17+
void buffer_concat(buffer_T* destination, buffer_T* source);
18+
void buffer_free(buffer_T* buffer);
1919

20-
char *buffer_value(buffer_T *buffer);
20+
char* buffer_value(buffer_T* buffer);
2121

22-
size_t buffer_length(buffer_T *buffer);
23-
size_t buffer_capacity(buffer_T *buffer);
22+
size_t buffer_length(buffer_T* buffer);
23+
size_t buffer_capacity(buffer_T* buffer);
2424
size_t buffer_sizeof(void);
2525

2626
#endif

0 commit comments

Comments
 (0)