File Library and TypeDecl syntax change

This commit is contained in:
gingerBill
2016-08-22 11:52:49 +01:00
parent 680274b6f1
commit a98e93f03f
15 changed files with 515 additions and 168 deletions
+6 -3
View File
@@ -1,9 +1,9 @@
#load "runtime.odin"
#load "win32.odin"
#load "file.odin"
print_string :: proc(s: string) {
for i := 0; i < len(s); i++ {
putchar(s[i] as i32);
}
file_write(file_get_standard(FILE_STANDARD_OUTPUT), ^s[0], len(s));
}
byte_reverse :: proc(b: []byte) {
@@ -53,6 +53,9 @@ print_rune :: proc(r: rune) {
print_string(str);
}
print_space :: proc() { print_rune(' '); }
print_nl :: proc() { print_rune('\n'); }
print_int :: proc(i: int) {
print_int_base(i, 10);
}
+24 -53
View File
@@ -4,12 +4,12 @@
#load "game.odin"
main :: proc() {
_ = hellope();
procedures();
variables();
constants();
types();
data_control();
// _ = hellope();
// procedures();
// variables();
// constants();
// types();
// data_control();
run_game();
}
@@ -263,21 +263,21 @@ types :: proc() {
type Vec4: {4}f32;
type Array3Int: [3]int;
Vec4 :: type {4}f32;
Array3Int :: type [3]int;
type Vec3: struct {
Vec3 :: type struct {
x, y, z: f32
}
type BinaryNode: struct {
BinaryNode :: type struct {
left, right: ^BinaryNode, // same format as procedure argument
data: rawptr,
}
type AddProc: proc(a, b: int) -> int
AddProc :: type proc(a, b: int) -> int
type Packed: struct #packed {
Packed :: type struct #packed {
a: u8,
b: u16,
c: u32,
@@ -286,7 +286,7 @@ types :: proc() {
{
type MyInt: int
MyInt :: type int;
x: int = 1;
y: MyInt = 2;
// z := x + y; // Failure - types cannot implicit convert*
@@ -347,7 +347,7 @@ types :: proc() {
f := {4}f32{1}; // broadcasts to all
// g := {4}f32{1, 2}; // require either 1 or 4 elements
type Vec2: {2}f32;
Vec2 :: type {2}f32;
h := Vec2{1, 2};
@@ -428,7 +428,7 @@ void main() {
{ // size, align, offset
type Thing: struct {
Thing :: type struct {
a: u8,
b: u16,
c, d, e: u32,
@@ -527,46 +527,17 @@ data_control :: proc() {
context.allocator = __default_allocator();
defer context.allocator = prev_allocator;
// C strings, yuk!
to_c_string := proc(s: string) -> ^u8 {
c := alloc(len(s)+1) as ^u8;
mem_copy(c, ^s[0], len(s));
c[len(s)] = 0;
return c;
};
fopen :: proc(filename, mode: ^u8) -> rawptr #foreign
fclose :: proc(f: rawptr) -> i32 #foreign
filename := to_c_string("../examples/base.odin");
mode := to_c_string("rb");
defer dealloc(filename);
defer dealloc(mode);
f := fopen(filename, mode);
if f == null {
/*
type File: struct { filename: string }
type FileError: int
open_file :: proc(filename: string) -> (File, FileError) { ... }
close_file :: proc(f: ^File) { ... }
f, err := open_file("Test");
if err != 0 {
// handle error
}
defer if f != null {
_ = fclose(f);
}
// rest of code
// Better version
/*
type File: struct { filename: string }
type FileError: int
open_file :: proc(filename: string) -> (File, FileError) { ... }
close_file :: proc(f: ^File) { ... }
f, err := open_file("Test");
if err != 0 {
// handle error
}
defer close_file(^f);
*/
defer close_file(^f);
*/
}
+107
View File
@@ -0,0 +1,107 @@
#load "win32.odin"
FileHandle :: type HANDLE;
File :: type struct {
handle: FileHandle,
}
file_open :: proc(name: string) -> (File, bool) {
buf: [300]byte;
_ = copy(buf[:], name as []byte);
handle := CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null);
f: File;
f.handle = handle as FileHandle;
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
return f, success;
}
file_create :: proc(name: string) -> (File, bool) {
buf: [300]byte;
_ = copy(buf[:], name as []byte);
handle := CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null);
f: File;
f.handle = handle as FileHandle;
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
return f, success;
}
file_close :: proc(f: ^File) {
CloseHandle(f.handle);
}
file_write :: proc(f: ^File, buf: rawptr, len: int) -> bool {
bytes_written: i32;
return WriteFile(f.handle, buf, len as i32, ^bytes_written, null) != 0;
}
FileStandardType :: type int;
FILE_STANDARD_INPUT : FileStandardType : 0;
FILE_STANDARD_OUTPUT : FileStandardType : 1;
FILE_STANDARD_ERROR : FileStandardType : 2;
FILE_STANDARD__COUNT : FileStandardType : 3;
__std_file_set := false;
__std_files: [FILE_STANDARD__COUNT]File;
file_get_standard :: proc(std: FileStandardType) -> ^File {
if (!__std_file_set) {
__std_files[FILE_STANDARD_INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
__std_files[FILE_STANDARD_OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
__std_files[FILE_STANDARD_ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
__std_file_set = true;
}
return ^__std_files[std as int];
}
read_entire_file :: proc(name: string) -> (string, bool) {
buf: [300]byte;
_ = copy(buf[:], name as []byte);
c_string := ^buf[0];
f, file_ok := file_open(name);
if !file_ok {
return "", false;
}
defer file_close(^f);
length: i64;
file_size_ok := GetFileSizeEx(f.handle as HANDLE, ^length) != 0;
if !file_size_ok {
return "", false;
}
data: ^u8 = alloc(length as int);
if data == null {
return "", false;
}
single_read_length: i32;
total_read: i64;
for total_read < length {
remaining := length - total_read;
to_read: u32;
MAX :: 0x7fffffff;
if remaining <= MAX {
to_read = remaining as u32;
} else {
to_read = MAX;
}
ReadFile(f.handle as HANDLE, ^data[total_read], to_read, ^single_read_length, null);
if single_read_length <= 0 {
dealloc(data);
return "", false;
}
total_read += single_read_length as i64;
}
return data[:length] as string, true;
}
+3 -4
View File
@@ -1,5 +1,4 @@
#load "basic.odin"
#load "win32.odin"
#load "opengl.odin"
#load "math.odin"
@@ -28,13 +27,13 @@ win32_print_last_error :: proc() {
// Yuk!
to_c_string :: proc(s: string) -> ^u8 {
c_str: ^u8 = heap_alloc(len(s)+1);
mem_copy(c_str, ^s[0], len(s));
memory_copy(c_str, ^s[0], len(s));
c_str[len(s)] = 0;
return c_str;
}
type Window: struct {
Window :: type struct {
width, height: int,
wc: WNDCLASSEXA,
dc: HDC,
@@ -123,7 +122,7 @@ display_window :: proc(w: ^Window) {
}
type Entity: struct {
Entity :: type struct {
pos: Vec2,
dim: Vec2,
}
+8 -6
View File
@@ -13,14 +13,16 @@ MATH_LOG_TEN :: 2.30258509299404568401799145468436421;
MATH_EPSILON :: 1.19209290e-7;
τ :: MATH_TAU;
π :: MATH_PI;
type Vec2: {2}f32
type Vec3: {3}f32
type Vec4: {4}f32
Vec2 :: type {2}f32;
Vec3 :: type {3}f32;
Vec4 :: type {4}f32;
type Mat2: {4}f32
type Mat3: {9}f32
type Mat4: {16}f32
Mat2 :: type {4}f32;
Mat3 :: type {9}f32;
Mat4 :: type {16}f32;
fsqrt :: proc(x: f32) -> f32 #foreign "llvm.sqrt.f32"
+208 -22
View File
@@ -1,17 +1,203 @@
putchar :: proc(c: i32) -> i32 #foreign
mem_compare :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcmp"
mem_copy :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcpy"
mem_move :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memmove"
debug_trap :: proc() #foreign "llvm.debugtrap"
// TODO(bill): make custom heap procedures
heap_realloc :: proc(ptr: rawptr, sz: int) -> rawptr #foreign "realloc"
heap_alloc :: proc(sz: int) -> rawptr { return heap_realloc(null, sz); }
heap_free :: proc(ptr: rawptr) { _ = heap_realloc(ptr, 0); }
heap_alloc :: proc(len: int) -> rawptr {
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
}
heap_free :: proc(ptr: rawptr) {
_ = HeapFree(GetProcessHeap(), 0, ptr);
}
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
s1, s2: ^u8 = dst, src;
for i := 0; i < len; i++ {
if s1[i] != s2[i] {
return (s1[i] - s2[i]) as int;
}
}
return 0;
}
memory_copy :: proc(dst, src: rawptr, n: int) #inline {
if dst == src {
return;
}
v128b :: type {4}u32;
static_assert(align_of(v128b) == 16);
d, s: ^u8 = dst, src;
for ; s as uint % 16 != 0 && n != 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
if d as uint % 16 == 0 {
for ; n >= 16; d, s, n = ^d[16], ^s[16], n-16 {
(d as ^v128b)[0] = (s as ^v128b)[0];
}
if n&8 != 0 {
(d as ^u64)[0] = (s as ^u64)[0];
d, s = ^d[8], ^s[8];
}
if n&4 != 0 {
(d as ^u32)[0] = (s as ^u32)[0];
d, s = ^d[4], ^s[4];
}
if n&2 != 0 {
(d as ^u16)[0] = (s as ^u16)[0];
d, s = ^d[2], ^s[2];
}
if n&1 != 0 {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
return;
}
// IMPORTANT NOTE(bill): Little endian only
LS :: proc(a, b: u32) -> u32 #inline { return a << b; }
RS :: proc(a, b: u32) -> u32 #inline { return a >> b; }
/* NOTE(bill): Big endian version
LS :: proc(a, b: u32) -> u32 #inline { return a >> b; }
RS :: proc(a, b: u32) -> u32 #inline { return a << b; }
*/
w, x: u32;
if d as uint % 4 == 1 {
w = (s as ^u32)^;
d[0] = s[0];
d[1] = s[1];
d[2] = s[2];
d, s, n = ^d[3], ^s[3], n-3;
for n > 16 {
d32 := d as ^u32;
x = (^s[1] as ^u32)^; d32[0] = LS(w, 24) | RS(x, 8);
w = (^s[5] as ^u32)^; d32[1] = LS(x, 24) | RS(w, 8);
x = (^s[9] as ^u32)^; d32[2] = LS(w, 24) | RS(x, 8);
w = (^s[13] as ^u32)^; d32[3] = LS(x, 24) | RS(w, 8);
d, s, n = ^d[16], ^s[16], n-16;
}
} else if d as uint % 4 == 2 {
w = (s as ^u32)^;
d[0] = s[0];
d[1] = s[1];
d, s, n = ^d[2], ^s[2], n-2;
for n > 17 {
d32 := d as ^u32;
x = (^s[2] as ^u32)^; d32[0] = LS(w, 16) | RS(x, 16);
w = (^s[6] as ^u32)^; d32[1] = LS(x, 16) | RS(w, 16);
x = (^s[10] as ^u32)^; d32[2] = LS(w, 16) | RS(x, 16);
w = (^s[14] as ^u32)^; d32[3] = LS(x, 16) | RS(w, 16);
d, s, n = ^d[16], ^s[16], n-16;
}
} else if d as uint % 4 == 3 {
w = (s as ^u32)^;
d[0] = s[0];
d, s, n = ^d[1], ^s[1], n-1;
for n > 18 {
d32 := d as ^u32;
x = (^s[3] as ^u32)^; d32[0] = LS(w, 8) | RS(x, 24);
w = (^s[7] as ^u32)^; d32[1] = LS(x, 8) | RS(w, 24);
x = (^s[11] as ^u32)^; d32[2] = LS(w, 8) | RS(x, 24);
w = (^s[15] as ^u32)^; d32[3] = LS(x, 8) | RS(w, 24);
d, s, n = ^d[16], ^s[16], n-16;
}
}
if n&16 != 0 {
(d as ^v128b)[0] = (s as ^v128b)[0];
d, s = ^d[16], ^s[16];
}
if n&8 != 0 {
(d as ^u64)[0] = (s as ^u64)[0];
d, s = ^d[8], ^s[8];
}
if n&4 != 0 {
(d as ^u32)[0] = (s as ^u32)[0];
d, s = ^d[4], ^s[4];
}
if n&2 != 0 {
(d as ^u16)[0] = (s as ^u16)[0];
d, s = ^d[2], ^s[2];
}
if n&1 != 0 {
d[0] = s[0];
}
}
memory_move :: proc(dst, src: rawptr, n: int) #inline {
d, s: ^u8 = dst, src;
if d == s {
return;
}
if d >= ^s[n] || ^d[n] <= s {
memory_copy(d, s, n);
return;
}
// TODO(bill): Vectorize the shit out of this
if d < s {
if s as int % size_of(int) == d as int % size_of(int) {
for d as int % size_of(int) != 0 {
if n == 0 {
return;
}
n--;
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
di, si := d as ^int, s as ^int;
for n >= size_of(int) {
di[0] = si[0];
di, si = ^di[1], ^si[1];
n -= size_of(int);
}
}
for ; n > 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
} else {
if s as int % size_of(int) == d as int % size_of(int) {
for ^d[n] as int % size_of(int) != 0 {
if n == 0 {
return;
}
n--;
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
for n >= size_of(int) {
n -= size_of(int);
di, si := ^d[n] as ^int, ^s[n] as ^int;
di[0] = si[0];
}
for ; n > 0; n-- {
d[0] = s[0];
d, s = ^d[1], ^s[1];
}
}
for n > 0 {
n--;
d[n] = s[n];
}
}
}
__string_eq :: proc(a, b : string) -> bool {
if len(a) != len(b) {
return false;
@@ -19,10 +205,10 @@ __string_eq :: proc(a, b : string) -> bool {
if ^a[0] == ^b[0] {
return true;
}
return mem_compare(^a[0], ^b[0], len(a)) == 0;
return memory_compare(^a[0], ^b[0], len(a)) == 0;
}
__string_ne :: proc(a, b : string) -> bool {
__string_ne :: proc(a, b : string) -> bool #inline {
return !__string_eq(a, b);
}
@@ -48,30 +234,30 @@ __string_cmp :: proc(a, b : string) -> int {
return 0;
}
__string_lt :: proc(a, b : string) -> bool { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b : string) -> bool { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b : string) -> bool { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b : string) -> bool { return __string_cmp(a, b) >= 0; }
__string_lt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) < 0; }
__string_gt :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) > 0; }
__string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <= 0; }
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0; }
type AllocationMode: int;
AllocationMode :: type int;
ALLOCATION_ALLOC :: 0;
ALLOCATION_DEALLOC :: 1;
ALLOCATION_DEALLOC_ALL :: 2;
ALLOCATION_RESIZE :: 3;
type AllocatorProc: proc(allocator_data: rawptr, mode: AllocationMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
size, alignment: int,
old_memory: rawptr, old_size: int, flags: u64) -> rawptr;
type Allocator: struct {
Allocator :: type struct {
procedure: AllocatorProc,
data: rawptr,
}
type Context: struct {
Context :: type struct {
thread_id: i32,
user_index: i32,
@@ -157,7 +343,7 @@ __default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocationMode,
if mode == ALLOCATION_ALLOC {
return heap_alloc(size);
} else if mode == ALLOCATION_RESIZE {
return heap_realloc(old_memory, size);
return default_resize_align(old_memory, old_size, size, alignment);
} else if mode == ALLOCATION_DEALLOC {
heap_free(old_memory);
} else if mode == ALLOCATION_DEALLOC_ALL {
+71 -29
View File
@@ -1,7 +1,3 @@
STD_INPUT_HANDLE :: -10;
STD_OUTPUT_HANDLE :: -11;
STD_ERROR_HANDLE :: -12;
CS_VREDRAW :: 1;
CS_HREDRAW :: 2;
CW_USEDEFAULT :: 0x80000000;
@@ -21,27 +17,29 @@ WM_QUIT :: 0x12;
PM_REMOVE :: 1;
COLOR_BACKGROUND: rawptr : 1; // NOTE(bill): cast to HBRUSH when needed
COLOR_BACKGROUND :: 1 as HBRUSH;
type HANDLE: rawptr
type HWND: HANDLE
type HDC: HANDLE
type HINSTANCE: HANDLE
type HICON: HANDLE
type HCURSOR: HANDLE
type HMENU: HANDLE
type HBRUSH: HANDLE
type WPARAM: uint
type LPARAM: int
type LRESULT: int
type ATOM: i16
type BOOL: i32
type POINT: struct { x, y: i32 }
HANDLE :: type rawptr;
HWND :: type HANDLE;
HDC :: type HANDLE;
HINSTANCE :: type HANDLE;
HICON :: type HANDLE;
HCURSOR :: type HANDLE;
HMENU :: type HANDLE;
HBRUSH :: type HANDLE;
WPARAM :: type uint;
LPARAM :: type int;
LRESULT :: type int;
ATOM :: type i16;
BOOL :: type i32;
POINT :: type struct { x, y: i32 };
type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
INVALID_HANDLE_VALUE :: (-1 as int) as HANDLE;
type WNDCLASSEXA: struct {
WNDPROC :: type proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
WNDCLASSEXA :: type struct {
size, style: u32,
wnd_proc: WNDPROC,
cls_extra, wnd_extra: i32,
@@ -53,7 +51,7 @@ type WNDCLASSEXA: struct {
sm: HICON,
}
type MSG: struct {
MSG :: type struct {
hwnd: HWND,
message: u32,
wparam: WPARAM,
@@ -63,9 +61,7 @@ type MSG: struct {
}
GetStdHandle :: proc(h: i32) -> HANDLE #foreign
CloseHandle :: proc(h: HANDLE) -> i32 #foreign
WriteFileA :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> i32 #foreign
GetLastError :: proc() -> i32 #foreign
ExitProcess :: proc(exit_code: u32) #foreign
GetDesktopWindow :: proc() -> HWND #foreign
@@ -113,6 +109,52 @@ GetQueryPerformanceFrequency :: proc() -> i64 {
// File Stuff
CloseHandle :: proc(h: HANDLE) -> i32 #foreign
GetStdHandle :: proc(h: i32) -> HANDLE #foreign
CreateFileA :: proc(filename: ^u8, desired_access, share_mode: u32,
security: rawptr,
creation, flags_and_attribs: u32, template_file: HANDLE) -> HANDLE #foreign
ReadFile :: proc(h: HANDLE, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> BOOL #foreign
WriteFile :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> i32 #foreign
GetFileSizeEx :: proc(file_handle: HANDLE, file_size: ^i64) -> BOOL #foreign
FILE_SHARE_READ :: 0x00000001;
FILE_SHARE_WRITE :: 0x00000002;
FILE_SHARE_DELETE :: 0x00000004;
FILE_GENERIC_ALL :: 0x10000000;
FILE_GENERIC_EXECUTE :: 0x20000000;
FILE_GENERIC_WRITE :: 0x40000000;
FILE_GENERIC_READ :: 0x80000000;
STD_INPUT_HANDLE :: -10;
STD_OUTPUT_HANDLE :: -11;
STD_ERROR_HANDLE :: -12;
CREATE_NEW :: 1;
CREATE_ALWAYS :: 2;
OPEN_EXISTING :: 3;
OPEN_ALWAYS :: 4;
TRUNCATE_EXISTING :: 5;
HeapAlloc :: proc(h: HANDLE, flags: u32, bytes: int) -> rawptr #foreign
HeapFree :: proc(h: HANDLE, flags: u32, memory: rawptr) -> BOOL #foreign
GetProcessHeap :: proc() -> HANDLE #foreign
HEAP_ZERO_MEMORY :: 0x00000008;
// Windows OpenGL
@@ -139,12 +181,12 @@ PFD_DEPTH_DONTCARE :: 0x20000000;
PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000;
PFD_STEREO_DONTCARE :: 0x80000000;
type HGLRC: HANDLE
type PROC: proc()
type wglCreateContextAttribsARBType: proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC
HGLRC :: type HANDLE;
PROC :: type proc();
wglCreateContextAttribsARBType :: type proc(hdc: HDC, hshareContext: rawptr, attribList: ^i32) -> HGLRC;
type PIXELFORMATDESCRIPTOR: struct {
PIXELFORMATDESCRIPTOR :: type struct {
size,
version,
flags: u32,