Skip to content

Commit

Permalink
Add clang formatting, minor changes to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-is-coding committed May 10, 2023
1 parent 4b395e5 commit 2c73e4c
Show file tree
Hide file tree
Showing 26 changed files with 1,239 additions and 994 deletions.
135 changes: 135 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Always
...
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ Here is an example of a "Hello, world!" program in Hadron:

```c
func main {
log("Hello, world!")
log("Hello, world!")
}
```

And here is an example of a function that calculates the nth Fibonacci number:

```c
func fib(n) {
if (n < 2) return n;
else return fib(n-1) + fib(n-2);
if (n < 2) return n;
else return fib(n-1) + fib(n-2);
}

func main() {
Expand Down
93 changes: 55 additions & 38 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,57 @@
static boolean check(Array *, boolean);
static boolean isLang(char *);
static boolean isFile(char *);
static Lang parseLang(char *);
static void init(int, char *[], Array *);
static Lang parseLang(char *);
static void init(int, char *[], Array *);

static struct Args {
Lang lang;
Mode mode;
Lang lang;
Mode mode;
boolean set;
char *file;
char *file;
} args;

void init(int argc, char *argv[], Array *errs) {
args.lang = L_UNKN;
args.mode = INTERPRET;
args.set = false;
args.set = false;
args.file = NULL;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--compile") == 0) {
if (args.set && args.mode == INTERPRET)
pushArray(errs, clierror(3, "\x1b[96m--interpret", " and \x1b[96m--compile", " are mutually exclusive"));
pushArray(errs,
clierror(3, "\x1b[96m--interpret", " and \x1b[96m--compile",
" are mutually exclusive"));
args.mode = COMPILE;
args.set = true;
} else if (strcmp(argv[i], "-i") == 0 || strcmp(argv[i], "--interpret") == 0) {
if (args.lang) pushArray(errs, clierror(1, "language is not configurable in interpreter"));
args.set = true;
} else if (strcmp(argv[i], "-i") == 0 ||
strcmp(argv[i], "--interpret") == 0) {
if (args.lang)
pushArray(errs,
clierror(1, "language is not configurable in interpreter"));
if (args.set && args.mode == COMPILE)
pushArray(errs, clierror(3, "\x1b[96m--interpret", " and \x1b[96m--compile", " are mutually exclusive"));
pushArray(errs,
clierror(3, "\x1b[96m--interpret", " and \x1b[96m--compile",
" are mutually exclusive"));
args.mode = INTERPRET;
args.set = true;
} else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--lang") == 0) {
if (isLang(argv[i+1])) args.lang = parseLang(argv[++i]);
} else if (isFile(argv[i])) args.file = argv[i];
args.set = true;
} else if (strcmp(argv[i], "-l") == 0 ||
strcmp(argv[i], "--lang") == 0) {
if (isLang(argv[i + 1])) args.lang = parseLang(argv[++i]);
} else if (isFile(argv[i]))
args.file = argv[i];
};
if (args.mode == INTERPRET && args.lang) pushArray(errs, clierror(1, "language is not available in interpreter"));
if (args.mode == COMPILE && !args.file) pushArray(errs, clierror(1, "you should provide at least one file to compile"));
if (args.mode == COMPILE && !args.lang) pushArray(errs, clierror(1, "you should choose one language to compile to"));
if (args.mode == INTERPRET && !args.file) pushArray(errs, clierror(1, "REPL interpret mode not available yet"));
if (args.mode == INTERPRET && args.lang)
pushArray(
errs, clierror(1, "language is not available in interpreter"));
if (args.mode == COMPILE && !args.file)
pushArray(errs,
clierror(1, "you should provide at least one file to compile"));
if (args.mode == COMPILE && !args.lang)
pushArray(
errs, clierror(1, "you should choose one language to compile to"));
if (args.mode == INTERPRET && !args.file)
pushArray(errs, clierror(1, "REPL interpret mode not available yet"));
}

int main(int argc, char *argv[]) {
Expand All @@ -58,13 +74,16 @@ int main(int argc, char *argv[]) {
size_t fsize = ftell(fp);
rewind(fp);

char *contents = malloc(fsize+1);
char *contents = malloc(fsize + 1);

if (contents == NULL) pushArray(errors, clierror(1, "could not allocate enough space for file"));
if (contents == NULL)
pushArray(
errors, clierror(1, "could not allocate enough space for file"));
if (check(errors, false)) return -1;

size_t read = fread(contents, 1UL, fsize, fp);
if (read < fsize) pushArray(errors, clierror(1, "could not read file entirely"));
if (read < fsize)
pushArray(errors, clierror(1, "could not read file entirely"));
if (check(errors, true)) return -1;

contents[fsize] = 0;
Expand All @@ -91,40 +110,38 @@ boolean check(Array *errors, boolean drop) {
printErrors(errors);
if (drop) freeArray(errors);
return 1;
}; if (drop) freeArray(errors);
};
if (drop) freeArray(errors);
return 0;
}

boolean isLang(char *arg) {
for (size_t i = 0; i < strlen(arg); i++) {
if (
!((arg[i] >= 'a' && arg[i] <= 'z')
|| (arg[i] >= 'A' && arg[i] <= 'Z')
|| arg[i] == '+')
) return false;
if (!((arg[i] >= 'a' && arg[i] <= 'z') ||
(arg[i] >= 'A' && arg[i] <= 'Z') || arg[i] == '+'))
return false;
}
return true;
}

boolean isFile(char *arg) {
for (size_t i = 0; i < strlen(arg); i++) {
if (!(
(arg[i] >= 'a' && arg[i] <= 'z')
|| (arg[i] >= 'A' && arg[i] <= 'Z')
|| (arg[i] >= '0' && arg[i] <= '9')
|| arg[i] == '+' || arg[i] == '.'
|| arg[i] == '$' || arg[i] == '~'
|| arg[i] == '-' || arg[i] == '\\'
|| arg[i] == '/' || arg[i] == '_'
)) return false;
if (!((arg[i] >= 'a' && arg[i] <= 'z') ||
(arg[i] >= 'A' && arg[i] <= 'Z') ||
(arg[i] >= '0' && arg[i] <= '9') || arg[i] == '+' ||
arg[i] == '.' || arg[i] == '$' || arg[i] == '~' ||
arg[i] == '-' || arg[i] == '\\' || arg[i] == '/' ||
arg[i] == '_'))
return false;
}
return true;
}

Lang parseLang(char *arg) {
if (strcasecmp(arg, "js") == 0 || strcasecmp(arg, "javascript") == 0) {
return L_JS;
} else if (strcasecmp(arg, "asm") == 0 || strcasecmp(arg, "assembly") == 0) {
} else if (strcasecmp(arg, "asm") == 0 ||
strcasecmp(arg, "assembly") == 0) {
return L_ASM;
} else if (strcasecmp(arg, "c") == 0) {
return L_C;
Expand Down
14 changes: 6 additions & 8 deletions main.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "src/tokenizer.h"
#include "src/errors.h"
#include "src/parser.h"
#include "src/tokenizer.h"
#include "src/types.h"
#include "src/errors.h"
#include "src/util/print.h"
#include "src/util/str.h"
#include "src/compilers/c.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

typedef enum __attribute__((__packed__)) Langs {
L_UNKN,
Expand All @@ -17,7 +15,7 @@ typedef enum __attribute__((__packed__)) Langs {
L_ASM
} Lang;

typedef enum __attribute__((__packed__))Modes {
typedef enum __attribute__((__packed__)) Modes {
COMPILE,
INTERPRET,
DEBUG
Expand Down
Loading

0 comments on commit 2c73e4c

Please sign in to comment.