From cdeebfb8f91d25f171d63ecb32f1da6bd3be165e Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 15 Jun 2022 10:51:45 +0200 Subject: [PATCH] Ported unicode conversions --- ast.cpp | 8 ++++++ ccodegen.cpp | 10 +++++++ compiler.h | 2 ++ parsing.cpp | 4 +++ programs/Windows.kl | 16 +++++------ programs/main.kl | 66 ++++++++++++++++++++++++++++----------------- typechecking.cpp | 1 + 7 files changed, 75 insertions(+), 32 deletions(-) diff --git a/ast.cpp b/ast.cpp index a943f0d..2a1982b 100644 --- a/ast.cpp +++ b/ast.cpp @@ -26,6 +26,7 @@ enum Ast_Kind: U32{ AST_LENGTH_OF, AST_ALIGN_OF, + AST_BREAK, AST_COMPOUND, AST_TYPE, AST_VAR, @@ -146,6 +147,7 @@ struct Ast_If: Ast{ }; struct Ast_Pass: Ast{}; +struct Ast_Break: Ast{}; struct Ast_For: Ast{ Ast_Expr *init; @@ -357,6 +359,12 @@ ast_pass(Token *pos){ return result; } +function Ast_Break * +ast_break(Token *pos){ + AST_NEW(Break, BREAK, pos, AST_STMT); + return result; +} + function Ast_Return * ast_return(Token *pos, Ast_Expr *expr){ AST_NEW(Return, RETURN, pos, AST_STMT); diff --git a/ccodegen.cpp b/ccodegen.cpp index d50f2a2..50a675d 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -415,6 +415,14 @@ gen_ast(Ast *ast){ BREAK(); } + + CASE(BREAK, Break){ + unused(node); + gen("break;"); + BREAK(); + } + + CASE(PASS, Pass){ unused(node); gen("//pass"); @@ -695,6 +703,8 @@ typedef struct String{ S64 len; }String; #define LIT(x) (String){.str=(U8 *)x, .len=sizeof(x)-1} +#define assert(x) do{if(!(x))__debugbreak();}while(0) +#define assert_msg(x,...) assert(x) )=="); #endif diff --git a/compiler.h b/compiler.h index ee91575..afc8a04 100644 --- a/compiler.h +++ b/compiler.h @@ -163,6 +163,7 @@ Intern_String keyword_true; Intern_String keyword_false; Intern_String keyword_for; Intern_String keyword_pass; +Intern_String keyword_break; Intern_String keyword_elif; Intern_String keyword_assert; Intern_String keyword_sizeof; @@ -210,6 +211,7 @@ lex_init(Allocator *token_string_arena, Allocator *map_allocator, Lexer *l){ keyword_lengthof = l->intern("length_of"_s); keyword_alignof = l->intern("align_of"_s); keyword_true = l->intern("true"_s); + keyword_break = l->intern("break"_s); keyword_false = l->intern("false"_s); keyword_return = l->intern("return"_s); keyword_assert = l->intern("assert"_s); diff --git a/parsing.cpp b/parsing.cpp index c63d20b..10c564f 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -220,6 +220,10 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){ scope->stmts.add(ast_return(token, expr)); } + else if(token_match_keyword(keyword_break)){ + scope->stmts.add(ast_break(token)); + } + else if(token_match_keyword(keyword_pass)){ scope->stmts.add(ast_pass(token)); } diff --git a/programs/Windows.kl b/programs/Windows.kl index 9aeff6f..0ea267f 100644 --- a/programs/Windows.kl +++ b/programs/Windows.kl @@ -27,7 +27,6 @@ MEM_COMMIT :: 0x00001000 MEM_RESERVE :: 0x00002000 MEM_RESET :: 0x00080000 MEM_RESET_UNDO :: 0x1000000 - MEM_DECOMMIT :: 0x00004000 MEM_RELEASE :: 0x00008000 @@ -35,16 +34,17 @@ PAGE_NOACCESS :: 1 PAGE_READONLY :: 2 PAGE_READWRITE :: 4 PAGE_WRITECOPY :: 8 -PAGE_EXECUTE :: 0x10 -PAGE_EXECUTE_READ :: 0x20 -PAGE_EXECUTE_READWRITE :: 0x40 -PAGE_EXECUTE_WRITECOPY :: 0x80 - +PAGE_EXECUTE :: 0x10; PAGE_EXECUTE_READ :: 0x20; PAGE_EXECUTE_READWRITE :: 0x40; PAGE_EXECUTE_WRITECOPY :: 0x80 VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID -VirtualFree :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, dwFreeType: DWORD): BOOL +VirtualFree :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, dwFreeType: DWORD): BOOL + +HEAP_ZERO_MEMORY :: 0x8; HEAP_NO_SERIALIZE :: 0x1; HEAP_GENERATE_EXCEPTIONS :: 0x4 +GetProcessHeap :: #foreign (): HANDLE +HeapAlloc :: #foreign (hHeap: HANDLE, dwFlags: DWORD, dwByte: SIZE_T): LPVOID +HeapFree :: #foreign (hHeap: HANDLE, dwFlags: DWORD, lpMe: LPVOID): BOOL STD_INPUT_HANDLE :: 4294967286//(-10)->DWORD STD_OUTPUT_HANDLE :: 4294967285//(-11)->DWORD //STD_ERROR_HANDLE :: (-12)->DWORD -GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE +GetStdHandle :: #foreign (nStdHandle: DWORD): HANDLE WriteConsoleA :: #foreign (hConsoleOutput: HANDLE,lpBuffer: *VOID,nNumberOfCharsToWrite: DWORD,lpNumberOfCharsWritten: LPDWORD,lpReserve: LPVOID): BOOL \ No newline at end of file diff --git a/programs/main.kl b/programs/main.kl index 7f2c169..f1849c7 100644 --- a/programs/main.kl +++ b/programs/main.kl @@ -7,11 +7,13 @@ String32 :: struct;; str: *U32; len: S64 UTF32_Result :: struct out_str: U32 advance: S64 - error : S32 + error : S64 UTF16_Result :: struct out_str: [2]U16 - len : S32 - error : S32 + len : S64 + error : S64 +question_mark16 :: 0x003f + utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result result: UTF32_Result @@ -68,26 +70,34 @@ utf32_to_utf16 :: (codepoint: U32): UTF16_Result 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); -// } +process_heap: HANDLE +allocate :: (size: U64): *void + if process_heap == 0 + process_heap = GetProcessHeap() + return HeapAlloc(process_heap, 0, size) -// result.str[result.len] = 0; -// return result; -// } +string_to_string16 :: (in: String): String16 + in_str := &in[0] + // @Note(Krzosa): Should be more then enough space + alloc_size := (length_of(in)*2)+1 + result := String16{str = allocate(alloc_size->U64)->*U16} + for i := 0, i < length_of(in) + decode := utf8_to_utf32(in_str + i, length_of(in) - i) + if !decode.error + i += decode.advance + encode := utf32_to_utf16(decode.out_str) + if !encode.error + for j := 0, j < encode.len, j++ + result.str[result.len++] = encode.out_str[j] + else + result.str[result.len++] = question_mark16 + break + else + result.str[result.len++] = question_mark16 + break + + result.str[result.len] = 0 + return result Vec2I :: struct;; x: S32; y: S32 Vec2 :: struct;; x: F32; y: F32 @@ -129,8 +139,16 @@ window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRE main :: (argc: int, argv: **char): int bitmap := create_bitmap({1280, 720}) + + string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..." + string_result := string_to_string16(string) + result := utf8_to_utf32(&"A"[0], 1) assert(result.out_str == 'A, "Invalid decode") - result = utf8_to_utf32(&"ć"[0], 2) - result = utf8_to_utf32(&"ó"[0], 2) + + result = utf8_to_utf32(&"ć"[0], 2) + assert(result.out_str == 0x107, "Invalid decode") + + result = utf8_to_utf32(&"ó"[0], 2) + assert(result.out_str == 0xF3, "Invalid decode") diff --git a/typechecking.cpp b/typechecking.cpp index d917b41..b99760c 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -438,6 +438,7 @@ resolve_stmt(Ast *ast, Ast_Type *ret){ BREAK(); } + case AST_BREAK: CASE(PASS, Pass){ unused(node); BREAK();