// FUNCTIONS
-args_get :: proc (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "args_get"---
-args_sizes_get :: proc (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "args_sizes_get" ---
-
-environ_get :: proc (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_snapshot_preview1" "environ_get" ---
-environ_sizes_get :: proc (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "environ_sizes_get" ---
-
-clock_res_get :: proc (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_res_get" ---
-clock_time_get :: proc (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_snapshot_preview1" "clock_time_get" ---
-
-fd_advise :: proc (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_snapshot_preview1" "fd_advise" ---
-fd_allocate :: proc (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_allocate" ---
-fd_close :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_close" ---
-fd_datasync :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_datasync" ---
-fd_fdstat_get :: proc (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_get" ---
-fd_fdstat_set_flags :: proc (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_flags" ---
-fd_fdstat_set_rights :: proc (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_snapshot_preview1" "fd_fdstat_set_rights" ---
-fd_filestat_get :: proc (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_get" ---
-fd_filestat_set_size :: proc (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_size" ---
-fd_filestat_set_times :: proc (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "fd_filestat_set_times" ---
-fd_pread :: proc (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pread" ---
-fd_prestat_get :: proc (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_get" ---
-fd_prestat_dir_name :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_snapshot_preview1" "fd_prestat_dir_name" ---
-fd_pwrite :: proc (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_pwrite" ---
-fd_read :: proc (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_read" ---
-fd_readdir :: proc (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_readdir" ---
-fd_renumber :: proc (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_renumber" ---
-fd_seek :: proc (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_seek" ---
-fd_sync :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_snapshot_preview1" "fd_sync" ---
-fd_tell :: proc (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_snapshot_preview1" "fd_tell" ---
-fd_write :: proc (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "fd_write" ---
-
-path_create_directory :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_create_directory" ---
-path_filestat_get :: proc (fd: FileDescriptor, flags: LookupFlags, path: string, buf: ^FileStat) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_get" ---
-path_filestat_set_times :: proc (fd: FileDescriptor, flags: LookupFlags, path: string, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_snapshot_preview1" "path_filestat_set_times" ---
-
-path_link :: proc (fd: FileDescriptor, old_flags: LookupFlags, old_path: string, new_fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_link" ---
+args_get :: proc (argv: ^^u8, argv_buf: ^u8) -> Errno #foreign "wasi_unstable" "args_get"---
+args_sizes_get :: proc (argc: ^Size, argv_buf_size: ^Size) -> Errno #foreign "wasi_unstable" "args_sizes_get" ---
+
+environ_get :: proc (environ: ^^u8, environ_buf: ^u8) -> Errno #foreign "wasi_unstable" "environ_get" ---
+environ_sizes_get :: proc (environc: ^Size, environ_buf_size: ^Size) -> Errno #foreign "wasi_unstable" "environ_sizes_get" ---
+
+clock_res_get :: proc (id: ClockID, resolution: ^Timestamp) -> Errno #foreign "wasi_unstable" "clock_res_get" ---
+clock_time_get :: proc (id: ClockID, precision: Timestamp, time: ^Timestamp) -> Errno #foreign "wasi_unstable" "clock_time_get" ---
+
+fd_advise :: proc (fd: FileDescriptor, offset: Filesize, len: Filesize, advice: Advice) -> Errno #foreign "wasi_unstable" "fd_advise" ---
+fd_allocate :: proc (fd: FileDescriptor, offset: Filesize, len: Filesize) -> Errno #foreign "wasi_unstable" "fd_allocate" ---
+fd_close :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_close" ---
+fd_datasync :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_datasync" ---
+fd_fdstat_get :: proc (fd: FileDescriptor, stat: ^FDStat) -> Errno #foreign "wasi_unstable" "fd_fdstat_get" ---
+fd_fdstat_set_flags :: proc (fd: FileDescriptor, flags: FDFlags) -> Errno #foreign "wasi_unstable" "fd_fdstat_set_flags" ---
+fd_fdstat_set_rights :: proc (fd: FileDescriptor, rights_base: Rights, rights_inheriting: Rights) -> Errno #foreign "wasi_unstable" "fd_fdstat_set_rights" ---
+fd_filestat_get :: proc (fd: FileDescriptor, buf: ^FileStat) -> Errno #foreign "wasi_unstable" "fd_filestat_get" ---
+fd_filestat_set_size :: proc (fd: FileDescriptor, size: Filesize) -> Errno #foreign "wasi_unstable" "fd_filestat_set_size" ---
+fd_filestat_set_times :: proc (fd: FileDescriptor, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_unstable" "fd_filestat_set_times" ---
+fd_pread :: proc (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nread: ^Size) -> Errno #foreign "wasi_unstable" "fd_pread" ---
+fd_prestat_get :: proc (fd: FileDescriptor, buf: ^PrestatTagged) -> Errno #foreign "wasi_unstable" "fd_prestat_get" ---
+fd_prestat_dir_name :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_unstable" "fd_prestat_dir_name" ---
+fd_pwrite :: proc (fd: FileDescriptor, iovs: IOVecArray, offset: Filesize, nwritten: ^Size) -> Errno #foreign "wasi_unstable" "fd_pwrite" ---
+fd_read :: proc (fd: FileDescriptor, iovs: IOVecArray, nread: ^Size) -> Errno #foreign "wasi_unstable" "fd_read" ---
+fd_readdir :: proc (fd: FileDescriptor, buf: ^u8, buf_len: Size, cookie: DirCookie, bufused: ^Size) -> Errno #foreign "wasi_unstable" "fd_readdir" ---
+fd_renumber :: proc (fd: FileDescriptor, to: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_renumber" ---
+fd_seek :: proc (fd: FileDescriptor, offset: FileDelta, whence: Whence, newoffset: ^Filesize) -> Errno #foreign "wasi_unstable" "fd_seek" ---
+fd_sync :: proc (fd: FileDescriptor) -> Errno #foreign "wasi_unstable" "fd_sync" ---
+fd_tell :: proc (fd: FileDescriptor, offset: ^Filesize) -> Errno #foreign "wasi_unstable" "fd_tell" ---
+fd_write :: proc (fd: FileDescriptor, iovs: IOVecArray, nwritten: ^Size) -> Errno #foreign "wasi_unstable" "fd_write" ---
+
+path_create_directory :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_unstable" "path_create_directory" ---
+path_filestat_get :: proc (fd: FileDescriptor, flags: LookupFlags, path: string, buf: ^FileStat) -> Errno #foreign "wasi_unstable" "path_filestat_get" ---
+path_filestat_set_times :: proc (fd: FileDescriptor, flags: LookupFlags, path: string, atim: Timestamp, mtim: Timestamp, fst_flags: FSTFlags) -> Errno #foreign "wasi_unstable" "path_filestat_set_times" ---
+
+path_link :: proc (fd: FileDescriptor, old_flags: LookupFlags, old_path: string, new_fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_unstable" "path_link" ---
path_open :: proc
( fd: FileDescriptor
, dirflags: LookupFlags
, fdflags: FDFlags
, opened_fd: ^FileDescriptor
) -> Errno
- #foreign "wasi_snapshot_preview1" "path_open" ---
+ #foreign "wasi_unstable" "path_open" ---
-path_readlink :: proc (fd: FileDescriptor, path: string, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "path_readlink" ---
-path_remove_directory :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_remove_directory" ---
-path_rename :: proc (fd: FileDescriptor, old_path: string, new_fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_rename" ---
-path_symlink :: proc (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_symlink" ---
-path_unlink_file :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_snapshot_preview1" "path_unlink_file" ---
+path_readlink :: proc (fd: FileDescriptor, path: string, buf: ^u8, buf_len: Size, bufused: ^Size) -> Errno #foreign "wasi_unstable" "path_readlink" ---
+path_remove_directory :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_unstable" "path_remove_directory" ---
+path_rename :: proc (fd: FileDescriptor, old_path: string, new_fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_unstable" "path_rename" ---
+path_symlink :: proc (old_path: ^u8, old_path_len: Size, fd: FileDescriptor, new_path: string) -> Errno #foreign "wasi_unstable" "path_symlink" ---
+path_unlink_file :: proc (fd: FileDescriptor, path: string) -> Errno #foreign "wasi_unstable" "path_unlink_file" ---
-poll_oneoff :: proc (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "poll_oneoff" ---
+poll_oneoff :: proc (in: ^Subscription, out: ^Event, nsubscriptions: Size, nevents: ^Size) -> Errno #foreign "wasi_unstable" "poll_oneoff" ---
-proc_exit :: proc (rval: ExitCode) #foreign "wasi_snapshot_preview1" "proc_exit" ---
-proc_raise :: proc (sig: Signal) -> Errno #foreign "wasi_snapshot_preview1" "proc_raise" ---
+proc_exit :: proc (rval: ExitCode) #foreign "wasi_unstable" "proc_exit" ---
+proc_raise :: proc (sig: Signal) -> Errno #foreign "wasi_unstable" "proc_raise" ---
-sched_yield :: proc -> Errno #foreign "wasi_snapshot_preview1" "proc_raise" ---
+sched_yield :: proc -> Errno #foreign "wasi_unstable" "sched_yield" ---
-random_get :: proc (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_snapshot_preview1" "random_get" ---
+random_get :: proc (buf: ^u8, buf_len: Size) -> Errno #foreign "wasi_unstable" "random_get" ---
-sock_recv :: proc (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_recv" ---
-sock_send :: proc (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_snapshot_preview1" "sock_send" ---
-sock_shutdown :: proc (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_snapshot_preview1" "sock_shutdown" ---
+sock_recv :: proc (fd: FileDescriptor, ri_data: IOVecArray, ri_flags: RIFlags, ro_datalen: ^Size, ro_flags: ^ROFlags) -> Errno #foreign "wasi_unstable" "sock_recv" ---
+sock_send :: proc (fd: FileDescriptor, si_data: IOVecArray, si_flags: SIFlags, so_datalen: ^Size) -> Errno #foreign "wasi_unstable" "sock_send" ---
+sock_shutdown :: proc (fd: FileDescriptor, how: SDFlags) -> Errno #foreign "wasi_unstable" "sock_shutdown" ---
#define VERSION "0.1"
+
+#ifndef CORE_INSTALLATION
+ #ifdef __unix__
+ #define CORE_INSTALLATION "/usr/share/onyx/core"
+ #endif
+#endif
+
+
static const char* docstring = "Onyx compiler version " VERSION "\n"
"\n"
"The compiler for the Onyx programming language.\n"
.target_file = "out.wasm",
};
- bh_arr_new(alloc, options.files, 1);
- bh_arr_new(alloc, options.included_folders, 1);
+ bh_arr_new(alloc, options.files, 2);
+ bh_arr_new(alloc, options.included_folders, 2);
// NOTE: Add the current folder
+ bh_arr_push(options.included_folders, CORE_INSTALLATION);
bh_arr_push(options.included_folders, ".");
fori(i, 1, argc) {
OnyxWasmModule wasm_mod;
} CompilerState;
+static char* lookup_included_file(CompilerState* cs, char* filename);
+
static void compiler_state_init(CompilerState* compiler_state, OnyxCompileOptions* opts) {
compiler_state->options = opts;
bh_arr_new(opts->allocator, compiler_state->queued_files, 4);
+ bh_arr_push(compiler_state->queued_files, lookup_included_file(compiler_state, "builtin"));
+
// NOTE: Add all files passed by command line to the queue
bh_arr_each(const char *, filename, opts->files)
bh_arr_push(compiler_state->queued_files, (char *) *filename);
-static char* lookup_included_file(CompilerState* cs, OnyxToken* filename) {
+static char* lookup_included_file(CompilerState* cs, char* filename) {
static char path[256];
fori (i, 0, 256) path[i] = 0;
static char fn[128];
fori (i, 0, 128) fn[i] = 0;
- token_toggle_end(filename);
- if (!bh_str_ends_with(filename->text, ".onyx")) {
- bh_snprintf(fn, 128, "%s.onyx", filename->text);
+ if (!bh_str_ends_with(filename, ".onyx")) {
+ bh_snprintf(fn, 128, "%s.onyx", filename);
} else {
- bh_snprintf(fn, 128, "%s", filename->text);
+ bh_snprintf(fn, 128, "%s", filename);
}
- token_toggle_end(filename);
bh_arr_each(const char *, folder, cs->options->included_folders) {
if ((*folder)[strlen(*folder) - 1] != '/')
static void merge_parse_results(CompilerState* compiler_state, ParseResults* results) {
bh_arr_each(AstInclude *, include, results->includes) {
if ((*include)->kind == Ast_Kind_Include_File) {
- char* filename = lookup_included_file(compiler_state, (*include)->name);
- char* formatted_name = bh_strdup(global_heap_allocator, filename);
+ token_toggle_end((*include)->name);
+ char* filename = lookup_included_file(compiler_state, (*include)->name->text);
+ token_toggle_end((*include)->name);
+ char* formatted_name = bh_strdup(global_heap_allocator, filename);
bh_arr_push(compiler_state->queued_files, formatted_name);
} else if ((*include)->kind == Ast_Kind_Include_Folder) {
const char* folder = bh_aprintf(global_heap_allocator, "%b", (*include)->name->text, (*include)->name->length);
}
+static AstNumLit* make_int_literal(bh_allocator a, i64 i) {
+ AstNumLit* num = onyx_ast_node_new(a, sizeof(AstNumLit), Ast_Kind_NumLit);
+ if (bh_abs(i) >= ((u64) 1 << 32))
+ num->type_node = (AstType *) &basic_type_i64;
+ else
+ num->type_node = (AstType *) &basic_type_i32;
+
+ num->value.l = i;
+ return num;
+}
+
static AstNumLit* parse_int_literal(OnyxParser* parser) {
AstNumLit* int_node = make_node(AstNumLit, Ast_Kind_NumLit);
retval = (AstTyped *) fc;
break;
}
+ else if (parse_possible_directive(parser, "file")) {
+ OnyxToken* dir_token = parser->curr - 2;
+
+ OnyxToken* str_token = bh_alloc(parser->allocator, sizeof(OnyxToken));
+ str_token->text = bh_strdup(global_heap_allocator, (char *) dir_token->pos.filename);
+ str_token->length = strlen(dir_token->pos.filename);
+ str_token->pos = dir_token->pos;
+ str_token->type = Token_Type_Literal_String;
+
+ AstStrLit* filename = make_node(AstStrLit, Ast_Kind_StrLit);
+ filename->token = str_token;
+ filename->addr = 0;
+
+ add_node_to_process(parser, (AstNode *) filename);
+ retval = (AstTyped *) filename;
+ break;
+ }
+ else if (parse_possible_directive(parser, "line")) {
+ OnyxToken* dir_token = parser->curr - 2;
+
+ AstNumLit* line_num = make_int_literal(parser->allocator, dir_token->pos.line);
+ retval = (AstTyped *) line_num;
+ break;
+ }
+ else if (parse_possible_directive(parser, "column")) {
+ OnyxToken* dir_token = parser->curr - 2;
+
+ AstNumLit* col_num = make_int_literal(parser->allocator, dir_token->pos.column);
+ retval = (AstTyped *) col_num;
+ break;
+ }
else if (parse_possible_directive(parser, "char")) {
AstNumLit* char_lit = make_node(AstNumLit, Ast_Kind_NumLit);
char_lit->flags |= Ast_Flag_Comptime;