Skip to content

Commit 600da6a

Browse files
authored
Merge pull request #45 from quark-programming/dev
0.3.1c With C Keyword Fix and Include Flag
2 parents f1c64cb + 4ad9a86 commit 600da6a

7 files changed

Lines changed: 81 additions & 18 deletions

File tree

.gitignore

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
# MacOS
55
.DS_Store
66

7-
# CLion builds
7+
# Debugger Tools
88
*.dSYM/
99

10-
# Local testing
10+
# bin
1111
qc
12-
testing/
12+
main
13+
14+
# Local Testing
1315
tests/
14-
build/
16+
testing/
1517

16-
# Unit tests
18+
# Unit Tests
1719
unit-tests/unit-tests
1820

1921
# Build
20-
build/
22+
build/

main

-34.4 KB
Binary file not shown.

src/compiler/righthand/declaration/function_declaration.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ static void function_declaration_compiler_hoisted(FunctionDeclaration* const sel
1919
if(i) strf(&declaration_line, ", ");
2020

2121
compile(self->arguments.data[i].type, &declaration_line, compiler);
22-
if(!hoisted) strf(&declaration_line, " %.*s", PRINT(self->arguments.data[i].identifier));
22+
if(!hoisted) {
23+
strf(&declaration_line, " ");
24+
compile_identifier_base(self->arguments.data[i].identifier, &declaration_line);
25+
}
2326
}
2427
strf(&declaration_line, hoisted ? ");" : ") {");
2528
push(&compiler->sections.data[(size_t) hoisted ? (size_t) hoisted : section].lines, declaration_line);

src/compiler/righthand/declaration/identifier.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@
22

33
#include "../../../parser/type/stringify_type.h"
44

5+
StringHashSet global_c_keywords = 0;
6+
7+
void populate_global_c_keywords() {
8+
// https://en.cppreference.com/w/c/keyword.html
9+
char* keywords[] = {
10+
"alignas", "alignas", "alignof", "auto", "bool", "break", "case", "char", "const", "constexpr", "continue",
11+
"default", "do", "double", "else", "enum", "extern", "false", "float", "for", "goto", "if", "inline", "int",
12+
"long", "nullptr", "register", "restrict", "return", "short", "signed", "sizeof", "static", "static_assert",
13+
"struct", "switch", "thread_local", "true", "typedef", "typeof", "typeof_unequal", "union", "unsigned", "void",
14+
"volatile", "while", "_Alignas", "_Alignof", "_Atomic", "_BitInt", "_Bool", "_Complex", "_Decimal128",
15+
"_Decimal32", "_Decimal64", "_Generic", "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local",
16+
};
17+
18+
for(int i = 0; i < sizeof(keywords) / sizeof(char*); i++) {
19+
put(&global_c_keywords, ((String) { strlen(keywords[i]), 0, keywords[i] }));
20+
}
21+
}
22+
23+
void compile_identifier_base(const String base, String* line) {
24+
strf(line, "%.*s", PRINT(base));
25+
26+
if(get(global_c_keywords, base)) {
27+
strf(line, "_");
28+
}
29+
}
30+
531
void compile_identifier(const Identifier identifier, String* line) {
632
if(!identifier.is_external) {
733
if(identifier.parent_scope && identifier.parent_scope->id == NodeFunctionDeclaration) {
@@ -20,7 +46,7 @@ void compile_identifier(const Identifier identifier, String* line) {
2046
}
2147

2248
// TODO: change `PRINT` macro to `FMT` or `STRFMT`
23-
strf(line, "%.*s", PRINT(identifier.base));
49+
compile_identifier_base(identifier.base, line);
2450

2551
if(identifier.parent_declaration->generics.type_arguments_stack.size && !identifier.is_external) {
2652
stringify_generics(line, last(identifier.parent_declaration->generics.type_arguments_stack),

src/compiler/righthand/declaration/identifier.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
#include "../../compiler.h"
55

6+
7+
typedef HashMap(UsableVoid) StringHashSet;
8+
9+
extern StringHashSet global_c_keywords;
10+
11+
void compile_identifier_base(String base, String* line);
12+
613
void compile_identifier(Identifier identifier, String* line);
714

15+
void populate_global_c_keywords();
16+
817
#endif

src/compiler/righthand/declaration/variable_declaration.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ static void compile_struct_declaration(StructType* self, String* line, Compiler*
1010
strf(&typedef_line, " { ");
1111
for(size_t i = 0; i < self->fields.size; i++) {
1212
compile(self->fields.data[i].type, &typedef_line, compiler);
13-
strf(&typedef_line, " %.*s; ", PRINT(self->fields.data[i].identifier));
13+
strf(&typedef_line, " ");
14+
compile_identifier_base(self->fields.data[i].identifier, &typedef_line);
15+
strf(&typedef_line, "; ");
1416
}
1517
strf(&typedef_line, "};");
1618

src/main.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "parser/statement/statement.h"
88
#include "parser/type/types.h"
99
#include "parser/keywords.h"
10+
#include "compiler/righthand/declaration/identifier.h"
1011

1112
#define QUARK_VERSION "0.3.1b"
1213
#define QUARK_STABILITY "untested"
@@ -47,23 +48,38 @@ int main(int argc, char** argv) {
4748
CStringVector input_files = { 0 };
4849
char* output_file = "out.c";
4950

51+
CStringVector include_paths = { 0 };
52+
5053
int flag;
51-
while((flag = clflag()))
54+
while((flag = clflag())) {
5255
switch(flag) {
5356
case 'h':
5457
printf(help_message, name, name);
5558
return 0;
5659
case 'v':
5760
puts("Quark Compiler version " QUARK_VERSION " \33[90m" QUARK_STABILITY "\33[0m");
5861
return 0;
59-
case -1: push(&input_files, clarg());
62+
case -1:
63+
push(&input_files, clarg());
64+
break;
65+
case 'o':
66+
output_file = clarg();
6067
break;
61-
case 'o': output_file = clarg();
68+
case 'l':
69+
global_library_path = clarg();
6270
break;
63-
case 'l': global_library_path = clarg();
71+
case 'i':
72+
push(&include_paths, clarg());
6473
break;
6574
default: panicf("unknown flag '-%c'\n hint: %s -h\n", flag, name);
6675
}
76+
}
77+
78+
push(&include_paths, "stdint.h");
79+
push(&include_paths, "stdio.h");
80+
push(&include_paths, "string.h");
81+
push(&include_paths, "stdlib.h");
82+
push(&include_paths, "stdbool.h");
6783

6884
if(input_files.size == 0) {
6985
panicf("missing input files\n hint: %s -h\n", name);
@@ -84,18 +100,23 @@ int main(int argc, char** argv) {
84100

85101
push(&compiler.sections, (CompilerSection) { 0 });
86102
push(&compiler.sections, (CompilerSection) { 0 });
87-
push(&compiler.sections.data[0].lines, String("#include <stdint.h>"));
88-
push(&compiler.sections.data[0].lines, String("#include <stdio.h>"));
89-
push(&compiler.sections.data[0].lines, String("#include <string.h>"));
90-
push(&compiler.sections.data[0].lines, String("#include <stdlib.h>"));
91-
push(&compiler.sections.data[0].lines, String("#include <stdbool.h>"));
103+
104+
for(size_t i = 0; i < include_paths.size; i++) {
105+
push(&compiler.sections.data[0].lines, strf(0, "#include \"%s\"", include_paths.data[i]));
106+
}
107+
// push(&compiler.sections.data[0].lines, String("#include <stdint.h>"));
108+
// push(&compiler.sections.data[0].lines, String("#include <stdio.h>"));
109+
// push(&compiler.sections.data[0].lines, String("#include <string.h>"));
110+
// push(&compiler.sections.data[0].lines, String("#include <stdlib.h>"));
111+
// push(&compiler.sections.data[0].lines, String("#include <stdbool.h>"));
92112

93113
push(&parser.stack, new_scope(NULL));
94114

95115
FunctionDeclaration* entry = entry_declaration();
96116
push(&parser.stack, entry->body);
97117

98118
populate_keyword_table();
119+
populate_global_c_keywords();
99120

100121
push(&entry->body->children, eval_w("lib::std", "import lib::std;", &parser, &statement));
101122
const NodeVector body = collect_until(&parser, &statement, 0, 0);

0 commit comments

Comments
 (0)