Porting unicode code

This commit is contained in:
Krzosa Karol
2022-06-15 10:12:40 +02:00
parent 43f424c414
commit e984049429
4 changed files with 87 additions and 8 deletions

View File

@@ -262,7 +262,9 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
CASE(INDEX, Index){ CASE(INDEX, Index){
gen("("); gen("(");
gen_expr(node->expr); gen_expr(node->expr);
if(node->expr->resolved_type == type_string) gen(".str"); if(is_string(node->resolved_type))
if(!(type_of_var && type_of_var == type_pointer_to_char))
gen(".str");
gen("["); gen("[");
gen_expr(node->index); gen_expr(node->index);
gen("]"); gen("]");

View File

@@ -7,21 +7,91 @@ String32 :: struct;; str: *U32; len: S64
UTF32_Result :: struct UTF32_Result :: struct
out_str: U32 out_str: U32
advance: S64 advance: S64
error : Bool error : S32
UTF16_Result :: struct UTF16_Result :: struct
out_str: [2]U16 out_str: [2]U16
len : S32 len : S32
error : Bool error : S32
BINARY :: 0b1001 BINARY :: 0b1001
utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result
result: UTF32_Result result: UTF32_Result
if (c[0] & 0b10000000) == 0
if max_advance >= 1
c0 := c[0]->U32
result.out_str = c0
result.advance = 1
else;; result.error = 1
elif (c[0] & 0b11100000) == 0b11000000
if (c[1] & 0b11000000) == 0b10000000 // Continuation byte required
if max_advance >= 2
c0 := c[0]->U32; c1 := c[1]->U32
result.out_str = (c0 & 0b00011111) << 6 | (c1 & 0b00111111)
result.advance = 2
else;; result.error = 2
else;; result.error = 2
elif (c[0] & 0b11110000) == 0b11100000
if (c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000 // Two continuation bytes required
if max_advance >= 3
c0 := c[0]->U32; c1 := c[1]->U32; c2 := c[2]->U32
result.out_str = (c0 & 0b00001111) << 12 | (c1 & 0b00111111) << 6 | (c2 & 0b00111111)
result.advance = 3
else;; result.error = 3
else;; result.error = 3
elif (c[0] & 0b11111000) == 0b11110000
if (c[1] & 0b11000000) == 0b10000000 && (c[2] & 0b11000000) == 0b10000000 && (c[3] & 0b11000000) == 0b10000000 // Three continuation bytes required
if max_advance >= 4
c0 := c[0]->U32; c1 := c[1]->U32; c2 := c[2]->U32; c3 := c[3]->U32
result.out_str = (c0 & 0b00001111) << 18 | (c1 & 0b00111111) << 12 | (c2 & 0b00111111) << 6 | (c3 & 0b00111111)
result.advance = 4
else;; result.error = 4
else;; result.error = 4
else;; result.error = 4
return result return result
utf32_to_utf16 :: (codepoint: U32): UTF16_Result
result: UTF16_Result
if codepoint < 0x10000
result.out_str[0] = codepoint->U16
result.out_str[1] = 0
result.len = 1
elif codepoint <= 0x10FFFF
code: U32 = (codepoint - 0x10000)
result.out_str[0] = (0xD800 | (code >> 10))->U16
result.out_str[1] = (0xDC00 | (code & 0x3FF))->U16
result.len = 2
else
result.error = 1
return result
// string_to_string16 :: (allocator: *Allocator, int: String): String16
// String16 result = {exp_alloc_array(allocator, U16, (in.len*2)+1)}; // @Note(Krzosa): Should be more then enough space
// for(S64 i = 0; i < in.len;){
// UTF32_Result decode = utf8_to_utf32(in.str + i, in.len - i);
// if(!decode.error){
// i += decode.advance;
// UTF16_Result encode = utf32_to_utf16(decode.out_str);
// if(!encode.error){
// for(S32 j = 0; j < encode.len; j++){
// result.str[result.len++] = encode.out_str[j];
// }
// }
// else unicode_error(question_mark16);
// }
// else unicode_error(question_mark16);
// }
// result.str[result.len] = 0;
// return result;
// }
Vec2I :: struct;; x: S32; y: S32
Vec2 :: struct;; x: F32; y: F32
Windows_Bitmap :: struct Windows_Bitmap :: struct
size: Vec2I size: Vec2I
@@ -29,6 +99,10 @@ Windows_Bitmap :: struct
hdc: HDC hdc: HDC
dib: HBITMAP dib: HBITMAP
Vec2I :: struct;; x: S32; y: S32
Vec2 :: struct;; x: F32; y: F32
create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap create_bitmap :: (size: Vec2I, bottom_up: Bool = true): Windows_Bitmap
result: Windows_Bitmap = {size = size} result: Windows_Bitmap = {size = size}
if bottom_up == false if bottom_up == false
@@ -57,9 +131,11 @@ window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRE
if msg == WM_DESTROY if msg == WM_DESTROY
PostQuitMessage(0) PostQuitMessage(0)
return 0 return 0
else;; return DefWindowProc(hwnd, msg, wparam, lparam) else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
main :: (argc: int, argv: **char): int main :: (argc: int, argv: **char): int
bitmap := create_bitmap({1280, 720}) bitmap := create_bitmap({1280, 720})
result := utf8_to_utf32(&"A"[0], 1)
result = utf8_to_utf32(&"ć"[0], 2)
result = utf8_to_utf32(&"ó"[0], 2)

View File

@@ -3,7 +3,7 @@ WNDPROC :: (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT
WNDCLASSW :: struct;; style: UINT; lpfnWndProc: WNDPROC; cbClsExtra: int; cbWndExtra: int; hInstance: HINSTANCE; hIcon: HICON; hCursor: HCURSOR; hbrBackground: HBRUSH; lpszMenuName: LPCWSTR; lpszClassName: LPCWSTR WNDCLASSW :: struct;; style: UINT; lpfnWndProc: WNDPROC; cbClsExtra: int; cbWndExtra: int; hInstance: HINSTANCE; hIcon: HICON; hCursor: HCURSOR; hbrBackground: HBRUSH; lpszMenuName: LPCWSTR; lpszClassName: LPCWSTR
PostQuitMessage :: #foreign(nExitCode: int) PostQuitMessage :: #foreign(nExitCode: int)
DefWindowProc :: #foreign (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT DefWindowProcW :: #foreign (hwnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM): LRESULT
GetDC :: #foreign (hWnd: HWND): HDC GetDC :: #foreign (hWnd: HWND): HDC
CreateWindowA :: #foreign (dwExStyle: DWORD, lpClassName: *char, lpWindowName: *char, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpParam: *void): HWND CreateWindowA :: #foreign (dwExStyle: DWORD, lpClassName: *char, lpWindowName: *char, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpParam: *void): HWND
CreateWindowExW :: #foreign (dwExStyle: DWORD, lpClassName: LPCWSTR, lpWindowName: LPCWSTR, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpPara: LPVOID): HWND CreateWindowExW :: #foreign (dwExStyle: DWORD, lpClassName: LPCWSTR, lpWindowName: LPCWSTR, dwStyle: DWORD, X: int, Y: int, nWidth: int, nHeight: int, hWndParent: HWND, hMenu: HMENU, hInstance: HINSTANCE, lpPara: LPVOID): HWND

View File

@@ -776,6 +776,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
} }
node->index_original_type = left.type; node->index_original_type = left.type;
assert(left.type);
if(is_string(left.type)){ if(is_string(left.type)){
return operand_lvalue(type_u8); return operand_lvalue(type_u8);
} }