Skip to content

Commit

Permalink
My proposal for the c group.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerbenAaltink committed Aug 30, 2024
0 parents commit e24f4df
Show file tree
Hide file tree
Showing 13 changed files with 644 additions and 0 deletions.
192 changes: 192 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
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: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
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
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
...

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
*.tar
*.pdf
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
CC = clang
CFLAGS = -Wall -Wextra -g -pedantic

EXAMPLES = example1_traditional example2_traditional example3_traditional
SOURCES = example1_traditional.c example1_new.c example2_traditional.c example2_new.c example3_new.c example3_traditional.c

all: build_and_run_all

build_and_run_all: $(EXAMPLES) run_all

$(EXAMPLES): %: %.c
@echo "Compiling $<..."
-$(CC) $(CFLAGS) $< -o $@ || true
@echo ""

run_all: $(EXAMPLES)
@for exe in $(EXAMPLES); do \
echo "Running $$exe..."; \
./$$exe && echo "" || true; \
done

run_%: %
@echo "Running $<..."
./$< || true

lint:
-clang-tidy *.c
-cppcheck *.c

archive:
tar -cvf ifwhile.tar $(SOURCES) Makefile README.pdf

.PHONY: run_example1_traditional run_example1_new run_example2_traditional run_example2_new run_example3_new run_example3_traditional clean build_and_run_all run_all lint archive docs

clean:
rm -f $(EXAMPLES)

docs:
pandoc README.md -o README.pdf --pdf-engine=xelatex \
-V mainfont="Roboto" \
-V monofont="Ubuntu Mono" \
-V geometry:margin=1in \
-V colorlinks \
-V linkcolor=blue \
-V urlcolor=blue \
-V textcolor=white \
-V backgroundcolor=black \
-V codebackgroundcolor="#2E2E2E" \
-V quotebackgroundcolor="#1E1E1E"

102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# PROPOSALS

This document and source code contain my proposal(s) and examples for a change in the C language.

The conditional while (first proposal) is what I'm serious about. It is an update to an existing feature (while) but shouldn't break any existing code base.

The ifwhile (second proposal) is an addition to the first one and is a whole new feature. After writing the example files I wasn't so fond of this feature myself anymore. But I included it as well. Maybe you find it interesting.

The source code contains a Makefile that can be executed to run all (traditional) examples by just executing `make`.

## Conditional while

The conditional while is the traditional while that only returns a bool if ran at least once.

### Usage
```
/*
Returns a part containing only one format from the input parameter.
E.g: abc123 translates to abc. 123abc translates to 123.
*/
bool lex(char * value, char * output){
if(while(isalpha(*value)){
// Process alpha lexing
// update output with valid content
}) else if(while(isdigit(*value)){
// Process number lexing
// update output with valid content
}) else {
return false;
}
return true;
}
```
This is what I required for my interpreter.
Without the conditional while you have to check twice for isalpha or isdigit.
Also, you'll have an extra nesting:
```
bool lex(char * value, char * output){
if(isalpha(*value)){
while(isalpha(*value)){
// Process alpha lexing
// update output with valid content
}
return true;
}else if(isdigit(*value)){
while(isdigit(*value)){
// Process number lexing
// update output with valid content
}
return true;
}
return false;
}
```
and setting the `is_alpha` many times or I had to do a special check before it.
I'm quite positive about this example. Also, I expect existing source code not to break because of this.

### Example files:
- example3_new.c
- example3_traditional.c

## Ifwhile

The ifwhile is a while that gets executed like an if. Difference with the conditional while is that it executes the code behind it if it's true under the same rules as an if statement.

### Usage
```
/*
Returns a part containing only one format from the input parameter.
E.g: abc123 translates to abc. 123abc translates to 123.
*/
bool lex(char * value, char * output){
ifwhile(isalpha(*value)){
// Process alpha lexing
// update output with valid content
}else ifwhile(isdigit(*value)){
// Process number lexing
// update output with valid content
} else {
return false;
}
return true;
}
```

## Example files:
- example1_new.c
- example1_traditional.c
- example2_new.c
- example2_traditional.c

# Summary sources
```
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 6 0 6 291
-------------------------------------------------------------------------------
SUM: 6 0 6 291
-------------------------------------------------------------------------------
```
47 changes: 47 additions & 0 deletions example1_new.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <ctype.h> // isdigit isalpha
#include <stdbool.h> // bool
#include <stdio.h> // printf
#include <string.h> // strlen
void process_string(char *value) { printf("String processed: %s\n", value); }
void process_number(char *value) { printf("Number processed: %s\n", value); }
bool lex(const char *value) {
return ifwhile (*value) {
const unsigned int temp_length = 1984;
char temp[temp_length];
char *tptr = temp;
unsigned int length = 0;
success = ifwhile (isalpha(*value)) {
*tptr = *value;
tptr++;
value++;
length++;
}{
*temp = '\0';
tptr -= length;
process_string(tptr);
value++;
} else ifwhile (isdigit(*value)) {
*temp = *value;
temp++;
value++;
length++;
}{
*tptr = '\0';
tptr -= length;
process_number(tptr);
value++;
} else {
// Unexpected data, skip
value++;
}
}
}
int main() {
char value[] = "1337*cat+42-dog_1984/pony";
if (lex(value)) {
printf("Lex result: %s\n", "success!");
} else {
printf("Lex result: %s\n", "failure!");
}
return 0;
}
Binary file added example1_traditional
Binary file not shown.
Loading

0 comments on commit e24f4df

Please sign in to comment.