From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 2/6] codestyle: never BreakBeforeBraces
Date: Thu, 10 Oct 2024 01:33:33 +0000 (UTC) [thread overview]
Message-ID: <20241010013318.222905-3-carlos@maniero.me> (raw)
In-Reply-To: <20241010013318.222905-1-carlos@maniero.me>
Before we had this standard:
struct
{
}
void main() # This breaks
{
if (true) { # This does not
}
}
Now:
struct {
}
void main() {
if (true) { # This does not
}
}
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
.clang-format | 18 ++++-----
src/arena.c | 15 +++-----
src/arena.h | 3 +-
src/ast.c | 42 +++++++-------------
src/ast.h | 68 +++++++++++----------------------
src/checker.c | 15 +++-----
src/checker.h | 3 +-
src/cli.c | 18 +++------
src/cli.h | 9 ++---
src/codegen_linux_aarch64.c | 9 ++---
src/codegen_linux_x86_64.c | 42 +++++++-------------
src/codegen_linux_x86_64.h | 3 +-
src/lexer.c | 54 +++++++++-----------------
src/lexer.h | 18 +++------
src/list.c | 18 +++------
src/list.h | 6 +--
src/main.c | 18 +++------
src/map.c | 24 ++++--------
src/map.h | 9 ++---
src/parser.c | 72 ++++++++++++-----------------------
src/parser.h | 3 +-
src/pretty_print_ast.c | 24 ++++--------
src/scope.c | 18 +++------
src/scope.h | 6 +--
src/string_view.c | 9 ++---
src/string_view.h | 3 +-
src/type.c | 6 +--
src/type.h | 30 ++++-----------
tests/unit/arena_test.c | 9 ++---
tests/unit/list_test.c | 12 ++----
tests/unit/map_test.c | 12 ++----
tests/unit/string_view_test.c | 9 ++---
32 files changed, 201 insertions(+), 404 deletions(-)
diff --git a/.clang-format b/.clang-format
index 5d4dd36..68c0e5b 100644
--- a/.clang-format
+++ b/.clang-format
@@ -50,26 +50,26 @@ BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: false
- AfterClass: true
+ AfterClass: false
AfterControlStatement: Never
- AfterEnum: true
- AfterFunction: true
+ AfterEnum: false
+ AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
- AfterStruct: true
- AfterUnion: true
- AfterExternBlock: true
+ AfterStruct: false
+ AfterUnion: false
+ AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
- SplitEmptyFunction: true
+ SplitEmptyFunction: false
SplitEmptyRecord: false
- SplitEmptyNamespace: true
+ SplitEmptyNamespace: false
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
-BreakBeforeBraces: Mozilla
+BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
diff --git a/src/arena.c b/src/arena.c
index ad2e535..12045fe 100644
--- a/src/arena.c
+++ b/src/arena.c
@@ -19,8 +19,7 @@
#include <stdlib.h>
arena_t
-arena_new(size_t size)
-{
+arena_new(size_t size) {
arena_t arena;
arena.offset = 0;
arena.region = malloc(sizeof(uint8_t) * size);
@@ -32,8 +31,7 @@ static uint8_t
arena_padding(size_t bytes);
void *
-arena_alloc(arena_t *arena, size_t bytes)
-{
+arena_alloc(arena_t *arena, size_t bytes) {
if ((arena->offset + bytes) > arena->size) {
return NULL;
}
@@ -44,20 +42,17 @@ arena_alloc(arena_t *arena, size_t bytes)
}
void
-arena_release(arena_t *arena)
-{
+arena_release(arena_t *arena) {
arena->offset = 0;
}
void
-arena_free(arena_t *arena)
-{
+arena_free(arena_t *arena) {
arena->size = 0;
free(arena->region);
}
static uint8_t
-arena_padding(size_t bytes)
-{
+arena_padding(size_t bytes) {
return (ARENA_ALIGNMENT_BYTES - bytes) & ARENA_ALIGNMENT_BYTES_MASK;
}
diff --git a/src/arena.h b/src/arena.h
index 03fd803..168a39f 100644
--- a/src/arena.h
+++ b/src/arena.h
@@ -22,8 +22,7 @@
#define ARENA_ALIGNMENT_BYTES 16
#define ARENA_ALIGNMENT_BYTES_MASK (ARENA_ALIGNMENT_BYTES - 1)
-typedef struct arena
-{
+typedef struct arena {
size_t offset;
size_t size;
uint8_t *region;
diff --git a/src/ast.c b/src/ast.c
index 797f5fd..1a23864 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -23,8 +23,7 @@
#include "string_view.h"
ast_node_t *
-ast_new_translation_unit(arena_t *arena)
-{
+ast_new_translation_unit(arena_t *arena) {
ast_node_t *node = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node);
@@ -47,8 +46,7 @@ ast_new_node_fn_def(
list_t *params,
type_t *return_type,
ast_node_t *block
-)
-{
+) {
assert(arena);
assert(params);
assert(block);
@@ -69,8 +67,7 @@ ast_new_node_fn_def(
}
ast_node_t *
-ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args)
-{
+ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *args) {
assert(arena);
assert(args);
@@ -88,8 +85,7 @@ ast_new_node_fn_call(arena_t *arena, token_loc_t loc, string_view_t id, list_t *
}
ast_node_t *
-ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value)
-{
+ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *type, ast_node_t *value) {
ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_var_def);
@@ -105,8 +101,7 @@ ast_new_node_var_def(arena_t *arena, token_loc_t loc, string_view_t id, type_t *
}
ast_node_t *
-ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs)
-{
+ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs) {
ast_node_t *node_bin_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_bin_op);
@@ -120,8 +115,7 @@ ast_new_node_bin_op(arena_t *arena, token_loc_t loc, ast_binary_op_kind_t kind,
}
ast_node_t *
-ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr)
-{
+ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind, ast_node_t *expr) {
ast_node_t *node_unary_op = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_unary_op);
@@ -134,8 +128,7 @@ ast_new_node_unary_op(arena_t *arena, token_loc_t loc, ast_unary_op_kind_t kind,
}
ast_node_t *
-ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value)
-{
+ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value) {
ast_node_t *node_literal = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_literal);
@@ -148,8 +141,7 @@ ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value)
}
ast_node_t *
-ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id)
-{
+ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id) {
ast_node_t *node_ref = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_ref);
@@ -161,8 +153,7 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id)
}
ast_node_t *
-ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr)
-{
+ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, ast_node_t *expr) {
ast_node_t *node_var_assign_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_var_assign_stmt);
@@ -175,8 +166,7 @@ ast_new_node_var_assign_stmt(arena_t *arena, token_loc_t loc, ast_node_t *ref, a
}
ast_node_t *
-ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr)
-{
+ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr) {
ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_return_stmt);
@@ -188,8 +178,7 @@ ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr)
}
ast_node_t *
-ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else)
-{
+ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then, ast_node_t *_else) {
ast_node_t *node_if_stmt = arena_alloc(arena, sizeof(ast_node_t));
assert(node_if_stmt);
@@ -203,8 +192,7 @@ ast_new_node_if_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node
}
ast_node_t *
-ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then)
-{
+ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_node_t *then) {
ast_node_t *node_while_stmt = arena_alloc(arena, sizeof(ast_node_t));
assert(node_while_stmt);
@@ -217,8 +205,7 @@ ast_new_node_while_stmt(arena_t *arena, token_loc_t loc, ast_node_t *cond, ast_n
}
ast_node_t *
-ast_new_node_block(arena_t *arena)
-{
+ast_new_node_block(arena_t *arena) {
ast_node_t *node_block = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_block);
@@ -233,8 +220,7 @@ ast_new_node_block(arena_t *arena)
}
ast_fn_param_t *
-ast_new_fn_param(arena_t *arena, string_view_t id, type_t *type)
-{
+ast_new_fn_param(arena_t *arena, string_view_t id, type_t *type) {
ast_fn_param_t *fn_param = (ast_fn_param_t *)arena_alloc(arena, sizeof(ast_fn_param_t));
assert(fn_param);
diff --git a/src/ast.h b/src/ast.h
index 44caf68..b220708 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -28,8 +28,7 @@
typedef union ast_node ast_node_t;
-typedef enum
-{
+typedef enum {
AST_NODE_TRANSLATION_UNIT,
AST_NODE_BLOCK,
AST_NODE_FN_DEF,
@@ -46,32 +45,27 @@ typedef enum
AST_NODE_UNKNOWN
} ast_node_kind_t;
-typedef struct ast_node_meta
-{
+typedef struct ast_node_meta {
ast_node_kind_t kind;
token_loc_t loc;
} ast_node_meta_t;
-typedef struct ast_block
-{
+typedef struct ast_block {
ast_node_meta_t meta;
list_t *nodes;
} ast_block_t;
-typedef struct ast_translation_unit
-{
+typedef struct ast_translation_unit {
ast_node_meta_t meta;
list_t *decls;
} ast_translation_unit_t;
-typedef struct ast_fn_param
-{
+typedef struct ast_fn_param {
string_view_t id;
type_t *type;
} ast_fn_param_t;
-typedef struct ast_fn_definition
-{
+typedef struct ast_fn_definition {
ast_node_meta_t meta;
string_view_t id;
list_t *params;
@@ -80,16 +74,14 @@ typedef struct ast_fn_definition
scope_t *scope;
} ast_fn_definition_t;
-typedef struct ast_fn_call
-{
+typedef struct ast_fn_call {
ast_node_meta_t meta;
string_view_t id;
list_t *args;
scope_t *scope;
} ast_fn_call_t;
-typedef struct ast_var_definition
-{
+typedef struct ast_var_definition {
ast_node_meta_t meta;
string_view_t id;
type_t *type;
@@ -97,30 +89,23 @@ typedef struct ast_var_definition
scope_t *scope;
} ast_var_definition_t;
-typedef enum
-{
- AST_LITERAL_U32
-} ast_literal_kind_t;
+typedef enum { AST_LITERAL_U32 } ast_literal_kind_t;
-typedef struct ast_literal
-{
+typedef struct ast_literal {
ast_node_meta_t meta;
ast_literal_kind_t kind;
- union
- {
+ union {
uint32_t as_u32;
};
} ast_literal_t;
-typedef struct ast_ref
-{
+typedef struct ast_ref {
ast_node_meta_t meta;
string_view_t id;
scope_t *scope;
} ast_ref_t;
-typedef enum ast_binary_op_kind
-{
+typedef enum ast_binary_op_kind {
AST_BINOP_ADDITION,
AST_BINOP_SUBTRACTION,
AST_BINOP_MULTIPLICATION,
@@ -141,16 +126,14 @@ typedef enum ast_binary_op_kind
AST_BINOP_LOGICAL_OR,
} ast_binary_op_kind_t;
-typedef struct ast_binary_op
-{
+typedef struct ast_binary_op {
ast_node_meta_t meta;
ast_binary_op_kind_t kind;
ast_node_t *lhs;
ast_node_t *rhs;
} ast_binary_op_t;
-typedef enum ast_unary_op_kind
-{
+typedef enum ast_unary_op_kind {
AST_UNARY_BITWISE_NOT,
AST_UNARY_LOGICAL_NOT,
AST_UNARY_NEGATIVE,
@@ -159,46 +142,39 @@ typedef enum ast_unary_op_kind
AST_UNARY_ADDRESSOF,
} ast_unary_op_kind_t;
-typedef struct ast_unary_op
-{
+typedef struct ast_unary_op {
ast_node_meta_t meta;
ast_unary_op_kind_t kind;
ast_node_t *expr;
} ast_unary_op_t;
-typedef struct ast_var_assign_stmt
-{
+typedef struct ast_var_assign_stmt {
ast_node_meta_t meta;
ast_node_t *ref;
ast_node_t *expr;
} ast_var_assign_stmt_t;
-typedef struct ast_return_stmt
-{
+typedef struct ast_return_stmt {
ast_node_meta_t meta;
ast_node_t *expr;
} ast_return_stmt_t;
-typedef struct ast_if_stmt
-{
+typedef struct ast_if_stmt {
ast_node_meta_t meta;
ast_node_t *cond;
ast_node_t *then;
ast_node_t *_else;
} ast_if_stmt_t;
-typedef struct ast_while_stmt
-{
+typedef struct ast_while_stmt {
ast_node_meta_t meta;
ast_node_t *cond;
ast_node_t *then;
} ast_while_stmt_t;
-typedef union ast_node
-{
+typedef union ast_node {
// inlined ast_node_meta_t struct.
- struct
- {
+ struct {
ast_node_kind_t kind;
token_loc_t loc;
};
diff --git a/src/checker.c b/src/checker.c
index 62d612f..1613c04 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -24,8 +24,7 @@ static void
populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast);
checker_t *
-checker_new(arena_t *arena)
-{
+checker_new(arena_t *arena) {
assert(arena);
checker_t *checker = (checker_t *)arena_alloc(arena, sizeof(checker_t));
@@ -38,8 +37,7 @@ checker_new(arena_t *arena)
}
static type_t
-type_from_id(string_view_t id)
-{
+type_from_id(string_view_t id) {
type_t type = { 0 };
type.id = id;
if (string_view_eq_to_cstr(id, "u8")) {
@@ -75,8 +73,7 @@ type_from_id(string_view_t id)
* transform unknown types into actual types
*/
static void
-type_resolve(type_t *type)
-{
+type_resolve(type_t *type) {
switch (type->kind) {
case TYPE_UNKNOWN:
*type = type_from_id(type->as_unknown.id);
@@ -89,8 +86,7 @@ type_resolve(type_t *type)
}
void
-checker_check(checker_t *checker, ast_node_t *ast)
-{
+checker_check(checker_t *checker, ast_node_t *ast) {
assert(checker);
assert(ast);
@@ -101,8 +97,7 @@ checker_check(checker_t *checker, ast_node_t *ast)
}
static void
-populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
-{
+populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast) {
switch (ast->kind) {
case AST_NODE_TRANSLATION_UNIT: {
list_item_t *item = list_head(ast->as_translation_unit.decls);
diff --git a/src/checker.h b/src/checker.h
index 681f405..ee517d9 100644
--- a/src/checker.h
+++ b/src/checker.h
@@ -20,8 +20,7 @@
#include "arena.h"
#include "ast.h"
-typedef struct checker
-{
+typedef struct checker {
arena_t *arena;
} checker_t;
diff --git a/src/cli.c b/src/cli.c
index 2faf6ce..d57945a 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -33,8 +33,7 @@ static void
cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args);
cli_opts_t
-cli_parse_args(int argc, char **argv)
-{
+cli_parse_args(int argc, char **argv) {
cli_args_t args = { .argc = argc, .argv = argv };
cli_opts_t opts = { 0 };
@@ -72,8 +71,7 @@ cli_parse_args(int argc, char **argv)
}
static char *
-cli_args_shift(cli_args_t *args)
-{
+cli_args_shift(cli_args_t *args) {
if (args->argc == 0)
return NULL;
--(args->argc);
@@ -81,8 +79,7 @@ cli_args_shift(cli_args_t *args)
}
static void
-cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args)
-{
+cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args) {
assert(opts && "opts is required");
assert(args && "args is required");
@@ -99,8 +96,7 @@ cli_opts_parse_output(cli_opts_t *opts, cli_args_t *args)
}
static void
-cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args)
-{
+cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args) {
assert(opts && "opts is required");
assert(args && "args is required");
@@ -121,8 +117,7 @@ cli_opts_parse_arch(cli_opts_t *opts, cli_args_t *args)
}
static void
-cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args)
-{
+cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args) {
assert(opts && "opts is required");
assert(args && "args is required");
@@ -139,8 +134,7 @@ cli_opts_parse_sysroot(cli_opts_t *opts, cli_args_t *args)
}
void
-cli_print_usage(FILE *stream, char *compiler_path)
-{
+cli_print_usage(FILE *stream, char *compiler_path) {
fprintf(
stream,
"Usage: %s [options] file...\n"
diff --git a/src/cli.h b/src/cli.h
index 1a93443..0a294a4 100644
--- a/src/cli.h
+++ b/src/cli.h
@@ -20,14 +20,12 @@
#include <stdint.h>
#include <stdio.h>
-typedef struct cli_args
-{
+typedef struct cli_args {
int argc;
char **argv;
} cli_args_t;
-typedef struct cli_opts
-{
+typedef struct cli_opts {
uint32_t options;
char *arch;
char *sysroot;
@@ -36,8 +34,7 @@ typedef struct cli_opts
string_view_t output_bin;
} cli_opts_t;
-typedef enum
-{
+typedef enum {
CLI_OPT_DUMP_TOKENS = 1 << 0,
CLI_OPT_OUTPUT = 1 << 1,
CLI_OPT_SAVE_TEMPS = 1 << 2,
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index d8187ab..e37bd36 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -42,8 +42,7 @@ static void
codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn);
void
-codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node)
-{
+codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node) {
codegen_linux_aarch64_emit_start_entrypoint(out);
assert(node->kind == AST_NODE_TRANSLATION_UNIT);
@@ -72,8 +71,7 @@ codegen_linux_aarch64_emit_translation_unit(FILE *out, ast_node_t *node)
}
static void
-codegen_linux_aarch64_emit_start_entrypoint(FILE *out)
-{
+codegen_linux_aarch64_emit_start_entrypoint(FILE *out) {
fprintf(out, ".text\n");
fprintf(out, ".globl _start\n\n");
@@ -84,8 +82,7 @@ codegen_linux_aarch64_emit_start_entrypoint(FILE *out)
}
static void
-codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
-{
+codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn) {
ast_node_t *block_node = fn->block;
assert(block_node->kind == AST_NODE_BLOCK);
ast_block_t block = block_node->as_block;
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 9a8d76e..c0b7326 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -33,8 +33,7 @@
#define bytes_max(a, b) ((a) > (b) ? (a) : (b))
-typedef enum x86_64_register_type
-{
+typedef enum x86_64_register_type {
REG_ACCUMULATOR,
REG_BASE,
REG_COUNTER,
@@ -82,8 +81,7 @@ static char *
get_reg_for(x86_64_register_type_t type, size_t bytes);
void
-codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
-{
+codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out) {
assert(codegen);
assert(arena);
assert(codegen);
@@ -94,8 +92,7 @@ codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
}
void
-codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *node)
-{
+codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t *node) {
codegen->label_index = 0;
codegen_linux_x86_64_emit_start_entrypoint(codegen);
@@ -125,8 +122,7 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen, ast_node_t
}
static void
-codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen)
-{
+codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen) {
fprintf(codegen->out, ".text\n");
fprintf(codegen->out, ".globl _start\n\n");
@@ -138,16 +134,14 @@ codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen)
}
static size_t
-codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen)
-{
+codegen_linux_x86_64_get_next_label(codegen_x86_64_t *codegen) {
return ++codegen->label_index;
}
typedef size_t size_in_bytes_t;
static size_in_bytes_t
-codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
-{
+codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node) {
switch (expr_node->kind) {
case AST_NODE_LITERAL: {
ast_literal_t literal_u32 = expr_node->as_literal;
@@ -577,8 +571,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
}
}
static void
-codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
-{
+codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block) {
size_t block_offset = codegen->base_offset;
size_t nodes_len = list_size(block->nodes);
@@ -686,8 +679,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
}
static void
-codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt)
-{
+codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt) {
ast_node_t *cond = if_stmt.cond;
ast_node_t *then = if_stmt.then;
ast_node_t *_else = if_stmt._else;
@@ -721,8 +713,7 @@ codegen_linux_x86_64_emit_if(codegen_x86_64_t *codegen, ast_if_stmt_t if_stmt)
}
static size_t
-type_to_bytes(type_t *type)
-{
+type_to_bytes(type_t *type) {
switch (type->kind) {
case TYPE_PRIMITIVE: {
return type->as_primitive.size;
@@ -742,8 +733,7 @@ type_to_bytes(type_t *type)
}
static size_t
-calculate_fn_local_size(scope_t *scope)
-{
+calculate_fn_local_size(scope_t *scope) {
assert(scope);
// The local_size starts with 8 bytes since the first 8 bytes from the
@@ -777,8 +767,7 @@ calculate_fn_local_size(scope_t *scope)
}
static void
-codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn_def)
-{
+codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn_def) {
codegen->base_offset = X86_CALL_EIP_STACK_OFFSET;
ast_node_t *block_node = fn_def->block;
@@ -826,8 +815,7 @@ 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)
-{
+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;
@@ -838,8 +826,7 @@ codegen_linux_x86_64_put_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbo
}
static size_t
-codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol)
-{
+codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbol) {
char symbol_ptr[PTR_HEX_CSTR_SIZE];
sprintf(symbol_ptr, "%lx", (uintptr_t)symbol);
@@ -847,8 +834,7 @@ codegen_linux_x86_64_get_stack_offset(codegen_x86_64_t *codegen, symbol_t *symbo
}
static char *
-get_reg_for(x86_64_register_type_t type, size_t bytes)
-{
+get_reg_for(x86_64_register_type_t type, size_t bytes) {
switch (type) {
case REG_ACCUMULATOR: {
if (bytes <= 1) {
diff --git a/src/codegen_linux_x86_64.h b/src/codegen_linux_x86_64.h
index a4137de..3c5a10e 100644
--- a/src/codegen_linux_x86_64.h
+++ b/src/codegen_linux_x86_64.h
@@ -22,8 +22,7 @@
#include "map.h"
#include <stdio.h>
-typedef struct codegen_x86_64
-{
+typedef struct codegen_x86_64 {
arena_t *arena;
size_t base_offset;
size_t label_index;
diff --git a/src/lexer.c b/src/lexer.c
index 9e012bd..352f979 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -22,8 +22,7 @@
#include <stdio.h>
void
-lexer_init(lexer_t *lexer, source_code_t src)
-{
+lexer_init(lexer_t *lexer, source_code_t src) {
assert(lexer);
lexer->src = src;
lexer->cur.offset = 0;
@@ -59,8 +58,7 @@ static token_kind_t
lexer_str_to_token_kind(string_view_t text);
void
-lexer_next_token(lexer_t *lexer, token_t *token)
-{
+lexer_next_token(lexer_t *lexer, token_t *token) {
if (lexer_is_eof(lexer)) {
lexer_init_eof_token(lexer, token);
return;
@@ -329,15 +327,13 @@ static char *token_kind_str_table[] = {
};
char *
-token_kind_to_cstr(token_kind_t kind)
-{
+token_kind_to_cstr(token_kind_t kind) {
assert(kind < sizeof(token_kind_str_table));
return token_kind_str_table[kind];
}
bool
-token_kind_is_binary_op(token_kind_t kind)
-{
+token_kind_is_binary_op(token_kind_t kind) {
switch (kind) {
case TOKEN_PLUS:
case TOKEN_DASH:
@@ -364,14 +360,12 @@ token_kind_is_binary_op(token_kind_t kind)
}
static char
-lexer_current_char(lexer_t *lexer)
-{
+lexer_current_char(lexer_t *lexer) {
return lexer->src.code.chars[lexer->cur.offset];
}
static void
-lexer_skip_char(lexer_t *lexer)
-{
+lexer_skip_char(lexer_t *lexer) {
assert(lexer->cur.offset < lexer->src.code.size);
if (lexer_current_char(lexer) == '\n') {
lexer->cur.row++;
@@ -382,47 +376,40 @@ lexer_skip_char(lexer_t *lexer)
}
static bool
-lexer_is_eof(lexer_t *lexer)
-{
+lexer_is_eof(lexer_t *lexer) {
return lexer->cur.offset >= lexer->src.code.size;
}
static bool
-lexer_is_not_eof(lexer_t *lexer)
-{
+lexer_is_not_eof(lexer_t *lexer) {
return !lexer_is_eof(lexer);
}
static bool
-_isspace(char c)
-{
+_isspace(char c) {
return c != '\n' && isspace(c);
}
static void
-lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind)
-{
+lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind) {
string_view_t str = { .chars = lexer->src.code.chars + lexer->cur.offset, .size = 1 };
*token = (token_t){ .kind = kind, .value = str, .loc = (token_loc_t){ .src = lexer->src, .cur = lexer->cur } };
}
static void
-lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, lexer_cursor_t cur)
-{
+lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, lexer_cursor_t cur) {
string_view_t str = { .chars = lexer->src.code.chars + cur.offset, .size = lexer->cur.offset - cur.offset };
*token = (token_t){ .kind = kind, .value = str, .loc = (token_loc_t){ .src = lexer->src, .cur = cur } };
}
static void
-lexer_init_eof_token(lexer_t *lexer, token_t *token)
-{
+lexer_init_eof_token(lexer_t *lexer, token_t *token) {
string_view_t str = { 0 };
*token = (token_t){ .kind = TOKEN_EOF, .value = str, .loc = (token_loc_t){ .src = lexer->src, .cur = lexer->cur } };
}
static token_kind_t
-lexer_str_to_token_kind(string_view_t text)
-{
+lexer_str_to_token_kind(string_view_t text) {
if (string_view_eq_to_cstr(text, "if")) {
return TOKEN_IF;
}
@@ -451,14 +438,12 @@ lexer_str_to_token_kind(string_view_t text)
}
void
-lexer_peek_next(lexer_t *lexer, token_t *token)
-{
+lexer_peek_next(lexer_t *lexer, token_t *token) {
lexer_lookahead(lexer, token, 1);
}
void
-lexer_lookahead(lexer_t *lexer, token_t *token, size_t n)
-{
+lexer_lookahead(lexer_t *lexer, token_t *token, size_t n) {
lexer_cursor_t previous_cur = lexer->cur;
for (size_t i = 0; i < n; ++i) {
@@ -469,8 +454,7 @@ lexer_lookahead(lexer_t *lexer, token_t *token, size_t n)
}
string_view_t
-token_loc_to_line(token_loc_t loc)
-{
+token_loc_to_line(token_loc_t loc) {
size_t offset = loc.cur.bol;
string_view_t line = { .chars = loc.src.code.chars + offset, .size = 0 };
@@ -482,13 +466,11 @@ token_loc_to_line(token_loc_t loc)
}
size_t
-token_loc_to_lineno(token_loc_t loc)
-{
+token_loc_to_lineno(token_loc_t loc) {
return loc.cur.row + 1;
}
size_t
-token_loc_to_colno(token_loc_t loc)
-{
+token_loc_to_colno(token_loc_t loc) {
return loc.cur.offset - loc.cur.bol + 1;
}
diff --git a/src/lexer.h b/src/lexer.h
index 774f619..beb5486 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -21,27 +21,23 @@
#include <stdint.h>
#include <stdio.h>
-typedef struct source_code
-{
+typedef struct source_code {
char *filepath;
string_view_t code;
} source_code_t;
-typedef struct lexer_cursor
-{
+typedef struct lexer_cursor {
size_t offset;
size_t row;
size_t bol;
} lexer_cursor_t;
-typedef struct lexer
-{
+typedef struct lexer {
source_code_t src;
lexer_cursor_t cur;
} lexer_t;
-typedef enum token_kind
-{
+typedef enum token_kind {
TOKEN_UNKNOWN,
TOKEN_ID,
TOKEN_NUMBER,
@@ -92,14 +88,12 @@ typedef enum token_kind
TOKEN_EOF
} token_kind_t;
-typedef struct token_loc
-{
+typedef struct token_loc {
source_code_t src;
lexer_cursor_t cur;
} token_loc_t;
-typedef struct token
-{
+typedef struct token {
token_kind_t kind;
string_view_t value;
token_loc_t loc;
diff --git a/src/list.c b/src/list.c
index a3dd3fe..fcdaa4f 100644
--- a/src/list.c
+++ b/src/list.c
@@ -18,8 +18,7 @@
#include <assert.h>
void
-list_init(list_t *list, arena_t *arena)
-{
+list_init(list_t *list, arena_t *arena) {
assert(list != NULL);
list->size = 0;
list->arena = arena;
@@ -27,8 +26,7 @@ list_init(list_t *list, arena_t *arena)
}
void
-list_append(list_t *list, void *value)
-{
+list_append(list_t *list, void *value) {
assert(list != NULL);
list_item_t *item = arena_alloc(list->arena, sizeof(list_item_t));
item->value = value;
@@ -46,8 +44,7 @@ list_append(list_t *list, void *value)
}
list_item_t *
-list_get(list_t *list, size_t index)
-{
+list_get(list_t *list, size_t index) {
assert(list != NULL);
assert(index < list->size);
@@ -63,22 +60,19 @@ list_get(list_t *list, size_t index)
}
list_item_t *
-list_head(list_t *list)
-{
+list_head(list_t *list) {
assert(list != NULL);
return list->head;
}
list_item_t *
-list_next(list_item_t *item)
-{
+list_next(list_item_t *item) {
assert(item != NULL);
return item->next;
}
size_t
-list_size(list_t *list)
-{
+list_size(list_t *list) {
assert(list != NULL);
return list->size;
}
diff --git a/src/list.h b/src/list.h
index 901d27e..3b709d2 100644
--- a/src/list.h
+++ b/src/list.h
@@ -18,14 +18,12 @@
#define LIST_H
#include "arena.h"
-typedef struct list_item
-{
+typedef struct list_item {
void *value;
struct list_item *next;
} list_item_t;
-typedef struct list
-{
+typedef struct list {
size_t size;
arena_t *arena;
list_item_t *head;
diff --git a/src/main.c b/src/main.c
index d7738bd..bb0e451 100644
--- a/src/main.c
+++ b/src/main.c
@@ -50,8 +50,7 @@ source_code_t
read_entire_file(char *filepath, arena_t *arena);
int
-main(int argc, char **argv)
-{
+main(int argc, char **argv) {
cli_opts_t opts = cli_parse_args(argc, argv);
if (opts.options & CLI_OPT_DUMP_TOKENS) {
@@ -73,8 +72,7 @@ main(int argc, char **argv)
}
void
-handle_dump_tokens(cli_opts_t *opts)
-{
+handle_dump_tokens(cli_opts_t *opts) {
if (opts->filepath == NULL) {
cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
@@ -98,8 +96,7 @@ handle_dump_tokens(cli_opts_t *opts)
}
void
-handle_dump_ast(cli_opts_t *opts)
-{
+handle_dump_ast(cli_opts_t *opts) {
if (opts->filepath == NULL) {
cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
@@ -120,8 +117,7 @@ handle_dump_ast(cli_opts_t *opts)
}
void
-handle_codegen_linux(cli_opts_t *opts)
-{
+handle_codegen_linux(cli_opts_t *opts) {
if (opts->filepath == NULL) {
cli_print_usage(stderr, opts->compiler_path);
exit(EXIT_FAILURE);
@@ -207,8 +203,7 @@ handle_codegen_linux(cli_opts_t *opts)
}
source_code_t
-read_entire_file(char *filepath, arena_t *arena)
-{
+read_entire_file(char *filepath, arena_t *arena) {
FILE *stream = fopen(filepath, "rb");
if (stream == NULL) {
@@ -244,8 +239,7 @@ read_entire_file(char *filepath, arena_t *arena)
}
static void
-print_token(token_t *token)
-{
+print_token(token_t *token) {
printf(
"%s:%lu:%lu: <%s>\n",
token->loc.src.filepath,
diff --git a/src/map.c b/src/map.c
index a6bc3a3..9109eaa 100644
--- a/src/map.c
+++ b/src/map.c
@@ -38,8 +38,7 @@ static uint32_t
map_get_index(map_t *map, uint32_t hash);
map_t *
-map_new(arena_t *arena)
-{
+map_new(arena_t *arena) {
map_t *map = (map_t *)arena_alloc(arena, sizeof(map_t));
if (map == NULL) {
fprintf(stderr, "[FATAL] Out of memory: map_new: %s\n", strerror(errno));
@@ -51,8 +50,7 @@ map_new(arena_t *arena)
}
static void
-map_init(map_t *map)
-{
+map_init(map_t *map) {
assert(map);
map->entries = (map_entry_t *)arena_alloc(map->arena, MAP_INITIAL_CAPACITY * sizeof(map_entry_t));
assert(map->entries != NULL);
@@ -65,8 +63,7 @@ map_init(map_t *map)
}
static uint32_t
-u32_fnv1a_hash(const char *s)
-{
+u32_fnv1a_hash(const char *s) {
uint32_t hash = U32_FNV1A_OFFSET_BASIS;
size_t len = strlen(s);
for (size_t i = 0; i < len; ++i) {
@@ -77,8 +74,7 @@ u32_fnv1a_hash(const char *s)
}
bool
-map_put(map_t *map, char *key, void *value)
-{
+map_put(map_t *map, char *key, void *value) {
assert(map && key);
map->size++;
@@ -108,8 +104,7 @@ map_put(map_t *map, char *key, void *value)
}
void *
-map_get(map_t *map, char *key)
-{
+map_get(map_t *map, char *key) {
uint32_t hash = u32_fnv1a_hash(key);
map_entry_t *entry = map->entries + map_get_index(map, hash);
while (entry != NULL) {
@@ -122,15 +117,13 @@ map_get(map_t *map, char *key)
}
static uint32_t
-map_get_index(map_t *map, uint32_t hash)
-{
+map_get_index(map_t *map, uint32_t hash) {
uint32_t capacity_mask = map->capacity - 1;
return hash & capacity_mask;
}
void
-map_get_kvs(map_t *map, map_kv_t **kvs)
-{
+map_get_kvs(map_t *map, map_kv_t **kvs) {
size_t index = 0;
for (size_t j = 0; j < map->capacity; ++j) {
@@ -144,8 +137,7 @@ map_get_kvs(map_t *map, map_kv_t **kvs)
}
static char *
-_strdup(const char *s, arena_t *arena)
-{
+_strdup(const char *s, arena_t *arena) {
size_t slen = strlen(s);
char *result = arena_alloc(arena, slen + 1);
if (result == NULL) {
diff --git a/src/map.h b/src/map.h
index 6f8681c..fd26ea2 100644
--- a/src/map.h
+++ b/src/map.h
@@ -32,22 +32,19 @@ typedef struct map map_t;
typedef struct map_bucket map_bucket_t;
typedef struct map_entry map_entry_t;
-typedef struct map
-{
+typedef struct map {
arena_t *arena;
map_entry_t *entries;
size_t capacity;
size_t size;
} map_t;
-typedef struct map_kv
-{
+typedef struct map_kv {
char *key;
void *value;
} map_kv_t;
-typedef struct map_entry
-{
+typedef struct map_entry {
char *key;
void *value;
uint32_t hash;
diff --git a/src/parser.c b/src/parser.c
index 9d3d6e0..385d1a5 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -77,8 +77,7 @@ static void
peek_next_non_lf_token(lexer_t *lexer, token_t *token);
void
-parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena)
-{
+parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena) {
assert(parser && "parser is required");
assert(lexer && "lexer is required");
parser->lexer = lexer;
@@ -86,8 +85,7 @@ parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena)
}
ast_node_t *
-parser_parse_translation_unit(parser_t *parser)
-{
+parser_parse_translation_unit(parser_t *parser) {
token_t token;
ast_node_t *translation_unit_node = ast_new_translation_unit(parser->arena);
@@ -111,8 +109,7 @@ parser_parse_translation_unit(parser_t *parser)
}
static ast_binary_op_kind_t
-token_kind_to_binary_op_kind(token_kind_t kind)
-{
+token_kind_to_binary_op_kind(token_kind_t kind) {
switch (kind) {
case TOKEN_PLUS:
return AST_BINOP_ADDITION;
@@ -157,8 +154,7 @@ token_kind_to_binary_op_kind(token_kind_t kind)
}
}
-typedef enum
-{
+typedef enum {
BINOP_MIN_PREC,
BINOP_LOGICAL_OR_PREC,
BINOP_LOGICAL_AND_PREC,
@@ -173,8 +169,7 @@ typedef enum
} binary_op_precedence_t;
static binary_op_precedence_t
-get_binary_op_precedence(token_kind_t kind)
-{
+get_binary_op_precedence(token_kind_t kind) {
switch (kind) {
case TOKEN_PLUS:
case TOKEN_DASH:
@@ -210,8 +205,7 @@ get_binary_op_precedence(token_kind_t kind)
}
static ast_unary_op_kind_t
-token_kind_to_unary_op_kind(token_kind_t token_kind)
-{
+token_kind_to_unary_op_kind(token_kind_t token_kind) {
switch (token_kind) {
case TOKEN_AND:
return AST_UNARY_ADDRESSOF;
@@ -231,8 +225,7 @@ token_kind_to_unary_op_kind(token_kind_t token_kind)
}
static ast_node_t *
-parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
-{
+parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence) {
token_t lookahead_token;
lexer_peek_next(parser->lexer, &lookahead_token);
@@ -264,8 +257,7 @@ parser_parse_expr_1(parser_t *parser, ast_node_t *lhs, size_t prev_precedence)
}
static ast_node_t *
-parser_parse_expr(parser_t *parser)
-{
+parser_parse_expr(parser_t *parser) {
ast_node_t *lhs = parser_parse_factor(parser);
if (lhs == NULL) {
return NULL;
@@ -275,8 +267,7 @@ parser_parse_expr(parser_t *parser)
}
static ast_node_t *
-parser_parse_factor(parser_t *parser)
-{
+parser_parse_factor(parser_t *parser) {
token_t token;
lexer_next_token(parser->lexer, &token);
@@ -331,8 +322,7 @@ parser_parse_factor(parser_t *parser)
}
static list_t *
-parser_parse_fn_args(parser_t *parser)
-{
+parser_parse_fn_args(parser_t *parser) {
if (!skip_expected_token(parser, TOKEN_OPAREN)) {
return NULL;
}
@@ -373,8 +363,7 @@ parser_parse_fn_args(parser_t *parser)
}
static list_t *
-parser_parse_fn_params(parser_t *parser)
-{
+parser_parse_fn_params(parser_t *parser) {
if (!skip_expected_token(parser, TOKEN_OPAREN)) {
return NULL;
}
@@ -425,8 +414,7 @@ parser_parse_fn_params(parser_t *parser)
}
ast_node_t *
-parser_parse_fn_definition(parser_t *parser)
-{
+parser_parse_fn_definition(parser_t *parser) {
if (!skip_expected_token(parser, TOKEN_FN)) {
return NULL;
}
@@ -463,8 +451,7 @@ parser_parse_fn_definition(parser_t *parser)
}
static type_t *
-parser_parse_type(parser_t *parser)
-{
+parser_parse_type(parser_t *parser) {
skip_line_feeds(parser->lexer);
if (!skip_expected_token(parser, TOKEN_COLON)) {
@@ -500,8 +487,7 @@ parser_parse_type(parser_t *parser)
}
static ast_node_t *
-parser_parse_block(parser_t *parser)
-{
+parser_parse_block(parser_t *parser) {
if (!skip_expected_token(parser, TOKEN_OCURLY)) {
return NULL;
}
@@ -576,8 +562,7 @@ EndLoop:
}
static ast_node_t *
-parser_parse_return_stmt(parser_t *parser)
-{
+parser_parse_return_stmt(parser_t *parser) {
token_t token_ret;
if (!expected_next_token(parser, &token_ret, TOKEN_RETURN)) {
@@ -596,8 +581,7 @@ parser_parse_return_stmt(parser_t *parser)
}
static ast_node_t *
-parser_parse_if_stmt(parser_t *parser)
-{
+parser_parse_if_stmt(parser_t *parser) {
token_t token_if;
if (!expected_next_token(parser, &token_if, TOKEN_IF)) {
return NULL;
@@ -648,8 +632,7 @@ parser_parse_if_stmt(parser_t *parser)
}
static ast_node_t *
-parser_parse_while_stmt(parser_t *parser)
-{
+parser_parse_while_stmt(parser_t *parser) {
token_t token_while;
if (!expected_next_token(parser, &token_while, TOKEN_WHILE)) {
return NULL;
@@ -680,8 +663,7 @@ parser_parse_while_stmt(parser_t *parser)
}
static ast_node_t *
-parser_parse_var_def(parser_t *parser)
-{
+parser_parse_var_def(parser_t *parser) {
if (!skip_expected_token(parser, TOKEN_VAR)) {
return NULL;
}
@@ -712,8 +694,7 @@ parser_parse_var_def(parser_t *parser)
}
static ast_node_t *
-parser_parse_var_assign_stmt(parser_t *parser)
-{
+parser_parse_var_assign_stmt(parser_t *parser) {
token_t token_id;
if (!expected_next_token(parser, &token_id, TOKEN_ID)) {
@@ -733,22 +714,19 @@ parser_parse_var_assign_stmt(parser_t *parser)
}
static bool
-skip_expected_token(parser_t *parser, token_kind_t expected_kind)
-{
+skip_expected_token(parser_t *parser, token_kind_t expected_kind) {
token_t token;
return expected_next_token(parser, &token, expected_kind);
}
static bool
-expected_next_token(parser_t *parser, token_t *token, token_kind_t expected_kind)
-{
+expected_next_token(parser_t *parser, token_t *token, token_kind_t expected_kind) {
lexer_next_token(parser->lexer, token);
return expected_token(token, expected_kind);
}
static bool
-expected_token(token_t *token, token_kind_t expected_kind)
-{
+expected_token(token_t *token, token_kind_t expected_kind) {
if (token->kind != expected_kind) {
fprintf(
stderr,
@@ -769,8 +747,7 @@ expected_token(token_t *token, token_kind_t expected_kind)
}
static void
-skip_line_feeds(lexer_t *lexer)
-{
+skip_line_feeds(lexer_t *lexer) {
token_t token;
lexer_peek_next(lexer, &token);
@@ -781,8 +758,7 @@ skip_line_feeds(lexer_t *lexer)
}
static void
-peek_next_non_lf_token(lexer_t *lexer, token_t *token)
-{
+peek_next_non_lf_token(lexer_t *lexer, token_t *token) {
lexer_cursor_t cur = lexer->cur;
skip_line_feeds(lexer);
diff --git a/src/parser.h b/src/parser.h
index 7db2b74..7726788 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -21,8 +21,7 @@
#include "ast.h"
#include "lexer.h"
-typedef struct parser
-{
+typedef struct parser {
lexer_t *lexer;
arena_t *arena;
} parser_t;
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index f5cd571..38199ed 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -31,23 +31,20 @@
#define PP_IS_BIT_SET(data, index) ((data) & 1 << index)
// clang-format on
-typedef struct pretty_print_node
-{
+typedef struct pretty_print_node {
char *name;
list_t *children;
} pretty_print_node_t;
static bool
-stdout_supports_color()
-{
+stdout_supports_color() {
struct stat st;
fstat(STDOUT_FILENO, &st);
return S_ISCHR(st.st_mode);
}
static void
-pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children)
-{
+pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children) {
assert(level < 64);
bool support_color = stdout_supports_color();
@@ -79,8 +76,7 @@ pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children)
}
static void
-pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, bool lst_children)
-{
+pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, bool lst_children) {
pretty_print_print_ident(prefix, level, lst_children);
list_t *list = node->children;
@@ -99,8 +95,7 @@ pretty_print_tree(pretty_print_node_t *node, uint64_t *prefix, size_t level, boo
}
static pretty_print_node_t *
-pretty_print_node_new(arena_t *arena)
-{
+pretty_print_node_new(arena_t *arena) {
pretty_print_node_t *node = (pretty_print_node_t *)arena_alloc(arena, sizeof(pretty_print_node_t));
node->children = (list_t *)arena_alloc(arena, sizeof(list_t));
list_init(node->children, arena);
@@ -108,8 +103,7 @@ pretty_print_node_new(arena_t *arena)
}
static pretty_print_node_t *
-pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena)
-{
+pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena) {
pretty_print_node_t *node = pretty_print_node_new(arena);
char name[256];
sprintf(name, "Param_Definition <name:" SV_FMT "> <type:" SV_FMT ">", SV_ARG(param->id), SV_ARG(param->type->id));
@@ -119,8 +113,7 @@ pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena)
}
static pretty_print_node_t *
-ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
-{
+ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena) {
switch (ast->kind) {
case AST_NODE_TRANSLATION_UNIT: {
pretty_print_node_t *node = pretty_print_node_new(arena);
@@ -413,8 +406,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
}
void
-pretty_print_ast(ast_node_t *ast)
-{
+pretty_print_ast(ast_node_t *ast) {
arena_t arena = arena_new(8 * 1024);
pretty_print_node_t *root = ast_node_to_pretty_print_node(ast, &arena);
uint64_t prefix = 0;
diff --git a/src/scope.c b/src/scope.c
index ff9067c..0cc4a74 100644
--- a/src/scope.c
+++ b/src/scope.c
@@ -23,8 +23,7 @@
#include "scope.h"
scope_t *
-scope_new(arena_t *arena)
-{
+scope_new(arena_t *arena) {
assert(arena);
scope_t *scope = (scope_t *)arena_alloc(arena, sizeof(scope_t));
if (scope == NULL) {
@@ -49,8 +48,7 @@ scope_new(arena_t *arena)
}
symbol_t *
-symbol_new(arena_t *arena, string_view_t id, type_t *type)
-{
+symbol_new(arena_t *arena, string_view_t id, type_t *type) {
assert(arena);
symbol_t *symbol = (symbol_t *)arena_alloc(arena, sizeof(symbol_t));
if (symbol == NULL) {
@@ -63,8 +61,7 @@ symbol_new(arena_t *arena, string_view_t id, type_t *type)
}
symbol_t *
-scope_lookup(scope_t *scope, string_view_t id)
-{
+scope_lookup(scope_t *scope, string_view_t id) {
assert(scope);
while (scope != NULL) {
char cstr_id[id.size + 1];
@@ -81,8 +78,7 @@ scope_lookup(scope_t *scope, string_view_t id)
}
void
-scope_insert(scope_t *scope, symbol_t *symbol)
-{
+scope_insert(scope_t *scope, symbol_t *symbol) {
assert(scope);
assert(symbol);
@@ -94,8 +90,7 @@ scope_insert(scope_t *scope, symbol_t *symbol)
}
scope_t *
-scope_push(scope_t *scope)
-{
+scope_push(scope_t *scope) {
assert(scope);
scope_t *child = scope_new(scope->arena);
@@ -107,8 +102,7 @@ scope_push(scope_t *scope)
}
scope_t *
-scope_pop(scope_t *scope)
-{
+scope_pop(scope_t *scope) {
assert(scope);
assert(scope->parent);
return scope->parent;
diff --git a/src/scope.h b/src/scope.h
index 08eb681..ed08837 100644
--- a/src/scope.h
+++ b/src/scope.h
@@ -23,14 +23,12 @@
#include "string_view.h"
#include "type.h"
-typedef struct symbol
-{
+typedef struct symbol {
string_view_t id;
type_t *type;
} symbol_t;
-typedef struct scope
-{
+typedef struct scope {
struct scope *parent;
list_t *children;
arena_t *arena;
diff --git a/src/string_view.c b/src/string_view.c
index b4d4103..22cb61e 100644
--- a/src/string_view.c
+++ b/src/string_view.c
@@ -22,14 +22,12 @@
#include <string.h>
string_view_t
-string_view_from_cstr(char *cstr)
-{
+string_view_from_cstr(char *cstr) {
return (string_view_t){ .chars = cstr, .size = strlen(cstr) };
}
bool
-string_view_eq_to_cstr(string_view_t str, char *cstr)
-{
+string_view_eq_to_cstr(string_view_t str, char *cstr) {
size_t cstr_len = strlen(cstr);
if (str.size != cstr_len) {
return false;
@@ -43,8 +41,7 @@ string_view_eq_to_cstr(string_view_t str, char *cstr)
}
uint32_t
-string_view_to_u32(string_view_t str)
-{
+string_view_to_u32(string_view_t str) {
char ret[str.size + 1];
ret[str.size] = 0;
memcpy(ret, str.chars, str.size);
diff --git a/src/string_view.h b/src/string_view.h
index 0a3654f..fdb6148 100644
--- a/src/string_view.h
+++ b/src/string_view.h
@@ -24,8 +24,7 @@
#define SV_FMT "%.*s"
#define SV_ARG(sv) (int)(sv).size, (sv).chars
-typedef struct string_view
-{
+typedef struct string_view {
char *chars;
size_t size;
diff --git a/src/type.c b/src/type.c
index 4a8d6f4..185f0fa 100644
--- a/src/type.c
+++ b/src/type.c
@@ -18,8 +18,7 @@
#include "assert.h"
type_t *
-type_new_unknown(arena_t *arena, string_view_t id)
-{
+type_new_unknown(arena_t *arena, string_view_t id) {
type_t *type = arena_alloc(arena, sizeof(type_t));
assert(type);
@@ -29,8 +28,7 @@ type_new_unknown(arena_t *arena, string_view_t id)
}
type_t *
-type_new_ptr(arena_t *arena, string_view_t id, type_t *ref_type)
-{
+type_new_ptr(arena_t *arena, string_view_t id, type_t *ref_type) {
type_t *type = arena_alloc(arena, sizeof(type_t));
assert(type);
diff --git a/src/type.h b/src/type.h
index d930a88..edc421b 100644
--- a/src/type.h
+++ b/src/type.h
@@ -21,46 +21,30 @@
typedef union type type_t;
-typedef enum
-{
- TYPE_UNKNOWN,
- TYPE_PRIMITIVE,
- TYPE_PTR
-} type_kind_t;
+typedef enum { TYPE_UNKNOWN, TYPE_PRIMITIVE, TYPE_PTR } type_kind_t;
-typedef enum
-{
- TYPE_U8,
- TYPE_U16,
- TYPE_U32,
- TYPE_U64
-} type_primitive_kind_t;
+typedef enum { TYPE_U8, TYPE_U16, TYPE_U32, TYPE_U64 } type_primitive_kind_t;
-typedef struct type_primitive
-{
+typedef struct type_primitive {
type_kind_t _type_kind;
string_view_t id;
type_primitive_kind_t kind;
short size;
} type_primitive_t;
-typedef struct type_unknown
-{
+typedef struct type_unknown {
type_kind_t _type_kind;
string_view_t id;
} type_unknown_t;
-typedef struct type_ptr
-{
+typedef struct type_ptr {
type_kind_t _type_kind;
string_view_t id;
type_t *type;
} type_ptr_t;
-typedef union type
-{
- struct
- {
+typedef union type {
+ struct {
type_kind_t kind;
string_view_t id;
};
diff --git a/tests/unit/arena_test.c b/tests/unit/arena_test.c
index a471572..81553c9 100644
--- a/tests/unit/arena_test.c
+++ b/tests/unit/arena_test.c
@@ -19,8 +19,7 @@
#include "munit.h"
static MunitResult
-arena_alloc_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+arena_alloc_test(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(ARENA_ALIGNMENT_BYTES * 2);
uint8_t *a = arena_alloc(&arena, sizeof(uint8_t));
@@ -50,8 +49,7 @@ arena_alloc_test(const MunitParameter params[], void *user_data_or_fixture)
}
static MunitResult
-arena_padding_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+arena_padding_test(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(512);
// Allocated bytes is < ARENA_ALIGNMENT_BYTES
@@ -91,8 +89,7 @@ static MunitTest tests[] = { { "/arena_alloc_test", arena_alloc_test, NULL, NULL
static const MunitSuite suite = { "/arena", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
-main(int argc, char *argv[])
-{
+main(int argc, char *argv[]) {
return munit_suite_main(&suite, NULL, argc, argv);
return EXIT_SUCCESS;
}
diff --git a/tests/unit/list_test.c b/tests/unit/list_test.c
index 8b759f9..fe19a23 100644
--- a/tests/unit/list_test.c
+++ b/tests/unit/list_test.c
@@ -21,8 +21,7 @@
#include <stdio.h>
static MunitResult
-list_append_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+list_append_test(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(sizeof(list_item_t));
list_t list;
@@ -41,8 +40,7 @@ list_append_test(const MunitParameter params[], void *user_data_or_fixture)
}
static MunitResult
-list_get_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+list_get_test(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(sizeof(list_item_t) * 3);
list_t list;
@@ -66,8 +64,7 @@ list_get_test(const MunitParameter params[], void *user_data_or_fixture)
}
static MunitResult
-list_next_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+list_next_test(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(sizeof(list_item_t) * 3);
list_t list;
@@ -103,8 +100,7 @@ static MunitTest tests[] = { { "/list_append_test", list_append_test, NULL, NULL
static const MunitSuite suite = { "/list", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
-main(int argc, char *argv[])
-{
+main(int argc, char *argv[]) {
return munit_suite_main(&suite, NULL, argc, argv);
return EXIT_SUCCESS;
}
diff --git a/tests/unit/map_test.c b/tests/unit/map_test.c
index 9497c7c..15d82ef 100644
--- a/tests/unit/map_test.c
+++ b/tests/unit/map_test.c
@@ -24,8 +24,7 @@
#define MAP_TEST_ARENA_CAPACITY (1024 * 16)
static MunitResult
-test_create_new(const MunitParameter params[], void *user_data_or_fixture)
-{
+test_create_new(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(MAP_TEST_ARENA_CAPACITY);
map_t *map = map_new(&arena);
@@ -38,8 +37,7 @@ test_create_new(const MunitParameter params[], void *user_data_or_fixture)
}
static MunitResult
-test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
-{
+test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(MAP_TEST_ARENA_CAPACITY);
map_t *map = map_new(&arena);
@@ -63,8 +61,7 @@ test_map_put_and_get(const MunitParameter params[], void *user_data_or_fixture)
}
static MunitResult
-test_map_get_kvs(const MunitParameter params[], void *user_data_or_fixture)
-{
+test_map_get_kvs(const MunitParameter params[], void *user_data_or_fixture) {
arena_t arena = arena_new(MAP_TEST_ARENA_CAPACITY);
map_t *map = map_new(&arena);
@@ -100,8 +97,7 @@ static MunitTest tests[] = {
static const MunitSuite suite = { "/map", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
-main(int argc, char *argv[])
-{
+main(int argc, char *argv[]) {
return munit_suite_main(&suite, NULL, argc, argv);
return EXIT_SUCCESS;
}
diff --git a/tests/unit/string_view_test.c b/tests/unit/string_view_test.c
index 7a6776c..f8ca792 100644
--- a/tests/unit/string_view_test.c
+++ b/tests/unit/string_view_test.c
@@ -22,8 +22,7 @@
#include <string.h>
static MunitResult
-string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fixture) {
char *name = "John Doe";
string_view_t str = { .chars = name, .size = strlen(name) };
@@ -40,8 +39,7 @@ string_view_eq_to_cstr_test(const MunitParameter params[], void *user_data_or_fi
}
static MunitResult
-string_view_to_u32_test(const MunitParameter params[], void *user_data_or_fixture)
-{
+string_view_to_u32_test(const MunitParameter params[], void *user_data_or_fixture) {
char *number = "69";
string_view_t str = { .chars = number, .size = strlen(number) };
@@ -64,8 +62,7 @@ static MunitTest tests[] = {
static const MunitSuite suite = { "/string_view", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
-main(int argc, char *argv[])
-{
+main(int argc, char *argv[]) {
return munit_suite_main(&suite, NULL, argc, argv);
return EXIT_SUCCESS;
}
--
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 ` [PATCH olang v1 1/6] codestyle: change AlignAfterOpenBracket to BlockIndent Carlos Maniero
2024-10-10 1:33 ` Carlos Maniero [this message]
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-3-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