Skip to content

Commit 49a391a

Browse files
committed
add clang formater
1 parent 0d330d1 commit 49a391a

33 files changed

+626
-559
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ jobs:
2020

2121
- name: run tests
2222
run: ./run_erbx_tests
23+
formatting-check:
24+
name: Formatting Check
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install clang-format
30+
run: sudo apt-get install -y clang-format
31+
32+
- name: Check formatting
33+
run: |
34+
find . -name '*.[ch]' -not -path './vendor/*' | xargs clang-format -i
35+
git diff --exit-code || (echo "❌ Code formatting check failed! Run clang-format locally and commit changes." && exit 1)

bin/format

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
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 {} +
8+
9+
echo "Formatting complete!"

ext/erbx/extension.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "ruby.h"
2-
#include "extconf.h"
31
#include "extension.h"
2+
#include "extconf.h"
3+
#include "ruby.h"
44

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

@@ -10,7 +10,9 @@ static const char *check_string(VALUE value) {
1010
}
1111

1212
if (!RB_TYPE_P(value, T_STRING)) {
13-
rb_raise(rb_eTypeError, "wrong argument type %" PRIsVALUE " (expected String)", rb_obj_class(value));
13+
rb_raise(rb_eTypeError,
14+
"wrong argument type %" PRIsVALUE " (expected String)",
15+
rb_obj_class(value));
1416
}
1517

1618
return RSTRING_PTR(value);
@@ -19,7 +21,7 @@ static const char *check_string(VALUE value) {
1921
VALUE rb_erbx_lex(VALUE self, VALUE source) {
2022
const char *string = check_string(source);
2123

22-
erbx_lex((char *) string);
24+
erbx_lex((char *)string);
2325

2426
return Qnil;
2527
}

src/array.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
#include "include/array.h"
22

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

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

108
array->size = 0;
119
array->capacity = capacity;
12-
array->items = (void**) malloc(sizeof(void*) * capacity);
10+
array->items = (void **)malloc(sizeof(void *) * capacity);
1311

1412
return array;
1513
}
1614

17-
void array_append(array_T* array, void* item) {
15+
void array_append(array_T *array, void *item) {
1816
if (array->size >= array->capacity) {
1917
array->capacity *= 2;
20-
array->items = (void**) realloc(array->items, sizeof(void*) * array->capacity);
18+
array->items =
19+
(void **)realloc(array->items, sizeof(void *) * array->capacity);
2120
}
2221

2322
array->items[array->size] = item;
2423
array->size++;
2524
}
2625

27-
void* array_get(array_T* array, int index) {
26+
void *array_get(array_T *array, int index) {
2827
if (index >= array->size || index < 0) {
2928
return NULL;
3029
}
3130

3231
return array->items[index];
3332
}
3433

35-
void array_set(array_T* array, int index, void* item) {
34+
void array_set(array_T *array, int index, void *item) {
3635
if (index >= array->size || index < 0) {
3736
return;
3837
}
3938

4039
array->items[index] = item;
4140
}
4241

43-
void array_remove(array_T* array, int index) {
42+
void array_remove(array_T *array, int index) {
4443
if (index >= array->size || index < 0) {
4544
return;
4645
}
@@ -52,15 +51,11 @@ void array_remove(array_T* array, int index) {
5251
array->size--;
5352
}
5453

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

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

63-
void array_free(array_T* array) {
58+
void array_free(array_T *array) {
6459
free(array->items);
6560
free(array);
6661
}

src/ast.c

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

33
#include <stdlib.h>
44

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

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

src/buffer.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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));
@@ -16,28 +16,20 @@ bool buffer_init(buffer_T* buffer) {
1616
return buffer != NULL;
1717
}
1818

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

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

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

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

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

3830
if (buffer->length + text_length >= buffer->capacity) {
3931
size_t new_capacity = (buffer->length + text_length) * 2;
40-
char* new_buffer = realloc(buffer->value, new_capacity);
32+
char *new_buffer = realloc(buffer->value, new_capacity);
4133

4234
if (new_buffer) {
4335
buffer->value = new_buffer;
@@ -52,8 +44,9 @@ void buffer_append(buffer_T* buffer, const char* text) {
5244
buffer->length += text_length;
5345
}
5446

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

5851
size_t text_length = strlen(text);
5952
size_t new_length = buffer->length + text_length;
@@ -71,8 +64,9 @@ void buffer_prepend(buffer_T* buffer, const char* text) {
7164
buffer->value[buffer->length] = '\0';
7265
}
7366

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

7771
size_t new_length = destination->length + source->length;
7872

@@ -87,7 +81,7 @@ void buffer_concat(buffer_T* destination, buffer_T* source) {
8781
destination->length = new_length;
8882
}
8983

90-
void buffer_free(buffer_T* buffer) {
84+
void buffer_free(buffer_T *buffer) {
9185
free(buffer->value);
9286
buffer->value = NULL;
9387
buffer->length = buffer->capacity = 0;

src/erbx.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
#include "include/erbx.h"
2+
#include "include/array.h"
3+
#include "include/buffer.h"
24
#include "include/io.h"
35
#include "include/lexer.h"
4-
#include "include/token.h"
56
#include "include/parser.h"
6-
#include "include/buffer.h"
7-
#include "include/array.h"
7+
#include "include/token.h"
88
#include "include/version.h"
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

1818
while ((token = lexer_next_token(lexer))->type != TOKEN_EOF) {
1919
array_append(tokens, token);
@@ -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

3939
for (int i = 0; i < array_size(tokens); i++) {
40-
token_T* token = array_get(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,6 +47,4 @@ 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) {
51-
return ERBX_VERSION;
52-
}
50+
const char *erbx_version(void) { return ERBX_VERSION; }

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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
#ifndef ERBX_BUFFER_H
22
#define ERBX_BUFFER_H
33

4-
#include <stdlib.h>
54
#include <stdbool.h>
5+
#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)