http server
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#library "raylib";
|
||||
raylib :: #library "raylib";
|
||||
|
||||
Color :: struct {
|
||||
r, g, b, a: u8;
|
||||
@@ -8,10 +8,10 @@ Vector2 :: struct {
|
||||
x, y: f32;
|
||||
}
|
||||
|
||||
InitWindow :: (width: s32, height: s32, title: [:0]u8) -> void #foreign;
|
||||
CloseWindow :: () -> void #foreign;
|
||||
WindowShouldClose :: () -> bool #foreign;
|
||||
BeginDrawing :: () -> void #foreign;
|
||||
EndDrawing :: () -> void #foreign;
|
||||
ClearBackground :: (color: Color) -> void #foreign;
|
||||
DrawTriangle :: (v1: Vector2, v2: Vector2, v3: Vector2, color: Color) -> void #foreign;
|
||||
InitWindow :: (width: s32, height: s32, title: [:0]u8) -> void #foreign raylib;
|
||||
CloseWindow :: () -> void #foreign raylib;
|
||||
WindowShouldClose :: () -> bool #foreign raylib;
|
||||
BeginDrawing :: () -> void #foreign raylib;
|
||||
EndDrawing :: () -> void #foreign raylib;
|
||||
ClearBackground :: (color: Color) -> void #foreign raylib;
|
||||
DrawTriangle :: (v1: Vector2, v2: Vector2, v3: Vector2, color: Color) -> void #foreign raylib;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#library "SDL3";
|
||||
sdl3 :: #library "SDL3";
|
||||
|
||||
// SDL_InitFlags
|
||||
SDL_INIT_VIDEO :u32: 0x20;
|
||||
@@ -317,17 +317,17 @@ SDL_Event :: enum struct { tag: u32; _: u32; payload: [30]u32; } {
|
||||
}
|
||||
|
||||
// Functions
|
||||
SDL_Init :: (flags: u32) -> bool #foreign;
|
||||
SDL_Quit :: () -> void #foreign;
|
||||
SDL_CreateWindow :: (title: [:0]u8, w: s32, h: s32, flags: u64) -> *void #foreign;
|
||||
SDL_DestroyWindow :: (window: *void) -> void #foreign;
|
||||
SDL_GL_SetAttribute :: (attr: s32, value: s32) -> bool #foreign;
|
||||
SDL_GL_CreateContext :: (window: *void) -> *void #foreign;
|
||||
SDL_GL_DestroyContext :: (context: *void) -> bool #foreign;
|
||||
SDL_GL_MakeCurrent :: (window: *void, context: *void) -> bool #foreign;
|
||||
SDL_GL_SwapWindow :: (window: *void) -> bool #foreign;
|
||||
SDL_GL_SetSwapInterval :: (interval: s32) -> bool #foreign;
|
||||
SDL_GL_GetProcAddress :: (proc: [:0]u8) -> *void #foreign;
|
||||
SDL_PollEvent :: (event: *SDL_Event) -> bool #foreign;
|
||||
SDL_GetTicks :: () -> u64 #foreign;
|
||||
SDL_Delay :: (ms: u32) -> void #foreign;
|
||||
SDL_Init :: (flags: u32) -> bool #foreign sdl3;
|
||||
SDL_Quit :: () -> void #foreign sdl3;
|
||||
SDL_CreateWindow :: (title: [:0]u8, w: s32, h: s32, flags: u64) -> *void #foreign sdl3;
|
||||
SDL_DestroyWindow :: (window: *void) -> void #foreign sdl3;
|
||||
SDL_GL_SetAttribute :: (attr: s32, value: s32) -> bool #foreign sdl3;
|
||||
SDL_GL_CreateContext :: (window: *void) -> *void #foreign sdl3;
|
||||
SDL_GL_DestroyContext :: (context: *void) -> bool #foreign sdl3;
|
||||
SDL_GL_MakeCurrent :: (window: *void, context: *void) -> bool #foreign sdl3;
|
||||
SDL_GL_SwapWindow :: (window: *void) -> bool #foreign sdl3;
|
||||
SDL_GL_SetSwapInterval :: (interval: s32) -> bool #foreign sdl3;
|
||||
SDL_GL_GetProcAddress :: (proc: [:0]u8) -> *void #foreign sdl3;
|
||||
SDL_PollEvent :: (event: *SDL_Event) -> bool #foreign sdl3;
|
||||
SDL_GetTicks :: () -> u64 #foreign sdl3;
|
||||
SDL_Delay :: (ms: u32) -> void #foreign sdl3;
|
||||
|
||||
33
examples/modules/socket.sx
Normal file
33
examples/modules/socket.sx
Normal file
@@ -0,0 +1,33 @@
|
||||
// POSIX socket module (macOS only)
|
||||
// sockaddr_in layout and constants are platform-specific.
|
||||
|
||||
libc :: #library "c";
|
||||
|
||||
// POSIX socket API
|
||||
socket :: (domain: s32, kind: s32, protocol: s32) -> s32 #foreign libc;
|
||||
setsockopt :: (fd: s32, level: s32, optname: s32, optval: *s32, optlen: u32) -> s32 #foreign libc;
|
||||
bind :: (fd: s32, addr: *SockAddr, addrlen: u32) -> s32 #foreign libc;
|
||||
listen :: (fd: s32, backlog: s32) -> s32 #foreign libc;
|
||||
accept :: (fd: s32, addr: *SockAddr, addrlen: *u32) -> s32 #foreign libc;
|
||||
read :: (fd: s32, buf: [:0]u8, count: s64) -> s64 #foreign libc;
|
||||
write :: (fd: s32, buf: [:0]u8, count: s64) -> s64 #foreign libc;
|
||||
close :: (fd: s32) -> s32 #foreign libc;
|
||||
|
||||
// Constants (macOS)
|
||||
AF_INET :s32: 2;
|
||||
SOCK_STREAM :s32: 1;
|
||||
SOL_SOCKET :s32: 0xFFFF;
|
||||
SO_REUSEADDR :s32: 0x4;
|
||||
|
||||
// macOS sockaddr_in (16 bytes, has sin_len field)
|
||||
SockAddr :: struct {
|
||||
sin_len: u8;
|
||||
sin_family: u8;
|
||||
sin_port: u16;
|
||||
sin_addr: u32;
|
||||
sin_zero: u64;
|
||||
}
|
||||
|
||||
htons :: (port: s64) -> u16 {
|
||||
cast(u16) ((port / 256) | ((port % 256) * 256));
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
Vector :: ($N: int, $T: Type) -> Type #builtin;
|
||||
write :: (str: string) -> void #builtin;
|
||||
out :: (str: string) -> void #builtin;
|
||||
sqrt :: (x: $T) -> T #builtin;
|
||||
sin :: (x: $T) -> T #builtin;
|
||||
cos :: (x: $T) -> T #builtin;
|
||||
@@ -315,10 +315,81 @@ build_print :: (fmt: string) -> string {
|
||||
code = concat(code, int_to_string(fmt.len - seg_start));
|
||||
code = concat(code, ")); ");
|
||||
}
|
||||
code = concat(code, "write(result);");
|
||||
code = concat(code, "out(result);");
|
||||
code;
|
||||
}
|
||||
|
||||
build_format :: (fmt: string) -> string {
|
||||
code := "result := \"\"; ";
|
||||
seg_start := 0;
|
||||
i := 0;
|
||||
arg_idx := 0;
|
||||
while i < fmt.len {
|
||||
if fmt[i] == 123 {
|
||||
if i + 1 < fmt.len {
|
||||
if fmt[i + 1] == 125 {
|
||||
if i > seg_start {
|
||||
code = concat(code, "result = concat(result, substr(fmt, ");
|
||||
code = concat(code, int_to_string(seg_start));
|
||||
code = concat(code, ", ");
|
||||
code = concat(code, int_to_string(i - seg_start));
|
||||
code = concat(code, ")); ");
|
||||
}
|
||||
code = concat(code, "result = concat(result, any_to_string(args[");
|
||||
code = concat(code, int_to_string(arg_idx));
|
||||
code = concat(code, "])); ");
|
||||
arg_idx += 1;
|
||||
i += 2;
|
||||
seg_start = i;
|
||||
} else if fmt[i + 1] == 123 {
|
||||
code = concat(code, "result = concat(result, substr(fmt, ");
|
||||
code = concat(code, int_to_string(seg_start));
|
||||
code = concat(code, ", ");
|
||||
code = concat(code, int_to_string(i - seg_start + 1));
|
||||
code = concat(code, ")); ");
|
||||
i += 2;
|
||||
seg_start = i;
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
} else if fmt[i] == 125 {
|
||||
if i + 1 < fmt.len {
|
||||
if fmt[i + 1] == 125 {
|
||||
code = concat(code, "result = concat(result, substr(fmt, ");
|
||||
code = concat(code, int_to_string(seg_start));
|
||||
code = concat(code, ", ");
|
||||
code = concat(code, int_to_string(i - seg_start + 1));
|
||||
code = concat(code, ")); ");
|
||||
i += 2;
|
||||
seg_start = i;
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
if seg_start < fmt.len {
|
||||
code = concat(code, "result = concat(result, substr(fmt, ");
|
||||
code = concat(code, int_to_string(seg_start));
|
||||
code = concat(code, ", ");
|
||||
code = concat(code, int_to_string(fmt.len - seg_start));
|
||||
code = concat(code, ")); ");
|
||||
}
|
||||
code = concat(code, "result;");
|
||||
code;
|
||||
}
|
||||
|
||||
format :: ($fmt: string, args: ..Any) -> string {
|
||||
#insert build_format(fmt);
|
||||
}
|
||||
|
||||
print :: ($fmt: string, args: ..Any) {
|
||||
#insert build_print(fmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user