Skip to content

Commit 0562a05

Browse files
authored
Merge pull request #172 from onyx-lang/feature/separate-cli
Feature: Separate the CLI from Compiler Logic
2 parents 2612565 + f4f5f9a commit 0562a05

39 files changed

+6049
-5419
lines changed

.github/workflows/onyx-build.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ jobs:
6565
runtime_library: none
6666
artifact_name: 'onyx-linux-none-arm64'
6767
- build: darwin-arm64
68-
os: macos-12
68+
os: macos-13
6969
runtime_library: none
7070
target: aarch64-apple-darwin
7171
artifact_name: 'onyx-darwin-none-arm64'
7272
- build: darwin-arm64
73-
os: macos-12
73+
os: macos-13
7474
runtime_library: wasmer
7575
target: aarch64-apple-darwin
7676
artifact_name: 'onyx-darwin-wasmer-arm64'
7777
- build: darwin-amd64
78-
os: macos-12
78+
os: macos-13
7979
runtime_library: none
8080
artifact_name: 'onyx-darwin-none-amd64'
8181
- build: darwin-amd64
82-
os: macos-12
82+
os: macos-13
8383
runtime_library: wasmer
8484
artifact_name: 'onyx-darwin-wasmer-amd64'
8585
- build: darwin-amd64
86-
os: macos-12
86+
os: macos-13
8787
runtime_library: ovmwasm
8888
artifact_name: 'onyx-darwin-ovm-amd64'
8989
- build: darwin-arm64
90-
os: macos-12
90+
os: macos-13
9191
runtime_library: ovmwasm
9292
target: aarch64-apple-darwin
9393
artifact_name: 'onyx-darwin-ovm-arm64'

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ bin/onyx-run
2424
bin/onyx
2525
releases/
2626
compiler/onyx
27+
compiler/libonyx.so
28+
compiler/libonyx.dylib
29+
compiler/libonyx.dll
2730
runtime/onyx_runtime.so
2831
runtime/onyx_runtime.dylib
2932
runtime/onyx_runtime.dll

build.bat

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@echo off
22

33
REM Compile the compiler
4-
set SOURCE_FILES=compiler/src/onyx.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/symres.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
4+
set SOURCE_FILES=compiler/src/library_main.c compiler/cli/main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/symres.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
55

66
if "%1" == "1" (
77
set FLAGS=/Od /MTd /Z7
@@ -23,9 +23,19 @@ if %ERRORLEVEL% neq 0 (
2323
exit /b %ERRORLEVEL%
2424
)
2525

26+
set SOURCE_FILES=compiler/src/library_main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/symres.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
27+
cl.exe %FLAGS% /Icompiler/include /std:c17 /TC %SOURCE_FILES% /link /DLL /IGNORE:4217 %LINK_OPTIONS% /OUT:onyx.dll
28+
29+
REM Don't continue if we had compilation errors. This prevents CI to succeed.
30+
if %ERRORLEVEL% neq 0 (
31+
echo Compiler library compilation failed.
32+
exit /b %ERRORLEVEL%
33+
)
34+
2635
del *.pdb > NUL 2> NUL
2736
del *.ilk > NUL 2> NUL
2837
del *.obj > NUL 2> NUL
38+
del *.exp > NUL 2> NUL
2939
del misc\icon_resource.res
3040

3141
cl /MT /std:c17 /TC /I compiler/include /I shared/include /D_USRDLL /D_WINDLL runtime\onyx_runtime.c /link /DLL ws2_32.lib bcrypt.lib Synchronization.lib kernel32.lib /OUT:onyx_runtime.dll
@@ -50,8 +60,12 @@ if "%1" == "dist" (
5060
copy misc\onyx.sublime-syntax dist\misc\onyx.sublime-syntax
5161
copy misc\vscode\onyxlang-0.1.9.vsix dist\misc\onyxlang-0.1.9.vsix
5262

63+
mkdir dist\include
64+
copy shared\include\onyx.h dist\include\onyx.h
5365
copy onyx_runtime.dll dist\onyx_runtime.dll
5466
copy onyx.exe dist\onyx.exe
67+
copy onyx.dll dist\onyx.dll
68+
copy onyx.lib dist\onyx.lib
5569

5670
mkdir dist\tools
5771
copy scripts\onyx-pkg.onyx dist\tools\onyx-pkg.onyx

build.sh

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,26 @@ compile_all() {
2525
package_all() {
2626
rm -rf "$DIST_DIR"
2727
mkdir -p "$DIST_DIR"
28+
mkdir -p "$DIST_DIR/lib"
29+
mkdir -p "$DIST_DIR/include"
2830

2931
echo "Installing on '$(uname -a)'"
3032
echo "Installing core libs"
3133
[ -d "$DIST_DIR/core" ] && rm -r "$DIST_DIR/core"
3234
cp -r ./core "$DIST_DIR/core"
3335

36+
case "$(uname)" in
37+
Linux) suffix='so' ;;
38+
*BSD) suffix='so' ;;
39+
Darwin) suffix='dylib' ;;
40+
*) suffix='dll' ;;
41+
esac
42+
3443
echo "Installing core tools"
3544
mkdir -p "$DIST_DIR/bin"
3645
cp compiler/onyx "$DIST_DIR/bin/"
46+
cp compiler/libonyx.$suffix "$DIST_DIR/lib/"
47+
cp "shared/include/onyx.h" "$DIST_DIR/include/onyx.h"
3748

3849
mkdir -p "$DIST_DIR/tools"
3950
mkdir -p "$DIST_DIR/tools/pkg_templates"
@@ -43,15 +54,6 @@ package_all() {
4354

4455
if [ ! -z ${ONYX_RUNTIME_LIBRARY+x} ]; then
4556
echo "Installing runtime library"
46-
mkdir -p "$DIST_DIR/lib"
47-
mkdir -p "$DIST_DIR/include"
48-
49-
case "$(uname)" in
50-
Linux) suffix='so' ;;
51-
*BSD) suffix='so' ;;
52-
Darwin) suffix='dylib' ;;
53-
*) suffix='dll' ;;
54-
esac
5557

5658
[ -f runtime/onyx_runtime.$suffix ] && cp runtime/onyx_runtime.$suffix "$DIST_DIR/lib/"
5759
cp "shared/include/onyx_library.h" "$DIST_DIR/include/onyx_library.h"
@@ -77,15 +79,11 @@ compress_all() {
7779
# Sign the binaries on MacOS
7880
[ "$(uname)" = 'Darwin' ] && \
7981
codesign -s - "$DIST_DIR/bin/onyx" && \
82+
codesign -s - "$DIST_DIR/lib/libonyx.dylib" && \
8083
[ -f "$DIST_DIR/lib/onyx_runtime.dylib" ] && \
8184
codesign -s - "$DIST_DIR/lib/onyx_runtime.dylib"
8285

83-
if [ ! -z ${ONYX_RUNTIME_LIBRARY+x} ]; then
84-
# When including a runtime library, include the lib and include folders
85-
tar -C "$DIST_DIR" -zcvf onyx.tar.gz bin core examples include lib misc tools LICENSE
86-
else
87-
tar -C "$DIST_DIR" -zcvf onyx.tar.gz bin core examples misc tools LICENSE
88-
fi
86+
tar -C "$DIST_DIR" -zcvf onyx.tar.gz bin core examples include lib misc tools LICENSE
8987

9088
mv onyx.tar.gz dist/
9189
}

compiler/build.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
C_FILES="onyx astnodes builtins checker clone doc entities errors lex parser symres types utils wasm_emit extensions "
3+
C_FILES="library_main astnodes builtins checker clone doc entities errors lex parser symres types utils wasm_emit extensions "
44
LIBS="-lpthread -ldl -lm"
55
INCLUDES="-I./include -I../shared/include -I../shared/include/dyncall"
66

@@ -12,7 +12,7 @@ else
1212
FLAGS="$WARNINGS -O3"
1313
fi
1414

15-
FLAGS="$FLAGS -DENABLE_DEBUG_INFO"
15+
FLAGS="$FLAGS -DENABLE_DEBUG_INFO -fvisibility=hidden"
1616

1717
if [ ! -z ${ONYX_TARGET+x} ]; then
1818
FLAGS="$FLAGS --target=$ONYX_TARGET"
@@ -51,8 +51,25 @@ if [ "$ONYX_USE_DYNCALL" = "1" ] && [ "$ONYX_RUNTIME_LIBRARY" = "ovmwasm" ]; the
5151
FLAGS="$FLAGS -DUSE_DYNCALL"
5252
fi
5353

54-
echo "Compiling onyx"
55-
$ONYX_CC -o "onyx" \
56-
$FLAGS $INCLUDES \
57-
$(echo "$C_FILES" | sed 's/ /\n/g;s/\([a-zA-Z_0-9]*\)\n/src\/\1.c\n/g;s/\n/ /g') \
58-
$LIBS
54+
case "$(uname)" in
55+
Linux) suffix='so' ;;
56+
*BSD) suffix='so' ;;
57+
Darwin) suffix='dylib' ;;
58+
*) suffix='dll' ;;
59+
esac
60+
61+
echo "Compiling libonyx.$suffix"
62+
for c_file in $(echo "$C_FILES" | sed 's/ /\n/g;s/\([a-zA-Z_0-9]*\)\n/src\/\1.c\n/g;s/\n/ /g'); do
63+
$ONYX_CC $FLAGS $INCLUDES -fPIC -o $(basename $c_file).o -c $c_file
64+
done
65+
66+
echo "Compiling onyx executable"
67+
$ONYX_CC $INCLUDES $FLAGS cli/main.c *.o -o onyx $LIBS
68+
69+
FLAGS=""
70+
if [ ! -z ${ONYX_TARGET+x} ]; then
71+
FLAGS="$FLAGS --target=$ONYX_TARGET"
72+
fi
73+
$ONYX_CC -shared $FLAGS -o "libonyx.$suffix" *.o $LIBS
74+
75+
rm *.o

compiler/cli/error_printing.h

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
typedef struct onyx_error_details_t {
2+
onyx_error_t rank;
3+
const char *message;
4+
const char *filename;
5+
int line;
6+
int column;
7+
int length;
8+
char *line_text;
9+
} onyx_error_details_t;
10+
11+
static void print_error_text(const char *text, b32 color) {
12+
if (!color) {
13+
bh_printf("%s", text);
14+
return;
15+
}
16+
17+
const char *ch = text;
18+
b32 in_color = 0;
19+
20+
while (*ch != '\0') {
21+
if (*ch == '\'') {
22+
in_color = !in_color;
23+
if (in_color) bh_printf("\033[92m");
24+
else bh_printf("\033[0m");
25+
} else {
26+
bh_printf("%c", *ch);
27+
}
28+
29+
ch++;
30+
}
31+
}
32+
33+
static void print_underline(onyx_error_details_t *err, i32 len, i32 first_non_whitespace, b32 colored_printing) {
34+
len = bh_min(len, 1024);
35+
char* pointer_str = alloca(sizeof(char) * len);
36+
memset(pointer_str, ' ', len);
37+
38+
int c = err->column - 1;
39+
int l = err->length;
40+
41+
memcpy(pointer_str, err->line_text, first_non_whitespace);
42+
memset(pointer_str + c + 1, '~', l - 1);
43+
pointer_str[c] = '^';
44+
pointer_str[c + l] = 0;
45+
46+
if (colored_printing) bh_printf("\033[91m");
47+
bh_printf("%s\n", pointer_str);
48+
if (colored_printing) bh_printf("\033[0m\n");
49+
}
50+
51+
static void print_detailed_message_v1(onyx_error_details_t *err, b32 colored_printing) {
52+
bh_printf("(%s:%l,%l) %s\n", err->filename, err->line, err->column, err->message);
53+
54+
i32 linelength = 0;
55+
i32 first_char = 0;
56+
char* walker = err->line_text;
57+
while (*walker == ' ' || *walker == '\t') first_char++, linelength++, walker++;
58+
while (*walker && *walker != '\n') linelength++, walker++;
59+
60+
if (colored_printing) bh_printf("\033[90m");
61+
i32 numlen = bh_printf(" %d | ", err->line);
62+
if (colored_printing) bh_printf("\033[94m");
63+
bh_printf("%b\n", err->line_text, linelength);
64+
65+
fori (i, 0, numlen - 1) bh_printf(" ");
66+
print_underline(err, linelength, first_char, colored_printing);
67+
}
68+
69+
static void print_detailed_message_v2(onyx_error_details_t* err, b32 colored_printing) {
70+
if (colored_printing) {
71+
switch (err->rank) {
72+
case ONYX_ERROR_WARNING:
73+
bh_printf("\033[93mwarning\033[0m: ");
74+
print_error_text(err->message, colored_printing);
75+
bh_printf("\n\033[90m at: %s:%l,%l\033[0m\n", err->filename, err->line, err->column);
76+
break;
77+
78+
default:
79+
bh_printf("\033[91merror\033[0m: ");
80+
print_error_text(err->message, colored_printing);
81+
bh_printf("\n\033[90m at: %s:%l,%l\033[0m\n", err->filename, err->line, err->column);
82+
break;
83+
}
84+
} else {
85+
switch (err->rank) {
86+
case ONYX_ERROR_WARNING:
87+
bh_printf("warning: ");
88+
print_error_text(err->message, colored_printing);
89+
bh_printf("\n at: %s:%l,%l\n", err->filename, err->line, err->column);
90+
break;
91+
92+
default:
93+
bh_printf("error: ");
94+
print_error_text(err->message, colored_printing);
95+
bh_printf("\n at: %s:%l,%l\n", err->filename, err->line, err->column);
96+
break;
97+
}
98+
}
99+
100+
i32 linelength = 0;
101+
i32 first_char = 0;
102+
char* walker = err->line_text;
103+
while (*walker == ' ' || *walker == '\t') first_char++, linelength++, walker++;
104+
while (*walker && *walker != '\n') linelength++, walker++;
105+
106+
char numbuf[32] = {0};
107+
i32 numlen = bh_snprintf(numbuf, 31, " %d | ", err->line);
108+
109+
if (colored_printing) bh_printf("\033[90m");
110+
fori (i, 0, numlen - 3) bh_printf(" ");
111+
bh_printf("|\n%s", numbuf);
112+
if (colored_printing) bh_printf("\033[94m");
113+
114+
bh_printf("%b\n", err->line_text, linelength);
115+
116+
if (colored_printing) bh_printf("\033[90m");
117+
fori (i, 0, numlen - 3) bh_printf(" ");
118+
bh_printf("| ");
119+
if (colored_printing) bh_printf("\033[94m");
120+
121+
print_underline(err, linelength, first_char, colored_printing);
122+
}
123+
124+
static void print_detailed_message_json(onyx_error_details_t* err, b32 colored_printing) {
125+
bh_printf(
126+
"{\"rank\":%d,\"file\":\"%s\",\"line\":%d,\"column\":%d,\"length\":%d,\"msg\":\"%s\"}",
127+
err->rank,
128+
err->filename,
129+
err->line,
130+
err->column,
131+
err->length,
132+
err->message
133+
);
134+
}
135+
136+
static void print_detailed_message(onyx_error_details_t* err, b32 colored_printing, char *error_format) {
137+
if (!err->filename) {
138+
// This makes the assumption that if a file is not specified for an error,
139+
// the error must have come from the command line.
140+
141+
if (colored_printing) {
142+
bh_printf("\033[91merror\033[0m: ");
143+
bh_printf("%s\n", err->message);
144+
bh_printf("\033[90m at: command line argument\033[0m\n");
145+
} else {
146+
bh_printf("error: ");
147+
bh_printf("%s\n", err->message);
148+
bh_printf(" at: command line argument\n");
149+
}
150+
151+
return;
152+
}
153+
154+
if (!strcmp(error_format, "v2")) {
155+
print_detailed_message_v2(err, colored_printing);
156+
}
157+
else if (!strcmp(error_format, "v1")) {
158+
print_detailed_message_v1(err, colored_printing);
159+
}
160+
else if (!strcmp(error_format, "json")) {
161+
print_detailed_message_json(err, colored_printing);
162+
}
163+
else {
164+
bh_printf("Unknown error format: '%s'.\n", error_format);
165+
}
166+
}
167+
168+
void onyx_errors_print(onyx_context_t *ctx, char *error_format, b32 colored_printing, b32 show_all_errors) {
169+
b32 error_format_json = !strcmp(error_format, "json");
170+
if (error_format_json) bh_printf("[");
171+
172+
onyx_error_t last_rank = onyx_error_rank(ctx, 0);
173+
fori (i, 0, onyx_error_count(ctx)) {
174+
onyx_error_details_t err;
175+
err.rank = onyx_error_rank(ctx, i);
176+
err.message = onyx_error_message(ctx, i);
177+
err.filename = onyx_error_filename(ctx, i);
178+
err.line = onyx_error_line(ctx, i);
179+
err.column = onyx_error_column(ctx, i);
180+
err.length = onyx_error_length(ctx, i);
181+
182+
char line_text[256];
183+
onyx_error_line_text(ctx, i, line_text, 255);
184+
err.line_text = line_text;
185+
186+
if (!show_all_errors && last_rank != err.rank) break;
187+
if (error_format_json && i != 0) bh_printf(",");
188+
189+
print_detailed_message(&err, colored_printing, error_format);
190+
191+
last_rank = err.rank;
192+
}
193+
194+
if (error_format_json) bh_printf("]");
195+
}
196+

0 commit comments

Comments
 (0)