From b0d5c39d8acbd1429990e383ddd67226fc7bc0f9 Mon Sep 17 00:00:00 2001 From: Fabrice Date: Mon, 2 Mar 2026 11:59:47 +0100 Subject: working on source control --- .clang-format | 334 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ .gitignore | 3 +- format.sh | 2 + makefile | 35 +++--- src/common.cc | 75 +++++++++++-- src/memory.cc | 22 ++-- src/source.cc | 8 +- src/token.cc | 14 ++- src/voidc.cc | 9 +- 9 files changed, 461 insertions(+), 41 deletions(-) create mode 100644 .clang-format create mode 100755 format.sh diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..2d373d0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,334 @@ +--- +Language: Cpp +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: true + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseArrows: false + AlignCaseColons: false +AlignConsecutiveTableGenBreakingDAGArgColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenCondOperatorColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveTableGenDefinitionColons: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionDeclarations: false + AlignFunctionPointers: false + PadOperators: false +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Never +AllowShortCaseExpressionOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: WithoutElse +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: true +AllowShortNamespacesOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AttributeMacros: + - __capability + - absl_nonnull + - absl_nullable + - absl_nullability_unknown +BinPackArguments: true +BinPackLongBracedList: true +BinPackParameters: BinPack +BitFieldColonSpacing: Both +BracedInitializerIndentWidth: -1 +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakAfterReturnType: None +BreakArrays: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: Attach +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTemplateCloser: false +BreakBeforeTernaryOperators: true +BreakBinaryOperations: Never +BreakConstructorInitializers: BeforeColon +BreakFunctionDefinitionParameters: false +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +BreakTemplateDeclarations: Yes +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +EnumTrailingComma: Leave +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^<.*\.h>' + Priority: 1 + SortPriority: 0 + CaseSensitive: false + - Regex: '^<.*' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 3 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '([-_](test|unittest))?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: true +IndentExportBlock: true +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 2 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLines: + AtEndOfFile: false + AtStartOfBlock: false + AtStartOfFile: true +KeepFormFeed: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MainIncludeChar: Quote +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +OneLineFormatOffRegex: '' +PackConstructorInitializers: NextLine +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakBeforeMemberAccess: 150 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +PPIndentWidth: -1 +QualifierAlignment: Leave +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + - ParseTestProto + - ParsePartialTestProto + CanonicalDelimiter: pb + BasedOnStyle: google +ReferenceAlignment: Pointer +ReflowComments: Always +RemoveBracesLLVM: false +RemoveEmptyLinesInUnwrappedLines: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: + Enabled: true + IgnoreCase: false +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterOperatorKeyword: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterNot: false + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + ExceptDoubleParentheses: false + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Auto +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TableGenBreakInsideDAGArg: DontBreak +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +WrapNamespaceBodyWithEmptyLines: Leave +... + diff --git a/.gitignore b/.gitignore index 931f0b9..765fd78 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,5 @@ dkms.conf # Custom *.json -/voidc \ No newline at end of file +/voidc +.cache/ diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..2951e7c --- /dev/null +++ b/format.sh @@ -0,0 +1,2 @@ +#!/bin/bash +clang-format -i $(find src -name "*.h" -o -name "*.cc") diff --git a/makefile b/makefile index dacc687..c8b11e2 100644 --- a/makefile +++ b/makefile @@ -2,26 +2,14 @@ .DEFAULT_GOAL := all # ====== Configurations and Binaries ====== # +RELEASE ?= 0 + CC := clang CXX := clang++ CMAKE := cmake RM := rm -f PRINTF := printf -# ====== Flags ====== # -CCXX_FLAGS := \ - -Wall -Wextra -Werror -pedantic \ - -MMD -MP -I$(SRC_DIR) - -CXX_FLAGS := -std=c++14 \ - $(CCXX_FLAGS) -fno-rtti -fno-exceptions - -LD_FLAGS := -lc -nostdlib++ - -MI_FLAGS := -G"Unix Makefiles" -DCMAKE_C_COMPILER=$(CC) \ - -DMI_BUILD_TESTS=OFF -DMI_BUILD_STATIC=OFF -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=ON \ - -DMI_OVERRIDE=ON -DMI_NO_USE_CXX=ON -DMI_DEBUG=0 - # ====== Sources ====== # TOP_DIR := . SRC_DIR := $(TOP_DIR)/src @@ -35,6 +23,25 @@ VOIDC_O := $(VOIDC_CXX:.cc=.o) VOIDC_D := $(VOIDC_CXX:.cc=.d) VOIDC := $(TOP_DIR)/voidc +# ====== Flags ====== # +CXX_FLAGS := -std=c++14 -Wall -Wextra -Werror -pedantic \ + -MMD -MP -fno-rtti -fno-exceptions \ + -I$(SRC_DIR) + +LD_FLAGS := -lc -nostdlib++ + +MI_FLAGS := -G"Unix Makefiles" -DCMAKE_C_COMPILER=$(CC) \ + -DMI_BUILD_TESTS=OFF -DMI_BUILD_STATIC=OFF -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=ON \ + -DMI_OVERRIDE=ON -DMI_NO_USE_CXX=ON + +ifeq ($(RELEASE),1) + CXX_FLAGS += -flto=auto -O2 + MI_FLAGS += -DMI_DEBUG=0 +else + CCXX_FLAGS += -g3 + MI_FLAGS += -DMI_DEBUG=2 +endif + # ====== Rules ====== # .PHONY: all all: $(VOIDC) diff --git a/src/common.cc b/src/common.cc index ef790b2..141b079 100644 --- a/src/common.cc +++ b/src/common.cc @@ -1,8 +1,10 @@ #ifndef COMMON_CC #define COMMON_CC +#include #include #include +#include /* typedefs for common types */ @@ -19,9 +21,36 @@ typedef int64_t i64; typedef uintptr_t usize; typedef intptr_t isize; +/* intrinsics */ +#define likely(cond) __builtin_expect(!!(cond), 1) +#define unlikely(cond) __builtin_expect(!!(cond), 0) + +/* error handling sort of */ +[[noreturn]] void panic_impl(const char* file, i32 line, const char* fmt, ...) { + fprintf(stderr, "PANIC at %s:%d: ", file, line); + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + fputc('\n', stderr); + fflush(stderr); + abort(); +} + +#define panic(fmt, ...) panic_impl(__FILE__, __LINE__, fmt, __VA_ARGS__) +#define spanic(msg) panic("%s", msg) + +#undef assert +#define assert(cond) \ + do { \ + if (unlikely(!(cond))) panic("assertion failed: %s", #cond); \ + } while (0) + /* slice and string handling */ -template +template struct Slice { T* ptr; usize length; @@ -30,21 +59,53 @@ struct Slice { Slice(T* ptr, usize length) : ptr(ptr), length(length) {} T* operator[](usize index) { - return ptr + index; + assert(index < this->length); + return this->ptr + index; } - inline usize size() const { - return this->length * sizeof(T); + const T* operator[](usize index) const { + assert(index < this->size()); + return this->ptr + index; } + + inline usize size() const { return this->length * sizeof(T); } }; -template +template bool slice_write(const Slice* slice, FILE* stream) { usize rc = fwrite(slice->ptr, sizeof(T), slice->length, stream); - if(rc == 0 || slice->size() > rc) return false; + if (rc == 0 || slice->size() > rc) return false; return true; -} +} typedef Slice String; +/* linked list */ + +struct Link { + Link* next; + Link* prev; + + Link(Link* next, Link* prev) : next(next), prev(prev) {} +}; + +struct List { + Link head; + Link tail; +}; + +static inline void link_after(Link* prev, Link* nlink) { + assert(prev != nullptr); + assert(nlink != nullptr); + + Link* next = prev->next; + + nlink->prev = prev; + nlink->next = next; + prev->next = nlink; + + if(next == nullptr) return; + next->prev = nlink; +} + #endif diff --git a/src/memory.cc b/src/memory.cc index 518b761..2d83413 100644 --- a/src/memory.cc +++ b/src/memory.cc @@ -1,10 +1,12 @@ #ifndef MEMORY_CC #define MEMORY_CC -#include "common.cc" -#include #include +#include + +#include "common.cc" + typedef u8* (*Allocator_Allocate)(u8* self, usize length, usize align); typedef void (*Allocator_Deallocate)(u8* self, u8* ptr); @@ -33,14 +35,15 @@ static inline void heap_deallocate(u8* self, u8* ptr) { } Allocator* heap_allocator() { - thread_local Allocator HEAP_ALLOCATOR = {nullptr, heap_allocate, heap_deallocate}; + thread_local Allocator HEAP_ALLOCATOR = {nullptr, heap_allocate, + heap_deallocate}; return &HEAP_ALLOCATOR; } -template -bool slice_copy(Allocator* allocator, const Slice *source, Slice *out) { +template +bool slice_copy(Allocator* allocator, const Slice* source, Slice* out) { T* new_ptr = allocate(allocator, source->size(), alignof(T)); - if(new_ptr == nullptr) return false; + if (new_ptr == nullptr) return false; memcpy((u8*)new_ptr, (u8*)source->ptr, source->length); @@ -49,9 +52,9 @@ bool slice_copy(Allocator* allocator, const Slice *source, Slice *out) { return true; } -template -void slice_deallocate(Allocator* allocator, Slice *slice) { - if(slice->ptr == nullptr) return; +template +void slice_deallocate(Allocator* allocator, Slice* slice) { + if (slice->ptr == nullptr) return; deallocate(allocator, (u8*)slice->ptr); @@ -60,4 +63,3 @@ void slice_deallocate(Allocator* allocator, Slice *slice) { } #endif - diff --git a/src/source.cc b/src/source.cc index df3a90e..9234ea9 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1,8 +1,12 @@ #ifndef SOURCE_CC #define SOURCE_CC -struct Source { - +#include "common.cc" +#include "memory.cc" + +struct Buffer { + String content; + String file; }; #endif diff --git a/src/token.cc b/src/token.cc index f5cf1de..4a218ef 100644 --- a/src/token.cc +++ b/src/token.cc @@ -4,14 +4,22 @@ #include "common.cc" #define TOKEN_KINDS_NOLEX \ - X(Eof) \ - X(Invalid_Char) \ + X(Eof) \ + X(Invalid_Char) \ X(Invalid_Literal) +#define TOKEN_KINDS_SLEX X(Hash, '#') + +#define TOKEN_KIND(name) Token_Kind_##name + enum Token_Kind { -#define X(name) Token_Kind_##name, +#define X(name) TOKEN_KIND(name), TOKEN_KINDS_NOLEX #undef X + +#define X(name, _) TOKEN_KIND(name), + TOKEN_KINDS_SLEX +#undef X }; struct Token { diff --git a/src/voidc.cc b/src/voidc.cc index e69b8a6..8110184 100644 --- a/src/voidc.cc +++ b/src/voidc.cc @@ -1,7 +1,8 @@ -#include "memory.cc" -#include "common.cc" #include +#include "common.cc" +#include "memory.cc" + static const char* SOURCE = R"( #include @@ -10,13 +11,13 @@ int main() { } )"; -static const String SOURCE_STRING = { (u8*)SOURCE, strlen(SOURCE) }; +static const String SOURCE_STRING = {(u8*)SOURCE, strlen(SOURCE)}; int main() { String source; slice_copy(heap_allocator(), &SOURCE_STRING, &source); slice_write(&source, stdout); - + slice_deallocate(heap_allocator(), &source); } -- cgit v1.2.3