Generic (grouped) declarations: var, let, const, type, import, include

This commit is contained in:
Ginger Bill
2016-12-20 18:58:17 +00:00
parent 478d63424f
commit d0e1efe622
20 changed files with 763 additions and 730 deletions
+17 -22
View File
@@ -1,36 +1,31 @@
#import "atomic.odin"; import (
#import "fmt.odin"; "atomic.odin";
#import "hash.odin"; "fmt.odin";
#import "math.odin"; "hash.odin";
#import "mem.odin"; "math.odin";
#import "opengl.odin"; "mem.odin";
#import "os.odin"; "opengl.odin";
#import "sync.odin"; "os.odin";
#import "utf8.odin"; "sync.odin";
"utf8.odin";
type float32 f32; )
const (
X = iota;
Y;
Z;
A = iota+1;
B;
C;
);
type Byte_Size f64; type Byte_Size f64;
const ( const (
_ = iota; // ignore first value by assigning to blank identifier _ = iota; // ignore first value by assigning to blank identifier
KB Byte_Size = 1 << (10 * iota); KB Byte_Size = 1 << (10 * iota);
// Because there is no type or expression, the previous one is used but
// with `iota` incremented by one
MB; MB;
GB; GB;
TB; TB;
PB; PB;
EB; EB;
); )
proc main() { proc main() {
var x = 123; fmt.println("Here");
fmt.println(x);
} }
+80 -78
View File
@@ -1,8 +1,10 @@
#shared_global_scope; #shared_global_scope;
#import "os.odin"; import (
#import "fmt.odin"; "os.odin";
#import "mem.odin"; "fmt.odin";
"mem.odin";
)
// IMPORTANT NOTE(bill): `type_info` & `type_info_val` cannot be used within a // IMPORTANT NOTE(bill): `type_info` & `type_info_val` cannot be used within a
// #shared_global_scope due to the internals of the compiler. // #shared_global_scope due to the internals of the compiler.
@@ -12,65 +14,67 @@
// IMPORTANT NOTE(bill): Do not change the order of any of this data // IMPORTANT NOTE(bill): Do not change the order of any of this data
// The compiler relies upon this _exact_ order // The compiler relies upon this _exact_ order
type Type_Info_Member struct #ordered { type (
name string; // can be empty if tuple Type_Info_Member struct #ordered {
type_info ^Type_Info; name string; // can be empty if tuple
offset int; // offsets are not used in tuples type_info ^Type_Info;
} offset int; // offsets are not used in tuples
type Type_Info_Record struct #ordered { }
fields []Type_Info_Member; Type_Info_Record struct #ordered {
size int; // in bytes fields []Type_Info_Member;
align int; // in bytes size int; // in bytes
packed bool; align int; // in bytes
ordered bool; packed bool;
} ordered bool;
}
type Type_Info union { Type_Info union {
Named struct #ordered { Named struct #ordered {
name string; name string;
base ^Type_Info; // This will _not_ be a Type_Info.Named base ^Type_Info; // This will _not_ be a Type_Info.Named
}; };
Integer struct #ordered { Integer struct #ordered {
size int; // in bytes size int; // in bytes
signed bool; signed bool;
}; };
Float struct #ordered { Float struct #ordered {
size int; // in bytes size int; // in bytes
}; };
Any struct #ordered {}; Any struct #ordered {};
String struct #ordered {}; String struct #ordered {};
Boolean struct #ordered {}; Boolean struct #ordered {};
Pointer struct #ordered { Pointer struct #ordered {
elem ^Type_Info; // nil -> rawptr elem ^Type_Info; // nil -> rawptr
}; };
Maybe struct #ordered { Maybe struct #ordered {
elem ^Type_Info; elem ^Type_Info;
}; };
Procedure struct #ordered { Procedure struct #ordered {
params ^Type_Info; // Type_Info.Tuple params ^Type_Info; // Type_Info.Tuple
results ^Type_Info; // Type_Info.Tuple results ^Type_Info; // Type_Info.Tuple
variadic bool; variadic bool;
}; };
Array struct #ordered { Array struct #ordered {
elem ^Type_Info; elem ^Type_Info;
elem_size int; elem_size int;
count int; count int;
}; };
Slice struct #ordered { Slice struct #ordered {
elem ^Type_Info; elem ^Type_Info;
elem_size int; elem_size int;
}; };
Vector struct #ordered { Vector struct #ordered {
elem ^Type_Info; elem ^Type_Info;
elem_size int; elem_size int;
count int; count int;
align int; align int;
}; };
Tuple Type_Info_Record; Tuple Type_Info_Record;
Struct Type_Info_Record; Struct Type_Info_Record;
Union Type_Info_Record; Union Type_Info_Record;
Raw_Union Type_Info_Record; Raw_Union Type_Info_Record;
}; }
)
proc type_info_base(info ^Type_Info) -> ^Type_Info { proc type_info_base(info ^Type_Info) -> ^Type_Info {
if info == nil { if info == nil {
@@ -115,26 +119,24 @@ const (
ALLOCATOR_FREE_ALL; ALLOCATOR_FREE_ALL;
ALLOCATOR_RESIZE; ALLOCATOR_RESIZE;
); );
type Allocator_Proc proc(allocator_data rawptr, mode Allocator_Mode, type (
size, alignment int, Allocator_Proc proc(allocator_data rawptr, mode Allocator_Mode,
old_memory rawptr, old_size int, flags u64) -> rawptr; size, alignment int,
old_memory rawptr, old_size int, flags u64) -> rawptr;
Allocator struct #ordered {
procedure Allocator_Proc;
data rawptr;
}
Context struct #ordered {
thread_id int;
allocator Allocator;
type Allocator struct #ordered { user_data rawptr;
procedure Allocator_Proc; user_index int;
data rawptr; }
} );
type Context struct #ordered {
thread_id int;
allocator Allocator;
user_data rawptr;
user_index int;
}
#thread_local var __context Context; #thread_local var __context Context;
+1 -1
View File
@@ -1,7 +1,7 @@
// TODO(bill): Use assembly instead here to implement atomics // TODO(bill): Use assembly instead here to implement atomics
// Inline vs external file? // Inline vs external file?
#import win32 "sys/windows.odin" when ODIN_OS == "windows"; import win32 "sys/windows.odin" when ODIN_OS == "windows";
var _ = compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version var _ = compile_assert(ODIN_ARCH == "amd64"); // TODO(bill): x86 version
+5 -3
View File
@@ -1,6 +1,8 @@
#import "os.odin"; import (
#import "mem.odin"; "os.odin";
#import "utf8.odin"; "mem.odin";
"utf8.odin";
)
const PRINT_BUF_SIZE = 1<<12; const PRINT_BUF_SIZE = 1<<12;
+9 -8
View File
@@ -16,16 +16,17 @@ const (
τ = TAU; τ = TAU;
π = PI; π = PI;
); )
type Vec2 [vector 2]f32; type (
type Vec3 [vector 3]f32; Vec2 [vector 2]f32;
type Vec4 [vector 4]f32; Vec3 [vector 3]f32;
Vec4 [vector 4]f32;
type Mat2 [2]Vec2;
type Mat3 [3]Vec3;
type Mat4 [4]Vec4;
Mat2 [2]Vec2;
Mat3 [3]Vec3;
Mat4 [4]Vec4;
)
proc sqrt32(x f32) -> f32 #foreign "llvm.sqrt.f32" proc sqrt32(x f32) -> f32 #foreign "llvm.sqrt.f32"
proc sqrt64(x f64) -> f64 #foreign "llvm.sqrt.f64" proc sqrt64(x f64) -> f64 #foreign "llvm.sqrt.f64"
+17 -14
View File
@@ -1,5 +1,7 @@
#import "fmt.odin"; import (
#import "os.odin"; "fmt.odin";
"os.odin";
)
proc set(data rawptr, value i32, len int) -> rawptr #link_name "__mem_set" { proc set(data rawptr, value i32, len int) -> rawptr #link_name "__mem_set" {
proc llvm_memset_64bit(dst rawptr, val byte, len int, align i32, is_volatile bool) #foreign "llvm.memset.p0i8.i64" proc llvm_memset_64bit(dst rawptr, val byte, len int, align i32, is_volatile bool) #foreign "llvm.memset.p0i8.i64"
@@ -92,6 +94,7 @@ proc align_forward(ptr rawptr, align int) -> rawptr {
type Allocation_Header struct { type Allocation_Header struct {
size int; size int;
} }
proc allocation_header_fill(header ^Allocation_Header, data rawptr, size int) { proc allocation_header_fill(header ^Allocation_Header, data rawptr, size int) {
header.size = size; header.size = size;
var ptr = (header+1) as ^int; var ptr = (header+1) as ^int;
@@ -113,18 +116,18 @@ proc allocation_header(data rawptr) -> ^Allocation_Header {
// Custom allocators // Custom allocators
type (
Arena struct {
backing Allocator;
memory []byte;
temp_count int;
}
type Arena struct { Arena_Temp_Memory struct {
backing Allocator; arena ^Arena;
memory []byte; original_count int;
temp_count int; }
} )
type Arena_Temp_Memory struct {
arena ^Arena;
original_count int;
}
@@ -270,7 +273,7 @@ proc align_of_type_info(type_info ^Type_Info) -> int {
proc align_formula(size, align int) -> int { proc align_formula(size, align int) -> int {
var result = size + align-1; var result = size + align-1;
return result - result%align; return result - result%align;
}; }
proc size_of_type_info(type_info ^Type_Info) -> int { proc size_of_type_info(type_info ^Type_Info) -> int {
const WORD_SIZE = size_of(int); const WORD_SIZE = size_of(int);
+2 -2
View File
@@ -1,6 +1,6 @@
#foreign_system_library "opengl32" when ODIN_OS == "windows"; #foreign_system_library "opengl32" when ODIN_OS == "windows";
#import win32 "sys/windows.odin" when ODIN_OS == "windows"; import win32 "sys/windows.odin" when ODIN_OS == "windows";
#include "opengl_constants.odin"; include "opengl_constants.odin";
proc Clear (mask u32) #foreign "glClear" proc Clear (mask u32) #foreign "glClear"
proc ClearColor (r, g, b, a f32) #foreign "glClearColor" proc ClearColor (r, g, b, a f32) #foreign "glClearColor"
+1 -1
View File
@@ -1,2 +1,2 @@
#include "os_windows.odin" when ODIN_OS == "windows" include "os_windows.odin" when ODIN_OS == "windows"
+25 -20
View File
@@ -1,17 +1,21 @@
#import win32 "sys/windows.odin"; import (
#import "fmt.odin"; win32 "sys/windows.odin";
"fmt.odin";
)
type File_Time u64; type (
File_Time u64;
type File_Handle raw_union { File_Handle raw_union {
p rawptr; p rawptr;
i int; i int;
} }
type File struct { File struct {
handle File_Handle; handle File_Handle;
last_write_time File_Time; last_write_time File_Time;
} }
)
proc open(name string) -> (File, bool) { proc open(name string) -> (File, bool) {
using win32; using win32;
@@ -92,16 +96,17 @@ const (
); );
// NOTE(bill): Uses startup to initialize it // NOTE(bill): Uses startup to initialize it
var __std_files = [FILE_STANDARD_COUNT]File{ var (
{handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE) transmute File_Handle }, __std_files = [FILE_STANDARD_COUNT]File{
{handle = win32.GetStdHandle(win32.STD_OUTPUT_HANDLE) transmute File_Handle }, {handle = win32.GetStdHandle(win32.STD_INPUT_HANDLE) transmute File_Handle },
{handle = win32.GetStdHandle(win32.STD_ERROR_HANDLE) transmute File_Handle }, {handle = win32.GetStdHandle(win32.STD_OUTPUT_HANDLE) transmute File_Handle },
}; {handle = win32.GetStdHandle(win32.STD_ERROR_HANDLE) transmute File_Handle },
};
var stdin = ^__std_files[FILE_STANDARD_INPUT];
var stdout = ^__std_files[FILE_STANDARD_OUTPUT];
var stderr = ^__std_files[FILE_STANDARD_ERROR];
stdin = ^__std_files[FILE_STANDARD_INPUT];
stdout = ^__std_files[FILE_STANDARD_OUTPUT];
stderr = ^__std_files[FILE_STANDARD_ERROR];
)
proc read_entire_file(name string) -> ([]byte, bool) { proc read_entire_file(name string) -> ([]byte, bool) {
+15 -12
View File
@@ -1,17 +1,20 @@
#import win32 "sys/windows.odin" when ODIN_OS == "windows"; import (
#import "atomic.odin"; win32 "sys/windows.odin" when ODIN_OS == "windows";
"atomic.odin";
)
type Semaphore struct { type (
handle win32.HANDLE; Semaphore struct {
} handle win32.HANDLE;
}
type Mutex struct {
semaphore Semaphore;
counter i32;
owner i32;
recursion i32;
}
Mutex struct {
semaphore Semaphore;
counter i32;
owner i32;
recursion i32;
}
)
proc current_thread_id() -> i32 { proc current_thread_id() -> i32 {
return win32.GetCurrentThreadId() as i32; return win32.GetCurrentThreadId() as i32;
+250 -237
View File
@@ -1,117 +1,124 @@
#foreign_system_library "user32" when ODIN_OS == "windows"; #foreign_system_library "user32" when ODIN_OS == "windows";
#foreign_system_library "gdi32" when ODIN_OS == "windows"; #foreign_system_library "gdi32" when ODIN_OS == "windows";
type HANDLE rawptr; type (
type HWND HANDLE; HANDLE rawptr;
type HDC HANDLE; HWND HANDLE;
type HINSTANCE HANDLE; HDC HANDLE;
type HICON HANDLE; HINSTANCE HANDLE;
type HCURSOR HANDLE; HICON HANDLE;
type HMENU HANDLE; HCURSOR HANDLE;
type HBRUSH HANDLE; HMENU HANDLE;
type HGDIOBJ HANDLE; HBRUSH HANDLE;
type HMODULE HANDLE; HGDIOBJ HANDLE;
type WPARAM uint; HMODULE HANDLE;
type LPARAM int; WPARAM uint;
type LRESULT int; LPARAM int;
type ATOM i16; LRESULT int;
type BOOL i32; ATOM i16;
type WNDPROC proc(hwnd HWND, msg u32, wparam WPARAM, lparam LPARAM) -> LRESULT; BOOL i32;
WNDPROC proc(hwnd HWND, msg u32, wparam WPARAM, lparam LPARAM) -> LRESULT;
)
const INVALID_HANDLE_VALUE = (-1 as int) as HANDLE; const (
INVALID_HANDLE_VALUE = (-1 as int) as HANDLE;
const CS_VREDRAW = 0x0001; CS_VREDRAW = 0x0001;
const CS_HREDRAW = 0x0002; CS_HREDRAW = 0x0002;
const CS_OWNDC = 0x0020; CS_OWNDC = 0x0020;
const CW_USEDEFAULT = -0x80000000; CW_USEDEFAULT = -0x80000000;
const WS_OVERLAPPED = 0; WS_OVERLAPPED = 0;
const WS_MAXIMIZEBOX = 0x00010000; WS_MAXIMIZEBOX = 0x00010000;
const WS_MINIMIZEBOX = 0x00020000; WS_MINIMIZEBOX = 0x00020000;
const WS_THICKFRAME = 0x00040000; WS_THICKFRAME = 0x00040000;
const WS_SYSMENU = 0x00080000; WS_SYSMENU = 0x00080000;
const WS_CAPTION = 0x00C00000; WS_CAPTION = 0x00C00000;
const WS_VISIBLE = 0x10000000; WS_VISIBLE = 0x10000000;
const WS_OVERLAPPEDWINDOW = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX; WS_OVERLAPPEDWINDOW = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX;
const WM_DESTROY = 0x0002; WM_DESTROY = 0x0002;
const WM_CLOSE = 0x0010; WM_CLOSE = 0x0010;
const WM_QUIT = 0x0012; WM_QUIT = 0x0012;
const WM_KEYDOWN = 0x0100; WM_KEYDOWN = 0x0100;
const WM_KEYUP = 0x0101; WM_KEYUP = 0x0101;
const PM_REMOVE = 1; PM_REMOVE = 1;
const COLOR_BACKGROUND = 1 as HBRUSH; COLOR_BACKGROUND = 1 as HBRUSH;
const BLACK_BRUSH = 4; BLACK_BRUSH = 4;
const SM_CXSCREEN = 0; SM_CXSCREEN = 0;
const SM_CYSCREEN = 1; SM_CYSCREEN = 1;
const SW_SHOW = 5; SW_SHOW = 5;
)
type POINT struct #ordered { type (
x, y i32; POINT struct #ordered {
} x, y i32;
}
WNDCLASSEXA struct #ordered {
size, style u32;
wnd_proc WNDPROC;
cls_extra, wnd_extra i32;
instance HINSTANCE;
icon HICON;
cursor HCURSOR;
background HBRUSH;
menu_name, class_name ^u8;
sm HICON;
}
type WNDCLASSEXA struct #ordered { MSG struct #ordered {
size, style u32; hwnd HWND;
wnd_proc WNDPROC; message u32;
cls_extra, wnd_extra i32; wparam WPARAM;
instance HINSTANCE; lparam LPARAM;
icon HICON; time u32;
cursor HCURSOR; pt POINT;
background HBRUSH; }
menu_name, class_name ^u8;
sm HICON;
}
type MSG struct #ordered { RECT struct #ordered {
hwnd HWND; left i32;
message u32; top i32;
wparam WPARAM; right i32;
lparam LPARAM; bottom i32;
time u32; }
pt POINT;
}
type RECT struct #ordered { FILETIME struct #ordered {
left i32; low_date_time, high_date_time u32;
top i32; }
right i32;
bottom i32;
}
type FILETIME struct #ordered { BY_HANDLE_FILE_INFORMATION struct #ordered {
low_date_time, high_date_time u32; file_attributes u32;
} creation_time,
last_access_time,
last_write_time FILETIME;
volume_serial_number,
file_size_high,
file_size_low,
number_of_links,
file_index_high,
file_index_low u32;
}
type BY_HANDLE_FILE_INFORMATION struct #ordered { WIN32_FILE_ATTRIBUTE_DATA struct #ordered {
file_attributes u32; file_attributes u32;
creation_time, creation_time,
last_access_time, last_access_time,
last_write_time FILETIME; last_write_time FILETIME;
volume_serial_number, file_size_high,
file_size_high, file_size_low u32;
file_size_low, }
number_of_links,
file_index_high,
file_index_low u32;
}
type WIN32_FILE_ATTRIBUTE_DATA struct #ordered { GET_FILEEX_INFO_LEVELS i32;
file_attributes u32; )
creation_time, const (
last_access_time, GetFileExInfoStandard = 0 as GET_FILEEX_INFO_LEVELS;
last_write_time FILETIME; GetFileExMaxInfoLevel = 1 as GET_FILEEX_INFO_LEVELS;
file_size_high, )
file_size_low u32;
}
type GET_FILEEX_INFO_LEVELS i32;
const GetFileExInfoStandard = 0 as GET_FILEEX_INFO_LEVELS;
const GetFileExMaxInfoLevel = 1 as GET_FILEEX_INFO_LEVELS;
proc GetLastError () -> i32 #foreign #dll_import proc GetLastError () -> i32 #foreign #dll_import
proc ExitProcess (exit_code u32) #foreign #dll_import proc ExitProcess (exit_code u32) #foreign #dll_import
@@ -176,24 +183,25 @@ proc GetFileSizeEx (file_handle HANDLE, file_size ^i64) -> BOOL #for
proc GetFileAttributesExA (filename ^u8, info_level_id GET_FILEEX_INFO_LEVELS, file_info rawptr) -> BOOL #foreign #dll_import proc GetFileAttributesExA (filename ^u8, info_level_id GET_FILEEX_INFO_LEVELS, file_info rawptr) -> BOOL #foreign #dll_import
proc GetFileInformationByHandle(file_handle HANDLE, file_info ^BY_HANDLE_FILE_INFORMATION) -> BOOL #foreign #dll_import proc GetFileInformationByHandle(file_handle HANDLE, file_info ^BY_HANDLE_FILE_INFORMATION) -> BOOL #foreign #dll_import
const FILE_SHARE_READ = 0x00000001; const (
const FILE_SHARE_WRITE = 0x00000002; FILE_SHARE_READ = 0x00000001;
const FILE_SHARE_DELETE = 0x00000004; FILE_SHARE_WRITE = 0x00000002;
const FILE_GENERIC_ALL = 0x10000000; FILE_SHARE_DELETE = 0x00000004;
const FILE_GENERIC_EXECUTE = 0x20000000; FILE_GENERIC_ALL = 0x10000000;
const FILE_GENERIC_WRITE = 0x40000000; FILE_GENERIC_EXECUTE = 0x20000000;
const FILE_GENERIC_READ = 0x80000000; FILE_GENERIC_WRITE = 0x40000000;
FILE_GENERIC_READ = 0x80000000;
const STD_INPUT_HANDLE = -10; STD_INPUT_HANDLE = -10;
const STD_OUTPUT_HANDLE = -11; STD_OUTPUT_HANDLE = -11;
const STD_ERROR_HANDLE = -12; STD_ERROR_HANDLE = -12;
const CREATE_NEW = 1;
const CREATE_ALWAYS = 2;
const OPEN_EXISTING = 3;
const OPEN_ALWAYS = 4;
const TRUNCATE_EXISTING = 5;
CREATE_NEW = 1;
CREATE_ALWAYS = 2;
OPEN_EXISTING = 3;
OPEN_ALWAYS = 4;
TRUNCATE_EXISTING = 5;
)
@@ -240,31 +248,34 @@ proc ReadBarrier () #foreign
// GDI // GDI
type (
type BITMAPINFOHEADER struct #ordered { BITMAPINFOHEADER struct #ordered {
size u32; size u32;
width, height i32; width, height i32;
planes, bit_count i16; planes, bit_count i16;
compression u32; compression u32;
size_image u32; size_image u32;
x_pels_per_meter i32; x_pels_per_meter i32;
y_pels_per_meter i32; y_pels_per_meter i32;
clr_used u32; clr_used u32;
clr_important u32; clr_important u32;
} }
type BITMAPINFO struct #ordered { BITMAPINFO struct #ordered {
using header BITMAPINFOHEADER; using header BITMAPINFOHEADER;
colors [1]RGBQUAD; colors [1]RGBQUAD;
} }
type RGBQUAD struct #ordered { RGBQUAD struct #ordered {
blue, green, red, reserved byte; blue, green, red, reserved byte;
} }
)
const BI_RGB = 0; const (
const DIB_RGB_COLORS = 0x00; BI_RGB = 0;
const SRCCOPY = 0x00cc0020 as u32; DIB_RGB_COLORS = 0x00;
SRCCOPY = 0x00cc0020 as u32;
)
proc StretchDIBits(hdc HDC, proc StretchDIBits(hdc HDC,
x_dst, y_dst, width_dst, height_dst i32, x_dst, y_dst, width_dst, height_dst i32,
@@ -284,64 +295,67 @@ proc GetClientRect(hwnd HWND, rect ^RECT) -> BOOL #foreign
// Windows OpenGL // Windows OpenGL
const (
PFD_TYPE_RGBA = 0;
PFD_TYPE_COLORINDEX = 1;
PFD_MAIN_PLANE = 0;
PFD_OVERLAY_PLANE = 1;
PFD_UNDERLAY_PLANE = -1;
PFD_DOUBLEBUFFER = 1;
PFD_STEREO = 2;
PFD_DRAW_TO_WINDOW = 4;
PFD_DRAW_TO_BITMAP = 8;
PFD_SUPPORT_GDI = 16;
PFD_SUPPORT_OPENGL = 32;
PFD_GENERIC_FORMAT = 64;
PFD_NEED_PALETTE = 128;
PFD_NEED_SYSTEM_PALETTE = 0x00000100;
PFD_SWAP_EXCHANGE = 0x00000200;
PFD_SWAP_COPY = 0x00000400;
PFD_SWAP_LAYER_BUFFERS = 0x00000800;
PFD_GENERIC_ACCELERATED = 0x00001000;
PFD_DEPTH_DONTCARE = 0x20000000;
PFD_DOUBLEBUFFER_DONTCARE = 0x40000000;
PFD_STEREO_DONTCARE = 0x80000000;
)
const PFD_TYPE_RGBA = 0; type (
const PFD_TYPE_COLORINDEX = 1; HGLRC HANDLE;
const PFD_MAIN_PLANE = 0; PROC proc();
const PFD_OVERLAY_PLANE = 1; wglCreateContextAttribsARBType proc(hdc HDC, hshareContext rawptr, attribList ^i32) -> HGLRC;
const PFD_UNDERLAY_PLANE = -1;
const PFD_DOUBLEBUFFER = 1;
const PFD_STEREO = 2;
const PFD_DRAW_TO_WINDOW = 4;
const PFD_DRAW_TO_BITMAP = 8;
const PFD_SUPPORT_GDI = 16;
const PFD_SUPPORT_OPENGL = 32;
const PFD_GENERIC_FORMAT = 64;
const PFD_NEED_PALETTE = 128;
const PFD_NEED_SYSTEM_PALETTE = 0x00000100;
const PFD_SWAP_EXCHANGE = 0x00000200;
const PFD_SWAP_COPY = 0x00000400;
const PFD_SWAP_LAYER_BUFFERS = 0x00000800;
const PFD_GENERIC_ACCELERATED = 0x00001000;
const PFD_DEPTH_DONTCARE = 0x20000000;
const PFD_DOUBLEBUFFER_DONTCARE = 0x40000000;
const PFD_STEREO_DONTCARE = 0x80000000;
type HGLRC HANDLE;
type PROC proc();
type wglCreateContextAttribsARBType proc(hdc HDC, hshareContext rawptr, attribList ^i32) -> HGLRC;
type PIXELFORMATDESCRIPTOR struct #ordered { PIXELFORMATDESCRIPTOR struct #ordered {
size, size,
version, version,
flags u32; flags u32;
pixel_type, pixel_type,
color_bits, color_bits,
red_bits, red_bits,
red_shift, red_shift,
green_bits, green_bits,
green_shift, green_shift,
blue_bits, blue_bits,
blue_shift, blue_shift,
alpha_bits, alpha_bits,
alpha_shift, alpha_shift,
accum_bits, accum_bits,
accum_red_bits, accum_red_bits,
accum_green_bits, accum_green_bits,
accum_blue_bits, accum_blue_bits,
accum_alpha_bits, accum_alpha_bits,
depth_bits, depth_bits,
stencil_bits, stencil_bits,
aux_buffers, aux_buffers,
layer_type, layer_type,
reserved byte; reserved byte;
layer_mask, layer_mask,
visible_mask, visible_mask,
damage_mask u32; damage_mask u32;
} }
)
proc GetDC (h HANDLE) -> HDC #foreign proc GetDC (h HANDLE) -> HDC #foreign
proc SetPixelFormat (hdc HDC, pixel_format i32, pfd ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign #dll_import proc SetPixelFormat (hdc HDC, pixel_format i32, pfd ^PIXELFORMATDESCRIPTOR ) -> BOOL #foreign #dll_import
@@ -349,11 +363,13 @@ proc ChoosePixelFormat(hdc HDC, pfd ^PIXELFORMATDESCRIPTOR) -> i32 #foreign #dll
proc SwapBuffers (hdc HDC) -> BOOL #foreign #dll_import proc SwapBuffers (hdc HDC) -> BOOL #foreign #dll_import
proc ReleaseDC (wnd HWND, hdc HDC) -> i32 #foreign #dll_import proc ReleaseDC (wnd HWND, hdc HDC) -> i32 #foreign #dll_import
const WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091; const (
const WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092; WGL_CONTEXT_MAJOR_VERSION_ARB = 0x2091;
const WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126; WGL_CONTEXT_MINOR_VERSION_ARB = 0x2092;
const WGL_CONTEXT_CORE_PROFILE_BIT_ARB = 0x0001; WGL_CONTEXT_PROFILE_MASK_ARB = 0x9126;
const WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB = 0x0002; WGL_CONTEXT_CORE_PROFILE_BIT_ARB = 0x0001;
WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB = 0x0002;
)
proc wglCreateContext (hdc HDC) -> HGLRC #foreign #dll_import proc wglCreateContext (hdc HDC) -> HGLRC #foreign #dll_import
proc wglMakeCurrent (hdc HDC, hglrc HGLRC) -> BOOL #foreign #dll_import proc wglMakeCurrent (hdc HDC, hglrc HGLRC) -> BOOL #foreign #dll_import
@@ -365,60 +381,57 @@ proc wglDeleteContext (hglrc HGLRC) -> BOOL #foreign #dll_import
proc GetKeyState (v_key i32) -> i16 #foreign #dll_import proc GetKeyState (v_key i32) -> i16 #foreign #dll_import
proc GetAsyncKeyState(v_key i32) -> i16 #foreign #dll_import proc GetAsyncKeyState(v_key i32) -> i16 #foreign #dll_import
proc is_key_down(key Key_Code) -> bool { proc is_key_down(key i32) -> bool #inline { return GetAsyncKeyState(key) < 0; }
return GetAsyncKeyState(key as i32) < 0;
}
type Key_Code i32;
const ( const (
KEY_LBUTTON Key_Code = 0x01; KEY_LBUTTON = 0x01;
KEY_RBUTTON = 0x02; KEY_RBUTTON = 0x02;
KEY_CANCEL = 0x03; KEY_CANCEL = 0x03;
KEY_MBUTTON = 0x04; KEY_MBUTTON = 0x04;
KEY_BACK = 0x08; KEY_BACK = 0x08;
KEY_TAB = 0x09; KEY_TAB = 0x09;
KEY_CLEAR = 0x0C; KEY_CLEAR = 0x0C;
KEY_RETURN = 0x0D; KEY_RETURN = 0x0D;
KEY_SHIFT = 0x10; KEY_SHIFT = 0x10;
KEY_CONTROL = 0x11; KEY_CONTROL = 0x11;
KEY_MENU = 0x12; KEY_MENU = 0x12;
KEY_PAUSE = 0x13; KEY_PAUSE = 0x13;
KEY_CAPITAL = 0x14; KEY_CAPITAL = 0x14;
KEY_KANA = 0x15; KEY_KANA = 0x15;
KEY_HANGEUL = 0x15; KEY_HANGEUL = 0x15;
KEY_HANGUL = 0x15; KEY_HANGUL = 0x15;
KEY_JUNJA = 0x17; KEY_JUNJA = 0x17;
KEY_FINAL = 0x18; KEY_FINAL = 0x18;
KEY_HANJA = 0x19; KEY_HANJA = 0x19;
KEY_KANJI = 0x19; KEY_KANJI = 0x19;
KEY_ESCAPE = 0x1B; KEY_ESCAPE = 0x1B;
KEY_CONVERT = 0x1C; KEY_CONVERT = 0x1C;
KEY_NONCONVERT = 0x1D; KEY_NONCONVERT = 0x1D;
KEY_ACCEPT = 0x1E; KEY_ACCEPT = 0x1E;
KEY_MODECHANGE = 0x1F; KEY_MODECHANGE = 0x1F;
KEY_SPACE = 0x20; KEY_SPACE = 0x20;
KEY_PRIOR = 0x21; KEY_PRIOR = 0x21;
KEY_NEXT = 0x22; KEY_NEXT = 0x22;
KEY_END = 0x23; KEY_END = 0x23;
KEY_HOME = 0x24; KEY_HOME = 0x24;
KEY_LEFT = 0x25; KEY_LEFT = 0x25;
KEY_UP = 0x26; KEY_UP = 0x26;
KEY_RIGHT = 0x27; KEY_RIGHT = 0x27;
KEY_DOWN = 0x28; KEY_DOWN = 0x28;
KEY_SELECT = 0x29; KEY_SELECT = 0x29;
KEY_PRINT = 0x2A; KEY_PRINT = 0x2A;
KEY_EXECUTE = 0x2B; KEY_EXECUTE = 0x2B;
KEY_SNAPSHOT = 0x2C; KEY_SNAPSHOT = 0x2C;
KEY_INSERT = 0x2D; KEY_INSERT = 0x2D;
KEY_DELETE = 0x2E; KEY_DELETE = 0x2E;
KEY_HELP = 0x2F; KEY_HELP = 0x2F;
KEY_NUM0 = '0'; KEY_NUM0 = '0';
KEY_NUM1 = '1'; KEY_NUM1 = '1';
@@ -522,5 +535,5 @@ const (
KEY_NONAME = 0xFC; KEY_NONAME = 0xFC;
KEY_PA1 = 0xFD; KEY_PA1 = 0xFD;
KEY_OEM_CLEAR = 0xFE; KEY_OEM_CLEAR = 0xFE;
); )
+27 -25
View File
@@ -14,33 +14,35 @@ type Accept_Range struct {
lo, hi u8; lo, hi u8;
}; };
var accept_ranges = [5]Accept_Range{ var (
{0x80, 0xbf}, accept_ranges = [5]Accept_Range{
{0xa0, 0xbf}, {0x80, 0xbf},
{0x80, 0x9f}, {0xa0, 0xbf},
{0x90, 0xbf}, {0x80, 0x9f},
{0x80, 0x8f}, {0x90, 0xbf},
}; {0x80, 0x8f},
};
var accept_sizes = [256]byte{ accept_sizes = [256]byte{
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x00-0x0f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x10-0x1f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x20-0x2f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x30-0x3f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x30-0x3f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x40-0x4f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x40-0x4f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x50-0x5f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x50-0x5f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x60-0x6f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x60-0x6f
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x70-0x7f 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // 0x70-0x7f
0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x80-0x8f 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x80-0x8f
0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x90-0x9f 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0x90-0x9f
0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xa0-0xaf 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xa0-0xaf
0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xb0-0xbf 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xb0-0xbf
0xf1, 0xf1, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xc0-0xcf 0xf1, 0xf1, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xc0-0xcf
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, // 0xd0-0xdf
0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x23, 0x03, 0x03, // 0xe0-0xef
0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff 0x34, 0x04, 0x04, 0x04, 0x44, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, // 0xf0-0xff
}; };
)
proc encode_rune(r rune) -> ([4]byte, int) { proc encode_rune(r rune) -> ([4]byte, int) {
var buf [4]byte; var buf [4]byte;
+41 -40
View File
@@ -1137,13 +1137,23 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
} }
AstNodeValueSpec empty_spec_ = {0}, *empty_spec = &empty_spec_; AstNodeValueSpec empty_spec_ = {0}, *empty_spec = &empty_spec_;
AstNodeValueSpec *last = NULL; AstNodeValueSpec *prev_spec = NULL;
for_array(iota, gd->specs) { for_array(iota, gd->specs) {
AstNode *spec = gd->specs.e[iota]; AstNode *spec = gd->specs.e[iota];
switch (spec->kind) { switch (spec->kind) {
case_ast_node(is, ImportSpec, spec);
if (!parent_scope->is_file) {
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedDecl di = {parent_scope, spec};
array_add(&c->delayed_imports, di);
case_end;
case_ast_node(vs, ValueSpec, spec); case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) { switch (vs->keyword) {
case Token_let:
case Token_var: { case Token_var: {
// NOTE(bill): You need to store the entity information here unline a constant declaration // NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_count = vs->names.count; isize entity_count = vs->names.count;
@@ -1168,7 +1178,7 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind])); error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue; continue;
} }
Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL); Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL, vs->keyword == Token_let);
e->identifier = name; e->identifier = name;
entities[entity_index++] = e; entities[entity_index++] = e;
@@ -1189,9 +1199,9 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
case Token_const: { case Token_const: {
if (vs->type != NULL || vs->values.count > 0) { if (vs->type != NULL || vs->values.count > 0) {
last = vs; prev_spec = vs;
} else if (last == NULL) { } else if (prev_spec == NULL) {
last = empty_spec; prev_spec = empty_spec;
} }
for_array(i, vs->names) { for_array(i, vs->names) {
@@ -1206,36 +1216,42 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
e->identifier = name; e->identifier = name;
AstNode *init = NULL; AstNode *init = NULL;
if (i < last->values.count) { if (i < prev_spec->values.count) {
init = last->values.e[i]; init = prev_spec->values.e[i];
} }
DeclInfo *di = make_declaration_info(c->allocator, e->scope); DeclInfo *di = make_declaration_info(c->allocator, e->scope);
di->type_expr = last->type; di->type_expr = prev_spec->type;
di->init_expr = init; di->init_expr = init;
add_entity_and_decl_info(c, name, e, di); add_entity_and_decl_info(c, name, e, di);
} }
check_arity_match(c, vs, last); check_arity_match(c, vs, prev_spec);
} break; } break;
} }
case_end; case_end;
case_ast_node(ts, TypeSpec, spec);
if (ts->name->kind != AstNode_Ident) {
error_node(ts->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[ts->name->kind]));
continue;
}
ast_node(n, Ident, ts->name);
Entity *e = make_entity_type_name(c->allocator, parent_scope, *n, NULL);
e->identifier = ts->name;
DeclInfo *d = make_declaration_info(c->allocator, e->scope);
d->type_expr = ts->type;
d->init_expr = ts->type;
add_entity_and_decl_info(c, ts->name, e, d);
case_end;
default: default:
error(ast_node_token(spec), "Invalid specification in declaration: `%.*s`", LIT(ast_node_strings[spec->kind])); error(ast_node_token(spec), "Invalid specification in declaration: `%.*s`", LIT(ast_node_strings[spec->kind]));
break; break;
} }
} }
case_end; case_end;
case_ast_node(id, ImportDecl, decl);
if (!parent_scope->is_file) {
// NOTE(bill): _Should_ be caught by the parser
// TODO(bill): Better error handling if it isn't
continue;
}
DelayedDecl di = {parent_scope, decl};
array_add(&c->delayed_imports, di);
case_end;
case_ast_node(fl, ForeignLibrary, decl); case_ast_node(fl, ForeignLibrary, decl);
if (!parent_scope->is_file) { if (!parent_scope->is_file) {
// NOTE(bill): _Should_ be caught by the parser // NOTE(bill): _Should_ be caught by the parser
@@ -1246,20 +1262,6 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
DelayedDecl di = {parent_scope, decl}; DelayedDecl di = {parent_scope, decl};
array_add(&c->delayed_foreign_libraries, di); array_add(&c->delayed_foreign_libraries, di);
case_end; case_end;
case_ast_node(td, TypeDecl, decl);
if (td->name->kind != AstNode_Ident) {
error_node(td->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[td->name->kind]));
continue;
}
ast_node(n, Ident, td->name);
Entity *e = make_entity_type_name(c->allocator, parent_scope, *n, NULL);
e->identifier = td->name;
DeclInfo *d = make_declaration_info(c->allocator, e->scope);
d->type_expr = td->type;
d->init_expr = td->type;
add_entity_and_decl_info(c, td->name, e, d);
case_end;
case_ast_node(pd, ProcDecl, decl); case_ast_node(pd, ProcDecl, decl);
if (pd->name->kind != AstNode_Ident) { if (pd->name->kind != AstNode_Ident) {
error_node(pd->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[pd->name->kind])); error_node(pd->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[pd->name->kind]));
@@ -1286,8 +1288,9 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
void check_import_entities(Checker *c, MapScope *file_scopes) { void check_import_entities(Checker *c, MapScope *file_scopes) {
for_array(i, c->delayed_imports) { for_array(i, c->delayed_imports) {
Scope *parent_scope = c->delayed_imports.e[i].parent; Scope *parent_scope = c->delayed_imports.e[i].parent;
AstNode *decl = c->delayed_imports.e[i].decl; AstNode *spec = c->delayed_imports.e[i].decl;
ast_node(id, ImportDecl, decl); ast_node(id, ImportSpec, spec);
Token token = id->relpath;
HashKey key = hash_string(id->fullpath); HashKey key = hash_string(id->fullpath);
Scope **found = map_scope_get(file_scopes, key); Scope **found = map_scope_get(file_scopes, key);
@@ -1296,13 +1299,13 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
Scope *scope = file_scopes->entries.e[scope_index].value; Scope *scope = file_scopes->entries.e[scope_index].value;
gb_printf_err("%.*s\n", LIT(scope->file->tokenizer.fullpath)); gb_printf_err("%.*s\n", LIT(scope->file->tokenizer.fullpath));
} }
gb_printf_err("%.*s(%td:%td)\n", LIT(id->token.pos.file), id->token.pos.line, id->token.pos.column); gb_printf_err("%.*s(%td:%td)\n", LIT(token.pos.file), token.pos.line, token.pos.column);
GB_PANIC("Unable to find scope for file: %.*s", LIT(id->fullpath)); GB_PANIC("Unable to find scope for file: %.*s", LIT(id->fullpath));
} }
Scope *scope = *found; Scope *scope = *found;
if (scope->is_global) { if (scope->is_global) {
error(id->token, "Importing a #shared_global_scope is disallowed and unnecessary"); error(token, "Importing a #shared_global_scope is disallowed and unnecessary");
continue; continue;
} }
@@ -1332,7 +1335,7 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
if (!previously_added) { if (!previously_added) {
array_add(&parent_scope->imported, scope); array_add(&parent_scope->imported, scope);
} else { } else {
warning(id->token, "Multiple #import of the same file within this scope"); warning(token, "Multiple #import of the same file within this scope");
} }
if (str_eq(id->import_name.string, str_lit("."))) { if (str_eq(id->import_name.string, str_lit("."))) {
@@ -1381,9 +1384,7 @@ void check_import_entities(Checker *c, MapScope *file_scopes) {
if (is_string_an_identifier(filename)) { if (is_string_an_identifier(filename)) {
import_name = filename; import_name = filename;
} else { } else {
error(id->token, error(token, "File name, %.*s, cannot be as an import name as it is not a valid identifier", LIT(filename));
"File name, %.*s, cannot be as an import name as it is not a valid identifier",
LIT(filename));
} }
} }
+3 -3
View File
@@ -99,7 +99,7 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
} }
void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) { void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
GB_ASSERT(vs->keyword == Token_var); GB_ASSERT(vs->keyword == Token_var || vs->keyword == Token_let);
isize entity_count = vs->names.count; isize entity_count = vs->names.count;
isize entity_index = 0; isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count); Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
@@ -116,7 +116,7 @@ void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
found = current_scope_lookup_entity(c->context.scope, str); found = current_scope_lookup_entity(c->context.scope, str);
} }
if (found == NULL) { if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL); entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
add_entity_definition(&c->info, name, entity); add_entity_definition(&c->info, name, entity);
} else { } else {
TokenPos pos = found->token.pos; TokenPos pos = found->token.pos;
@@ -156,7 +156,7 @@ void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
e->type = init_type; e->type = init_type;
} }
check_arity_match(c, vs, NULL);
check_init_variables(c, entities, entity_count, vs->values, str_lit("variable declaration")); check_init_variables(c, entities, entity_count, vs->values, str_lit("variable declaration"));
for_array(i, vs->names) { for_array(i, vs->names) {
+9 -8
View File
@@ -55,9 +55,9 @@ struct Entity {
ExactValue value; ExactValue value;
} Constant; } Constant;
struct { struct {
b32 is_let; i32 field_index;
i32 field_index; i32 field_src_index;
i32 field_src_index; bool is_let;
} Variable; } Variable;
i32 TypeName; i32 TypeName;
struct { struct {
@@ -97,8 +97,9 @@ Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token,
return entity; return entity;
} }
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type) { Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type, bool is_let) {
Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type); Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type);
entity->Variable.is_let = is_let;
return entity; return entity;
} }
@@ -123,7 +124,7 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
} }
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous) { Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous) {
Entity *entity = make_entity_variable(a, scope, token, type); Entity *entity = make_entity_variable(a, scope, token, type, false);
entity->flags |= EntityFlag_Used; entity->flags |= EntityFlag_Used;
entity->flags |= EntityFlag_Anonymous*(anonymous != 0); entity->flags |= EntityFlag_Anonymous*(anonymous != 0);
entity->flags |= EntityFlag_Param; entity->flags |= EntityFlag_Param;
@@ -131,7 +132,7 @@ Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type,
} }
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, i32 field_src_index) { Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type); Entity *entity = make_entity_variable(a, scope, token, type, false);
entity->Variable.field_src_index = field_src_index; entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index; entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field; entity->flags |= EntityFlag_Field;
@@ -140,7 +141,7 @@ Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type,
} }
Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) { Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type); Entity *entity = make_entity_variable(a, scope, token, type, false);
entity->Variable.field_src_index = field_src_index; entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index; entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field; entity->flags |= EntityFlag_Field;
@@ -185,6 +186,6 @@ Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type, Impli
Entity *make_entity_dummy_variable(gbAllocator a, Scope *file_scope, Token token) { Entity *make_entity_dummy_variable(gbAllocator a, Scope *file_scope, Token token) {
token.string = str_lit("_"); token.string = str_lit("_");
return make_entity_variable(a, file_scope, token, NULL); return make_entity_variable(a, file_scope, token, NULL, false);
} }
+41 -41
View File
@@ -128,6 +128,44 @@ void check_local_collect_entities(Checker *c, AstNodeArray nodes, DelayedEntitie
} break; } break;
} }
case_end; case_end;
case_ast_node(ts, TypeSpec, spec);
if (ts->name->kind != AstNode_Ident) {
error_node(ts->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[ts->name->kind]));
break;
}
Token name_token = ts->name->Ident;
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, NULL);
e->identifier = ts->name;
DeclInfo *d = make_declaration_info(c->allocator, e->scope);
d->type_expr = ts->type;
add_entity_and_decl_info(c, ts->name, e, d);
DelayedEntity delay = {ts->name, e, d};
array_add(delayed_entities, delay);
if (dof != NULL) {
if (str_eq(name_token.string, str_lit("_"))) {
dof->other_fields[dof->other_field_index++] = e;
} else {
HashKey key = hash_string(name_token.string);
if (map_entity_get(dof->entity_map, key) != NULL) {
// TODO(bill): Scope checking already checks the declaration
error(name_token, "`%.*s` is already declared in this record", LIT(name_token.string));
} else {
map_entity_set(dof->entity_map, key, e);
dof->other_fields[dof->other_field_index++] = e;
}
add_entity(c, c->context.scope, ts->name, e);
add_entity_use(c, ts->name, e);
}
}
case_end;
} }
} }
case_end; case_end;
@@ -147,47 +185,6 @@ void check_local_collect_entities(Checker *c, AstNodeArray nodes, DelayedEntitie
check_entity_decl(c, e, d, NULL, NULL); check_entity_decl(c, e, d, NULL, NULL);
case_end; case_end;
#endif #endif
case_ast_node(td, TypeDecl, node);
if (!ast_node_expect(td->name, AstNode_Ident)) {
break;
}
if (td->name->kind != AstNode_Ident) {
error_node(td->name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[td->name->kind]));
continue;
}
Token name_token = td->name->Ident;
Entity *e = make_entity_type_name(c->allocator, c->context.scope, name_token, NULL);
e->identifier = td->name;
DeclInfo *d = make_declaration_info(c->allocator, e->scope);
d->type_expr = td->type;
add_entity_and_decl_info(c, td->name, e, d);
DelayedEntity delay = {td->name, e, d};
array_add(delayed_entities, delay);
if (dof != NULL) {
if (str_eq(name_token.string, str_lit("_"))) {
dof->other_fields[dof->other_field_index++] = e;
} else {
HashKey key = hash_string(name_token.string);
if (map_entity_get(dof->entity_map, key) != NULL) {
// TODO(bill): Scope checking already checks the declaration
error(name_token, "`%.*s` is already declared in this record", LIT(name_token.string));
} else {
map_entity_set(dof->entity_map, key, e);
dof->other_fields[dof->other_field_index++] = e;
}
add_entity(c, c->context.scope, td->name, e);
add_entity_use(c, td->name, e);
}
}
case_end;
} }
} }
@@ -900,6 +897,9 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
} }
#else #else
o->mode = Addressing_Variable; o->mode = Addressing_Variable;
if (e->Variable.is_let) {
o->mode = Addressing_Value;
}
#endif #endif
break; break;
+22 -8
View File
@@ -21,15 +21,27 @@ void check_stmt_list(Checker *c, AstNodeArray stmts, u32 flags) {
bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0; bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0;
u32 f = flags & (~Stmt_FallthroughAllowed); u32 f = flags & (~Stmt_FallthroughAllowed);
for_array(i, stmts) { isize max = stmts.count;
for (isize i = stmts.count-1; i >= 0; i--) {
if (stmts.e[i]->kind != AstNode_EmptyStmt) {
break;
}
max--;
}
for (isize i = 0; i < max; i++) {
AstNode *n = stmts.e[i]; AstNode *n = stmts.e[i];
if (n->kind == AstNode_EmptyStmt) { if (n->kind == AstNode_EmptyStmt) {
continue; continue;
} }
u32 new_flags = f; u32 new_flags = f;
if (ft_ok && i+1 == stmts.count) { if (ft_ok && i+1 == max) {
new_flags |= Stmt_FallthroughAllowed; new_flags |= Stmt_FallthroughAllowed;
} }
if (n->kind == AstNode_ReturnStmt && i+1 < max) {
error_node(n, "Statements after this `return` are never executed");
}
check_stmt(c, n, new_flags); check_stmt(c, n, new_flags);
} }
@@ -835,7 +847,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
tt = make_type_pointer(c->allocator, case_type); tt = make_type_pointer(c->allocator, case_type);
add_type_info_type(c, tt); add_type_info_type(c, tt);
} }
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt); Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt, false);
tag_var->flags |= EntityFlag_Used; tag_var->flags |= EntityFlag_Used;
add_entity(c, c->context.scope, ms->var, tag_var); add_entity(c, c->context.scope, ms->var, tag_var);
add_entity_use(c, ms->var, tag_var); add_entity_use(c, ms->var, tag_var);
@@ -1078,6 +1090,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
switch (spec->kind) { switch (spec->kind) {
case_ast_node(vs, ValueSpec, spec); case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) { switch (vs->keyword) {
case Token_let:
case Token_var: { case Token_var: {
isize entity_count = vs->names.count; isize entity_count = vs->names.count;
isize entity_index = 0; isize entity_index = 0;
@@ -1095,7 +1108,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
found = current_scope_lookup_entity(c->context.scope, str); found = current_scope_lookup_entity(c->context.scope, str);
} }
if (found == NULL) { if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL); entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
add_entity_definition(&c->info, name, entity); add_entity_definition(&c->info, name, entity);
} else { } else {
TokenPos pos = found->token.pos; TokenPos pos = found->token.pos;
@@ -1146,10 +1159,15 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
} break; } break;
case Token_const: case Token_const:
// NOTE(bill): Handled elsewhere
break; break;
} }
case_end; case_end;
case_ast_node(ts, TypeSpec, spec);
// NOTE(bill): Handled elsewhere
case_end;
default: default:
error(ast_node_token(spec), "Invalid specification in declaration: `%.*s`", LIT(ast_node_strings[spec->kind])); error(ast_node_token(spec), "Invalid specification in declaration: `%.*s`", LIT(ast_node_strings[spec->kind]));
break; break;
@@ -1157,10 +1175,6 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
} }
case_end; case_end;
case_ast_node(td, TypeDecl, node);
// NOTE(bill): Handled elsewhere
case_end;
case_ast_node(pd, ProcDecl, node); case_ast_node(pd, ProcDecl, node);
// NOTE(bill): Handled elsewhere // NOTE(bill): Handled elsewhere
#if 1 #if 1
+170 -181
View File
@@ -231,22 +231,29 @@ AST_NODE_KIND(_SpecBegin, "", i32) \
AstNode * type; \ AstNode * type; \
AstNodeArray values; \ AstNodeArray values; \
}) \ }) \
AST_NODE_KIND(TypeSpec, "type specification", struct { \
AstNode *name; \
AstNode *type; \
AstNode *note; \
}) \
AST_NODE_KIND(ImportSpec, "import specification", struct { \
Token relpath; \
String fullpath; \
Token import_name; \
bool is_load; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(_SpecEnd, "", i32) \ AST_NODE_KIND(_SpecEnd, "", i32) \
AST_NODE_KIND(_DeclBegin, "", i32) \ AST_NODE_KIND(_DeclBegin, "", i32) \
AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \ AST_NODE_KIND(BadDecl, "bad declaration", struct { Token begin, end; }) \
AST_NODE_KIND(GenericDecl, "generic declaration", struct { \ AST_NODE_KIND(GenericDecl, "declaration", struct { \
Token token; \ Token token; \
Token open, close; \ Token open, close; \
AstNodeArray specs; \ AstNodeArray specs; \
u64 tags; \ u64 tags; \
bool is_using; \ bool is_using; \
}) \ }) \
AST_NODE_KIND(TypeDecl, "type declaration", struct { \
Token token; \
AstNode *name; \
AstNode *type; \
AstNode *note; \
}) \
AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \ AST_NODE_KIND(ProcDecl, "procedure declaration", struct { \
AstNode *name; \ AstNode *name; \
AstNode *type; \ AstNode *type; \
@@ -256,14 +263,6 @@ AST_NODE_KIND(_DeclBegin, "", i32) \
String link_name; \ String link_name; \
AstNode *note; \ AstNode *note; \
}) \ }) \
AST_NODE_KIND(ImportDecl, "import declaration", struct { \
Token token, relpath; \
String fullpath; \
Token import_name; \
bool is_load; \
AstNode *cond; \
AstNode *note; \
}) \
AST_NODE_KIND(ForeignLibrary, "foreign library", struct { \ AST_NODE_KIND(ForeignLibrary, "foreign library", struct { \
Token token, filepath; \ Token token, filepath; \
String base_dir; \ String base_dir; \
@@ -467,15 +466,16 @@ Token ast_node_token(AstNode *node) {
return node->GenericDecl.token; return node->GenericDecl.token;
case AstNode_ProcDecl: case AstNode_ProcDecl:
return ast_node_token(node->ProcDecl.name); return ast_node_token(node->ProcDecl.name);
case AstNode_TypeDecl:
return ast_node_token(node->TypeDecl.name);
case AstNode_ImportDecl:
return node->ImportDecl.token;
case AstNode_ForeignLibrary: case AstNode_ForeignLibrary:
return node->ForeignLibrary.token; return node->ForeignLibrary.token;
case AstNode_ValueSpec: case AstNode_ValueSpec:
return ast_node_token(node->ValueSpec.names.e[0]); return ast_node_token(node->ValueSpec.names.e[0]);
case AstNode_TypeSpec:
return ast_node_token(node->TypeSpec.name);
case AstNode_ImportSpec:
return node->ImportSpec.relpath;
case AstNode_Parameter: { case AstNode_Parameter: {
if (node->Parameter.names.count > 0) { if (node->Parameter.names.count > 0) {
@@ -992,26 +992,6 @@ AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNodeArra
return result; return result;
} }
AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_TypeDecl);
result->TypeDecl.token = token;
result->TypeDecl.name = name;
result->TypeDecl.type = type;
return result;
}
AstNode *make_import_decl(AstFile *f, Token token, Token relpath, Token import_name,
AstNode *cond,
bool is_load) {
AstNode *result = make_node(f, AstNode_ImportDecl);
result->ImportDecl.token = token;
result->ImportDecl.relpath = relpath;
result->ImportDecl.import_name = import_name;
result->ImportDecl.cond = cond;
result->ImportDecl.is_load = is_load;
return result;
}
AstNode *make_foreign_library(AstFile *f, Token token, Token filepath, AstNode *cond, bool is_system) { AstNode *make_foreign_library(AstFile *f, Token token, Token filepath, AstNode *cond, bool is_system) {
AstNode *result = make_node(f, AstNode_ForeignLibrary); AstNode *result = make_node(f, AstNode_ForeignLibrary);
result->ForeignLibrary.token = token; result->ForeignLibrary.token = token;
@@ -1042,6 +1022,22 @@ AstNode *make_value_spec(AstFile *f, TokenKind keyword, AstNodeArray names, AstN
return result; return result;
} }
AstNode *make_type_spec(AstFile *f, AstNode *name, AstNode *type) {
AstNode *result = make_node(f, AstNode_TypeSpec);
result->TypeSpec.name = name;
result->TypeSpec.type = type;
return result;
}
AstNode *make_import_spec(AstFile *f, Token relpath, Token import_name, AstNode *cond, bool is_load) {
AstNode *result = make_node(f, AstNode_ImportSpec);
result->ImportSpec.relpath = relpath;
result->ImportSpec.import_name = import_name;
result->ImportSpec.cond = cond;
result->ImportSpec.is_load = is_load;
return result;
}
bool next_token(AstFile *f) { bool next_token(AstFile *f) {
@@ -1137,6 +1133,7 @@ void fix_advance_to_next_stmt(AstFile *f) {
return; return;
case Token_var: case Token_var:
case Token_let:
case Token_const: case Token_const:
case Token_type: case Token_type:
case Token_proc: case Token_proc:
@@ -1198,20 +1195,29 @@ void expect_semicolon(AstFile *f, AstNode *s) {
return; return;
} }
if (s != NULL) { if (s != NULL && prev_token.pos.line != f->curr_token.pos.line) {
switch (s->kind) { switch (s->kind) {
case AstNode_ProcDecl: case AstNode_ProcDecl:
return; return;
case AstNode_TypeDecl: case AstNode_GenericDecl:
if (s->GenericDecl.close.kind == Token_CloseParen) {
return;
} else if (s->GenericDecl.token.kind == Token_type) {
if (f->prev_token.kind == Token_CloseBrace) {
return;
}
}
break;
case AstNode_TypeSpec:
if (f->prev_token.kind == Token_CloseBrace) { if (f->prev_token.kind == Token_CloseBrace) {
return; return;
} }
break; break;
} }
syntax_error(prev_token, "Expected `;` after %.*s, got %.*s %d %d", syntax_error(prev_token, "Expected `;` after %.*s, got %.*s",
LIT(ast_node_strings[s->kind]), LIT(token_strings[prev_token.kind]), LIT(ast_node_strings[s->kind]), LIT(token_strings[prev_token.kind]));
Token_Semicolon, prev_token.kind);
} else { } else {
syntax_error(prev_token, "Expected `;`"); syntax_error(prev_token, "Expected `;`");
} }
@@ -1853,17 +1859,17 @@ AstNodeArray parse_identfier_list(AstFile *f) {
} }
void parse_check_name_list_for_reserves(AstFile *f, AstNodeArray names) { void parse_check_name_list_for_reserves(AstFile *f, AstNodeArray names) {
for_array(i, names) { // for_array(i, names) {
AstNode *name = names.e[i]; // AstNode *name = names.e[i];
if (name->kind == AstNode_Ident) { // if (name->kind == AstNode_Ident) {
String n = name->Ident.string; // String n = name->Ident.string;
// NOTE(bill): Check for reserved identifiers // // NOTE(bill): Check for reserved identifiers
if (str_eq(n, str_lit("context"))) { // if (str_eq(n, str_lit("context"))) {
syntax_error_node(name, "`context` is a reserved identifier"); // syntax_error_node(name, "`context` is a reserved identifier");
break; // break;
} // }
} // }
} // }
} }
AstNode *parse_type_attempt(AstFile *f) { AstNode *parse_type_attempt(AstFile *f) {
@@ -1933,6 +1939,7 @@ PARSE_SPEC_PROC(parse_value_spec) {
switch (keyword) { switch (keyword) {
case Token_var: case Token_var:
case Token_let:
if (type == NULL && values.count == 0 && names.count > 0) { if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing type or initialization"); syntax_error(f->curr_token, "Missing type or initialization");
return make_bad_decl(f, f->curr_token, f->curr_token); return make_bad_decl(f, f->curr_token, f->curr_token);
@@ -1953,13 +1960,66 @@ PARSE_SPEC_PROC(parse_value_spec) {
return make_value_spec(f, keyword, names, type, values); return make_value_spec(f, keyword, names, type, values);
} }
PARSE_SPEC_PROC(parse_type_spec) {
AstNode *parse_type_decl(AstFile *f) {
Token token = expect_token(f, Token_type);
AstNode *name = parse_identifier(f); AstNode *name = parse_identifier(f);
AstNode *type = parse_type(f); AstNode *type = parse_type(f);
return make_type_decl(f, token, name, type); return make_type_spec(f, name, type);
}
PARSE_SPEC_PROC(parse_import_spec) {
AstNode *cond = NULL;
Token import_name = {0};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Ident;
next_token(f);
break;
case Token_Ident:
import_name = f->curr_token;
next_token(f);
break;
default:
import_name.pos = f->curr_token.pos;
break;
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(import_name, "Illegal import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `import` within a procedure. This must be done at the file scope");
spec = make_bad_decl(f, import_name, file_path);
} else {
spec = make_import_spec(f, file_path, import_name, cond, false);
}
return spec;
}
PARSE_SPEC_PROC(parse_include_spec) {
AstNode *cond = NULL;
Token file_path = expect_token_after(f, Token_String, "include");
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
AstNode *spec = NULL;
if (f->curr_proc != NULL) {
syntax_error(import_name, "You cannot use `include` within a procedure. This must be done at the file scope");
spec = make_bad_decl(f, import_name, file_path);
} else {
spec = make_import_spec(f, file_path, import_name, cond, true);
}
return spec;
} }
AstNode *parse_proc_decl(AstFile *f); AstNode *parse_proc_decl(AstFile *f);
@@ -1967,15 +2027,21 @@ AstNode *parse_proc_decl(AstFile *f);
AstNode *parse_decl(AstFile *f) { AstNode *parse_decl(AstFile *f) {
switch (f->curr_token.kind) { switch (f->curr_token.kind) {
case Token_var: case Token_var:
case Token_let:
case Token_const: case Token_const:
return parse_generic_decl(f, f->curr_token.kind, parse_value_spec); return parse_generic_decl(f, f->curr_token.kind, parse_value_spec);
case Token_type: case Token_type:
return parse_type_decl(f); return parse_generic_decl(f, f->curr_token.kind, parse_type_spec);
case Token_proc: case Token_proc:
return parse_proc_decl(f); return parse_proc_decl(f);
case Token_import:
return parse_generic_decl(f, f->curr_token.kind, parse_import_spec);
case Token_include:
return parse_generic_decl(f, f->curr_token.kind, parse_include_spec);
default: { default: {
Token token = f->curr_token; Token token = f->curr_token;
syntax_error(token, "Expected a declaration"); syntax_error(token, "Expected a declaration");
@@ -1989,6 +2055,7 @@ AstNode *parse_decl(AstFile *f) {
AstNode *parse_simple_stmt(AstFile *f) { AstNode *parse_simple_stmt(AstFile *f) {
switch (f->curr_token.kind) { switch (f->curr_token.kind) {
case Token_var: case Token_var:
case Token_let:
case Token_const: case Token_const:
return parse_decl(f); return parse_decl(f);
} }
@@ -2276,41 +2343,6 @@ AstNode *parse_identifier_or_type(AstFile *f) {
return make_raw_union_type(f, token, decls, decl_count); return make_raw_union_type(f, token, decls, decl_count);
} }
case Token_enum: {
Token token = expect_token(f, Token_enum);
AstNode *base_type = NULL;
Token open, close;
if (f->curr_token.kind != Token_OpenBrace) {
base_type = parse_type(f);
}
AstNodeArray fields = make_ast_node_array(f);
open = expect_token_after(f, Token_OpenBrace, "`enum`");
while (f->curr_token.kind != Token_CloseBrace &&
f->curr_token.kind != Token_EOF) {
AstNode *name = parse_identifier(f);
AstNode *value = NULL;
Token eq = empty_token;
if (f->curr_token.kind == Token_Eq) {
eq = expect_token(f, Token_Eq);
value = parse_value(f);
}
AstNode *field = make_field_value(f, name, value, eq);
array_add(&fields, field);
if (f->curr_token.kind != Token_Comma) {
break;
}
next_token(f);
}
close = expect_token(f, Token_CloseBrace);
return make_enum_type(f, token, base_type, fields);
}
case Token_proc: case Token_proc:
return parse_proc_type(f); return parse_proc_type(f);
@@ -2722,9 +2754,6 @@ AstNode *parse_stmt(AstFile *f) {
Token token = f->curr_token; Token token = f->curr_token;
switch (token.kind) { switch (token.kind) {
// Operands // Operands
case Token_var:
case Token_const:
case Token_Ident: case Token_Ident:
case Token_Integer: case Token_Integer:
case Token_Float: case Token_Float:
@@ -2740,10 +2769,14 @@ AstNode *parse_stmt(AstFile *f) {
expect_semicolon(f, s); expect_semicolon(f, s);
return s; return s;
case Token_var:
case Token_let:
case Token_const:
case Token_proc: case Token_proc:
return parse_proc_decl(f);
case Token_type: case Token_type:
s = parse_type_decl(f); case Token_import:
case Token_include:
s = parse_decl(f);
expect_semicolon(f, s); expect_semicolon(f, s);
return s; return s;
@@ -2775,12 +2808,16 @@ AstNode *parse_stmt(AstFile *f) {
while (e->kind == AstNode_SelectorExpr) { while (e->kind == AstNode_SelectorExpr) {
e = unparen_expr(e->SelectorExpr.selector); e = unparen_expr(e->SelectorExpr.selector);
} }
if (e->kind == AstNode_Ident) { if (e->kind == AstNode_Ident) {
valid = true; valid = true;
} }
} break; } break;
case AstNode_GenericDecl: case AstNode_GenericDecl:
valid = node->GenericDecl.token.kind == Token_var; if (node->GenericDecl.token.kind == Token_var) {
valid = true;
} else if (node->GenericDecl.token.kind == Token_let) {
valid = true;
}
break; break;
} }
@@ -2861,60 +2898,6 @@ AstNode *parse_stmt(AstFile *f) {
} }
expect_semicolon(f, s); expect_semicolon(f, s);
return s; return s;
} else if (str_eq(tag, str_lit("import"))) {
AstNode *cond = NULL;
Token import_name = {0};
switch (f->curr_token.kind) {
case Token_Period:
import_name = f->curr_token;
import_name.kind = Token_Ident;
next_token(f);
break;
case Token_Ident:
import_name = f->curr_token;
next_token(f);
break;
default:
import_name.pos = hash_token.pos;
break;
}
if (str_eq(import_name.string, str_lit("_"))) {
syntax_error(token, "Illegal import name: `_`");
}
Token file_path = expect_token_after(f, Token_String, "#import");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
if (f->curr_proc != NULL) {
syntax_error(token, "You cannot use #import within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
} else {
s = make_import_decl(f, hash_token, file_path, import_name, cond, false);
}
expect_semicolon(f, s);
return s;
} else if (str_eq(tag, str_lit("include"))) {
AstNode *cond = NULL;
Token file_path = expect_token(f, Token_String);
Token import_name = file_path;
import_name.string = str_lit(".");
if (allow_token(f, Token_when)) {
cond = parse_expr(f, false);
}
if (f->curr_proc == NULL) {
s = make_import_decl(f, hash_token, file_path, import_name, cond, true);
} else {
syntax_error(token, "You cannot use #include within a procedure. This must be done at the file scope");
s = make_bad_decl(f, token, file_path);
}
expect_semicolon(f, s);
return s;
} else if (str_eq(tag, str_lit("thread_local"))) { } else if (str_eq(tag, str_lit("thread_local"))) {
AstNode *decl = parse_simple_stmt(f); AstNode *decl = parse_simple_stmt(f);
if (decl->kind == AstNode_GenericDecl && if (decl->kind == AstNode_GenericDecl &&
@@ -3143,35 +3126,41 @@ void parse_setup_file_decls(Parser *p, AstFile *f, String base_dir, AstNodeArray
node->kind != AstNode_EmptyStmt) { node->kind != AstNode_EmptyStmt) {
// NOTE(bill): Sanity check // NOTE(bill): Sanity check
syntax_error_node(node, "Only declarations are allowed at file scope %.*s", LIT(ast_node_strings[node->kind])); syntax_error_node(node, "Only declarations are allowed at file scope %.*s", LIT(ast_node_strings[node->kind]));
} else if (node->kind == AstNode_ImportDecl) { } else if (node->kind == AstNode_GenericDecl) {
AstNodeImportDecl *id = &node->ImportDecl; AstNodeGenericDecl *gd = &node->GenericDecl;
String file_str = id->relpath.string; for_array(spec_index, gd->specs) {
AstNode *spec = gd->specs.e[spec_index];
switch (spec->kind) {
case_ast_node(is, ImportSpec, spec);
String file_str = is->relpath.string;
if (!is_import_path_valid(file_str)) { if (!is_import_path_valid(file_str)) {
if (id->is_load) { if (is->is_load) {
syntax_error_node(node, "Invalid #include path: `%.*s`", LIT(file_str)); syntax_error_node(node, "Invalid #include path: `%.*s`", LIT(file_str));
} else { } else {
syntax_error_node(node, "Invalid #import path: `%.*s`", LIT(file_str)); syntax_error_node(node, "Invalid #import path: `%.*s`", LIT(file_str));
} }
// NOTE(bill): It's a naughty name // NOTE(bill): It's a naughty name
decls.e[i] = make_bad_decl(f, id->token, id->token); gd->specs.e[spec_index] = make_bad_decl(f, is->relpath, is->relpath);
continue; continue;
} }
gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator gbAllocator allocator = heap_allocator(); // TODO(bill): Change this allocator
String rel_path = get_fullpath_relative(allocator, base_dir, file_str); String rel_path = get_fullpath_relative(allocator, base_dir, file_str);
String import_file = rel_path; String import_file = rel_path;
if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated if (!gb_file_exists(cast(char *)rel_path.text)) { // NOTE(bill): This should be null terminated
String abs_path = get_fullpath_core(allocator, file_str); String abs_path = get_fullpath_core(allocator, file_str);
if (gb_file_exists(cast(char *)abs_path.text)) { if (gb_file_exists(cast(char *)abs_path.text)) {
import_file = abs_path; import_file = abs_path;
}
}
is->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
case_end;
} }
} }
id->fullpath = import_file;
try_add_import_path(p, import_file, file_str, ast_node_token(node).pos);
} else if (node->kind == AstNode_ForeignLibrary) { } else if (node->kind == AstNode_ForeignLibrary) {
AstNodeForeignLibrary *fl = &node->ForeignLibrary; AstNodeForeignLibrary *fl = &node->ForeignLibrary;
String file_str = fl->filepath.string; String file_str = fl->filepath.string;
+25 -23
View File
@@ -1233,7 +1233,8 @@ ssaValue *ssa_add_local_generated(ssaProcedure *proc, Type *type) {
Entity *e = make_entity_variable(proc->module->allocator, Entity *e = make_entity_variable(proc->module->allocator,
scope, scope,
empty_token, empty_token,
type); type,
false);
return ssa_add_local(proc, e); return ssa_add_local(proc, e);
} }
@@ -2603,6 +2604,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
// return v; // return v;
// } // }
return ssa_emit_load(proc, v); return ssa_emit_load(proc, v);
} else if (e != NULL && e->kind == Entity_Variable) {
return ssa_addr_load(proc, ssa_build_addr(proc, expr));
} }
return NULL; return NULL;
case_end; case_end;
@@ -3854,6 +3857,7 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
switch (vs->keyword) { switch (vs->keyword) {
case Token_const: case Token_const:
break; break;
case Token_let:
case Token_var: { case Token_var: {
ssaModule *m = proc->module; ssaModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena); gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
@@ -3910,6 +3914,24 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
} break; } break;
} }
case_end; case_end;
case_ast_node(ts, TypeSpec, spec);
// NOTE(bill): Generate a new name
// parent_proc.name-guid
String ts_name = ts->name->Ident.string;
isize name_len = proc->name.len + 1 + ts_name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len);
i32 guid = cast(i32)proc->module->members.entries.count;
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(ts_name), guid);
String name = make_string(name_text, name_len-1);
Entity **found = map_entity_get(&proc->module->info->definitions, hash_pointer(ts->name));
GB_ASSERT(found != NULL);
Entity *e = *found;
ssaValue *value = ssa_make_value_type_name(proc->module->allocator,
name, e->type);
map_string_set(&proc->module->type_names, hash_pointer(e->type), name);
ssa_gen_global_type_name(proc->module, e, name);
case_end;
} }
} }
case_end; case_end;
@@ -3987,26 +4009,6 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
} }
case_end; case_end;
case_ast_node(td, TypeDecl, node);
// NOTE(bill): Generate a new name
// parent_proc.name-guid
String td_name = td->name->Ident.string;
isize name_len = proc->name.len + 1 + td_name.len + 1 + 10 + 1;
u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len);
i32 guid = cast(i32)proc->module->members.entries.count;
name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s.%.*s-%d", LIT(proc->name), LIT(td_name), guid);
String name = make_string(name_text, name_len-1);
Entity **found = map_entity_get(&proc->module->info->definitions, hash_pointer(td->name));
GB_ASSERT(found != NULL);
Entity *e = *found;
ssaValue *value = ssa_make_value_type_name(proc->module->allocator,
name, e->type);
map_string_set(&proc->module->type_names, hash_pointer(e->type), name);
ssa_gen_global_type_name(proc->module, e, name);
case_end;
case_ast_node(ids, IncDecStmt, node); case_ast_node(ids, IncDecStmt, node);
ssa_emit_comment(proc, str_lit("IncDecStmt")); ssa_emit_comment(proc, str_lit("IncDecStmt"));
TokenKind op = ids->op.kind; TokenKind op = ids->op.kind;
@@ -4705,7 +4707,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
{ {
String name = str_lit(SSA_TYPE_INFO_DATA_NAME); String name = str_lit(SSA_TYPE_INFO_DATA_NAME);
isize count = c->info.type_info_map.entries.count; isize count = c->info.type_info_map.entries.count;
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count)); Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count), false);
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL); ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
g->Global.is_private = true; g->Global.is_private = true;
ssa_module_add_value(m, e, g); ssa_module_add_value(m, e, g);
@@ -4737,7 +4739,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
String name = str_lit(SSA_TYPE_INFO_DATA_MEMBER_NAME); String name = str_lit(SSA_TYPE_INFO_DATA_MEMBER_NAME);
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name),
make_type_array(m->allocator, t_type_info_member, count)); make_type_array(m->allocator, t_type_info_member, count), false);
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL); ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
ssa_module_add_value(m, e, g); ssa_module_add_value(m, e, g);
map_ssa_value_set(&m->members, hash_string(name), g); map_ssa_value_set(&m->members, hash_string(name), g);
+3 -3
View File
@@ -85,9 +85,10 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_type, "type"), \ TOKEN_KIND(Token_type, "type"), \
TOKEN_KIND(Token_proc, "proc"), \ TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_var, "var"), \ TOKEN_KIND(Token_var, "var"), \
TOKEN_KIND(Token_let, "let"), \
TOKEN_KIND(Token_const, "const"), \ TOKEN_KIND(Token_const, "const"), \
/* TOKEN_KIND(Token_import, "import"), */\ TOKEN_KIND(Token_import, "import"), \
/* TOKEN_KIND(Token_include, "include"), */\ TOKEN_KIND(Token_include, "include"), \
TOKEN_KIND(Token_macro, "macro"), \ TOKEN_KIND(Token_macro, "macro"), \
TOKEN_KIND(Token_match, "match"), \ TOKEN_KIND(Token_match, "match"), \
TOKEN_KIND(Token_break, "break"), \ TOKEN_KIND(Token_break, "break"), \
@@ -106,7 +107,6 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_struct, "struct"), \ TOKEN_KIND(Token_struct, "struct"), \
TOKEN_KIND(Token_union, "union"), \ TOKEN_KIND(Token_union, "union"), \
TOKEN_KIND(Token_raw_union, "raw_union"), \ TOKEN_KIND(Token_raw_union, "raw_union"), \
TOKEN_KIND(Token_enum, "enum"), \
TOKEN_KIND(Token_vector, "vector"), \ TOKEN_KIND(Token_vector, "vector"), \
TOKEN_KIND(Token_using, "using"), \ TOKEN_KIND(Token_using, "using"), \
TOKEN_KIND(Token_asm, "asm"), \ TOKEN_KIND(Token_asm, "asm"), \