Skip to content

Commit d75652c

Browse files
authored
Merge pull request #47 from quark-programming/dev
0.3.1d untested
2 parents 600da6a + 0291ae3 commit d75652c

13 files changed

Lines changed: 119 additions & 13 deletions

File tree

src/compiler/compiler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void compile(void* void_node, String* line, Compiler* compiler) {
3535
[NodeControlStatement] = &comp_ControlStatement,
3636
[NodeStructType] = &comp_StructType,
3737
[NodeStructLiteral] = &comp_StructLiteral,
38+
[NodeCast] = &comp_Cast,
3839
};
3940

4041
Node* const node = void_node;

src/compiler/literal/literals.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ void comp_StructLiteral(void* void_self, String* line, Compiler* compiler) {
3838
}
3939
strf(line, " }");
4040
}
41+
42+
void comp_Cast(void* void_self, String* line, Compiler* compiler) {
43+
Cast* const self = void_self;
44+
45+
strf(line, "(");
46+
compile(self->type, line, compiler);
47+
strf(line, ") ");
48+
compile(self->value, line, compiler);
49+
}

src/compiler/literal/literals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ void comp_External(void* void_self, String* line, Compiler* compiler);
1111

1212
void comp_StructLiteral(void* void_self, String* line, Compiler* compiler);
1313

14+
void comp_Cast(void* void_self, String* line, Compiler* compiler);
15+
1416
#endif

src/compiler/literal/wrapper.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ void comp_Variable(void* void_self, String* line, Compiler* compiler) {
1010
Node* const const_value = self->Variable.declaration->const_value;
1111
if(const_value && const_value->flags & fConstExpr) {
1212
if(!(self->flags & fType)) {
13-
strf(line, "(");
13+
strf(line, "((");
1414
compile(self->type, line, compiler);
1515
strf(line, ") ");
1616
}
1717

1818
compile(const_value, line, compiler);
19+
20+
if(!(self->flags & fType)) {
21+
strf(line, ")");
22+
}
1923
} else {
2024
compile_identifier(self->Variable.declaration->identifier, line);
2125
}

src/compiler/righthand/declaration/identifier.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void populate_global_c_keywords() {
2020
}
2121
}
2222

23+
DeclarationHashMap global_function_identifiers = 0;
24+
2325
void compile_identifier_base(const String base, String* line) {
2426
strf(line, "%.*s", PRINT(base));
2527

@@ -29,27 +31,53 @@ void compile_identifier_base(const String base, String* line) {
2931
}
3032

3133
void compile_identifier(const Identifier identifier, String* line) {
34+
String result = { 0 };
35+
3236
if(!identifier.is_external) {
3337
if(identifier.parent_scope && identifier.parent_scope->id == NodeFunctionDeclaration) {
3438
const Identifier parent_ident = identifier.parent_scope->FunctionDeclaration.identifier;
35-
compile_identifier(parent_ident, line);
36-
strf(line, "__");
39+
compile_identifier(parent_ident, &result);
40+
strf(&result, "__");
3741
}
3842

3943
if(identifier.parent_scope && identifier.parent_scope->id == NodeStructType
4044
&& !(identifier.parent_declaration->id == NodeVariableDeclaration
4145
&& !(identifier.parent_declaration->type->flags & fType))) {
4246
const Identifier parent_ident = ((StructType*) (void*) identifier.parent_scope)->parent->identifier;
43-
compile_identifier(parent_ident, line);
44-
strf(line, "__");
47+
compile_identifier(parent_ident, &result);
48+
strf(&result, "__");
4549
}
4650
}
4751

4852
// TODO: change `PRINT` macro to `FMT` or `STRFMT`
49-
compile_identifier_base(identifier.base, line);
53+
compile_identifier_base(identifier.base, &result);
5054

5155
if(identifier.parent_declaration->generics.type_arguments_stack.size && !identifier.is_external) {
52-
stringify_generics(line, last(identifier.parent_declaration->generics.type_arguments_stack),
56+
stringify_generics(&result, last(identifier.parent_declaration->generics.type_arguments_stack),
5357
StringifyAlphaNumeric);
5458
}
59+
60+
if(identifier.function_declaration_counter) strf(&result, "%u", identifier.function_declaration_counter);
61+
62+
if(!identifier.function_declaration_counter && identifier.parent_declaration->id == NodeFunctionDeclaration) {
63+
const size_t initial_size = result.size;
64+
unsigned counter = 0;
65+
Declaration** existing_declaration;
66+
67+
while(((existing_declaration = get(global_function_identifiers, result)))
68+
&& *existing_declaration != identifier.parent_declaration) {
69+
result.size = initial_size;
70+
strf(&result, "%u", ++counter);
71+
}
72+
73+
if(counter) {
74+
identifier.parent_declaration->identifier.function_declaration_counter = counter;
75+
}
76+
77+
if(!existing_declaration) {
78+
put(&global_function_identifiers, result, identifier.parent_declaration);
79+
}
80+
}
81+
82+
strf(line, "%.*s", PRINT(result));
5583
}

src/compiler/righthand/declaration/identifier.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

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

6-
76
typedef HashMap(UsableVoid) StringHashSet;
7+
typedef HashMap(Declaration*) DeclarationHashMap;
88

99
extern StringHashSet global_c_keywords;
10+
extern DeclarationHashMap global_function_identifiers;
1011

1112
void compile_identifier_base(String base, String* line);
1213

1314
void compile_identifier(Identifier identifier, String* line);
1415

1516
void populate_global_c_keywords();
1617

17-
#endif
18+
#endif

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "parser/keywords.h"
1010
#include "compiler/righthand/declaration/identifier.h"
1111

12-
#define QUARK_VERSION "0.3.1b"
12+
#define QUARK_VERSION "0.3.1d"
1313
#define QUARK_STABILITY "untested"
1414

1515
typedef Vector(char*) CStringVector;

src/parser/lefthand/lefthand.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111
#include "../statement/structure.h"
1212
#include "../literal/number.h"
1313
#include "../keywords.h"
14+
#include "parser/type/clash_types.h"
15+
16+
Type* boolean_type() {
17+
static Type boolean_type = {
18+
.External = {
19+
.id = NodeExternal,
20+
.type = &boolean_type,
21+
.flags = fType | fNumeric,
22+
.data = String("bool"),
23+
},
24+
};
25+
return &boolean_type;
26+
}
1427

1528
Node* lefthand_expression(Parser* parser) {
1629
Token token = next(parser->tokenizer);
@@ -53,6 +66,7 @@ Node* lefthand_expression(Parser* parser) {
5366
// TODO: wrap in surround and remove parenthesis in compiler to remove redundant parenthesis
5467
Node* expr = expression(parser);
5568
expect(parser->tokenizer, ')');
69+
5670
return new_node((Node) {
5771
.Wrapper = {
5872
.id = WrapperSurround,
@@ -79,6 +93,18 @@ Node* lefthand_expression(Parser* parser) {
7993

8094
case '[': return parse_array_literal(token.trace, parser);
8195

96+
case '!': {
97+
Node* expr = righthand_expression(lefthand_expression(parser), parser, 2);
98+
return new_node((Node) {
99+
.Wrapper = {
100+
.id = WrapperSurround,
101+
.type = boolean_type(),
102+
.trace = expr->trace,
103+
.Surround = { expr, String("!") },
104+
},
105+
});
106+
}
107+
82108
default:
83109
push(parser->tokenizer->messages, REPORT_ERR(token.trace,
84110
strf(0, "expected a \33[35mliteral\33[0m, but got '\33[35m%.*s\33[0m'",

src/parser/nodes/lefthand/cast.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef NODE_CAST_H
2+
#define NODE_CAST_H
3+
4+
#include "../fields.h"
5+
6+
typedef struct Cast {
7+
NODE_FIELDS;
8+
Node* value;
9+
} Cast;
10+
11+
#endif

src/parser/nodes/nodes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ typedef enum : uint32_t {
2020
NodeGenericReference,
2121
NodeStructType,
2222

23+
NodeCast,
24+
2325
NodeNumericLiteral,
2426
NodeStructLiteral,
2527
NodeMissing,
@@ -54,6 +56,8 @@ enum {
5456
#include "type/pointer_type.h"
5557
#include "type/struct_type.h"
5658

59+
#include "lefthand/cast.h"
60+
5761
#include "literal/wrapper.h"
5862
#include "literal/numeric_literal.h"
5963
#include "literal/struct_literal.h"
@@ -98,6 +102,7 @@ union Node {
98102
StructType StructType;
99103

100104
Wrapper Wrapper;
105+
Cast Cast;
101106
NumericLiteral NumericLiteral;
102107
StructLiteral StructLiteral;
103108
Missing Missing;

0 commit comments

Comments
 (0)