summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.c87
-rw-r--r--src/ast.h117
-rw-r--r--src/gas_assembly_generator.c70
-rw-r--r--src/gas_assembly_generator.h40
-rw-r--r--src/lexer.c50
-rw-r--r--src/lexer.h116
-rw-r--r--src/parser.c126
-rw-r--r--src/parser.h48
-rw-r--r--src/pipac.c57
-rw-r--r--src/string_view.c35
-rw-r--r--src/string_view.h47
-rw-r--r--src/vector.c38
-rw-r--r--src/vector.h51
13 files changed, 444 insertions, 438 deletions
diff --git a/src/ast.c b/src/ast.c
index 645a2dd..10bb79d 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -1,23 +1,23 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "ast.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
-#include "ast.h"
void
ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor)
@@ -55,10 +55,10 @@ ast_node_binary_operation_visitor(ast_node_t *node, ast_visitor_t *visitor)
visitor->visit_binary_operation(visitor, &node->data.binary_operation);
}
-ast_node_t*
+ast_node_t *
ast_node_new()
{
- ast_node_t *node = (ast_node_t*) malloc(sizeof(ast_node_t));
+ ast_node_t *node = (ast_node_t *)malloc(sizeof(ast_node_t));
if (node == NULL) {
printf("OOO: could no allocate a node");
exit(EXIT_FAILURE);
@@ -70,7 +70,7 @@ ast_node_new()
static void
ast_node_destroy_vector(vector_t *vector)
{
- for (size_t i=0; i < vector->size; i++) {
+ for (size_t i = 0; i < vector->size; i++) {
ast_node_destroy(vector_at(vector, i));
}
vector_destroy(vector);
@@ -108,27 +108,17 @@ ast_node_destroy(ast_node_t *node)
void
ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument)
{
- node->accept_visitor = &ast_node_return_stmt_accept_visitor,
+ node->accept_visitor = &ast_node_return_stmt_accept_visitor;
node->kind = AST_RETURN_STMT;
- node->data = (ast_node_data_t) {
- .return_stmt = {
- .argument = argument
- }
- };
+ node->data.return_stmt = (ast_return_stmt_t){ .argument = argument };
}
void
ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, vector_t *body)
{
- node->accept_visitor = &ast_node_function_accept_visitor,
+ node->accept_visitor = &ast_node_function_accept_visitor;
node->kind = AST_FUNCTION_DECLARATION;
- node->data = (ast_node_data_t) {
- .function = {
- .name = name,
- .return_type = return_type,
- .body = body
- }
- };
+ node->data = (ast_node_data_t){ .function = { .name = name, .return_type = return_type, .body = body } };
}
void
@@ -136,14 +126,7 @@ ast_literal_integer_create(ast_node_t *node, uint32_t number)
{
node->accept_visitor = &ast_node_literal_visitor;
node->kind = AST_LITERAL;
- node->data = (ast_node_data_t) {
- .literal = {
- .kind = AST_LITERAL_INTEGER,
- .value = {
- .integer = number
- }
- }
- };
+ node->data.literal = (ast_literal_t){ .kind = AST_LITERAL_INTEGER, .value = { .integer = number } };
}
void
@@ -151,13 +134,7 @@ ast_node_init_binary_operation(ast_node_t *node, string_view_t op, ast_node_t *l
{
node->accept_visitor = &ast_node_binary_operation_visitor;
node->kind = AST_BINARY_OPERATION;
- node->data = (ast_node_data_t) {
- .binary_operation = {
- .op = op,
- .left = left,
- .right = right
- }
- };
+ node->data = (ast_node_data_t){ .binary_operation = { .op = op, .left = left, .right = right } };
}
void
@@ -165,13 +142,7 @@ ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name
{
// FIXME: define the visitor strategy
node->kind = AST_VARIABLE_DECLARATION;
- node->data = (ast_node_data_t) {
- .variable = {
- .name = variable_name,
- .type = type,
- .value = value
- }
- };
+ node->data = (ast_node_data_t){ .variable = { .name = variable_name, .type = type, .value = value } };
}
void
@@ -179,9 +150,5 @@ ast_node_init_identifier(ast_node_t *node, string_view_t name)
{
// FIXME: define the visitor strategy
node->kind = AST_IDENTIFIER;
- node->data = (ast_node_data_t) {
- .variable = {
- .name = name,
- }
- };
+ node->data.identifier = (ast_identifier_t){ .name = name };
}
diff --git a/src/ast.h b/src/ast.h
index 5ba21ad..0907635 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -1,82 +1,93 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef AST_H
#define AST_H
-#include <stdint.h>
#include "string_view.h"
#include "vector.h"
+#include <stdint.h>
-#define ast_visitor_visit(visitor, node) ast_node_accept_visitor(node, (ast_visitor_t *) visitor);
+#define ast_visitor_visit(visitor, node) ast_node_accept_visitor(node, (ast_visitor_t *)visitor);
-typedef enum {
+typedef enum
+{
TYPE_I32
} type_t;
typedef struct ast_visitor_t ast_visitor_t;
typedef struct ast_node_t ast_node_t;
-typedef struct ast_return_stmt_t {
- ast_node_t* argument;
+typedef struct ast_return_stmt_t
+{
+ ast_node_t *argument;
} ast_return_stmt_t;
-typedef struct ast_function_declaration_t {
+typedef struct ast_function_declaration_t
+{
string_view_t name;
- type_t return_type;
- vector_t* body;
+ type_t return_type;
+ vector_t *body;
} ast_function_declaration_t;
-typedef struct ast_binary_operation_t {
+typedef struct ast_binary_operation_t
+{
// FIXME: We want to use enum to distinguish operators
string_view_t op;
- ast_node_t* left;
- ast_node_t* right;
+ ast_node_t *left;
+ ast_node_t *right;
} ast_binary_operation_t;
-typedef enum {
+typedef enum
+{
AST_LITERAL_INTEGER
} ast_literal_kind_t;
-typedef union {
+typedef union
+{
uint32_t integer;
} ast_literal_value_t;
-typedef struct ast_literal_t {
+typedef struct ast_literal_t
+{
ast_literal_kind_t kind;
ast_literal_value_t value;
} ast_literal_t;
-typedef struct ast_identifier_t {
+typedef struct ast_identifier_t
+{
string_view_t name;
} ast_identifier_t;
-typedef struct ast_variable_declaration_t {
+typedef struct ast_variable_declaration_t
+{
string_view_t name;
type_t type;
- ast_node_t* value;
+ ast_node_t *value;
} ast_variable_declaration_t;
-typedef struct ast_visitor_t {
+typedef struct ast_visitor_t
+{
void (*visit_function)(struct ast_visitor_t *, ast_function_declaration_t *);
void (*visit_return_stmt)(struct ast_visitor_t *, ast_return_stmt_t *);
void (*visit_literal)(struct ast_visitor_t *, ast_literal_t *);
void (*visit_binary_operation)(struct ast_visitor_t *, ast_binary_operation_t *);
} ast_visitor_t;
-typedef enum {
+typedef enum
+{
AST_BINARY_OPERATION,
AST_FUNCTION_DECLARATION,
AST_IDENTIFIER,
@@ -86,7 +97,8 @@ typedef enum {
AST_VARIABLE_DECLARATION,
} ast_node_kind_t;
-typedef union {
+typedef union
+{
ast_binary_operation_t binary_operation;
ast_function_declaration_t function;
ast_literal_t literal;
@@ -95,24 +107,33 @@ typedef union {
ast_identifier_t identifier;
} ast_node_data_t;
-typedef struct ast_node_t {
+typedef struct ast_node_t
+{
void (*accept_visitor)(ast_node_t *, ast_visitor_t *);
ast_node_kind_t kind;
ast_node_data_t data;
} ast_node_t;
-void ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor);
-
-ast_node_t* ast_node_new();
-void ast_node_destroy(ast_node_t *node);
-
-void ast_node_init_binary_operation(ast_node_t *node, string_view_t op, ast_node_t *left, ast_node_t *right);
-void ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, vector_t *body);
-void ast_node_init_identifier(ast_node_t *node, string_view_t name);
-void ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument);
-void ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name, type_t type, ast_node_t *value);
+void
+ast_node_accept_visitor(ast_node_t *node, ast_visitor_t *visitor);
+
+ast_node_t *
+ast_node_new();
+void
+ast_node_destroy(ast_node_t *node);
+
+void
+ast_node_init_binary_operation(ast_node_t *node, string_view_t op, ast_node_t *left, ast_node_t *right);
+void
+ast_node_init_function_declaration(ast_node_t *node, string_view_t name, type_t return_type, vector_t *body);
+void
+ast_node_init_identifier(ast_node_t *node, string_view_t name);
+void
+ast_node_init_return_stmt(ast_node_t *node, ast_node_t *argument);
+void
+ast_node_init_variable_declaration(ast_node_t *node, string_view_t variable_name, type_t type, ast_node_t *value);
// FIXME: use the naming convention
-void ast_literal_integer_create(ast_node_t* node, uint32_t number);
+void
+ast_literal_integer_create(ast_node_t *node, uint32_t number);
#endif /* AST_H */
-
diff --git a/src/gas_assembly_generator.c b/src/gas_assembly_generator.c
index 5572cb7..00897fd 100644
--- a/src/gas_assembly_generator.c
+++ b/src/gas_assembly_generator.c
@@ -1,40 +1,42 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#include "gas_assembly_generator.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
-static void gas_assembly_generator_visit_function(ast_visitor_t *visitor, ast_function_declaration_t *func);
-static void gas_assembly_generator_visit_return_stmt(ast_visitor_t *visitor, ast_return_stmt_t *return_stmt);
-static void gas_assembly_generator_visit_literal(ast_visitor_t *visitor, ast_literal_t *literal);
-static void gas_assembly_generator_binary_operation(ast_visitor_t *visitor, ast_binary_operation_t *binary_operation);
+static void
+gas_assembly_generator_visit_function(ast_visitor_t *visitor, ast_function_declaration_t *func);
+static void
+gas_assembly_generator_visit_return_stmt(ast_visitor_t *visitor, ast_return_stmt_t *return_stmt);
+static void
+gas_assembly_generator_visit_literal(ast_visitor_t *visitor, ast_literal_t *literal);
+static void
+gas_assembly_generator_binary_operation(ast_visitor_t *visitor, ast_binary_operation_t *binary_operation);
void
gas_assembly_generator_init(gas_assembly_generator_t *gen, FILE *out)
{
assert(gen && out);
- gen->super = (ast_visitor_t) {
- .visit_function = &gas_assembly_generator_visit_function,
- .visit_literal = &gas_assembly_generator_visit_literal,
- .visit_return_stmt = &gas_assembly_generator_visit_return_stmt,
- .visit_binary_operation = &gas_assembly_generator_binary_operation
- };
+ gen->super = (ast_visitor_t){ .visit_function = &gas_assembly_generator_visit_function,
+ .visit_literal = &gas_assembly_generator_visit_literal,
+ .visit_return_stmt = &gas_assembly_generator_visit_return_stmt,
+ .visit_binary_operation = &gas_assembly_generator_binary_operation };
gen->out = out;
}
@@ -48,13 +50,13 @@ gas_assembly_generator_visit_function(ast_visitor_t *visitor, ast_function_decla
exit(EXIT_FAILURE);
}
- gas_assembly_generator_t *gen = (gas_assembly_generator_t *) visitor;
+ gas_assembly_generator_t *gen = (gas_assembly_generator_t *)visitor;
- fprintf(gen->out,".global _start\n");
- fprintf(gen->out,".text\n");
- fprintf(gen->out,"_start:\n");
+ fprintf(gen->out, ".global _start\n");
+ fprintf(gen->out, ".text\n");
+ fprintf(gen->out, "_start:\n");
- for (size_t i=0; i < func->body->size; i++) {
+ for (size_t i = 0; i < func->body->size; i++) {
ast_visitor_visit(visitor, vector_at(func->body, i));
}
}
@@ -63,7 +65,7 @@ static void
gas_assembly_generator_visit_return_stmt(ast_visitor_t *visitor, ast_return_stmt_t *return_stmt)
{
assert(visitor && return_stmt);
- gas_assembly_generator_t *gen = (gas_assembly_generator_t *) visitor;
+ gas_assembly_generator_t *gen = (gas_assembly_generator_t *)visitor;
ast_visitor_visit(visitor, return_stmt->argument);
@@ -75,7 +77,7 @@ gas_assembly_generator_visit_return_stmt(ast_visitor_t *visitor, ast_return_stmt
static void
gas_assembly_generator_visit_literal(ast_visitor_t *visitor, ast_literal_t *literal)
{
- gas_assembly_generator_t *gen = (gas_assembly_generator_t *) visitor;
+ gas_assembly_generator_t *gen = (gas_assembly_generator_t *)visitor;
switch (literal->kind) {
case AST_LITERAL_INTEGER:
fprintf(gen->out, " mov $%d, %%rax\n", literal->value.integer);
@@ -88,7 +90,7 @@ gas_assembly_generator_visit_literal(ast_visitor_t *visitor, ast_literal_t *lite
static void
gas_assembly_generator_binary_operation(ast_visitor_t *visitor, ast_binary_operation_t *binary_operation)
{
- gas_assembly_generator_t *gen = (gas_assembly_generator_t *) visitor;
+ gas_assembly_generator_t *gen = (gas_assembly_generator_t *)visitor;
ast_visitor_visit(visitor, binary_operation->right);
@@ -119,6 +121,6 @@ gas_assembly_generator_binary_operation(ast_visitor_t *visitor, ast_binary_opera
return;
}
- fprintf(stderr, "no strategy defined for: "SVFMT"\n", SVARG(&binary_operation->op));
+ fprintf(stderr, "no strategy defined for: " SVFMT "\n", SVARG(&binary_operation->op));
assert(false);
}
diff --git a/src/gas_assembly_generator.h b/src/gas_assembly_generator.h
index 64f861e..c3192b7 100644
--- a/src/gas_assembly_generator.h
+++ b/src/gas_assembly_generator.h
@@ -1,33 +1,35 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef GAS_ASSEMBLY_GENERATOR_H
#define GAS_ASSEMBLY_GENERATOR_H
#include "ast.h"
#include <stdio.h>
-typedef struct gas_assembly_generator_t {
+typedef struct gas_assembly_generator_t
+{
ast_visitor_t super;
FILE *out;
} gas_assembly_generator_t;
-void gas_assembly_generator_init(gas_assembly_generator_t *, FILE *);
+void
+gas_assembly_generator_init(gas_assembly_generator_t *, FILE *);
-void gas_assembly_generator_visit(gas_assembly_generator_t * gen, ast_node_t* ast);
+void
+gas_assembly_generator_visit(gas_assembly_generator_t *gen, ast_node_t *ast);
#endif /* GAS_ASSEMBLY_GENERATOR_H */
-
diff --git a/src/lexer.c b/src/lexer.c
index e1f0d80..bbf29fc 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -1,27 +1,27 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "lexer.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
-#include "lexer.h"
void
lexer_init(lexer_t *lexer, char *filepath)
@@ -29,10 +29,10 @@ lexer_init(lexer_t *lexer, char *filepath)
assert(lexer && "lexer must be defined");
assert(filepath && "filepath must be defined");
lexer->filepath = filepath;
- lexer->srclen = 0;
- lexer->cur = 0;
- lexer->row = 0;
- lexer->bol = 0;
+ lexer->srclen = 0;
+ lexer->cur = 0;
+ lexer->row = 0;
+ lexer->bol = 0;
lexer_load_file_contents(lexer);
}
@@ -151,11 +151,8 @@ lexer_next_token(lexer_t *lexer, token_t *token)
return;
}
- if (lexer_current_char(lexer) == '+'
- || lexer_current_char(lexer) == '-'
- || lexer_current_char(lexer) == '*'
- || lexer_current_char(lexer) == '/'
- || lexer_current_char(lexer) == '=') {
+ if (lexer_current_char(lexer) == '+' || lexer_current_char(lexer) == '-' || lexer_current_char(lexer) == '*' ||
+ lexer_current_char(lexer) == '/' || lexer_current_char(lexer) == '=') {
lexer_define_literal_token_props(lexer, token, TOKEN_OP);
lexer_drop_char(lexer);
return;
@@ -197,7 +194,6 @@ lexer_load_file_contents(lexer_t *lexer)
fprintf(stderr, "could not read file '%s'\n", lexer->filepath);
exit(EXIT_FAILURE);
}
-
}
void
@@ -220,7 +216,6 @@ lexer_is_eof(lexer_t *lexer)
return lexer->cur >= lexer->srclen;
}
-
bool
lexer_is_not_eof(lexer_t *lexer)
{
@@ -263,4 +258,3 @@ token_kind_to_str(token_kind_t kind)
assert(false && "unreachable");
}
}
-
diff --git a/src/lexer.h b/src/lexer.h
index 9f45910..8c84745 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -1,77 +1,87 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef LEXER_H
#define LEXER_H
+#include "string_view.h"
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
-#include <stdbool.h>
-#include "string_view.h"
-typedef enum {
- TOKEN_NAME,
- TOKEN_OPAREN,
- TOKEN_CPAREN,
- TOKEN_COLON,
- TOKEN_SEMICOLON,
- TOKEN_OCURLY,
- TOKEN_CCURLY,
- TOKEN_NUMBER,
- TOKEN_OP,
- TOKEN_EOF,
- TOKEN_UNKNOWN
+typedef enum
+{
+ TOKEN_NAME,
+ TOKEN_OPAREN,
+ TOKEN_CPAREN,
+ TOKEN_COLON,
+ TOKEN_SEMICOLON,
+ TOKEN_OCURLY,
+ TOKEN_CCURLY,
+ TOKEN_NUMBER,
+ TOKEN_OP,
+ TOKEN_EOF,
+ TOKEN_UNKNOWN
} token_kind_t;
-typedef struct token_t {
- token_kind_t kind;
+typedef struct token_t
+{
+ token_kind_t kind;
string_view_t value;
- char *filepath;
- uint32_t row;
- uint32_t col;
- uint32_t bol;
+ char *filepath;
+ uint32_t row;
+ uint32_t col;
+ uint32_t bol;
} token_t;
-
-typedef struct lexer_t {
- char *filepath;
- char *src;
- size_t srclen;
- uint32_t cur;
- uint32_t row;
- uint32_t bol;
+typedef struct lexer_t
+{
+ char *filepath;
+ char *src;
+ size_t srclen;
+ uint32_t cur;
+ uint32_t row;
+ uint32_t bol;
} lexer_t;
-void lexer_init(lexer_t *lexer, char *filepath);
+void
+lexer_init(lexer_t *lexer, char *filepath);
-void lexer_next_token(lexer_t *lexer, token_t *token);
+void
+lexer_next_token(lexer_t *lexer, token_t *token);
-void lexer_load_file_contents(lexer_t *lexer);
+void
+lexer_load_file_contents(lexer_t *lexer);
-char lexer_current_char(lexer_t *lexer);
+char
+lexer_current_char(lexer_t *lexer);
-bool lexer_is_eof(lexer_t *lexer);
+bool
+lexer_is_eof(lexer_t *lexer);
-bool lexer_is_not_eof(lexer_t *lexer);
+bool
+lexer_is_not_eof(lexer_t *lexer);
-void lexer_drop_char(lexer_t *lexer);
+void
+lexer_drop_char(lexer_t *lexer);
-void lexer_step_back_to(lexer_t *lexer, token_t *token);
+void
+lexer_step_back_to(lexer_t *lexer, token_t *token);
-char * token_kind_to_str(token_kind_t kind);
+char *
+token_kind_to_str(token_kind_t kind);
#endif /* LEXER_H */
-
diff --git a/src/parser.c b/src/parser.c
index 5399cfd..f3c6328 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#include <assert.h>
#include <stdbool.h>
@@ -38,7 +38,7 @@ parser_init(parser_t *parser, lexer_t *lexer)
static void
parser_error_push_unexpected_kind(parser_t *parser, token_t *token, token_kind_t expected)
{
- parser_error_t* error = &parser->errors[parser->errors_len++];
+ parser_error_t *error = &parser->errors[parser->errors_len++];
error->token = *token;
if (token->kind == TOKEN_EOF) {
@@ -74,7 +74,8 @@ parser_parse_type(type_t *type, parser_t *parser)
{
token_t token;
- if(!expected_token(&token, parser, TOKEN_NAME)) return false;
+ if (!expected_token(&token, parser, TOKEN_NAME))
+ return false;
if (string_view_eq(token.value, string_view_from_str("i32"))) {
*type = TYPE_I32;
@@ -84,11 +85,7 @@ parser_parse_type(type_t *type, parser_t *parser)
parser_error_t error;
error.token = token;
- sprintf(
- error.message,
- "type '"SVFMT"' is not defined",
- SVARG(&token.value)
- );
+ sprintf(error.message, "type '" SVFMT "' is not defined", SVARG(&token.value));
parser->errors[parser->errors_len++] = error;
return false;
@@ -114,7 +111,8 @@ parser_parse_factor(parser_t *parser, ast_node_t *node)
return parser_literal_integer_node(node, &token);
} else if (token.kind == TOKEN_OPAREN) {
parser_parse_expression(parser, node);
- if (!drop_expected_token(parser, TOKEN_CPAREN)) return false;
+ if (!drop_expected_token(parser, TOKEN_CPAREN))
+ return false;
return true;
} else if (token.kind == TOKEN_NAME) {
/// FIXME: Check if the identifier is defined
@@ -125,12 +123,7 @@ parser_parse_factor(parser_t *parser, ast_node_t *node)
// FIXME: Extract this erros logic to a function
parser_error_t error;
error.token = token;
- sprintf(
- error.message,
- "unexpected '%s ("SVFMT")' token",
- token_kind_to_str(token.kind),
- SVARG(&token.value)
- );
+ sprintf(error.message, "unexpected '%s (" SVFMT ")' token", token_kind_to_str(token.kind), SVARG(&token.value));
parser->errors[parser->errors_len++] = error;
return false;
@@ -139,17 +132,20 @@ parser_parse_factor(parser_t *parser, ast_node_t *node)
bool
parser_parse_term(parser_t *parser, ast_node_t *node)
{
- if (!parser_parse_factor(parser, node)) return false;
+ if (!parser_parse_factor(parser, node))
+ return false;
token_t token;
lexer_next_token(parser->lexer, &token);
- while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("*")) || string_view_eq(token.value, string_view_from_str("/")))) {
+ while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("*")) ||
+ string_view_eq(token.value, string_view_from_str("/")))) {
ast_node_t *left = ast_node_new();
*left = *node;
ast_node_t *right = ast_node_new();
- if (!parser_parse_factor(parser, right)) return false;
+ if (!parser_parse_factor(parser, right))
+ return false;
ast_node_init_binary_operation(node, token.value, left, right);
@@ -171,17 +167,20 @@ parser_parse_term(parser_t *parser, ast_node_t *node)
bool
parser_parse_expression(parser_t *parser, ast_node_t *node)
{
- if (!parser_parse_term(parser, node)) return false;
+ if (!parser_parse_term(parser, node))
+ return false;
token_t token;
lexer_next_token(parser->lexer, &token);
- while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("+")) || string_view_eq(token.value, string_view_from_str("-")))) {
+ while (token.kind == TOKEN_OP && (string_view_eq(token.value, string_view_from_str("+")) ||
+ string_view_eq(token.value, string_view_from_str("-")))) {
ast_node_t *left = ast_node_new();
*left = *node;
ast_node_t *right = ast_node_new();
- if (!parser_parse_term(parser, right)) return false;
+ if (!parser_parse_term(parser, right))
+ return false;
ast_node_init_binary_operation(node, token.value, left, right);
@@ -197,9 +196,11 @@ bool
parser_parse_return_stmt(parser_t *parser, ast_node_t *node)
{
ast_node_t *argument_token = ast_node_new();
- if (!parser_parse_expression(parser, argument_token)) return false;
+ if (!parser_parse_expression(parser, argument_token))
+ return false;
- if (!drop_expected_token(parser, TOKEN_SEMICOLON)) return false;
+ if (!drop_expected_token(parser, TOKEN_SEMICOLON))
+ return false;
ast_node_init_return_stmt(node, argument_token);
return true;
@@ -208,26 +209,25 @@ parser_parse_return_stmt(parser_t *parser, ast_node_t *node)
bool
parser_parse_variable_definition(parser_t *parser, string_view_t variable_name, ast_node_t *node)
{
- if (!drop_expected_token(parser, TOKEN_COLON)) return false;
+ if (!drop_expected_token(parser, TOKEN_COLON))
+ return false;
type_t type;
// FIXME: change the parameters order
- if (!parser_parse_type(&type, parser)) return false;
+ if (!parser_parse_type(&type, parser))
+ return false;
token_t equal_token;
- if (!expected_token(&equal_token, parser, TOKEN_OP)) return false;
-
+ if (!expected_token(&equal_token, parser, TOKEN_OP))
+ return false;
+
if (!string_view_eq(equal_token.value, string_view_from_str("="))) {
parser_error_t error;
error.token = equal_token;
- sprintf(
- error.message,
- "expected '=' but got "SVFMT,
- SVARG(&equal_token.value)
- );
+ sprintf(error.message, "expected '=' but got " SVFMT, SVARG(&equal_token.value));
parser->errors[parser->errors_len++] = error;
return false;
@@ -267,8 +267,7 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
}
vector_push_back(body, return_node);
- }
- else {
+ } else {
ast_node_t *variable_node = ast_node_new();
bool parsed_variable = parser_parse_variable_definition(parser, current_token.value, variable_node);
@@ -294,10 +293,7 @@ parser_parse_block_declarations(parser_t *parser, vector_t *body)
parser_error_t error;
error.token = current_token;
- sprintf(
- error.message,
- "expected 'return' keyword."
- );
+ sprintf(error.message, "expected 'return' keyword.");
parser->errors[parser->errors_len++] = error;
return false;
@@ -314,28 +310,28 @@ parser_parse_function_declaration(parser_t *parser, ast_node_t *node)
return false;
}
- if (!drop_expected_token(parser, TOKEN_OPAREN)) return false;
- if (!drop_expected_token(parser, TOKEN_CPAREN)) return false;
- if (!drop_expected_token(parser, TOKEN_COLON)) return false;
+ if (!drop_expected_token(parser, TOKEN_OPAREN))
+ return false;
+ if (!drop_expected_token(parser, TOKEN_CPAREN))
+ return false;
+ if (!drop_expected_token(parser, TOKEN_COLON))
+ return false;
type_t return_type;
- if(!parser_parse_type(&return_type, parser)) {
+ if (!parser_parse_type(&return_type, parser)) {
return false;
}
- if (!drop_expected_token(parser, TOKEN_OCURLY)) return false;
+ if (!drop_expected_token(parser, TOKEN_OCURLY))
+ return false;
- vector_t* body = vector_new();
+ vector_t *body = vector_new();
- if (!parser_parse_block_declarations(parser, body)) return false;
+ if (!parser_parse_block_declarations(parser, body))
+ return false;
- ast_node_init_function_declaration(
- node,
- func_name_token.value,
- return_type,
- body
- );
+ ast_node_init_function_declaration(node, func_name_token.value, return_type, body);
return true;
}
diff --git a/src/parser.h b/src/parser.h
index 4b6b5a9..bdf0c5a 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef PARSER_H
#define PARSER_H
@@ -21,22 +21,26 @@
#include "lexer.h"
#include "string_view.h"
-typedef struct parser_error_t {
+typedef struct parser_error_t
+{
token_t token;
- char message [256];
+ char message[256];
} parser_error_t;
-typedef struct parser_t {
+typedef struct parser_t
+{
lexer_t *lexer;
int errors_len;
// FIXME: replace with vector
parser_error_t errors[1];
} parser_t;
-void parser_init(parser_t *parser, lexer_t *lexer);
+void
+parser_init(parser_t *parser, lexer_t *lexer);
-bool parser_parse_function_declaration(parser_t *parser, ast_node_t *node);
-bool parser_parse_expression(parser_t *parser, ast_node_t *node);
+bool
+parser_parse_function_declaration(parser_t *parser, ast_node_t *node);
+bool
+parser_parse_expression(parser_t *parser, ast_node_t *node);
#endif /* PARSER_H */
-
diff --git a/src/pipac.c b/src/pipac.c
index 2b7b1f5..1acb4b6 100644
--- a/src/pipac.c
+++ b/src/pipac.c
@@ -1,28 +1,28 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "lexer.h"
#include "ast.h"
+#include "gas_assembly_generator.h"
+#include "lexer.h"
#include "parser.h"
#include "string_view.h"
-#include "gas_assembly_generator.h"
void
generate_gas_x86_64_linux(ast_node_t *func)
@@ -40,24 +40,27 @@ print_usage()
}
void
-print_tokens(lexer_t *lexer) {
+print_tokens(lexer_t *lexer)
+{
token_t token;
for (lexer_next_token(lexer, &token); token.kind != TOKEN_EOF; lexer_next_token(lexer, &token)) {
- printf("%s:%d:%d: [kind=%d, value='"SVFMT"']\n", lexer->filepath, token.row + 1, token.col + 1, token.kind, SVARG(&token.value));
+ printf("%s:%d:%d: [kind=%d, value='" SVFMT "']\n",
+ lexer->filepath,
+ token.row + 1,
+ token.col + 1,
+ token.kind,
+ SVARG(&token.value));
}
}
void
-parser_print_errors(parser_t *parser) {
- for (int i=0; i < parser->errors_len; i++) {
+parser_print_errors(parser_t *parser)
+{
+ for (int i = 0; i < parser->errors_len; i++) {
parser_error_t error = parser->errors[i];
fprintf(
- stderr,
- "%s:%d:%d: [ERROR]: %s\n",
- error.token.filepath, error.token.row + 1, error.token.col + 1,
- error.message
- );
+ stderr, "%s:%d:%d: [ERROR]: %s\n", error.token.filepath, error.token.row + 1, error.token.col + 1, error.message);
}
}
@@ -77,7 +80,7 @@ main(int argc, char **argv)
parser_t parser;
parser_init(&parser, &lexer);
- ast_node_t* func = ast_node_new();
+ ast_node_t *func = ast_node_new();
if (!parser_parse_function_declaration(&parser, func)) {
parser_print_errors(&parser);
diff --git a/src/string_view.c b/src/string_view.c
index d511e63..ed65602 100644
--- a/src/string_view.c
+++ b/src/string_view.c
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#include "string_view.h"
#include <assert.h>
#include <stdio.h>
@@ -24,10 +24,7 @@ string_view_new(const char *str, int size)
{
assert(str);
- return (string_view_t) {
- .size = size,
- .str = str
- };
+ return (string_view_t){ .size = size, .str = str };
}
string_view_t
diff --git a/src/string_view.h b/src/string_view.h
index 41094a2..02c2f27 100644
--- a/src/string_view.h
+++ b/src/string_view.h
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef STRING_VIEW
#define STRING_VIEW
@@ -22,17 +22,22 @@
#include <stdbool.h>
-typedef struct string_view_t {
+typedef struct string_view_t
+{
const char *str;
- int size;
+ int size;
} string_view_t;
-string_view_t string_view_new(const char *str, int size);
+string_view_t
+string_view_new(const char *str, int size);
-string_view_t string_view_from_str(const char *str);
+string_view_t
+string_view_from_str(const char *str);
-bool string_view_to_str(string_view_t *sv, char *ret);
+bool
+string_view_to_str(string_view_t *sv, char *ret);
-bool string_view_eq(string_view_t a, string_view_t b);
+bool
+string_view_eq(string_view_t a, string_view_t b);
#endif /* STRING_VIEW */
diff --git a/src/vector.c b/src/vector.c
index 99ebffc..1d018ef 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#include "vector.h"
#include <assert.h>
#include <errno.h>
@@ -21,10 +21,10 @@
#include <stdlib.h>
#include <string.h>
-vector_t*
+vector_t *
vector_new()
{
- vector_t *vector = (vector_t *) malloc(sizeof(vector_t));
+ vector_t *vector = (vector_t *)malloc(sizeof(vector_t));
if (vector == NULL) {
fprintf(stderr, "failed to create vector: %s\n", strerror(errno));
@@ -67,7 +67,7 @@ vector_push_back(vector_t *vector, void *item)
vector->items[vector->size++] = item;
}
-void*
+void *
vector_at(vector_t *vector, uint32_t index)
{
assert(vector);
@@ -75,7 +75,7 @@ vector_at(vector_t *vector, uint32_t index)
}
void
-vector_destroy(vector_t * vector)
+vector_destroy(vector_t *vector)
{
assert(vector);
free(vector->items);
diff --git a/src/vector.h b/src/vector.h
index 0f29085..de837b2 100644
--- a/src/vector.h
+++ b/src/vector.h
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Johnny Richard
-*
-* This program is free software: you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation, either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program. If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2023 Johnny Richard
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
#ifndef VECTOR_H
#define VECTOR_H
#include <stdint.h>
@@ -21,15 +21,20 @@
#define VECTOR_INITIAL_CAPACITY 4
-typedef struct vector_t {
- size_t capacity;
- size_t size;
- void **items;
+typedef struct vector_t
+{
+ size_t capacity;
+ size_t size;
+ void **items;
} vector_t;
-vector_t* vector_new();
-void vector_push_back(vector_t *vector, void *item);
-void* vector_at(vector_t *vector, uint32_t index);
-void vector_destroy(vector_t * vector);
+vector_t *
+vector_new();
+void
+vector_push_back(vector_t *vector, void *item);
+void *
+vector_at(vector_t *vector, uint32_t index);
+void
+vector_destroy(vector_t *vector);
#endif /* VECTOR_H */