summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2023-04-26 00:40:44 +0200
committerCarlos Maniero <carlosmaniero@gmail.com>2023-04-25 23:19:13 -0300
commit127dae1c3b48c3a4dceddb9000309bfeaa8d0a01 (patch)
tree4d92b3a7b4c29f0ace3a5ff699e4789f60b0aa46
parentc1a1bd2320b4c1508c4ab20d23b7c193a94d8026 (diff)
style: Use clang-format as formatter and linter tool
We want to keep the code style consistent, this first commit adds a .clang-format in order to "document" our style code. This patch also adds a target *linter* to Makefile which will complain if we have any style issue on test and src dirs. I have run the follow command to create the .clang-format file: $ clang-format -style=mozilla -dump-config > .clang-format And I also made some adjusts to .clang-format changing the following properties: PointerAlignment: Right ColumnLimit: 120 Commands executed to fix the current styling: $ find . -name *.h | xargs clang-format -i $ find . -name *.c | xargs clang-format -i Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r--.clang-format216
-rw-r--r--Makefile6
-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
-rw-r--r--test/Makefile4
-rw-r--r--test/integration_test.c50
-rw-r--r--test/lexer_test.c70
-rw-r--r--test/munit.c3
-rw-r--r--test/munit.h1
-rw-r--r--test/parser_test.c89
-rw-r--r--test/string_view_test.c64
-rw-r--r--test/vector_test.c72
23 files changed, 833 insertions, 624 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..2850fff
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,216 @@
+---
+Language: Cpp
+# BasedOnStyle: Mozilla
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignArrayOfStructures: None
+AlignConsecutiveAssignments:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ PadOperators: true
+AlignConsecutiveBitFields:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ PadOperators: false
+AlignConsecutiveDeclarations:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ PadOperators: false
+AlignConsecutiveMacros:
+ Enabled: false
+ AcrossEmptyLines: false
+ AcrossComments: false
+ AlignCompound: false
+ PadOperators: false
+AlignEscapedNewlines: Right
+AlignOperands: Align
+AlignTrailingComments: true
+AllowAllArgumentsOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortEnumsOnASingleLine: true
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortLambdasOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: TopLevel
+AlwaysBreakAfterReturnType: TopLevel
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: Yes
+AttributeMacros:
+ - __capability
+BinPackArguments: false
+BinPackParameters: false
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: true
+ AfterControlStatement: Never
+ AfterEnum: true
+ AfterFunction: true
+ AfterNamespace: false
+ AfterObjCDeclaration: false
+ AfterStruct: true
+ AfterUnion: true
+ AfterExternBlock: true
+ BeforeCatch: false
+ BeforeElse: false
+ BeforeLambdaBody: false
+ BeforeWhile: false
+ IndentBraces: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: false
+ SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeConceptDeclarations: Always
+BreakBeforeBraces: Mozilla
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeComma
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit: 120
+CommentPragmas: '^ IWYU pragma:'
+QualifierAlignment: Leave
+CompactNamespaces: false
+ConstructorInitializerIndentWidth: 2
+ContinuationIndentWidth: 2
+Cpp11BracedListStyle: false
+DeriveLineEnding: true
+DerivePointerAlignment: false
+DisableFormat: false
+EmptyLineAfterAccessModifier: Never
+EmptyLineBeforeAccessModifier: LogicalBlock
+ExperimentalAutoDetectBinPacking: false
+PackConstructorInitializers: BinPack
+BasedOnStyle: ''
+ConstructorInitializerAllOnOneLineOrOnePerLine: false
+AllowAllConstructorInitializersOnNextLine: true
+FixNamespaceComments: false
+ForEachMacros:
+ - foreach
+ - Q_FOREACH
+ - BOOST_FOREACH
+IfMacros:
+ - KJ_IF_MAYBE
+IncludeBlocks: Preserve
+IncludeCategories:
+ - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
+ Priority: 2
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '^(<|"(gtest|gmock|isl|json)/)'
+ Priority: 3
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '.*'
+ Priority: 1
+ SortPriority: 0
+ CaseSensitive: false
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentAccessModifiers: false
+IndentCaseLabels: true
+IndentCaseBlocks: false
+IndentGotoLabels: true
+IndentPPDirectives: None
+IndentExternBlock: AfterExternBlock
+IndentRequiresClause: true
+IndentWidth: 2
+IndentWrappedFunctionNames: false
+InsertBraces: false
+InsertTrailingCommas: None
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+LambdaBodyIndentation: Signature
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 2
+ObjCBreakBeforeNestedBlockParam: true
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: false
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakOpenParenthesis: 0
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 200
+PenaltyIndentedWhitespace: 0
+PointerAlignment: Right
+PPIndentWidth: -1
+ReferenceAlignment: Pointer
+ReflowComments: true
+RemoveBracesLLVM: false
+RequiresClausePosition: OwnLine
+SeparateDefinitionBlocks: Leave
+ShortNamespaceLines: 1
+SortIncludes: CaseSensitive
+SortJavaStaticImport: Before
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeParensOptions:
+ AfterControlStatements: true
+ AfterForeachMacros: true
+ AfterFunctionDefinitionName: false
+ AfterFunctionDeclarationName: false
+ AfterIfMacros: true
+ AfterOverloadedOperator: false
+ AfterRequiresInClause: false
+ AfterRequiresInExpression: false
+ BeforeNonEmptyParentheses: false
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles: Never
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInLineCommentPrefix:
+ Minimum: 1
+ Maximum: -1
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SpaceBeforeSquareBrackets: false
+BitFieldColonSpacing: Both
+Standard: Latest
+StatementAttributeLikeMacros:
+ - Q_EMIT
+StatementMacros:
+ - Q_UNUSED
+ - QT_REQUIRE_VERSION
+TabWidth: 8
+UseCRLF: false
+UseTab: Never
+WhitespaceSensitiveMacros:
+ - STRINGIZE
+ - PP_STRINGIZE
+ - BOOST_PP_STRINGIZE
+ - NS_SWIFT_NAME
+ - CF_SWIFT_NAME
+...
+
diff --git a/Makefile b/Makefile
index 4417423..ba12408 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@ BUILD_DIR := build
CFLAGS := -Wall -Wextra -pedantic -std=c11 -ggdb
SRCS := $(wildcard $(SRC_DIR)/*.c)
+HEADERS := $(wildcard $(SRC_DIR)/*.h)
OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRCS))
.PHONY: all
@@ -15,6 +16,11 @@ $(TARGET): $(BUILD_DIR) $(OBJS)
$(BUILD_DIR):
@mkdir -p $@
+.PHONY: linter
+linter: $(SRCS) $(HEADERS)
+ clang-format --dry-run --Werror $?
+ $(MAKE) -C test linter
+
.PHONY: test
test: $(TARGET)
$(MAKE) -C test
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 */
diff --git a/test/Makefile b/test/Makefile
index 40f5cf9..717b430 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -14,6 +14,10 @@ all: munit.o $(TESTS)
clean:
$(RM) *.o *_test
+.PHONY: linter
+linter: $(SRCS)
+ clang-format --dry-run --Werror $?
+
string_view_test: munit.o ../build/string_view.o string_view_test.o
$(CC) $? $(CFLAGS) -o $@
diff --git a/test/integration_test.c b/test/integration_test.c
index b6c0f36..739a938 100644
--- a/test/integration_test.c
+++ b/test/integration_test.c
@@ -1,26 +1,26 @@
/*
-* Copyright (C) 2023 Carlos Maniero
-*
-* 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 Carlos Maniero
+ *
+ * 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/>.
+ */
#define MUNIT_ENABLE_ASSERT_ALIASES
+#include "munit.h"
#include <stdio.h>
#include <string.h>
-#include "munit.h"
void
-assert_exit_status(char* filename, int expected_exit_status)
+assert_exit_status(char *filename, int expected_exit_status)
{
char command[255] = "../pipa --silent --out /tmp/pipa_program ";
strcat(command, filename);
@@ -32,15 +32,13 @@ assert_exit_status(char* filename, int expected_exit_status)
if (WIFEXITED(status)) {
int exit_status = WEXITSTATUS(status);
assert_int(expected_exit_status, ==, exit_status);
- }
- else {
+ } else {
assert_string_equal("", "Exited with error");
}
}
static MunitResult
-test_examples(const MunitParameter params[],
- void *user_data_or_fixture)
+test_examples(const MunitParameter params[], void *user_data_or_fixture)
{
assert_exit_status("../examples/main.pipa", 69);
assert_exit_status("../examples/arithmetics.pipa", 13);
@@ -48,14 +46,10 @@ test_examples(const MunitParameter params[],
return MUNIT_OK;
}
-static MunitTest tests[] = {
- { "/test_examples", test_examples, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
-};
+static MunitTest tests[] = { { "/test_examples", test_examples, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
-static const MunitSuite suite = {
- "/integration_tests", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
-};
+static const MunitSuite suite = { "/integration_tests", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[])
diff --git a/test/lexer_test.c b/test/lexer_test.c
index 0eebfe5..abfac16 100644
--- a/test/lexer_test.c
+++ b/test/lexer_test.c
@@ -1,32 +1,32 @@
/*
-* Copyright (C) 2023 Carlos Maniero
-*
-* 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 Carlos Maniero
+ *
+ * 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/>.
+ */
#define MUNIT_ENABLE_ASSERT_ALIASES
-#include "munit.h"
#include "lexer.h"
+#include "munit.h"
void
make_lexer_from_static_src(lexer_t *lexer, char *src, int srclen)
{
- lexer->srclen = 0;
- lexer->cur = 0;
- lexer->row = 0;
- lexer->bol = 0;
- lexer->src = src;
- lexer->srclen = srclen;
+ lexer->srclen = 0;
+ lexer->cur = 0;
+ lexer->row = 0;
+ lexer->bol = 0;
+ lexer->src = src;
+ lexer->srclen = srclen;
}
void
@@ -51,27 +51,24 @@ assert_token_at(char *source, int token_index, token_kind_t expected_kind, char
assert_string_equal(expected, actual);
}
-
static MunitResult
-test_tokenize_number(const MunitParameter params[],
- void *user_data_or_fixture)
+test_tokenize_number(const MunitParameter params[], void *user_data_or_fixture)
{
- assert_token_at("1", 0, TOKEN_NUMBER, "1");
- assert_token_at(" 13 ", 0, TOKEN_NUMBER, "13");
+ assert_token_at("1", 0, TOKEN_NUMBER, "1");
+ assert_token_at(" 13 ", 0, TOKEN_NUMBER, "13");
assert_token_at(" \n 13 ", 0, TOKEN_NUMBER, "13");
return MUNIT_OK;
}
static MunitResult
-test_tokenize_op(const MunitParameter params[],
- void *user_data_or_fixture)
+test_tokenize_op(const MunitParameter params[], void *user_data_or_fixture)
{
- assert_token_at(" + 2", 0, TOKEN_OP, "+");
+ assert_token_at(" + 2", 0, TOKEN_OP, "+");
assert_token_at(" - \n", 0, TOKEN_OP, "-");
- assert_token_at(" * ;", 0, TOKEN_OP, "*");
- assert_token_at(" / ", 0, TOKEN_OP, "/");
- assert_token_at(" = ", 0, TOKEN_OP, "=");
+ assert_token_at(" * ;", 0, TOKEN_OP, "*");
+ assert_token_at(" / ", 0, TOKEN_OP, "/");
+ assert_token_at(" = ", 0, TOKEN_OP, "=");
assert_token_at("1 * 2", 0, TOKEN_NUMBER, "1");
assert_token_at("1 * 2", 1, TOKEN_OP, "*");
@@ -81,8 +78,7 @@ test_tokenize_op(const MunitParameter params[],
}
static MunitResult
-test_tokenize_unknown(const MunitParameter params[],
- void *user_data_or_fixture)
+test_tokenize_unknown(const MunitParameter params[], void *user_data_or_fixture)
{
assert_token_at(" @ ", 0, TOKEN_UNKNOWN, "@");
assert_token_at(" $ ", 0, TOKEN_UNKNOWN, "$");
@@ -97,9 +93,7 @@ static MunitTest tests[] = {
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
-static const MunitSuite suite = {
- "/lexer_test", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
-};
+static const MunitSuite suite = { "/lexer_test", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[])
diff --git a/test/munit.c b/test/munit.c
index 00ede07..2500018 100644
--- a/test/munit.c
+++ b/test/munit.c
@@ -1,3 +1,4 @@
+// clang-format off
/* Copyright (c) 2013-2018 Evan Nemerson <evan@nemerson.com>
*
* Permission is hereby granted, free of charge, to any person
@@ -496,8 +497,8 @@ struct PsnipClockTimespec {
(defined(PSNIP_CLOCK_CPU_METHOD) && (PSNIP_CLOCK_CPU_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \
(defined(PSNIP_CLOCK_WALL_METHOD) && (PSNIP_CLOCK_WALL_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE)) || \
(defined(PSNIP_CLOCK_MONOTONIC_METHOD) && (PSNIP_CLOCK_MONOTONIC_METHOD == PSNIP_CLOCK_METHOD_GETRUSAGE))
-# include <sys/time.h>
# include <sys/resource.h>
+# include <sys/time.h>
#endif
#if \
diff --git a/test/munit.h b/test/munit.h
index 8460a45..c614c78 100644
--- a/test/munit.h
+++ b/test/munit.h
@@ -1,3 +1,4 @@
+// clang-format off
/* µnit Testing Framework
* Copyright (c) 2013-2017 Evan Nemerson <evan@nemerson.com>
*
diff --git a/test/parser_test.c b/test/parser_test.c
index daa2fe7..d8aee07 100644
--- a/test/parser_test.c
+++ b/test/parser_test.c
@@ -1,19 +1,19 @@
/*
-* Copyright (C) 2023 Carlos Maniero
-*
-* 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 Carlos Maniero
+ *
+ * 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/>.
+ */
#define MUNIT_ENABLE_ASSERT_ALIASES
#include "ast.h"
#include "lexer.h"
@@ -22,22 +22,24 @@
#include "string.h"
#include "vector.h"
-void assert_string_view_equal(char *expected, string_view_t actual);
+void
+assert_string_view_equal(char *expected, string_view_t actual);
void
make_lexer_from_static_src(lexer_t *lexer, char *src)
{
lexer->filepath = "test.pipa";
- lexer->srclen = 0;
- lexer->cur = 0;
- lexer->row = 0;
- lexer->bol = 0;
- lexer->src = src;
- lexer->srclen = strlen(src);
+ lexer->srclen = 0;
+ lexer->cur = 0;
+ lexer->row = 0;
+ lexer->bol = 0;
+ lexer->src = src;
+ lexer->srclen = strlen(src);
}
void
-assert_parser_error(char* src, char* error_msg) {
+assert_parser_error(char *src, char *error_msg)
+{
parser_t parser;
lexer_t lexer;
@@ -55,8 +57,7 @@ assert_parser_error(char* src, char* error_msg) {
}
static MunitResult
-test_parse_function(const MunitParameter params[],
- void *user_data_or_fixture)
+test_parse_function(const MunitParameter params[], void *user_data_or_fixture)
{
parser_t parser;
lexer_t lexer;
@@ -89,8 +90,7 @@ test_parse_function(const MunitParameter params[],
}
static MunitResult
-test_parse_variable_definition(const MunitParameter params[],
- void *user_data_or_fixture)
+test_parse_variable_definition(const MunitParameter params[], void *user_data_or_fixture)
{
parser_t parser;
lexer_t lexer;
@@ -129,8 +129,7 @@ test_parse_variable_definition(const MunitParameter params[],
}
static MunitResult
-test_parse_arithmetic_expression(const MunitParameter params[],
- void *user_data_or_fixture)
+test_parse_arithmetic_expression(const MunitParameter params[], void *user_data_or_fixture)
{
parser_t parser;
lexer_t lexer;
@@ -189,7 +188,8 @@ test_parse_arithmetic_expression(const MunitParameter params[],
return MUNIT_OK;
}
-void assert_string_view_equal(char *expected, string_view_t actual)
+void
+assert_string_view_equal(char *expected, string_view_t actual)
{
size_t expected_len = strlen(expected);
assert_int(expected_len, ==, actual.size);
@@ -197,22 +197,21 @@ void assert_string_view_equal(char *expected, string_view_t actual)
}
static MunitResult
-test_parse_basic_syntax_errors(const MunitParameter params[],
- void *user_data_or_fixture)
+test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or_fixture)
{
- assert_parser_error("(): i32 { return 42; }" , "expected 'TOKEN_NAME' but got '('");
- assert_parser_error("main): i32 { return 42; }" , "expected '(' but got ')'");
- assert_parser_error("main(: i32 { return 42; }" , "expected ')' but got ':'");
- assert_parser_error("main() i32 { return 42; }" , "expected ':' but got 'TOKEN_NAME'");
- assert_parser_error("main(): { return 42; }" , "expected 'TOKEN_NAME' but got '{'");
- assert_parser_error("main(): i32 return 42; }" , "expected '{' but got 'TOKEN_NAME'");
- assert_parser_error("main(): i32 { 42; }" , "expected 'TOKEN_NAME' but got 'TOKEN_NUMBER'");
- assert_parser_error("main(): i32 { return; }" , "unexpected '; (;)' token");
- assert_parser_error("main(): i32 { return 42;" , "expected '}' but got end of file");
- assert_parser_error("main(): beff { return 42; }" , "type 'beff' is not defined");
+ assert_parser_error("(): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('");
+ assert_parser_error("main): i32 { return 42; }", "expected '(' but got ')'");
+ assert_parser_error("main(: i32 { return 42; }", "expected ')' but got ':'");
+ assert_parser_error("main() i32 { return 42; }", "expected ':' but got 'TOKEN_NAME'");
+ assert_parser_error("main(): { return 42; }", "expected 'TOKEN_NAME' but got '{'");
+ assert_parser_error("main(): i32 return 42; }", "expected '{' but got 'TOKEN_NAME'");
+ assert_parser_error("main(): i32 { 42; }", "expected 'TOKEN_NAME' but got 'TOKEN_NUMBER'");
+ assert_parser_error("main(): i32 { return; }", "unexpected '; (;)' token");
+ assert_parser_error("main(): i32 { return 42;", "expected '}' but got end of file");
+ assert_parser_error("main(): beff { return 42; }", "type 'beff' is not defined");
// FIXME: once function calls are implemented, this error should inform that
// neither a variable or function call was found.
- assert_parser_error("main(): i32 { oxi 42; }" , "expected ':' but got 'TOKEN_NUMBER'");
+ assert_parser_error("main(): i32 { oxi 42; }", "expected ':' but got 'TOKEN_NUMBER'");
return MUNIT_OK;
}
@@ -227,9 +226,7 @@ static MunitTest tests[] = {
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};
-static const MunitSuite suite = {
- "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
-};
+static const MunitSuite suite = { "/parser", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[])
diff --git a/test/string_view_test.c b/test/string_view_test.c
index 130ec2c..d94f190 100644
--- a/test/string_view_test.c
+++ b/test/string_view_test.c
@@ -1,53 +1,50 @@
/*
-* 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/>.
+ */
#define MUNIT_ENABLE_ASSERT_ALIASES
#include "munit.h"
#include "string_view.h"
static MunitResult
-test_create_new(const MunitParameter params[],
- void *user_data_or_fixture)
+test_create_new(const MunitParameter params[], void *user_data_or_fixture)
{
char *str = "hello world";
string_view_t sv = string_view_new(str, strlen(str));
- assert_string_equal("hello world", (char *) sv.str);
+ assert_string_equal("hello world", (char *)sv.str);
assert_int(sv.size, ==, strlen(str));
return MUNIT_OK;
}
static MunitResult
-test_from_str(const MunitParameter params[],
- void *user_data_or_fixture)
+test_from_str(const MunitParameter params[], void *user_data_or_fixture)
{
char *str = "hello world";
string_view_t sv = string_view_from_str(str);
- assert_string_equal(str, (char *) sv.str);
+ assert_string_equal(str, (char *)sv.str);
assert_int(sv.size, ==, strlen(str));
return MUNIT_OK;
}
static MunitResult
-test_to_str(const MunitParameter params[],
- void *user_data_or_fixture)
+test_to_str(const MunitParameter params[], void *user_data_or_fixture)
{
char *str = "hello world";
@@ -62,8 +59,7 @@ test_to_str(const MunitParameter params[],
}
static MunitResult
-test_eq(const MunitParameter params[],
- void *user_data_or_fixture)
+test_eq(const MunitParameter params[], void *user_data_or_fixture)
{
string_view_t a = string_view_from_str("hello");
string_view_t b = string_view_from_str("world");
@@ -77,17 +73,13 @@ test_eq(const MunitParameter params[],
return MUNIT_OK;
}
-static MunitTest tests[] = {
- { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { "/test_from_str", test_from_str, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { "/test_to_str", test_to_str, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { "/test_eq", test_eq, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
-};
-
-static const MunitSuite suite = {
- "/string_view", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
-};
+static MunitTest tests[] = { { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_from_str", test_from_str, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_to_str", test_to_str, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_eq", test_eq, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
+
+static const MunitSuite suite = { "/string_view", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[])
diff --git a/test/vector_test.c b/test/vector_test.c
index e327735..11e2363 100644
--- a/test/vector_test.c
+++ b/test/vector_test.c
@@ -1,26 +1,25 @@
/*
-* 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/>.
+ */
#define MUNIT_ENABLE_ASSERT_ALIASES
#include "munit.h"
#include "vector.h"
static MunitResult
-test_create_new(const MunitParameter params[],
- void *user_data_or_fixture)
+test_create_new(const MunitParameter params[], void *user_data_or_fixture)
{
vector_t *vec = vector_new();
@@ -47,20 +46,19 @@ test_create_new(const MunitParameter params[],
}
static MunitResult
-test_push_back(const MunitParameter params[],
- void *user_data_or_fixture)
+test_push_back(const MunitParameter params[], void *user_data_or_fixture)
{
vector_t *vec = vector_new();
int number1 = 1;
int number2 = 2;
- vector_push_back(vec, (void *) &number1);
- vector_push_back(vec, (void *) &number2);
+ vector_push_back(vec, (void *)&number1);
+ vector_push_back(vec, (void *)&number2);
assert_int(vec->size, ==, 2);
- assert_int(*((int *) vector_at(vec, 0)), ==, number1);
- assert_int(*((int *) vector_at(vec, 1)), ==, number2);
+ assert_int(*((int *)vector_at(vec, 0)), ==, number1);
+ assert_int(*((int *)vector_at(vec, 1)), ==, number2);
vector_destroy(vec);
@@ -68,8 +66,7 @@ test_push_back(const MunitParameter params[],
}
static MunitResult
-test_vector_push_back_until_resize(const MunitParameter params[],
- void *user_data_or_fixture)
+test_vector_push_back_until_resize(const MunitParameter params[], void *user_data_or_fixture)
{
vector_t *vec = vector_new();
@@ -77,17 +74,17 @@ test_vector_push_back_until_resize(const MunitParameter params[],
for (int i = 0; i < VECTOR_INITIAL_CAPACITY; ++i) {
items[i] = 1;
- vector_push_back(vec, (void *) &items[i]);
+ vector_push_back(vec, (void *)&items[i]);
}
items[VECTOR_INITIAL_CAPACITY] = 1;
- vector_push_back(vec, (void *) &items[VECTOR_INITIAL_CAPACITY]);
+ vector_push_back(vec, (void *)&items[VECTOR_INITIAL_CAPACITY]);
assert_int(vec->size, ==, VECTOR_INITIAL_CAPACITY + 1);
assert_int(vec->capacity, ==, VECTOR_INITIAL_CAPACITY * 2);
for (int i = 0; i < vec->size; ++i) {
- assert_int(*((int *) vec->items[i]), ==, 1);
+ assert_int(*((int *)vec->items[i]), ==, 1);
}
vector_destroy(vec);
@@ -95,17 +92,18 @@ test_vector_push_back_until_resize(const MunitParameter params[],
return MUNIT_OK;
}
-static MunitTest tests[] = {
- { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { "/test_push_back", test_push_back, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
- { "/test_vector_push_back_until_resize", test_vector_push_back_until_resize, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+static MunitTest tests[] = { { "/test_create_new", test_create_new, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_push_back", test_push_back, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+ { "/test_vector_push_back_until_resize",
+ test_vector_push_back_until_resize,
+ NULL,
+ NULL,
+ MUNIT_TEST_OPTION_NONE,
+ NULL },
- { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
-};
+ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
-static const MunitSuite suite = {
- "/vector", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE
-};
+static const MunitSuite suite = { "/vector", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
int
main(int argc, char *argv[])