From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 1/6] codestyle: change AlignAfterOpenBracket to BlockIndent
Date: Thu, 10 Oct 2024 01:33:29 +0000 (UTC) [thread overview]
Message-ID: <20241010013318.222905-2-carlos@maniero.me> (raw)
In-Reply-To: <20241010013318.222905-1-carlos@maniero.me>
before:
my_call(ar1,
arg2);
now:
my_call(
ar1,
arg2
);
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
.clang-format | 2 +-
src/ast.c | 14 ++--
src/ast.h | 14 ++--
src/cli.c | 29 ++++---
src/codegen_linux_x86_64.c | 151 ++++++++++++++++++++++---------------
src/main.c | 24 +++---
src/parser.c | 16 ++--
src/pretty_print_ast.c | 11 +--
src/scope.c | 1 -
9 files changed, 155 insertions(+), 107 deletions(-)
diff --git a/.clang-format b/.clang-format
index b76afee..5d4dd36 100644
--- a/.clang-format
+++ b/.clang-format
@@ -2,7 +2,7 @@
Language: Cpp
# BasedOnStyle: Mozilla
AccessModifierOffset: -2
-AlignAfterOpenBracket: Align
+AlignAfterOpenBracket: BlockIndent
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
diff --git a/src/ast.c b/src/ast.c
index 5a36ccb..797f5fd 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -40,12 +40,14 @@ ast_new_translation_unit(arena_t *arena)
}
ast_node_t *
-ast_new_node_fn_def(arena_t *arena,
- token_loc_t loc,
- string_view_t id,
- list_t *params,
- type_t *return_type,
- ast_node_t *block)
+ast_new_node_fn_def(
+ arena_t *arena,
+ token_loc_t loc,
+ string_view_t id,
+ list_t *params,
+ type_t *return_type,
+ ast_node_t *block
+)
{
assert(arena);
assert(params);
diff --git a/src/ast.h b/src/ast.h
index ee94f57..44caf68 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -221,12 +221,14 @@ ast_node_t *
ast_new_translation_unit(arena_t *arena);
ast_node_t *
-ast_new_node_fn_def(arena_t *arena,
- token_loc_t loc,
- string_view_t id,
- list_t *params,
- type_t *return_type,
- ast_node_t *block);
+ast_new_node_fn_def(
+ arena_t *arena,
+ token_loc_t loc,
+ string_view_t id,
+ list_t *params,
+ type_t *return_type,
+ ast_node_t *block
+);
ast_node_t *
ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args);
diff --git a/src/cli.c b/src/cli.c
index 9d0f875..2faf6ce 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -107,7 +107,11 @@ cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args)
char *arch = cli_args_shift(args);
if (arch == NULL) {
- fprintf(stderr, "error: missing architecture for arg '--arch': available options (x86_64 | aarch64)\n");
+ fprintf(
+ stderr,
+ "error: missing architecture for arg '--arch': "
+ "available options (x86_64 | aarch64)\n"
+ );
cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
}
@@ -137,14 +141,17 @@ cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args)
void
cli_print_usage(FILE *stream, char *compiler_path)
{
- fprintf(stream,
- "Usage: %s [options] file...\n"
- "Options:\n"
- " --dump-tokens Display lexer token stream\n"
- " --dump-ast Display ast tree to stdout\n"
- " --arch <arch> Binary arch: default to x86_64 (x86_64 | aarch64)\n"
- " --sysroot <dir> System root dir where the GNU Assembler and GNU Linker are located: default to '/'\n"
- " -o <file> Compile program into a binary file\n"
- " --save-temps Keep temp files used to compile program\n",
- compiler_path);
+ fprintf(
+ stream,
+ "Usage: %s [options] file...\n"
+ "Options:\n"
+ " --dump-tokens Display lexer token stream\n"
+ " --dump-ast Display ast tree to stdout\n"
+ " --arch <arch> Binary arch: default to x86_64 (x86_64 | aarch64)\n"
+ " --sysroot <dir> System root dir where the GNU Assembler and GNU "
+ "Linker are located: default to '/'\n"
+ " -o <file> Compile program into a binary file\n"
+ " --save-temps Keep temp files used to compile program\n",
+ compiler_path
+ );
}
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index ae28aa5..9a8d76e 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -211,10 +211,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " add %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " add %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -262,10 +264,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
fprintf(codegen->out, " pop %%rcx\n");
fprintf(codegen->out, " xor %%rdx, %%rdx\n");
fprintf(codegen->out, " div %s\n", get_reg_for(REG_COUNTER, expr_bytes));
- fprintf(codegen->out,
- " mov %s, %s\n",
- get_reg_for(REG_DATA, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " mov %s, %s\n",
+ get_reg_for(REG_DATA, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -280,10 +284,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " sub %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " sub %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -298,10 +304,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " sete %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -318,10 +326,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " setl %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -338,10 +348,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " setg %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -358,10 +370,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " setne %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -378,10 +392,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " setle %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -398,10 +414,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " cmp %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " cmp %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
fprintf(codegen->out, " setge %%al\n");
fprintf(codegen->out, " movzb %%al, %s\n", get_reg_for(REG_ACCUMULATOR, expr_bytes));
@@ -444,10 +462,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " xor %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " xor %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -462,10 +482,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " and %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " and %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -480,10 +502,12 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
size_in_bytes_t expr_bytes = bytes_max(rhs_bytes, lhs_bytes);
fprintf(codegen->out, " pop %%rcx\n");
- fprintf(codegen->out,
- " or %s, %s\n",
- get_reg_for(REG_COUNTER, expr_bytes),
- get_reg_for(REG_ACCUMULATOR, expr_bytes));
+ fprintf(
+ codegen->out,
+ " or %s, %s\n",
+ get_reg_for(REG_COUNTER, expr_bytes),
+ get_reg_for(REG_ACCUMULATOR, expr_bytes)
+ );
return expr_bytes;
}
@@ -590,10 +614,12 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
size_t type_size = type_to_bytes(symbol->type);
- fprintf(codegen->out,
- " mov %s, -%ld(%%rbp)\n",
- get_reg_for(REG_ACCUMULATOR, type_size),
- codegen->base_offset);
+ fprintf(
+ codegen->out,
+ " mov %s, -%ld(%%rbp)\n",
+ get_reg_for(REG_ACCUMULATOR, type_size),
+ codegen->base_offset
+ );
codegen->base_offset += type_size;
break;
@@ -647,7 +673,8 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
break;
}
default: {
- // FIXME: improve error: replace the node->kind to a string representation
+ // FIXME: improve error: replace the node->kind to a string
+ // representation
fprintf(stderr, "node kind %d not supported\n", node->kind);
assert(0 && "unsupported block statement");
break;
@@ -704,7 +731,10 @@ type_to_bytes(type_t *type)
return 8;
}
case TYPE_UNKNOWN: {
- assert(0 && "cannot calculate size of an unknown type: probably a parser issue.");
+ assert(
+ 0 && "cannot calculate size of an unknown type: probably a "
+ "parser issue."
+ );
}
}
@@ -770,11 +800,13 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
codegen_linux_x86_64_put_stack_offset(codegen, symbol, codegen->base_offset);
- fprintf(codegen->out,
- " mov %s, -%ld(%%rbp)\n",
- // FIXME: Type may not be an as_primitive
- get_reg_for(x86_call_args[i], symbol->type->as_primitive.size),
- offset);
+ fprintf(
+ codegen->out,
+ " mov %s, -%ld(%%rbp)\n",
+ // FIXME: Type may not be an as_primitive
+ get_reg_for(x86_call_args[i], symbol->type->as_primitive.size),
+ offset
+ );
// FIXME: add offset according to the param size
codegen->base_offset += 8;
@@ -796,7 +828,6 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
static void
codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol, size_t offset)
{
-
size_t *stack_offset = arena_alloc(codegen->arena, sizeof(size_t));
*stack_offset = offset;
diff --git a/src/main.c b/src/main.c
index d1c76e3..d7738bd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -179,11 +179,13 @@ handle_codegen_linux(cli_opts_t *opts)
exit(exit_code);
}
- sprintf(command,
- "%s/bin/ld " SV_FMT ".o -o " SV_FMT "",
- opts->sysroot,
- SV_ARG(opts->output_bin),
- SV_ARG(opts->output_bin));
+ sprintf(
+ command,
+ "%s/bin/ld " SV_FMT ".o -o " SV_FMT "",
+ opts->sysroot,
+ SV_ARG(opts->output_bin),
+ SV_ARG(opts->output_bin)
+ );
exit_code = system(command);
@@ -244,9 +246,11 @@ read_entire_file(char *filepath, arena_t *arena)
static void
print_token(token_t *token)
{
- printf("%s:%lu:%lu: <%s>\n",
- token->loc.src.filepath,
- token_loc_to_lineno(token->loc),
- token_loc_to_colno(token->loc),
- token_kind_to_cstr(token->kind));
+ printf(
+ "%s:%lu:%lu: <%s>\n",
+ token->loc.src.filepath,
+ token_loc_to_lineno(token->loc),
+ token_loc_to_colno(token->loc),
+ token_kind_to_cstr(token->kind)
+ );
}
diff --git a/src/parser.c b/src/parser.c
index f712bfc..9d3d6e0 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -750,13 +750,15 @@ static bool
expected_token(token_t *token, token_kind_t expected_kind)
{
if (token->kind != expected_kind) {
- fprintf(stderr,
- "%s:%lu:%lu: syntax error: got '" SV_FMT "' token but expect '%s'\n",
- token->loc.src.filepath,
- token_loc_to_lineno(token->loc),
- token_loc_to_colno(token->loc),
- SV_ARG(token->value),
- token_kind_to_cstr(expected_kind));
+ fprintf(
+ stderr,
+ "%s:%lu:%lu: syntax error: got '" SV_FMT "' token but expect '%s'\n",
+ token->loc.src.filepath,
+ token_loc_to_lineno(token->loc),
+ token_loc_to_colno(token->loc),
+ SV_ARG(token->value),
+ token_kind_to_cstr(expected_kind)
+ );
fprintf(stderr, SV_FMT "\n", SV_ARG(token_loc_to_line(token->loc)));
fprintf(stderr, "%*s\n", (int)token_loc_to_colno(token->loc), "^");
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 3a42412..f5cd571 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -57,7 +57,6 @@ pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children)
}
for (size_t i = 0; i < level; ++i) {
-
if (!PP_IS_BIT_SET(*prefix, i)) {
printf(" ");
continue;
@@ -145,10 +144,12 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
ast_fn_definition_t fn_def = ast->as_fn_def;
char name[256];
- sprintf(name,
- "Function_Definition <name:" SV_FMT "> <return:" SV_FMT ">",
- SV_ARG(fn_def.id),
- SV_ARG(fn_def.return_type->id));
+ sprintf(
+ name,
+ "Function_Definition <name:" SV_FMT "> <return:" SV_FMT ">",
+ SV_ARG(fn_def.id),
+ SV_ARG(fn_def.return_type->id)
+ );
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
diff --git a/src/scope.c b/src/scope.c
index e483fbe..ff9067c 100644
--- a/src/scope.c
+++ b/src/scope.c
@@ -67,7 +67,6 @@ scope_lookup(scope_t *scope, string_view_t id)
{
assert(scope);
while (scope != NULL) {
-
char cstr_id[id.size + 1];
cstr_id[id.size] = 0;
memcpy(cstr_id, id.chars, id.size);
--
2.46.0
next prev parent reply other threads:[~2024-10-10 1:33 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-10 1:33 [PATCH olang 0/6] Suggestions in code style Carlos Maniero
2024-10-10 1:33 ` Carlos Maniero [this message]
2024-10-10 1:33 ` [PATCH olang v1 2/6] codestyle: never BreakBeforeBraces Carlos Maniero
2024-10-10 1:33 ` [PATCH olang v1 3/6] codestyle: prevent extra empty lines at EOF Carlos Maniero
2024-10-10 1:33 ` [PATCH olang v1 4/6] codestyle: do not allow single line enums Carlos Maniero
2024-10-10 1:33 ` [PATCH olang v1 5/6] codestyle: add trailing comma on struct initializer Carlos Maniero
2024-10-10 1:34 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-10 1:33 ` [PATCH olang v1 6/6] codestyle: limit the code to 80 characters Carlos Maniero
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241010013318.222905-2-carlos@maniero.me \
--to=carlos@maniero.me \
--cc=~johnnyrichard/olang-devel@lists.sr.ht \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.johnnyrichard.com/olang.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox