Skip to content

Commit c6daaf5

Browse files
authored
Merge pull request #41 from quark-programming/dev
Major bug fixes and compiler re-writes + a couple small features
2 parents 8e69818 + b82fe1e commit c6daaf5

46 files changed

Lines changed: 1088 additions & 477 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
# Local testing
1111
qc
1212
testing/
13+
tests/
14+
build/
1315

1416
# Unit tests
15-
unit-tests/unit-tests
17+
unit-tests/unit-tests
18+
19+
# Build
20+
build/

Makefile

Lines changed: 138 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,145 @@
1-
# CLion source config
2-
SRCS := $(wildcard src/*.c) $(wildcard src/*/*.c) $(wildcard src/*/*/*.c) $(wildcard src/*/*/*/*.c) \
3-
unit-tests/main.c $(wildcard unit-tests/*/*.c)
4-
CFLAGS := -Isrc/include -Isrc -Wall -Wno-missing-braces -Wno-char-subscripts
1+
#-------------------------------------------------
2+
# Makefile Usage
3+
# > make # release build + unit-tests
4+
# > make build # release build qc
5+
# > make build MODE=debug # debug build qc
6+
# > make test # qk tests
7+
# > make test-c # unit-tests
8+
# > make release # release build qc + unit-tests
9+
# > make debug # debug build qc + unit-tests
10+
# > make clean # remove build/files
11+
#-------------------------------------------------
512

6-
BSRCS := $(wildcard src/*.c) $(wildcard src/*/*.c) $(wildcard src/*/*/*.c) $(wildcard src/*/*/*/*.c)
7-
UT_SRCS := unit-tests/main.c $(wildcard src/*/*.c) $(wildcard src/*/*/*.c) $(wildcard src/*/*/*/*.c) \
8-
$(wildcard unit-tests/*/*.c)
9-
OUT = qc
13+
# ========= Tools =========
14+
CC := gcc
15+
QC := qc
16+
EXE :=
1017

11-
build: $(SRCS)
12-
$(CC) $(CFLAGS) $(BSRCS) -o $(OUT)
18+
# ========= Platform detection =========
19+
ifeq ($(OS),Windows_NT)
20+
PLATFORM := windows
21+
EXE := .exe
22+
CC := $(CC)$(EXE)
23+
QC := $(QC)$(EXE)
1324

14-
build-debug: $(SRCS)
15-
$(CC) $(CFLAGS) $(BSRCS) -o $(OUT) -g -ggdb -DEBUG
25+
MKDIR = if not exist "$(1)" mkdir "$(1)"
26+
RM_RF = if exist "$(1)" rmdir /S /Q "$(1)"
27+
RM_F = if exist "$(1)" del /Q "$(1)"
28+
COPY = copy /Y
29+
else
30+
UNAME_S := $(shell uname -s)
31+
ifeq ($(UNAME_S),Darwin)
32+
PLATFORM := macos
33+
else
34+
PLATFORM := linux
35+
endif
1636

17-
all: build test
37+
MKDIR = mkdir -p "$(1)"
38+
RM_RF = rm -rf "$(1)"
39+
RM_F = rm -f "$(1)"
40+
COPY = cp
41+
endif
1842

19-
test: $(UT_SRCS)
20-
$(CC) $(CFLAGS) $(UT_SRCS) -o unit-tests/unit-tests -g
43+
# ========= Flags =========
44+
CFLAGS := -Isrc/include -Isrc -Wall -Wno-missing-braces -Wno-char-subscripts
45+
LDFLAGS :=
2146

47+
# ========= Directories =========
48+
BUILD_DIR := build
49+
TEST_BUILD := $(BUILD_DIR)/tests
50+
UT_BUILD := $(BUILD_DIR)/unit_tests
51+
52+
# ========= Mode normalize =========
53+
MODE ?= release
54+
MODE_L := $(shell echo $(MODE) | tr A-Z a-z)
55+
56+
# ========= Sources =========
57+
MAIN_C := src/main.c
58+
SRCS := \
59+
$(wildcard src/*/*.c) \
60+
$(wildcard src/*/*/*.c) \
61+
$(wildcard src/*/*/*/*.c)
62+
63+
TEST_QK := $(wildcard tests/*.qk)
64+
65+
UT_SRCS := \
66+
$(wildcard unit-tests/*.c) \
67+
$(wildcard unit-tests/*/*.c) \
68+
$(wildcard unit-tests/*/*/*.c) \
69+
$(wildcard unit-tests/*/*/*/*.c) \
70+
71+
# ========= Output =========
72+
OUT := ./${QC}
73+
74+
.PHONY: all build test test-c release debug clean
75+
.DEFAULT_GOAL := all
76+
77+
# ========= Directories =========
78+
dirs:
79+
@$(call MKDIR,$(BUILD_DIR))
80+
@$(call MKDIR,$(TEST_BUILD))
81+
@$(call MKDIR,$(UT_BUILD))
82+
83+
# ========= Build qc =========
84+
build: dirs
85+
ifeq ($(MODE_L),debug)
86+
@echo ------ Debug build qc ------
87+
$(CC) $(CFLAGS) $(MAIN_C) $(SRCS) $(LDFLAGS) -g -ggdb -DEBUG -o $(BUILD_DIR)/$(QC)
88+
else
89+
@echo ------ Release build qc ------
90+
$(CC) $(CFLAGS) $(MAIN_C) $(SRCS) $(LDFLAGS) -O2 -o $(BUILD_DIR)/$(QC)
91+
endif
92+
@$(COPY) $(BUILD_DIR)/$(QC) $(OUT)
93+
94+
# ========= Default =========
95+
all: build test-c
96+
97+
# ========= qk tests =========
98+
test:
99+
@echo ------ qk tests ------
100+
@$(call MKDIR,$(TEST_BUILD))
101+
102+
ifeq ($(PLATFORM),windows)
103+
@if not "$(TEST_QK)"=="" ( \
104+
for %%f in ($(TEST_QK)) do ( \
105+
$(OUT) %%f -o $(TEST_BUILD)\%%~nf.c -l "$(CURDIR)" && \
106+
$(CC) $(CFLAGS) -g $(TEST_BUILD)\%%~nf.c -o $(TEST_BUILD)\%%~nf$(EXE) \
107+
) \
108+
) else ( \
109+
echo No .qk tests found \
110+
)
111+
else
112+
@set -e; \
113+
if [ -n "$(TEST_QK)" ]; then \
114+
for f in $(TEST_QK); do \
115+
base=$$(basename $$f .qk); \
116+
$(OUT) "$$f" -o "$(TEST_BUILD)/$$base.c" -l "$(CURDIR)"; \
117+
$(CC) $(CFLAGS) -g "$(TEST_BUILD)/$$base.c" -o "$(TEST_BUILD)/$$base$(EXE)"; \
118+
done; \
119+
else \
120+
echo "No .qk tests found"; \
121+
fi
122+
endif
123+
124+
# ========= unit-tests =========
125+
build-test-c: dirs
126+
$(CC) $(CFLAGS) -g $(UT_SRCS) $(SRCS) $(LDFLAGS) -o $(UT_BUILD)/unit-tests$(EXE)
127+
128+
test-c: build-test-c
129+
@echo ------ unit-tests ------
130+
@$(UT_BUILD)/unit-tests$(EXE)
131+
132+
# ========= Presets =========
133+
release:
134+
$(MAKE) build MODE=release
135+
$(MAKE) test-c
136+
137+
debug:
138+
$(MAKE) build MODE=debug
139+
$(MAKE) test-c
140+
141+
# ========= Clean =========
22142
clean:
23-
rm $(OUT)
143+
@$(call RM_RF,$(BUILD_DIR))
144+
@$(call RM_F,$(OUT))
145+

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,25 @@
99
> sure to use the [latest release](https://github.com/quark-programming/quark/releases) You can see and submit issues
1010
> on the repository's [issues page](https://github.com/quark-programming/quark/issues)
1111
12-
> [!CAUTION]
13-
> The main branch is currently being worked through an issue, download the more stable release,
14-
> [0.3.0](https://github.com/quark-programming/quark/releases/tag/0.3.0), instead of cloning
15-
1612
## Building the Compiler
1713

1814
To build the Compiler:
1915

2016
```sh
21-
make build # or specify output with `OUT` (defaults to `qc`)
22-
make build OUT=/path/to/executable
17+
make # or specify executable name
18+
make QC=my_executable
2319

2420
./qc -h
2521
```
2622

2723
## Compiling a Source File to C:
2824

2925
```sh
30-
$ ./qc -h
31-
$ ./qc <path> -o <out-path>
26+
export QUARK_ROOT=/path/to/quark
27+
export PATH=$PATH:$QUARK_ROOT
28+
29+
qc -h
30+
qc /path/to/input.qk -o /path/to/output.c -l $QUARK_ROOT
3231
```
3332

3433
## Learn More

SECURITY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
## Supported Versions
44

5-
Currently this repository does not have any secure versions (see table)
5+
Currently, this repository does not have any secure versions (see table)
66

77
| Version | Supported |
88
| ------- | ------------------ |
99
| 0.x.x | :x: |
1010

1111
## Reporting a Vulnerability
1212

13-
Reporting a vulnerability works like finding an issue. You can create an [issue](https://github.com/quark-programming/quark/issues), or submit a [pull request](https://github.com/quark-programming/quark/pulls) to fix an vulnerability you find.
13+
Reporting a vulnerability works like finding an issue. You can create an [issue](https://github.com/quark-programming/quark/issues), or submit a [pull request](https://github.com/quark-programming/quark/pulls)
14+
to fix a vulnerability you find.

main

34.4 KB
Binary file not shown.

src/compiler/literal/literals.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void comp_StructLiteral(void* void_self, String* line, Compiler* compiler) {
2929
compile(self->type, line, compiler);
3030
strf(line, ") {");
3131

32-
for(size_t i = 0; i < self->field_names.size; i++) {
32+
for(size_t i = 0; i < self->field_values.size; i++) {
3333
strf(line, i ? ", " : " ");
3434
if(self->field_names.data[i].size) {
3535
strf(line, ".%.*s = ", PRINT(self->field_names.data[i]));

src/compiler/literal/wrapper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ void comp_Surround(void* void_self, String* line, Compiler* compiler) {
4040
Wrapper* const self = void_self;
4141
const bool applied_action = apply_action(self->action, 0);
4242

43-
strf(line, self->Surround.no_parenthesis_wrap ? "%.*s" : "(%.*s", PRINT(self->Surround.prefix));
43+
strf(line, "%.*s", PRINT(self->Surround.prefix));
4444
compile(self->Surround.child, line, compiler);
45-
strf(line, self->Surround.no_parenthesis_wrap ? "%.*s" : "%.*s)", PRINT(self->Surround.postfix));
45+
strf(line, "%.*s", PRINT(self->Surround.postfix));
4646

4747
if(applied_action) remove_action(self->action, 0);
4848
}

src/compiler/righthand/declaration/identifier.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ void compile_identifier(const Identifier identifier, String* line) {
1111
}
1212

1313
if(identifier.parent_scope && identifier.parent_scope->id == NodeStructType
14-
&& identifier.parent_declaration->id != NodeVariableDeclaration) {
14+
&& !(identifier.parent_declaration->id == NodeVariableDeclaration
15+
&& !(identifier.parent_declaration->type->flags & fType))) {
1516
const Identifier parent_ident = ((StructType*) (void*) identifier.parent_scope)->parent->identifier;
1617
compile_identifier(parent_ident, line);
1718
strf(line, "__");
@@ -21,8 +22,7 @@ void compile_identifier(const Identifier identifier, String* line) {
2122
// TODO: change `PRINT` macro to `FMT` or `STRFMT`
2223
strf(line, "%.*s", PRINT(identifier.base));
2324

24-
if(identifier.parent_declaration && identifier.parent_declaration->generics.base_type_arguments.size
25-
&& identifier.parent_declaration->generics.type_arguments_stack.size && !identifier.is_external) {
25+
if(identifier.parent_declaration->generics.type_arguments_stack.size && !identifier.is_external) {
2626
stringify_generics(line, last(identifier.parent_declaration->generics.type_arguments_stack),
2727
StringifyAlphaNumeric);
2828
}

src/compiler/righthand/righthand.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
void comp_BinaryOperation(void* void_self, String* line, Compiler* compiler) {
44
BinaryOperation* const self = void_self;
55

6-
strf(line, "(");
76
compile(self->left, line, compiler);
8-
strf(line, " %.*s ", (int) self->operator.size, self->operator.data);
7+
8+
strf(line,
9+
self->operator.data[0] == '.' || (self->operator.data[0] == '-' && self->operator.data[1] == '>')
10+
? "%.*s"
11+
: " %.*s ",
12+
(int) self->operator.size, self->operator.data);
13+
914
compile(self->right, line, compiler);
10-
strf(line, ")");
1115
}
1216

1317
void comp_FunctionCall(void* void_self, String* line, Compiler* compiler) {
@@ -21,4 +25,3 @@ void comp_FunctionCall(void* void_self, String* line, Compiler* compiler) {
2125
}
2226
strf(line, ")");
2327
}
24-

src/include/vector-string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "vector.h"
1010
#include "helpers.h"
1111

12-
typedef Vector(char) String;
12+
typedef Vector(char, String) String;
1313

1414
typedef Vector(String) StringVector;
1515

0 commit comments

Comments
 (0)