Browse Source

Single file "main" file

gingerBill 7 years ago
parent
commit
1beff539d7
6 changed files with 63 additions and 27 deletions
  1. 1 1
      build.bat
  2. 1 1
      src/check_decl.cpp
  3. 4 4
      src/checker.cpp
  4. 14 1
      src/common.cpp
  5. 29 6
      src/parser.cpp
  6. 14 14
      src/parser.hpp

+ 1 - 1
build.bat

@@ -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 run examples/demo/demo.odin
 
 del *.obj > NUL 2> NUL
 

+ 1 - 1
src/check_decl.cpp

@@ -546,7 +546,7 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
 			error(e->token, "Procedure 'main' cannot have a custom calling convention");
 		}
 		pt->calling_convention = ProcCC_Contextless;
-		if (e->package->kind == ImportedPackage_Init) {
+		if (e->package->kind == Package_Init) {
 			if (c->info.entry_point != nullptr) {
 				error(e->token, "Redeclaration of the entry pointer procedure 'main'");
 			} else {

+ 4 - 4
src/checker.cpp

@@ -255,11 +255,11 @@ Scope *create_scope_from_package(Checker *c, AstPackage *p) {
 	if (p->fullpath == c->parser->init_fullpath) {
 		s->is_init = true;
 	} else {
-		s->is_init = p->kind == ImportedPackage_Init;
+		s->is_init = p->kind == Package_Init;
 	}
 
-	s->is_global = p->kind == ImportedPackage_Builtin;
-	if (p->kind == ImportedPackage_Builtin) {
+	if (p->kind == Package_Builtin) {
+		s->is_global = true;
 		universal_scope->shared = s;
 	}
 
@@ -2954,7 +2954,7 @@ void check_parsed_files(Checker *c) {
 		if (scope->is_init) {
 			c->info.init_scope = scope;
 		}
-		if (p->kind == ImportedPackage_Builtin) {
+		if (p->kind == Package_Builtin) {
 			GB_ASSERT(c->builtin_package == nullptr);
 			c->builtin_package = p;
 		}

+ 14 - 1
src/common.cpp

@@ -757,8 +757,21 @@ enum ReadDirectoryError {
 	ReadDirectory_COUNT,
 };
 
-#if defined(GB_SYSTEM_WINDOWS)
+i64 get_file_size(String path) {
+	char *c_str = alloc_cstring(heap_allocator(), path);
+	defer (gb_free(heap_allocator(), c_str));
+
+	gbFile f = {};
+	gbFileError err = gb_file_open(&f, c_str);
+	defer (gb_file_close(&f));
+	if (err != gbFileError_None) {
+		return -1;
+	}
+	return gb_file_size(&f);
+}
+
 
+#if defined(GB_SYSTEM_WINDOWS)
 ReadDirectoryError read_directory(String path, Array<FileInfo> *fi) {
 	GB_ASSERT(fi != nullptr);
 

+ 29 - 6
src/parser.cpp

@@ -3923,7 +3923,7 @@ void destroy_parser(Parser *p) {
 }
 
 // NOTE(bill): Returns true if it's added
-bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos, ImportedPackageKind kind = ImportedPackage_Normal) {
+bool try_add_import_path(Parser *p, String path, String rel_path, TokenPos pos, PackageKind kind = Package_Normal) {
 	if (build_context.generate_docs) {
 		return false;
 	}
@@ -4159,7 +4159,7 @@ void parse_file(Parser *p, AstFile *f) {
 	if (package_name.kind == Token_Ident) {
 		if (package_name.string == "_") {
 			error(package_name, "Invalid package name '_'");
-		} else if (f->package->kind != ImportedPackage_Builtin && package_name.string == "builtin") {
+		} else if (f->package->kind != Package_Builtin && package_name.string == "builtin") {
 			error(package_name, "Use of reserved package name '%.*s'", LIT(package_name.string));
 		}
 	}
@@ -4252,6 +4252,31 @@ 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;
+	String const ext = str_lit(".odin");
+
+	// NOTE(bill): Single file initial package
+	if (imported_package.kind == Package_Init && string_ends_with(import_path, ext)) {
+		AstPackage *package = gb_alloc_item(heap_allocator(), AstPackage);
+		package->kind = imported_package.kind;
+		package->fullpath = import_path;
+		map_init(&package->files, heap_allocator());
+
+		FileInfo fi = {};
+		fi.name = filename_from_path(import_path);
+		fi.fullpath = import_path;
+		fi.size = get_file_size(import_path);
+		fi.is_dir = false;
+
+		ParseFileError err = parse_imported_file(p, package, &fi, pos);
+		if (err != ParseFile_None) {
+			return err;
+		}
+
+		package->id = p->packages.count+1;
+		array_add(&p->packages, package);
+
+		return ParseFile_None;
+	}
 
 	Array<FileInfo> list = {};
 	ReadDirectoryError rd_err = read_directory(import_path, &list);
@@ -4305,7 +4330,6 @@ ParseFileError parse_import(Parser *p, ImportedPackage imported_package) {
 	for_array(i, list) {
 		FileInfo *fi = &list[i];
 		String name = fi->name;
-		String const ext = str_lit(".odin");
 		if (string_ends_with(name, ext)) {
 			if (is_excluded_target_filename(name)) {
 				continue;
@@ -4350,16 +4374,15 @@ ParseFileError parse_packages(Parser *p, String init_filename) {
 			gb_printf_err("Expected either a directory or a .odin file, got '%.*s'\n", LIT(init_filename));
 			return ParseFile_WrongExtension;
 		}
-		init_fullpath = directory_from_path(init_fullpath);
 	}
 
 	TokenPos init_pos = {};
-	ImportedPackage init_imported_package = {ImportedPackage_Init, init_fullpath, init_fullpath, init_pos};
+	ImportedPackage init_imported_package = {Package_Init, init_fullpath, init_fullpath, init_pos};
 
 	isize shared_package_count = 0;
 	if (!build_context.generate_docs) {
 		String s = get_fullpath_core(heap_allocator(), str_lit("builtin"));
-		try_add_import_path(p, s, s, init_pos, ImportedPackage_Builtin);
+		try_add_import_path(p, s, s, init_pos, Package_Builtin);
 		shared_package_count++;
 	}
 

+ 14 - 14
src/parser.hpp

@@ -25,18 +25,18 @@ struct CommentGroup {
 };
 
 
-enum ImportedPackageKind {
-	ImportedPackage_Normal,
-	ImportedPackage_Builtin,
-	ImportedPackage_Init,
+enum PackageKind {
+	Package_Normal,
+	Package_Builtin,
+	Package_Init,
 };
 
 struct ImportedPackage {
-	ImportedPackageKind kind;
-	String              path;
-	String              rel_path;
-	TokenPos            pos; // import
-	isize               index;
+	PackageKind kind;
+	String      path;
+	String      rel_path;
+	TokenPos    pos; // import
+	isize       index;
 };
 
 struct AstFile {
@@ -85,11 +85,11 @@ struct AstFile {
 
 
 struct AstPackage {
-	isize               id;
-	ImportedPackageKind kind;
-	String              name;
-	String              fullpath;
-	Map<AstFile *>      files; // Key: String (names)
+	isize          id;
+	PackageKind    kind;
+	String         name;
+	String         fullpath;
+	Map<AstFile *> files; // Key: String (names)
 
 	Scope *   scope;       // NOTE(bill): Created in checker
 	DeclInfo *decl_info;   // NOTE(bill): Created in checker