mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 01:21:38 -07:00
Parse directories to be packages
This commit is contained in:
@@ -42,7 +42,7 @@ del *.ilk > NUL 2> NUL
|
||||
|
||||
cl %compiler_settings% "src\main.cpp" ^
|
||||
/link %linker_settings% -OUT:%exe_name% ^
|
||||
&& odin run examples/demo.odin
|
||||
&& odin check examples/demo
|
||||
|
||||
del *.obj > NUL 2> NUL
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package atomics
|
||||
|
||||
// TODO(bill): Use assembly instead here to implement atomics
|
||||
// Inline vs external file?
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package bits
|
||||
|
||||
U8_MIN :: u8(0);
|
||||
U16_MIN :: u16(0);
|
||||
U32_MIN :: u32(0);
|
||||
@@ -1,3 +1,5 @@
|
||||
package c
|
||||
|
||||
CHAR_BIT :: 8;
|
||||
|
||||
c_bool :: bool;
|
||||
@@ -1,5 +1,6 @@
|
||||
// Multiple precision decimal numbers
|
||||
// NOTE: This is only for floating point printing and nothing else
|
||||
package decimal
|
||||
|
||||
Decimal :: struct {
|
||||
digits: [384]byte, // big-endian digits
|
||||
@@ -1,9 +1,11 @@
|
||||
import "core:os.odin"
|
||||
import "core:mem.odin"
|
||||
import "core:utf8.odin"
|
||||
import "core:types.odin"
|
||||
import "core:strconv.odin"
|
||||
import "core:raw.odin"
|
||||
package fmt
|
||||
|
||||
import "core:os"
|
||||
import "core:mem"
|
||||
import "core:unicode/utf8"
|
||||
import "core:types"
|
||||
import "core:strconv"
|
||||
import "core:raw"
|
||||
|
||||
|
||||
_BUFFER_SIZE :: 1<<12;
|
||||
@@ -1,4 +1,6 @@
|
||||
import "core:mem.odin"
|
||||
package hash
|
||||
|
||||
import "core:mem"
|
||||
|
||||
adler32 :: proc(data: []byte) -> u32 {
|
||||
ADLER_CONST :: 65521;
|
||||
@@ -1,3 +1,5 @@
|
||||
package math
|
||||
|
||||
TAU :: 6.28318530717958647692528676655900576;
|
||||
PI :: 3.14159265358979323846264338327950288;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
package rand
|
||||
|
||||
Rand :: struct {
|
||||
state: u64,
|
||||
inc: u64,
|
||||
@@ -1,5 +1,6 @@
|
||||
import "core:raw.odin"
|
||||
import "core:mem.odin"
|
||||
package mem
|
||||
|
||||
import "core:raw"
|
||||
|
||||
foreign __llvm_core {
|
||||
@(link_name = "llvm.bswap.i16") swap16 :: proc(b: u16) -> u16 ---;
|
||||
@@ -85,11 +86,11 @@ AllocationHeader :: struct {size: int};
|
||||
|
||||
allocation_header_fill :: proc(header: ^AllocationHeader, data: rawptr, size: int) {
|
||||
header.size = size;
|
||||
ptr := cast(^uint)(mem.ptr_offset(header, 1));
|
||||
n := mem.ptr_sub(cast(^uint)data, ptr);
|
||||
ptr := cast(^uint)(ptr_offset(header, 1));
|
||||
n := ptr_sub(cast(^uint)data, ptr);
|
||||
|
||||
for i in 0..n {
|
||||
mem.ptr_offset(ptr, i)^ = ~uint(0);
|
||||
ptr_offset(ptr, i)^ = ~uint(0);
|
||||
}
|
||||
}
|
||||
allocation_header :: proc(data: rawptr) -> ^AllocationHeader {
|
||||
@@ -1,7 +1,8 @@
|
||||
package opengl
|
||||
|
||||
when ODIN_OS == "windows" {
|
||||
foreign import lib "system:opengl32.lib"
|
||||
import win32 "core:sys/windows.odin"
|
||||
import "core:sys/wgl.odin"
|
||||
import win32 "core:sys/win32"
|
||||
} else when ODIN_OS == "linux" {
|
||||
foreign import lib "system:gl"
|
||||
}
|
||||
@@ -48,7 +49,7 @@ get_gl_proc_address :: proc(name: string) -> rawptr {
|
||||
}
|
||||
// NOTE(bill): null terminated
|
||||
assert((&name[0] + len(name))^ == 0);
|
||||
res := wgl.get_gl_proc_address(cstring(&name[0]));
|
||||
res := win32.get_gl_proc_address(cstring(&name[0]));
|
||||
if res == nil {
|
||||
res = win32.get_proc_address(_libgl, cstring(&name[0]));
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
package opengl
|
||||
|
||||
FALSE :: 0;
|
||||
TRUE :: 1;
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
when ODIN_OS == "windows" do export "core:os/windows.odin";
|
||||
when ODIN_OS == "osx" do export "core:os/osx.odin";
|
||||
when ODIN_OS == "linux" do export "core:os/linux.odin";
|
||||
when ODIN_OS == "essence" do export "core:os/essence.odin";
|
||||
package os
|
||||
|
||||
import "mem.odin";
|
||||
import "core:mem"
|
||||
|
||||
write_string :: proc(fd: Handle, str: string) -> (int, Errno) {
|
||||
return write(fd, cast([]byte)str);
|
||||
@@ -1,3 +1,5 @@
|
||||
package os
|
||||
|
||||
foreign import api "system:api"
|
||||
|
||||
Handle :: distinct int;
|
||||
@@ -17,8 +19,8 @@ ERROR_PATH_NOT_WITHIN_MOUNTED_VOLUME : Errno : -14;
|
||||
ERROR_PATH_NOT_FOUND : Errno : -15;
|
||||
ERROR_FILE_EXISTS : Errno : -19;
|
||||
ERROR_FILE_NOT_FOUND : Errno : -20;
|
||||
ERROR_DRIVE_ERROR_FILE_DAMAGED : Errno : -21;
|
||||
ERROR_ACCESS_NOT_WITHIN_FILE_BOUNDS : Errno : -22;
|
||||
ERROR_DRIVE_ERROR_FILE_DAMAGED : Errno : -21;
|
||||
ERROR_ACCESS_NOT_WITHIN_FILE_BOUNDS : Errno : -22;
|
||||
ERROR_ACCESS_DENIED : Errno : -23;
|
||||
ERROR_FILE_IN_EXCLUSIVE_USE : Errno : -24;
|
||||
ERROR_FILE_CANNOT_GET_EXCLUSIVE_USE : Errno : -25;
|
||||
@@ -1,8 +1,10 @@
|
||||
package os
|
||||
|
||||
foreign import dl "system:dl"
|
||||
foreign import libc "system:c"
|
||||
|
||||
import "core:strings.odin"
|
||||
import "core:mem.odin"
|
||||
import "core:strings"
|
||||
import "core:mem"
|
||||
|
||||
Handle :: distinct i32;
|
||||
File_Time :: distinct u64;
|
||||
@@ -1,8 +1,10 @@
|
||||
package os
|
||||
|
||||
foreign import dl "system:dl"
|
||||
foreign import libc "system:c"
|
||||
|
||||
import "core:strings.odin"
|
||||
import "core:mem.odin"
|
||||
import "core:strings"
|
||||
import "core:mem"
|
||||
|
||||
Handle :: distinct i32;
|
||||
File_Time :: distinct u64;
|
||||
@@ -1,5 +1,7 @@
|
||||
import win32 "core:sys/windows.odin"
|
||||
import "core:mem.odin"
|
||||
package os
|
||||
|
||||
import "core:sys/win32"
|
||||
import "core:mem"
|
||||
|
||||
Handle :: distinct uintptr;
|
||||
File_Time :: distinct u64;
|
||||
@@ -1,3 +1,5 @@
|
||||
package raw
|
||||
|
||||
Any :: struct {
|
||||
data: rawptr,
|
||||
typeid: typeid,
|
||||
@@ -1,10 +1,10 @@
|
||||
#shared_global_scope
|
||||
package runtime
|
||||
|
||||
import "core:os.odin"
|
||||
// import "core:fmt.odin" // TODO(bill): Remove the need for `fmt` here
|
||||
import "core:utf8.odin"
|
||||
import "core:raw.odin"
|
||||
import "core:mem.odin"
|
||||
import "core:os"
|
||||
// import "core:fmt" // TODO(bill): Remove the need for `fmt` here
|
||||
import "core:unicode/utf8"
|
||||
import "core:raw"
|
||||
import "core:mem"
|
||||
|
||||
// Naming Conventions:
|
||||
// In general, Ada_Case for types and snake_case for values
|
||||
@@ -1,4 +1,4 @@
|
||||
#shared_global_scope
|
||||
package runtime
|
||||
|
||||
/*
|
||||
@(link_name="__multi3")
|
||||
@@ -1,3 +1,5 @@
|
||||
package sort
|
||||
|
||||
bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
|
||||
assert(f != nil);
|
||||
count := len(array);
|
||||
@@ -1,4 +1,6 @@
|
||||
using import "core:decimal.odin"
|
||||
package strconv
|
||||
|
||||
using import "core:decimal"
|
||||
|
||||
Int_Flag :: enum {
|
||||
Prefix = 1<<0,
|
||||
@@ -1,5 +1,7 @@
|
||||
import "core:mem.odin"
|
||||
import "core:raw.odin"
|
||||
package strings
|
||||
|
||||
import "core:mem"
|
||||
import "core:raw"
|
||||
|
||||
new_string :: proc(s: string) -> string {
|
||||
c := make([]byte, len(s)+1);
|
||||
@@ -1,6 +1,7 @@
|
||||
package win32
|
||||
|
||||
when ODIN_OS == "windows" {
|
||||
foreign import "system:opengl32.lib"
|
||||
using import "core:sys/windows.odin"
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,5 @@
|
||||
package thread
|
||||
|
||||
#assert(ODIN_OS == "windows");
|
||||
|
||||
when ODIN_OS == "windows" {
|
||||
@@ -77,4 +79,4 @@ destroy :: proc(thread: ^Thread) {
|
||||
|
||||
terminate :: proc(using thread : ^Thread, exit_code : u32) {
|
||||
win32.terminate_thread(win32_thread, exit_code);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
package types
|
||||
|
||||
are_types_identical :: proc(a, b: ^Type_Info) -> bool {
|
||||
if a == b do return true;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import "utf8.odin"
|
||||
package utf8
|
||||
|
||||
import "core:unicode/utf8"
|
||||
|
||||
REPLACEMENT_CHAR :: '\uFFFD';
|
||||
MAX_RUNE :: '\U0010FFFF';
|
||||
@@ -1,3 +1,5 @@
|
||||
package utf8
|
||||
|
||||
RUNE_ERROR :: '\ufffd';
|
||||
RUNE_SELF :: 0x80;
|
||||
RUNE_BOM :: 0xfeff;
|
||||
+100
@@ -734,3 +734,103 @@ String path_to_full_path(gbAllocator a, String path) {
|
||||
char *fullpath = gb_path_get_full_name(a, path_c);
|
||||
return make_string_c(fullpath);
|
||||
}
|
||||
|
||||
|
||||
|
||||
struct FileInfo {
|
||||
String name;
|
||||
String fullpath;
|
||||
i64 size;
|
||||
bool is_dir;
|
||||
};
|
||||
|
||||
enum ReadDirectoryError {
|
||||
ReadDirectory_None,
|
||||
|
||||
ReadDirectory_InvalidPath,
|
||||
ReadDirectory_NotDir,
|
||||
ReadDirectory_EOF,
|
||||
ReadDirectory_Unknown,
|
||||
|
||||
ReadDirectory_COUNT,
|
||||
};
|
||||
|
||||
#if defined(GB_SYSTEM_WINDOWS)
|
||||
|
||||
ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) {
|
||||
GB_ASSERT(fi != nullptr);
|
||||
|
||||
while (path.len > 0) {
|
||||
Rune end = path[path.len-1];
|
||||
if (end == '/') {
|
||||
path.len -= 1;
|
||||
} else if (end == '\\') {
|
||||
path.len -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (path.len == 0) {
|
||||
return ReadDirectory_InvalidPath;
|
||||
}
|
||||
if (!path_is_directory(path)) {
|
||||
return ReadDirectory_NotDir;
|
||||
}
|
||||
|
||||
gbAllocator a = heap_allocator();
|
||||
|
||||
char *new_path = gb_alloc_array(a, char, path.len+3);
|
||||
defer (gb_free(a, new_path));
|
||||
|
||||
gb_memmove(new_path, path.text, path.len);
|
||||
gb_memmove(new_path+path.len, "/*", 2);
|
||||
new_path[path.len+2] = 0;
|
||||
|
||||
String np = make_string(cast(u8 *)new_path, path.len+2);
|
||||
String16 wstr = string_to_string16(a, np);
|
||||
defer (gb_free(a, wstr.text));
|
||||
|
||||
WIN32_FIND_DATAW file_data = {};
|
||||
HANDLE find_file = FindFirstFileW(wstr.text, &file_data);
|
||||
if (find_file == INVALID_HANDLE_VALUE) {
|
||||
return ReadDirectory_Unknown;
|
||||
}
|
||||
defer (FindClose(find_file));
|
||||
|
||||
array_init(fi, a, 0, 100);
|
||||
|
||||
do {
|
||||
wchar_t *filename_w = file_data.cFileName;
|
||||
i64 size = cast(i64)file_data.nFileSizeLow;
|
||||
size |= (cast(i64)file_data.nFileSizeHigh) << 32;
|
||||
String name = string16_to_string(a, make_string16_c(filename_w));
|
||||
if (name == "." || name == "..") {
|
||||
gb_free(a, name.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
String filepath = {};
|
||||
filepath.len = path.len+1+name.len;
|
||||
filepath.text = gb_alloc_array(a, u8, filepath.len+1);
|
||||
defer (gb_free(a, filepath.text));
|
||||
gb_memmove(filepath.text, path.text, path.len);
|
||||
gb_memmove(filepath.text+path.len, "/", 1);
|
||||
gb_memmove(filepath.text+path.len+1, name.text, name.len);
|
||||
|
||||
FileInfo info = {};
|
||||
info.name = name;
|
||||
info.fullpath = path_to_full_path(a, filepath);
|
||||
info.size = size;
|
||||
info.is_dir = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
array_add(fi, info);
|
||||
|
||||
} while (FindNextFileW(find_file, &file_data));
|
||||
|
||||
|
||||
return ReadDirectory_None;
|
||||
}
|
||||
|
||||
#else
|
||||
#error Implement read_directory
|
||||
#endif
|
||||
|
||||
+10
-10
@@ -93,15 +93,15 @@ void print_declaration(AstNode *decl) {
|
||||
}
|
||||
|
||||
void generate_documentation(Parser *parser) {
|
||||
for_array(file_index, parser->files) {
|
||||
AstFile *file = parser->files[file_index];
|
||||
Tokenizer *tokenizer = &file->tokenizer;
|
||||
String fullpath = tokenizer->fullpath;
|
||||
gb_printf("%.*s\n", LIT(fullpath));
|
||||
// for_array(file_index, parser->files) {
|
||||
// AstFile *file = parser->files[file_index];
|
||||
// Tokenizer *tokenizer = &file->tokenizer;
|
||||
// String fullpath = tokenizer->fullpath;
|
||||
// gb_printf("%.*s\n", LIT(fullpath));
|
||||
|
||||
for_array(decl_index, file->decls) {
|
||||
AstNode *decl = file->decls[decl_index];
|
||||
print_declaration(decl);
|
||||
}
|
||||
}
|
||||
// for_array(decl_index, file->decls) {
|
||||
// AstNode *decl = file->decls[decl_index];
|
||||
// print_declaration(decl);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
+7
-3
@@ -12,11 +12,13 @@
|
||||
#include "checker.hpp"
|
||||
|
||||
#include "parser.cpp"
|
||||
#if 0
|
||||
#include "docs.cpp"
|
||||
#include "checker.cpp"
|
||||
#include "ir.cpp"
|
||||
#include "ir_opt.cpp"
|
||||
#include "ir_print.cpp"
|
||||
#endif
|
||||
|
||||
// NOTE(bill): 'name' is used in debugging and profiling modes
|
||||
i32 system_exec_command_line_app(char *name, bool is_silent, char *fmt, ...) {
|
||||
@@ -772,8 +774,9 @@ int main(int arg_count, char **arg_ptr) {
|
||||
print_usage_line(0, "%s 32-bit is not yet supported", args[0]);
|
||||
return 1;
|
||||
}
|
||||
#if 0
|
||||
init_universal_scope();
|
||||
|
||||
#endif
|
||||
// TODO(bill): prevent compiling without a linker
|
||||
|
||||
timings_start_section(&timings, str_lit("parse files"));
|
||||
@@ -784,10 +787,11 @@ int main(int arg_count, char **arg_ptr) {
|
||||
}
|
||||
defer (destroy_parser(&parser));
|
||||
|
||||
if (parse_files(&parser, init_filename) != ParseFile_None) {
|
||||
if (parse_packages(&parser, init_filename) != ParseFile_None) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if 0
|
||||
if (build_context.generate_docs) {
|
||||
generate_documentation(&parser);
|
||||
return 0;
|
||||
@@ -1054,6 +1058,6 @@ int main(int arg_count, char **arg_ptr) {
|
||||
system_exec_command_line_app("odin run", false, "%.*s", LIT(output_base));
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
+116
-42
@@ -3712,17 +3712,18 @@ AstNode *parse_stmt(AstFile *f) {
|
||||
Token name = expect_token(f, Token_Ident);
|
||||
String tag = name.string;
|
||||
|
||||
if (tag == "shared_global_scope") {
|
||||
if (f->curr_proc == nullptr) {
|
||||
f->is_global_scope = true;
|
||||
s = ast_empty_stmt(f, f->curr_token);
|
||||
} else {
|
||||
syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope");
|
||||
s = ast_bad_decl(f, token, f->curr_token);
|
||||
}
|
||||
expect_semicolon(f, s);
|
||||
return s;
|
||||
} else if (tag == "bounds_check") {
|
||||
// if (tag == "shared_global_scope") {
|
||||
// if (f->curr_proc == nullptr) {
|
||||
// f->is_global_scope = true;
|
||||
// s = ast_empty_stmt(f, f->curr_token);
|
||||
// } else {
|
||||
// syntax_error(token, "You cannot use #shared_global_scope within a procedure. This must be done at the file scope");
|
||||
// s = ast_bad_decl(f, token, f->curr_token);
|
||||
// }
|
||||
// expect_semicolon(f, s);
|
||||
// return s;
|
||||
// } else
|
||||
if (tag == "bounds_check") {
|
||||
s = parse_stmt(f);
|
||||
s->stmt_state_flags |= StmtStateFlag_bounds_check;
|
||||
if ((s->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) {
|
||||
@@ -3830,6 +3831,7 @@ ParseFileError init_ast_file(AstFile *f, String fullpath, TokenPos *err_pos) {
|
||||
isize init_token_cap = cast(isize)gb_max(next_pow2(cast(i64)(file_size/2ll)), 16);
|
||||
array_init(&f->tokens, heap_allocator(), 0, gb_max(init_token_cap, 16));
|
||||
|
||||
|
||||
if (err == TokenizerInit_Empty) {
|
||||
Token token = {Token_EOF};
|
||||
token.pos.file = fullpath;
|
||||
@@ -3881,8 +3883,9 @@ void destroy_ast_file(AstFile *f) {
|
||||
|
||||
bool init_parser(Parser *p) {
|
||||
GB_ASSERT(p != nullptr);
|
||||
array_init(&p->files, heap_allocator());
|
||||
map_init(&p->packages, heap_allocator());
|
||||
array_init(&p->imports, heap_allocator());
|
||||
array_init(&p->files, heap_allocator());
|
||||
gb_mutex_init(&p->file_add_mutex);
|
||||
gb_mutex_init(&p->file_decl_mutex);
|
||||
return true;
|
||||
@@ -3921,8 +3924,8 @@ bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos)
|
||||
}
|
||||
}
|
||||
|
||||
ImportedFile item = {};
|
||||
item.kind = ImportedFile_Normal;
|
||||
ImportedPackage item = {};
|
||||
item.kind = ImportedPackage_Normal;
|
||||
item.path = path;
|
||||
item.rel_path = rel_path;
|
||||
item.pos = pos;
|
||||
@@ -4164,28 +4167,33 @@ void parse_file(Parser *p, AstFile *f) {
|
||||
|
||||
comsume_comment_groups(f, f->prev_token);
|
||||
|
||||
|
||||
f->package_token = expect_token(f, Token_package);
|
||||
Token package_name = expect_token_after(f, Token_Ident, "package");
|
||||
if (package_name.kind == Token_Ident) {
|
||||
if (package_name.string == "_") {
|
||||
error(package_name, "Invalid package name '_'");
|
||||
} else if (f->package->kind != ImportedPackage_Runtime && package_name.string == "runtime") {
|
||||
error(package_name, "Use of reserved package name '%.*s'", LIT(package_name.string));
|
||||
}
|
||||
}
|
||||
f->package_name = package_name.string;
|
||||
|
||||
f->decls = parse_stmt_list(f);
|
||||
parse_setup_file_decls(p, f, base_dir, f->decls);
|
||||
}
|
||||
|
||||
|
||||
|
||||
ParseFileError parse_import(Parser *p, ImportedFile imported_file) {
|
||||
String import_path = imported_file.path;
|
||||
String import_rel_path = imported_file.rel_path;
|
||||
TokenPos pos = imported_file.pos;
|
||||
ParseFileError parse_imported_file(Parser *p, AstPackage *package, FileInfo *fi, TokenPos pos) {
|
||||
AstFile *file = gb_alloc_item(heap_allocator(), AstFile);
|
||||
file->file_kind = imported_file.kind;
|
||||
if (file->file_kind == ImportedFile_Shared) {
|
||||
file->is_global_scope = true;
|
||||
}
|
||||
file->package = package;
|
||||
|
||||
TokenPos err_pos = {0};
|
||||
ParseFileError err = init_ast_file(file, import_path, &err_pos);
|
||||
ParseFileError err = init_ast_file(file, fi->fullpath, &err_pos);
|
||||
|
||||
if (err != ParseFile_None) {
|
||||
if (err == ParseFile_EmptyFile) {
|
||||
if (import_path == p->init_fullpath) {
|
||||
if (fi->fullpath == p->init_fullpath) {
|
||||
gb_printf_err("Initial file is empty - %.*s\n", LIT(p->init_fullpath));
|
||||
gb_exit(1);
|
||||
}
|
||||
@@ -4195,7 +4203,7 @@ ParseFileError parse_import(Parser *p, ImportedFile imported_file) {
|
||||
if (pos.line != 0) {
|
||||
gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column);
|
||||
}
|
||||
gb_printf_err("Failed to parse file: %.*s\n\t", LIT(import_rel_path));
|
||||
gb_printf_err("Failed to parse file: %.*s\n\t", LIT(fi->name));
|
||||
switch (err) {
|
||||
case ParseFile_WrongExtension:
|
||||
gb_printf_err("Invalid file extension: File must have the extension '.odin'");
|
||||
@@ -4207,7 +4215,7 @@ ParseFileError parse_import(Parser *p, ImportedFile imported_file) {
|
||||
gb_printf_err("File permissions problem");
|
||||
break;
|
||||
case ParseFile_NotFound:
|
||||
gb_printf_err("File cannot be found ('%.*s')", LIT(import_path));
|
||||
gb_printf_err("File cannot be found ('%.*s')", LIT(fi->fullpath));
|
||||
break;
|
||||
case ParseFile_InvalidToken:
|
||||
gb_printf_err("Invalid token found in file at (%td:%td)", err_pos.line, err_pos.column);
|
||||
@@ -4230,8 +4238,17 @@ skip:
|
||||
parse_file(p, file);
|
||||
|
||||
gb_mutex_lock(&p->file_add_mutex);
|
||||
file->id = imported_file.index;
|
||||
// file->id = imported_package.index;
|
||||
HashKey key = hash_string(fi->fullpath);
|
||||
map_set(&package->files, key, file);
|
||||
array_add(&p->files, file);
|
||||
|
||||
if (package->name.len == 0) {
|
||||
package->name = file->package_name;
|
||||
} else if (file->tokens.count > 0 && package->name != file->package_name) {
|
||||
error(file->package_token, "Different package name, expected '%.*s', got '%.*s'", LIT(package->name), LIT(file->package_name));
|
||||
}
|
||||
|
||||
p->total_line_count += file->tokenizer.line_count;
|
||||
p->total_token_count += file->tokens.count;
|
||||
gb_mutex_unlock(&p->file_add_mutex);
|
||||
@@ -4239,12 +4256,74 @@ skip:
|
||||
return ParseFile_None;
|
||||
}
|
||||
|
||||
|
||||
ParseFileError parse_import(Parser *p, ImportedPackage imported_package) {
|
||||
String import_path = imported_package.path;
|
||||
String import_rel_path = imported_package.rel_path;
|
||||
TokenPos pos = imported_package.pos;
|
||||
|
||||
HashKey path_key = hash_string(import_path);
|
||||
if (map_get(&p->packages, path_key) != nullptr) {
|
||||
return ParseFile_None;
|
||||
}
|
||||
|
||||
|
||||
Array<FileInfo> list = {};
|
||||
ReadDirectoryError rd_err = read_directory(import_path, &list);
|
||||
defer (array_free(&list));
|
||||
|
||||
if (rd_err != ReadDirectory_EOF && rd_err != ReadDirectory_None && pos.line != 0) {
|
||||
gb_printf_err("%.*s(%td:%td) ", LIT(pos.file), pos.line, pos.column);
|
||||
}
|
||||
|
||||
switch (rd_err) {
|
||||
case ReadDirectory_InvalidPath:
|
||||
gb_printf_err("Invalid path: %.*s\n", LIT(import_rel_path));
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
global_error_collector.count++;
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
return ParseFile_InvalidFile;
|
||||
case ReadDirectory_NotDir:
|
||||
gb_printf_err("Expected a directory for a package, got a file: %.*s\n", LIT(import_rel_path));
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
global_error_collector.count++;
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
return ParseFile_InvalidFile;
|
||||
case ReadDirectory_Unknown:
|
||||
gb_printf_err("Unknown error whilst reading directory");
|
||||
gb_mutex_lock(&global_error_collector.mutex);
|
||||
global_error_collector.count++;
|
||||
gb_mutex_unlock(&global_error_collector.mutex);
|
||||
return ParseFile_InvalidFile;
|
||||
case ReadDirectory_EOF:
|
||||
break;
|
||||
}
|
||||
|
||||
AstPackage *package = gb_alloc_item(heap_allocator(), AstPackage);
|
||||
package->kind = imported_package.kind;
|
||||
package->fullpath = import_path;
|
||||
map_init(&package->files, heap_allocator());
|
||||
|
||||
// TODO(bill): Fix concurrency
|
||||
for_array(i, list) {
|
||||
FileInfo *fi = &list[i];
|
||||
if (string_ends_with(fi->name, str_lit(".odin"))) {
|
||||
ParseFileError err = parse_imported_file(p, package, fi, pos);
|
||||
if (err != ParseFile_None) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ParseFile_None;
|
||||
}
|
||||
|
||||
GB_THREAD_PROC(parse_worker_file_proc) {
|
||||
if (thread == nullptr) return 0;
|
||||
auto *p = cast(Parser *)thread->user_data;
|
||||
isize index = thread->user_index;
|
||||
ImportedFile imported_file = p->imports[index];
|
||||
ParseFileError err = parse_import(p, imported_file);
|
||||
ImportedPackage imported_package = p->imports[index];
|
||||
ParseFileError err = parse_import(p, imported_package);
|
||||
return cast(isize)err;
|
||||
}
|
||||
|
||||
@@ -4254,29 +4333,24 @@ struct ParserThreadWork {
|
||||
isize import_index;
|
||||
};
|
||||
|
||||
ParseFileError parse_files(Parser *p, String init_filename) {
|
||||
ParseFileError parse_packages(Parser *p, String init_filename) {
|
||||
GB_ASSERT(init_filename.text[init_filename.len] == 0);
|
||||
|
||||
char *fullpath_str = gb_path_get_full_name(heap_allocator(), cast(char *)&init_filename[0]);
|
||||
String init_fullpath = string_trim_whitespace(make_string_c(fullpath_str));
|
||||
|
||||
TokenPos init_pos = {};
|
||||
ImportedFile init_imported_file = {ImportedFile_Init, init_fullpath, init_fullpath, init_pos};
|
||||
ImportedPackage init_imported_package = {ImportedPackage_Init, init_fullpath, init_fullpath, init_pos};
|
||||
|
||||
isize shared_file_count = 0;
|
||||
if (!build_context.generate_docs) {
|
||||
String s = get_fullpath_core(heap_allocator(), str_lit("_preload.odin"));
|
||||
ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos};
|
||||
array_add(&p->imports, runtime_file);
|
||||
shared_file_count++;
|
||||
}
|
||||
if (!build_context.generate_docs) {
|
||||
String s = get_fullpath_core(heap_allocator(), str_lit("_soft_numbers.odin"));
|
||||
ImportedFile runtime_file = {ImportedFile_Shared, s, s, init_pos};
|
||||
array_add(&p->imports, runtime_file);
|
||||
String s = get_fullpath_core(heap_allocator(), str_lit("runtime"));
|
||||
ImportedPackage runtime_package = {ImportedPackage_Runtime, s, s, init_pos};
|
||||
array_add(&p->imports, runtime_package);
|
||||
shared_file_count++;
|
||||
}
|
||||
|
||||
array_add(&p->imports, init_imported_file);
|
||||
array_add(&p->imports, init_imported_package);
|
||||
p->init_fullpath = init_fullpath;
|
||||
|
||||
// IMPORTANT TODO(bill): Figure out why this doesn't work on *nix sometimes
|
||||
|
||||
+33
-21
@@ -3,6 +3,8 @@ struct Scope;
|
||||
struct Type;
|
||||
struct Entity;
|
||||
struct DeclInfo;
|
||||
struct AstFile;
|
||||
struct AstPackage;
|
||||
|
||||
|
||||
enum ParseFileError {
|
||||
@@ -23,23 +25,23 @@ struct CommentGroup {
|
||||
};
|
||||
|
||||
|
||||
enum ImportedFileKind {
|
||||
ImportedFile_Normal,
|
||||
ImportedFile_Shared,
|
||||
ImportedFile_Init,
|
||||
enum ImportedPackageKind {
|
||||
ImportedPackage_Normal,
|
||||
ImportedPackage_Runtime,
|
||||
ImportedPackage_Init,
|
||||
};
|
||||
|
||||
struct ImportedFile {
|
||||
ImportedFileKind kind;
|
||||
String path;
|
||||
String rel_path;
|
||||
TokenPos pos; // import
|
||||
isize index;
|
||||
struct ImportedPackage {
|
||||
ImportedPackageKind kind;
|
||||
String path;
|
||||
String rel_path;
|
||||
TokenPos pos; // import
|
||||
isize index;
|
||||
};
|
||||
|
||||
|
||||
struct AstFile {
|
||||
isize id;
|
||||
AstPackage * package;
|
||||
// isize id;
|
||||
String fullpath;
|
||||
gbArena arena;
|
||||
Tokenizer tokenizer;
|
||||
@@ -47,6 +49,8 @@ struct AstFile {
|
||||
isize curr_token_index;
|
||||
Token curr_token;
|
||||
Token prev_token; // previous non-comment
|
||||
Token package_token;
|
||||
String package_name;
|
||||
|
||||
// >= 0: In Expression
|
||||
// < 0: In Control Clause
|
||||
@@ -58,8 +62,6 @@ struct AstFile {
|
||||
isize when_level;
|
||||
|
||||
Array<AstNode *> decls;
|
||||
ImportedFileKind file_kind;
|
||||
bool is_global_scope;
|
||||
Array<AstNode *> imports_and_exports; // 'import' 'using import' 'export'
|
||||
|
||||
|
||||
@@ -81,14 +83,23 @@ struct AstFile {
|
||||
};
|
||||
|
||||
|
||||
struct AstPackage {
|
||||
ImportedPackageKind kind;
|
||||
String name;
|
||||
String fullpath;
|
||||
Map<AstFile *> files; // Key: String (names)
|
||||
};
|
||||
|
||||
|
||||
struct Parser {
|
||||
String init_fullpath;
|
||||
Array<AstFile *> files;
|
||||
Array<ImportedFile> imports;
|
||||
isize total_token_count;
|
||||
isize total_line_count;
|
||||
gbMutex file_add_mutex;
|
||||
gbMutex file_decl_mutex;
|
||||
String init_fullpath;
|
||||
Map<AstPackage *> packages; // Key: String (fullpath)
|
||||
Array<AstFile *> files;
|
||||
Array<ImportedPackage> imports;
|
||||
isize total_token_count;
|
||||
isize total_line_count;
|
||||
gbMutex file_add_mutex;
|
||||
gbMutex file_decl_mutex;
|
||||
};
|
||||
|
||||
enum ProcInlining {
|
||||
@@ -526,3 +537,4 @@ gb_inline bool is_ast_node_when_stmt(AstNode *node) {
|
||||
return node->kind == AstNode_WhenStmt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -82,6 +82,10 @@ gb_inline String make_string_c(char *text) {
|
||||
return make_string(cast(u8 *)cast(void *)text, gb_strlen(text));
|
||||
}
|
||||
|
||||
gb_inline String16 make_string16_c(wchar_t *text) {
|
||||
return make_string16(text, string16_len(text));
|
||||
}
|
||||
|
||||
String substring(String const &s, isize lo, isize hi) {
|
||||
isize max = s.len;
|
||||
GB_ASSERT_MSG(lo <= hi && hi <= max, "%td..%td..%td", lo, hi, max);
|
||||
|
||||
@@ -85,6 +85,7 @@ TOKEN_KIND(Token__KeywordBegin, ""), \
|
||||
TOKEN_KIND(Token_import, "import"), \
|
||||
TOKEN_KIND(Token_export, "export"), \
|
||||
TOKEN_KIND(Token_foreign, "foreign"), \
|
||||
TOKEN_KIND(Token_package, "package"), \
|
||||
TOKEN_KIND(Token_type, "type"), \
|
||||
TOKEN_KIND(Token_when, "when"), \
|
||||
TOKEN_KIND(Token_if, "if"), \
|
||||
|
||||
Reference in New Issue
Block a user