From a16bdb215a31e66ae8a3d9132483f287fc7f53eb Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Mon, 5 Dec 2016 23:39:26 +0000 Subject: Go/BCPL style semicolon insertion during tokenizing stage --- code/demo.odin | 18 +--- code/http_test.odin | 146 +++++++++++++-------------- code/punity.odin | 286 ++++++++++++++++++++++++++-------------------------- 3 files changed, 220 insertions(+), 230 deletions(-) (limited to 'code') diff --git a/code/demo.odin b/code/demo.odin index 0e5c215ad..fb7f48b33 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -1,18 +1,8 @@ -#import "fmt.odin"; - -A :: type struct { - b: B; -}; -B :: type struct { - c: C; -}; -C :: type struct { - a: A; -}; +#import "fmt.odin" main :: proc() { - fmt.println(size_of(A)); - fmt.println(size_of(B)); - fmt.println(size_of(C)); + if true { + + } } diff --git a/code/http_test.odin b/code/http_test.odin index 09c6d0899..9048cfd63 100644 --- a/code/http_test.odin +++ b/code/http_test.odin @@ -1,10 +1,10 @@ -#import "fmt.odin"; +#import "fmt.odin" -#foreign_system_library "Ws2_32" when ODIN_OS == "windows"; +#foreign_system_library "Ws2_32" when ODIN_OS == "windows" -SOCKET :: type uint; -INVALID_SOCKET :: ~(0 as SOCKET); +SOCKET :: type uint +INVALID_SOCKET :: ~(0 as SOCKET) AF :: enum i32 { UNSPEC = 0, // unspecified @@ -37,45 +37,45 @@ AF :: enum i32 { MAX = 26, } -SOCK_STREAM :: 1; -SOCKET_ERROR :: -1; -IPPROTO_TCP :: 6; -AI_PASSIVE :: 0x0020; -SOMAXCONN :: 128; +SOCK_STREAM :: 1 +SOCKET_ERROR :: -1 +IPPROTO_TCP :: 6 +AI_PASSIVE :: 0x0020 +SOMAXCONN :: 128 -SD_RECEIVE :: 0; -SD_SEND :: 1; -SD_BOTH :: 2; +SD_RECEIVE :: 0 +SD_SEND :: 1 +SD_BOTH :: 2 -WSADESCRIPTION_LEN :: 256; -WSASYS_STATUS_LEN :: 128; +WSADESCRIPTION_LEN :: 256 +WSASYS_STATUS_LEN :: 128 WSADATA :: struct #ordered { - version: i16; - high_version: i16; + version: i16 + high_version: i16 // NOTE(bill): This is x64 ordering - max_sockets: u16; - max_udp_dg: u16; - vendor_info: ^byte; - description: [WSADESCRIPTION_LEN+1]byte; - system_status: [WSASYS_STATUS_LEN+1]byte; + max_sockets: u16 + max_udp_dg: u16 + vendor_info: ^byte + description: [WSADESCRIPTION_LEN+1]byte + system_status: [WSASYS_STATUS_LEN+1]byte } addrinfo :: struct #ordered { - flags: i32; - family: i32; - socktype: i32; - protocol: i32; - addrlen: uint; - canonname: ^u8; - addr: ^sockaddr; - next: ^addrinfo; + flags: i32 + family: i32 + socktype: i32 + protocol: i32 + addrlen: uint + canonname: ^u8 + addr: ^sockaddr + next: ^addrinfo } sockaddr :: struct #ordered { - family: u16; - data: [14]byte; + family: u16 + data: [14]byte } @@ -94,52 +94,52 @@ shutdown :: proc(s: SOCKET, how: i32) -> i32 WSAGetLastError :: proc() -> i32 #foreign #dll_import to_c_string :: proc(s: string) -> ^byte { - c_str := new_slice(byte, s.count+1); - assert(c_str.data != nil); - copy(c_str, s as []byte); - c_str[s.count] = 0; - return c_str.data; + c_str := new_slice(byte, s.count+1) + assert(c_str.data != nil) + copy(c_str, s as []byte) + c_str[s.count] = 0 + return c_str.data } run :: proc() { - wsa: WSADATA; - res: ^addrinfo = nil; - hints: addrinfo; - s, client: SOCKET; + wsa: WSADATA + res: ^addrinfo = nil + hints: addrinfo + s, client: SOCKET if WSAStartup(2 | (2 << 8), ^wsa) != 0 { - fmt.println("WSAStartup failed: ", WSAGetLastError()); - return; + fmt.println("WSAStartup failed: ", WSAGetLastError()) + return } - defer WSACleanup(); + defer WSACleanup() - hints.family = AF.INET as i32; - hints.socktype = SOCK_STREAM; - hints.protocol = IPPROTO_TCP; - hints.flags = AI_PASSIVE; + hints.family = AF.INET as i32 + hints.socktype = SOCK_STREAM + hints.protocol = IPPROTO_TCP + hints.flags = AI_PASSIVE if getaddrinfo(nil, to_c_string("8080"), ^hints, ^res) != 0 { - fmt.println("getaddrinfo failed: ", WSAGetLastError()); - return; + fmt.println("getaddrinfo failed: ", WSAGetLastError()) + return } - defer freeaddrinfo(res); + defer freeaddrinfo(res) - s = socket(res.family, res.socktype, res.protocol); + s = socket(res.family, res.socktype, res.protocol) if s == INVALID_SOCKET { - fmt.println("socket failed: ", WSAGetLastError()); - return; + fmt.println("socket failed: ", WSAGetLastError()) + return } - defer closesocket(s); + defer closesocket(s) - bind(s, res.addr, res.addrlen as i32); - listen(s, SOMAXCONN); + bind(s, res.addr, res.addrlen as i32) + listen(s, SOMAXCONN) - client = accept(s, nil, 0); + client = accept(s, nil, 0) if client == INVALID_SOCKET { - fmt.println("socket failed: ", WSAGetLastError()); - return; + fmt.println("socket failed: ", WSAGetLastError()) + return } - defer closesocket(client); + defer closesocket(client) html := `HTTP/1.1 200 OK @@ -154,27 +154,27 @@ Content-type: text/html

Odin Server Demo

-`; +` - buf: [1024]byte; + buf: [1024]byte for { - bytes := recv(client, ^buf[0], buf.count as i32, 0); + bytes := recv(client, ^buf[0], buf.count as i32, 0) if bytes > 0 { - // fmt.println(buf[:bytes] as string); - bytes_sent := send(client, html.data, (html.count-1) as i32, 0); + // fmt.println(buf[:bytes] as string) + bytes_sent := send(client, html.data, (html.count-1) as i32, 0) if bytes_sent == SOCKET_ERROR { - fmt.println("send failed: ", WSAGetLastError()); - return; + fmt.println("send failed: ", WSAGetLastError()) + return } - break; + break } else if bytes == 0 { - fmt.println("Connection closing..."); - break; + fmt.println("Connection closing...") + break } else { - fmt.println("recv failed: ", WSAGetLastError()); - return; + fmt.println("recv failed: ", WSAGetLastError()) + return } } - shutdown(client, SD_SEND); + shutdown(client, SD_SEND) } diff --git a/code/punity.odin b/code/punity.odin index 83d22854f..fb93189ec 100644 --- a/code/punity.odin +++ b/code/punity.odin @@ -1,34 +1,34 @@ -#import "win32.odin"; -#import "fmt.odin"; -#import "os.odin"; +#import "win32.odin" +#import "fmt.odin" +#import "os.odin" -CANVAS_WIDTH :: 128; -CANVAS_HEIGHT :: 128; -CANVAS_SCALE :: 3; -FRAME_TIME :: 1.0/30.0; -WINDOW_TITLE :: "Punity\x00"; +CANVAS_WIDTH :: 128 +CANVAS_HEIGHT :: 128 +CANVAS_SCALE :: 3 +FRAME_TIME :: 1.0/30.0 +WINDOW_TITLE :: "Punity\x00" -_ := compile_assert(CANVAS_WIDTH % 16 == 0); +_ := compile_assert(CANVAS_WIDTH % 16 == 0) -WINDOW_WIDTH :: CANVAS_WIDTH * CANVAS_SCALE; -WINDOW_HEIGHT :: CANVAS_HEIGHT * CANVAS_SCALE; +WINDOW_WIDTH :: CANVAS_WIDTH * CANVAS_SCALE +WINDOW_HEIGHT :: CANVAS_HEIGHT * CANVAS_SCALE -STACK_CAPACITY :: 1<<20; -STORAGE_CAPACITY :: 1<<20; +STACK_CAPACITY :: 1<<20 +STORAGE_CAPACITY :: 1<<20 -DRAW_LIST_RESERVE :: 128; +DRAW_LIST_RESERVE :: 128 -MAX_KEYS :: 256; +MAX_KEYS :: 256 Core :: struct { - stack: ^Bank; - storage: ^Bank; + stack: ^Bank + storage: ^Bank - running: bool; - key_modifiers: u32; - key_states: [MAX_KEYS]byte; - key_deltas: [MAX_KEYS]byte; + running: bool + key_modifiers: u32 + key_states: [MAX_KEYS]byte + key_deltas: [MAX_KEYS]byte perf_frame, perf_frame_inner, @@ -36,70 +36,70 @@ Core :: struct { perf_audio, perf_blit, perf_blit_cvt, - perf_blit_gdi: Perf_Span; + perf_blit_gdi: Perf_Span - frame: i64; + frame: i64 - canvas: Canvas; - draw_list: ^Draw_List; + canvas: Canvas + draw_list: ^Draw_List } Perf_Span :: struct { - stamp: f64; - delta: f32; + stamp: f64 + delta: f32 } Bank :: struct { - memory: []byte; - cursor: int; + memory: []byte + cursor: int } Bank_State :: struct { - state: Bank; - bank: ^Bank; + state: Bank + bank: ^Bank } Color :: raw_union { - using channels: struct{ a, b, g, r: byte; }; - rgba: u32; + using channels: struct{ a, b, g, r: byte; } + rgba: u32 } Palette :: struct { - colors: [256]Color; - colors_count: byte; + colors: [256]Color + colors_count: byte } Rect :: raw_union { using minmax: struct { - min_x, min_y, max_x, max_y: int; - }; + min_x, min_y, max_x, max_y: int + } using pos: struct { - left, top, right, bottom: int; - }; - e: [4]int; + left, top, right, bottom: int + } + e: [4]int } Bitmap :: struct { - pixels: []byte; - width: int; - height: int; + pixels: []byte + width: int + height: int } Font :: struct { - using bitmap: Bitmap; - char_width: int; - char_height: int; + using bitmap: Bitmap + char_width: int + char_height: int } Canvas :: struct { - using bitmap: ^Bitmap; - palette: Palette; - translate_x: int; - translate_y: int; - clip: Rect; - font: ^Font; + using bitmap: ^Bitmap + palette: Palette + translate_x: int + translate_y: int + clip: Rect + font: ^Font } DrawFlag :: enum { @@ -114,7 +114,7 @@ Draw_List :: struct { Item :: struct { } - items: []Item; + items: []Item } Key :: enum { @@ -272,77 +272,77 @@ Key :: enum { key_down :: proc(k: Key) -> bool { - return _core.key_states[k] != 0; + return _core.key_states[k] != 0 } key_pressed :: proc(k: Key) -> bool { - return (_core.key_deltas[k] != 0) && key_down(k); + return (_core.key_deltas[k] != 0) && key_down(k) } -win32_perf_count_freq := win32.GetQueryPerformanceFrequency(); +win32_perf_count_freq := win32.GetQueryPerformanceFrequency() time_now :: proc() -> f64 { - assert(win32_perf_count_freq != 0); + assert(win32_perf_count_freq != 0) - counter: i64; - win32.QueryPerformanceCounter(^counter); - result := counter as f64 / win32_perf_count_freq as f64; - return result; + counter: i64 + win32.QueryPerformanceCounter(^counter) + result := counter as f64 / win32_perf_count_freq as f64 + return result } -_core: Core; +_core: Core run :: proc(user_init, user_step: proc(c: ^Core)) { - using win32; + using win32 - _core.running = true; + _core.running = true win32_proc :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline #stdcall { win32_app_key_mods :: proc() -> u32 { - mods: u32 = 0; + mods: u32 = 0 if is_key_down(Key_Code.SHIFT) { - mods |= Key.MOD_SHIFT as u32; + mods |= Key.MOD_SHIFT as u32 } if is_key_down(Key_Code.CONTROL) { - mods |= Key.MOD_CONTROL as u32; + mods |= Key.MOD_CONTROL as u32 } if is_key_down(Key_Code.MENU) { - mods |= Key.MOD_ALT as u32; + mods |= Key.MOD_ALT as u32 } if is_key_down(Key_Code.LWIN) || is_key_down(Key_Code.RWIN) { - mods |= Key.MOD_SUPER as u32; + mods |= Key.MOD_SUPER as u32 } - return mods; + return mods } match msg { case WM_KEYDOWN: - _core.key_modifiers = win32_app_key_mods(); + _core.key_modifiers = win32_app_key_mods() if wparam < MAX_KEYS { - _core.key_states[wparam] = 1; - _core.key_deltas[wparam] = 1; + _core.key_states[wparam] = 1 + _core.key_deltas[wparam] = 1 } - return 0; + return 0 case WM_KEYUP: - _core.key_modifiers = win32_app_key_mods(); + _core.key_modifiers = win32_app_key_mods() if wparam < MAX_KEYS { - _core.key_states[wparam] = 0; - _core.key_deltas[wparam] = 1; + _core.key_states[wparam] = 0 + _core.key_deltas[wparam] = 1 } - return 0; + return 0 case WM_CLOSE: - PostQuitMessage(0); - _core.running = false; - return 0; + PostQuitMessage(0) + _core.running = false + return 0 } - return DefWindowProcA(hwnd, msg, wparam, lparam); + return DefWindowProcA(hwnd, msg, wparam, lparam) } @@ -354,26 +354,26 @@ run :: proc(user_init, user_step: proc(c: ^Core)) { wnd_proc = win32_proc, // wnd_proc = DefWindowProcA, background = GetStockObject(BLACK_BRUSH) as HBRUSH, - }; + } if RegisterClassExA(^window_class) == 0 { - fmt.fprintln(os.stderr, "RegisterClassExA failed"); - return; + fmt.fprintln(os.stderr, "RegisterClassExA failed") + return } - screen_width := GetSystemMetrics(SM_CXSCREEN); - screen_height := GetSystemMetrics(SM_CYSCREEN); + screen_width := GetSystemMetrics(SM_CXSCREEN) + screen_height := GetSystemMetrics(SM_CYSCREEN) - rc: RECT; - rc.left = (screen_width - WINDOW_WIDTH) / 2; - rc.top = (screen_height - WINDOW_HEIGHT) / 2; - rc.right = rc.left + WINDOW_WIDTH; - rc.bottom = rc.top + WINDOW_HEIGHT; + rc: RECT + rc.left = (screen_width - WINDOW_WIDTH) / 2 + rc.top = (screen_height - WINDOW_HEIGHT) / 2 + rc.right = rc.left + WINDOW_WIDTH + rc.bottom = rc.top + WINDOW_HEIGHT - style: u32 = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; - assert(AdjustWindowRect(^rc, style, 0) != 0); + style: u32 = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX + assert(AdjustWindowRect(^rc, style, 0) != 0) - wt := WINDOW_TITLE; + wt := WINDOW_TITLE win32_window := CreateWindowExA(0, window_class.class_name, @@ -382,101 +382,101 @@ run :: proc(user_init, user_step: proc(c: ^Core)) { rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, nil, nil, window_class.instance, - nil); + nil) if win32_window == nil { - fmt.fprintln(os.stderr, "CreateWindowExA failed"); - return; + fmt.fprintln(os.stderr, "CreateWindowExA failed") + return } - window_bmi: BITMAPINFO; - window_bmi.size = size_of(BITMAPINFO.HEADER) as u32; - window_bmi.width = CANVAS_WIDTH; - window_bmi.height = CANVAS_HEIGHT; - window_bmi.planes = 1; - window_bmi.bit_count = 32; - window_bmi.compression = BI_RGB; + window_bmi: BITMAPINFO + window_bmi.size = size_of(BITMAPINFO.HEADER) as u32 + window_bmi.width = CANVAS_WIDTH + window_bmi.height = CANVAS_HEIGHT + window_bmi.planes = 1 + window_bmi.bit_count = 32 + window_bmi.compression = BI_RGB - user_init(^_core); + user_init(^_core) - ShowWindow(win32_window, SW_SHOW); + ShowWindow(win32_window, SW_SHOW) - window_buffer := new_slice(u32, CANVAS_WIDTH * CANVAS_HEIGHT); - assert(window_buffer.data != nil); - defer free(window_buffer.data); + window_buffer := new_slice(u32, CANVAS_WIDTH * CANVAS_HEIGHT) + assert(window_buffer.data != nil) + defer free(window_buffer.data) for i := 0; i < window_buffer.count; i++ { - window_buffer[i] = 0xff00ff; + window_buffer[i] = 0xff00ff } - prev_time, curr_time,dt: f64; - prev_time = time_now(); - curr_time = time_now(); - total_time : f64 = 0; - offset_x := 0; - offset_y := 0; + prev_time, curr_time,dt: f64 + prev_time = time_now() + curr_time = time_now() + total_time : f64 = 0 + offset_x := 0 + offset_y := 0 - message: MSG; + message: MSG for _core.running { - curr_time = time_now(); - dt = curr_time - prev_time; - prev_time = curr_time; - total_time += dt; + curr_time = time_now() + dt = curr_time - prev_time + prev_time = curr_time + total_time += dt - offset_x += 1; - offset_y += 2; + offset_x += 1 + offset_y += 2 { - data: [128]byte; - buf := data[:0]; - fmt.bprintf(^buf, "Punity: % ms\x00", dt*1000); - win32.SetWindowTextA(win32_window, buf.data); + data: [128]byte + buf := data[:0] + fmt.bprintf(^buf, "Punity: % ms\x00", dt*1000) + win32.SetWindowTextA(win32_window, buf.data) } for y := 0; y < CANVAS_HEIGHT; y++ { for x := 0; x < CANVAS_WIDTH; x++ { - g := (x % 32) * 8; - b := (y % 32) * 8; - window_buffer[x + y*CANVAS_WIDTH] = (g << 8 | b) as u32; + g := (x % 32) * 8 + b := (y % 32) * 8 + window_buffer[x + y*CANVAS_WIDTH] = (g << 8 | b) as u32 } } - _core.key_deltas = nil; + _core.key_deltas = nil for PeekMessageA(^message, nil, 0, 0, PM_REMOVE) != 0 { if message.message == WM_QUIT { - _core.running = false; + _core.running = false } - TranslateMessage(^message); - DispatchMessageA(^message); + TranslateMessage(^message) + DispatchMessageA(^message) } - user_step(^_core); + user_step(^_core) - dc := GetDC(win32_window); + dc := GetDC(win32_window) StretchDIBits(dc, 0, 0, CANVAS_WIDTH * CANVAS_SCALE, CANVAS_HEIGHT * CANVAS_SCALE, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT, window_buffer.data, ^window_bmi, DIB_RGB_COLORS, - SRCCOPY); - ReleaseDC(win32_window, dc); + SRCCOPY) + ReleaseDC(win32_window, dc) { - delta := time_now() - prev_time; - ms := ((FRAME_TIME - delta) * 1000) as i32; + delta := time_now() - prev_time + ms := ((FRAME_TIME - delta) * 1000) as i32 if ms > 0 { - win32.Sleep(ms); + win32.Sleep(ms) } } - _core.frame++; + _core.frame++ } } -- cgit v1.2.3