Browse Source

#file #line directives

Ginger Bill 9 years ago
parent
commit
b6cb4f4d14
7 changed files with 113 additions and 73 deletions
  1. 4 2
      code/demo.odin
  2. 21 20
      code/game.odin
  3. 19 19
      code/opengl.odin
  4. 51 30
      src/checker/checker.cpp
  5. 5 1
      src/checker/stmt.cpp
  6. 1 1
      src/checker/type.cpp
  7. 12 0
      src/parser.cpp

+ 4 - 2
code/demo.odin

@@ -1,5 +1,5 @@
 #import "fmt.odin" as fmt
-#import "game.odin" as game
+// #import "game.odin" as game
 
 test_proc :: proc() {
 	fmt.println("Hello?")
@@ -7,6 +7,8 @@ test_proc :: proc() {
 
 
 main :: proc() {
+	x := 0
 	// fmt.println("% % % %", "Hellope", true, 6.28, {4}int{1, 2, 3, 4})
-	game.run()
+	fmt.println("%(%)", #file, #line)
+	// game.run()
 }

+ 21 - 20
code/game.odin

@@ -46,9 +46,8 @@ make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC)
 	w.width, w.height = msg, height
 
 	class_name   := "Win32-Odin-Window\x00"
-	c_class_name := ^class_name[0]
-	// w.c_title = to_c_string(title)
-	w.c_title = "Title\x00" as []byte
+	c_class_name := class_name.data
+	w.c_title = to_c_string(title)
 
 	instance := GetModuleHandleA(null)
 
@@ -66,7 +65,7 @@ make_window :: proc(title: string, msg, height: int, window_proc: win32.WNDPROC)
 	}
 
 	w.hwnd = CreateWindowExA(0,
-	                         c_class_name, ^w.c_title[0],
+	                         c_class_name, w.c_title.data,
 	                         WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
 	                         CW_USEDEFAULT, CW_USEDEFAULT,
 	                         w.width as i32, w.height as i32,
@@ -125,6 +124,8 @@ display_window :: proc(w: ^Window) {
 
 run :: proc() {
 	using win32
+	using math
+
 	win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline {
 		if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT {
 			ExitProcess(0)
@@ -144,7 +145,7 @@ run :: proc() {
 	prev_time := time_now()
 	running := true
 
-	pos := math.Vec2{100, 100}
+	pos := Vec2{100, 100}
 
 	for running {
 		curr_time := time_now()
@@ -166,38 +167,38 @@ run :: proc() {
 
 		{
 			SPEED :: 500
-			v: math.Vec2
+			v: Vec2
 
 			if is_key_down(Key_Code.RIGHT) { v[0] += 1 }
 			if is_key_down(Key_Code.LEFT)  { v[0] -= 1 }
 			if is_key_down(Key_Code.UP)    { v[1] += 1 }
 			if is_key_down(Key_Code.DOWN)  { v[1] -= 1 }
 
-			v = math.vec2_norm0(v)
+			v = vec2_norm0(v)
 
-			pos += v * math.Vec2{SPEED * dt}
+			pos += v * Vec2{SPEED * dt}
 		}
 
 
-		gl.clear_color(0.5, 0.7, 1.0, 1.0)
-		gl.clear(gl.COLOR_BUFFER_BIT)
+		gl.ClearColor(0.5, 0.7, 1.0, 1.0)
+		gl.Clear(gl.COLOR_BUFFER_BIT)
 
-		gl.load_identity()
-		gl.ortho(0, window.width as f64,
+		gl.LoadIdentity()
+		gl.Ortho(0, window.width as f64,
 		         0, window.height as f64, 0, 1)
 
 		draw_rect :: proc(x, y, w, h: f32) {
-			gl.begin(gl.TRIANGLES)
+			gl.Begin(gl.TRIANGLES)
 
-			gl.color3f(1, 0, 0); gl.vertex3f(x,   y,   0)
-			gl.color3f(0, 1, 0); gl.vertex3f(x+w, y,   0)
-			gl.color3f(0, 0, 1); gl.vertex3f(x+w, y+h, 0)
+			gl.Color3f(1, 0, 0); gl.Vertex3f(x,   y,   0)
+			gl.Color3f(0, 1, 0); gl.Vertex3f(x+w, y,   0)
+			gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0)
 
-			gl.color3f(0, 0, 1); gl.vertex3f(x+w, y+h, 0)
-			gl.color3f(1, 1, 0); gl.vertex3f(x,   y+h, 0)
-			gl.color3f(1, 0, 0); gl.vertex3f(x,   y,   0)
+			gl.Color3f(0, 0, 1); gl.Vertex3f(x+w, y+h, 0)
+			gl.Color3f(1, 1, 0); gl.Vertex3f(x,   y+h, 0)
+			gl.Color3f(1, 0, 0); gl.Vertex3f(x,   y,   0)
 
-			gl.end()
+			gl.End()
 		}
 
 		draw_rect(pos[0], pos[1], 50, 50)

+ 19 - 19
code/opengl.odin

@@ -27,23 +27,23 @@ TEXTURE_MIN_FILTER  :: 0x2801
 TEXTURE_WRAP_S      :: 0x2802
 TEXTURE_WRAP_T      :: 0x2803
 
-clear          :: proc(mask: u32)                                #foreign "glClear"
-clear_color    :: proc(r, g, b, a: f32)                          #foreign "glClearColor"
-begin          :: proc(mode: i32)                                #foreign "glBegin"
-end            :: proc()                                         #foreign "glEnd"
-color3f        :: proc(r, g, b: f32)                             #foreign "glColor3f"
-color4f        :: proc(r, g, b, a: f32)                          #foreign "glColor4f"
-vertex2f       :: proc(x, y: f32)                                #foreign "glVertex2f"
-vertex3f       :: proc(x, y, z: f32)                             #foreign "glVertex3f"
-tex_coord2f    :: proc(u, v: f32)                                #foreign "glTexCoord2f"
-load_identity  :: proc()                                         #foreign "glLoadIdentity"
-ortho          :: proc(left, right, bottom, top, near, far: f64) #foreign "glOrtho"
-blend_func     :: proc(sfactor, dfactor: i32)                    #foreign "glBlendFunc"
-enable         :: proc(cap: i32)                                 #foreign "glEnable"
-disable        :: proc(cap: i32)                                 #foreign "glDisable"
-gen_textures   :: proc(count: i32, result: ^u32)                 #foreign "glGenTextures"
-tex_parameteri :: proc(target, pname, param: i32)                #foreign "glTexParameteri"
-tex_parameterf :: proc(target: i32, pname: i32, param: f32)      #foreign "glTexParameterf"
-bind_texture   :: proc(target: i32, texture: u32)                #foreign "glBindTexture"
-tex_image2d    :: proc(target, level, internal_format, width, height, border, format, _type: i32, pixels: rawptr) #foreign "glTexImage2D"
+Clear          :: proc(mask: u32)                                #foreign "glClear"
+ClearColor     :: proc(r, g, b, a: f32)                          #foreign "glClearColor"
+Begin          :: proc(mode: i32)                                #foreign "glBegin"
+End            :: proc()                                         #foreign "glEnd"
+Color3f        :: proc(r, g, b: f32)                             #foreign "glColor3f"
+Color4f        :: proc(r, g, b, a: f32)                          #foreign "glColor4f"
+Vertex2f       :: proc(x, y: f32)                                #foreign "glVertex2f"
+Vertex3f       :: proc(x, y, z: f32)                             #foreign "glVertex3f"
+TexCoord2f     :: proc(u, v: f32)                                #foreign "glTexCoord2f"
+LoadIdentity   :: proc()                                         #foreign "glLoadIdentity"
+Ortho          :: proc(left, right, bottom, top, near, far: f64) #foreign "glOrtho"
+BlendFunc      :: proc(sfactor, dfactor: i32)                    #foreign "glBlendFunc"
+Enable         :: proc(cap: i32)                                 #foreign "glEnable"
+Disable        :: proc(cap: i32)                                 #foreign "glDisable"
+GenTextures    :: proc(count: i32, result: ^u32)                 #foreign "glGenTextures"
+TexParameteri  :: proc(target, pname, param: i32)                #foreign "glTexParameteri"
+TexParameterf  :: proc(target: i32, pname: i32, param: f32)      #foreign "glTexParameterf"
+BindTexture    :: proc(target: i32, texture: u32)                #foreign "glBindTexture"
+TexImage2D     :: proc(target, level, internal_format, width, height, border, format, _type: i32, pixels: rawptr) #foreign "glTexImage2D"
 

+ 51 - 30
src/checker/checker.cpp

@@ -402,6 +402,23 @@ Entity *scope_insert_entity(Scope *s, Entity *entity) {
 	return NULL;
 }
 
+void check_scope_usage(Checker *c, Scope *scope) {
+	// TODO(bill): Use this?
+#if 0
+	gb_for_array(i, scope->elements.entries) {
+		auto *entry = scope->elements.entries + i;
+		Entity *e = entry->value;
+		if (e->kind == Entity_Variable && !e->Variable.used) {
+			warning(e->token, "Unused variable: %.*s", LIT(e->token.string));
+		}
+	}
+
+	for (Scope *child = scope->first_child; child != NULL; child = child->next) {
+		check_scope_usage(c, child);
+	}
+#endif
+}
+
 
 void add_dependency(DeclInfo *d, Entity *e) {
 	map_set(&d->deps, hash_pointer(e), cast(b32)true);
@@ -807,6 +824,39 @@ void check_type_name_cycles(Checker *c, CycleCheck *cc, Entity *e) {
 	// }
 }
 
+void init_type_info_types(Checker *c) {
+	if (t_type_info == NULL) {
+		String type_info_str = make_string("Type_Info");
+		Entity *e = current_scope_lookup_entity(c->global_scope, type_info_str);
+		GB_ASSERT_MSG(e != NULL, "Internal Compiler Error: Could not find type declaration for `Type_Info`");
+		t_type_info = e->type;
+		t_type_info_ptr = make_type_pointer(c->allocator, t_type_info);
+
+		auto *record = &get_base_type(e->type)->Record;
+
+		t_type_info_member = record->other_fields[0]->type;
+		t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member);
+
+		GB_ASSERT_MSG(record->field_count == 16, "Internal Compiler Error: Invalid `Type_Info` layout");
+		t_type_info_named     = record->fields[ 1]->type;
+		t_type_info_integer   = record->fields[ 2]->type;
+		t_type_info_float     = record->fields[ 3]->type;
+		t_type_info_string    = record->fields[ 4]->type;
+		t_type_info_boolean   = record->fields[ 5]->type;
+		t_type_info_pointer   = record->fields[ 6]->type;
+		t_type_info_procedure = record->fields[ 7]->type;
+		t_type_info_array     = record->fields[ 8]->type;
+		t_type_info_slice     = record->fields[ 9]->type;
+		t_type_info_vector    = record->fields[10]->type;
+		t_type_info_tuple     = record->fields[11]->type;
+		t_type_info_struct    = record->fields[12]->type;
+		t_type_info_union     = record->fields[13]->type;
+		t_type_info_raw_union = record->fields[14]->type;
+		t_type_info_enum      = record->fields[15]->type;
+	}
+
+}
+
 
 void check_parsed_files(Checker *c) {
 
@@ -1016,37 +1066,8 @@ void check_parsed_files(Checker *c) {
 
 	check_global_entity(c, Entity_TypeName);
 
+	init_type_info_types(c);
 #if 1
-	if (t_type_info == NULL) {
-		String type_info_str = make_string("Type_Info");
-		Entity *e = current_scope_lookup_entity(c->global_scope, type_info_str);
-		GB_ASSERT_MSG(e != NULL, "Internal Compiler Error: Could not find type declaration for `Type_Info`");
-		t_type_info = e->type;
-		t_type_info_ptr = make_type_pointer(c->allocator, t_type_info);
-
-		auto *record = &get_base_type(e->type)->Record;
-
-		t_type_info_member = record->other_fields[0]->type;
-		t_type_info_member_ptr = make_type_pointer(c->allocator, t_type_info_member);
-
-		GB_ASSERT_MSG(record->field_count == 16, "Internal Compiler Error: Invalid `Type_Info` layout");
-		t_type_info_named     = record->fields[ 1]->type;
-		t_type_info_integer   = record->fields[ 2]->type;
-		t_type_info_float     = record->fields[ 3]->type;
-		t_type_info_string    = record->fields[ 4]->type;
-		t_type_info_boolean   = record->fields[ 5]->type;
-		t_type_info_pointer   = record->fields[ 6]->type;
-		t_type_info_procedure = record->fields[ 7]->type;
-		t_type_info_array     = record->fields[ 8]->type;
-		t_type_info_slice     = record->fields[ 9]->type;
-		t_type_info_vector    = record->fields[10]->type;
-		t_type_info_tuple     = record->fields[11]->type;
-		t_type_info_struct    = record->fields[12]->type;
-		t_type_info_union     = record->fields[13]->type;
-		t_type_info_raw_union = record->fields[14]->type;
-		t_type_info_enum      = record->fields[15]->type;
-	}
-
 	check_global_entity(c, Entity_Constant);
 	check_global_entity(c, Entity_Procedure);
 	check_global_entity(c, Entity_Variable);

+ 5 - 1
src/checker/stmt.cpp

@@ -388,6 +388,8 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
 	CheckerContext old_context = c->context;
 	c->context.scope = decl->scope;
 	c->context.decl = decl;
+	defer (c->context = old_context);
+
 
 	GB_ASSERT(type->kind == Type_Proc);
 	if (type->Proc.param_count > 0) {
@@ -431,7 +433,8 @@ void check_proc_body(Checker *c, Token token, DeclInfo *decl, Type *type, AstNod
 	}
 	pop_procedure(c);
 
-	c->context = old_context;
+
+	check_scope_usage(c, c->context.scope);
 }
 
 void check_proc_decl(Checker *c, Entity *e, DeclInfo *d, b32 check_body_later) {
@@ -1211,6 +1214,7 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) {
 				Type *tag_ptr_type = make_type_pointer(c->allocator, tag_type);
 				Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tag_ptr_type);
 				add_entity(c, c->context.scope, ms->var, tag_var);
+				add_entity_use(&c->info, ms->var, tag_var);
 			}
 			check_stmt_list(c, cc->stmts, mod_flags);
 			check_close_scope(c);

+ 1 - 1
src/checker/type.cpp

@@ -759,7 +759,7 @@ Selection lookup_field(Type *type_, String field_name, b32 is_type, Selection se
 			if (entity__string_data == NULL) {
 				Token token = {Token_Identifier};
 				token.string = data_str;
-				entity__string_data = make_entity_field(a, NULL, token, make_type_pointer(a, t_byte), false, 0);
+				entity__string_data = make_entity_field(a, NULL, token, make_type_pointer(a, t_u8), false, 0);
 			}
 
 			if (entity__string_count == NULL) {

+ 12 - 0
src/parser.cpp

@@ -1258,6 +1258,18 @@ AstNode *parse_operand(AstFile *f, b32 lhs) {
 				expect_token(f, Token_String);
 			}
 			operand = parse_operand(f, lhs);
+		} else if (are_strings_equal(name, make_string("file"))) {
+			Token token = operand->TagExpr.name;
+			token.kind = Token_String;
+			token.string = token.pos.file;
+			return make_basic_lit(f, token);
+		} else if (are_strings_equal(name, make_string("line"))) {
+			Token token = operand->TagExpr.name;
+			token.kind = Token_Integer;
+			char *str = gb_alloc_array(gb_arena_allocator(&f->arena), char, 20);
+			gb_i64_to_str(token.pos.line, str, 10);
+			token.string = make_string(str);
+			return make_basic_lit(f, token);
 		} else {
 			operand->TagExpr.expr = parse_expr(f, false);
 		}