added clang format

This commit is contained in:
Igor M 2024-03-11 16:00:55 +02:00
parent 60b7ce9672
commit b723a3351d
10 changed files with 477 additions and 466 deletions

View file

@ -1,12 +1,19 @@
CC = gcc CC = gcc
CFLAGS_EXTRA = CFLAGS_EXTRA =
CFLAGS = -Wall -Wextra -Wpedantic -std=c17 -O3 CFLAGS = -Wall -Wextra -Wpedantic -std=c17 -O3
CLANG_FORMAT_STYLE = '{ BasedOnStyle: Google, IndentWidth: 4 }'
.PHONY: clean build-dir hbas example .PHONY: clean hbas example format check-format
hbas: build/hbas hbas: build/hbas
example: build/example.hbf example: build/example.hbf
format:
clang-format --style=${CLANG_FORMAT_STYLE} -i src/*
check-format:
clang-format --style=${CLANG_FORMAT_STYLE} -i --dry-run -Werror src/*
build: build:
mkdir -p build mkdir -p build
@ -14,7 +21,7 @@ build/hbas: build src/hbas.c
${CC} ${CFLAGS} ${CFLAGS_EXTRA} src/hbas.c -o build/hbas ${CC} ${CFLAGS} ${CFLAGS_EXTRA} src/hbas.c -o build/hbas
build/example.hbf: build build/hbas examples/example.S build/example.hbf: build build/hbas examples/example.S
./hbas < examples/example.S > build/example.hbf ./build/hbas < examples/example.S > build/example.hbf
xxd build/example.hbf xxd build/example.hbf
clean: clean:

View file

@ -242,8 +242,8 @@ AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
} }
num_to_write = (uint64_t)tmp; num_to_write = (uint64_t)tmp;
} }
AsmError err = AsmError err = push_int_le(&rv->buf[rv->len], num_to_write,
push_int_le(&rv->buf[rv->len], num_to_write, meta.size, meta.sign); meta.size, meta.sign);
if (err != ErrOk) { if (err != ErrOk) {
return err; return err;
} }
@ -254,7 +254,8 @@ AsmError assemble_instr(InstHt ht, char *input, size_t len, Token *tok,
return ErrOk; return ErrOk;
} }
AsmError assemble(InstHt ht, char *input, size_t len, ByteVec *out, EInfo *einfo) { AsmError assemble(InstHt ht, char *input, size_t len, ByteVec *out,
EInfo *einfo) {
ByteVec rv = {malloc(MIN_SIZE), MIN_SIZE, 0}; ByteVec rv = {malloc(MIN_SIZE), MIN_SIZE, 0};
HoleVec holes = {malloc(MIN_SIZE * sizeof(Hole)), MIN_SIZE, 0}; HoleVec holes = {malloc(MIN_SIZE * sizeof(Hole)), MIN_SIZE, 0};
LabelVec labels = {malloc(MIN_SIZE * sizeof(Label)), MIN_SIZE, 0}; LabelVec labels = {malloc(MIN_SIZE * sizeof(Label)), MIN_SIZE, 0};
@ -355,7 +356,8 @@ AsmError assemble(InstHt ht, char *input, size_t len, ByteVec *out, EInfo *einfo
sign = 1; sign = 1;
num_to_write -= hole->origin; num_to_write -= hole->origin;
} }
err = push_int_le(&rv.buf[hole->location], num_to_write, hole->size, sign); err = push_int_le(&rv.buf[hole->location], num_to_write, hole->size,
sign);
if (err != 0) { if (err != 0) {
goto end; goto end;
} }
@ -396,7 +398,8 @@ int main(int argc, char **argv) {
err = assemble(ht, input.buf, input.len, &out, &einfo); err = assemble(ht, input.buf, input.len, &out, &einfo);
if (err != 0) { if (err != 0) {
size_t column = einfo.token.start - einfo.line_start + 1; size_t column = einfo.token.start - einfo.line_start + 1;
fprintf(stderr, "failed to assemble, %s, line=%zu, col=%zu token=%.*s\n", fprintf(stderr,
"failed to assemble, %s, line=%zu, col=%zu token=%.*s\n",
ERRORS[err], einfo.line, column, (int)einfo.token.len, ERRORS[err], einfo.line, column, (int)einfo.token.len,
&input.buf[einfo.token.start]); &input.buf[einfo.token.start]);
goto done; goto done;

View file

@ -23,7 +23,8 @@ Token token_ident(char *input, size_t len, size_t pos) {
while (pos < len) { while (pos < len) {
char chr = input[pos]; char chr = input[pos];
char chru = chr & ~0x20; char chru = chr & ~0x20;
int good = chr == '_' || (chr >= '0' && chr <= '9') || (chru >= 'A' && chru <= 'Z'); int good = chr == '_' || (chr >= '0' && chr <= '9') ||
(chru >= 'A' && chru <= 'Z');
if (!good) { if (!good) {
break; break;
} }