From e4e939ec04c00e8a2f62e533c7aefd84c59caa7e Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Tue, 14 Jun 2022 14:28:29 +0200 Subject: [PATCH] Restructure --- programs/Windows.kl | 40 +++++++++++++++ programs/base.kl | 24 +++++++++ programs/enums.kl | 7 +++ programs/euler.kl | 78 ++++++++++++++++++++++++++++++ programs/globals.kl | 91 ++++++++++++++++++++++++++++++++++ programs/lambdas.kl | 64 ++++++++++++++++++++++++ programs/main.kl | 9 ++++ programs/new_types.kl | 110 ++++++++++++++++++++++++++++++++++++++++++ programs/order1.kl | 21 ++++++++ programs/order2.kl | 69 ++++++++++++++++++++++++++ programs/os.kl | 44 +++++++++++++++++ programs/program.kl | 43 +++++++++++++++++ 12 files changed, 600 insertions(+) create mode 100644 programs/Windows.kl create mode 100644 programs/base.kl create mode 100644 programs/enums.kl create mode 100644 programs/euler.kl create mode 100644 programs/globals.kl create mode 100644 programs/lambdas.kl create mode 100644 programs/main.kl create mode 100644 programs/new_types.kl create mode 100644 programs/order1.kl create mode 100644 programs/order2.kl create mode 100644 programs/os.kl create mode 100644 programs/program.kl diff --git a/programs/Windows.kl b/programs/Windows.kl new file mode 100644 index 0000000..d9b2986 --- /dev/null +++ b/programs/Windows.kl @@ -0,0 +1,40 @@ +DWORD :: U32 +LPCSTR :: *char +HWND :: *void +HMENU :: *void +HINSTANCE :: *void +LPVOID :: *void +SIZE_T :: U64 +BOOL :: int +HANDLE :: *void +VOID :: void +LPDWORD :: *DWORD + +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 + + +MEM_COMMIT :: 0x00001000 +MEM_RESERVE :: 0x00002000 +MEM_RESET :: 0x00080000 +MEM_RESET_UNDO :: 0x1000000 + +MEM_DECOMMIT :: 0x00004000 +MEM_RELEASE :: 0x00008000 + +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 + +VirtualAlloc :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID +VirtualFree :: #foreign (lpAddress: LPVOID, dwSize: SIZE_T, dwFreeType: DWORD): BOOL + +STD_INPUT_HANDLE :: 4294967286//(-10)->DWORD +STD_OUTPUT_HANDLE :: 4294967285//(-11)->DWORD +//STD_ERROR_HANDLE :: (-12)->DWORD +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/base.kl b/programs/base.kl new file mode 100644 index 0000000..458e039 --- /dev/null +++ b/programs/base.kl @@ -0,0 +1,24 @@ +Os :: #import "os.kl" + +SizeU :: #strict U64 + +Arena :: struct + memory: Os.Memory + alignment: U64 + len: U64 + +clamp_top_sizeu :: (val: SizeU, max: SizeU): SizeU + if val > max + return max + return val + +get_align_offset :: (size: SizeU, align: SizeU): SizeU + mask := align - 1 + val := size & mask + if val != 0 + val = align - val + return val + +align_up :: (size: SizeU, align: SizeU): SizeU + result := size + get_align_offset(size, align) + return result diff --git a/programs/enums.kl b/programs/enums.kl new file mode 100644 index 0000000..82c8293 --- /dev/null +++ b/programs/enums.kl @@ -0,0 +1,7 @@ +Allocator_Kind :: enum #flag + Null; Arena; Heap + +kind := Allocator_Kind.Heap + +basic_type_assignment :: () // This works + return \ No newline at end of file diff --git a/programs/euler.kl b/programs/euler.kl new file mode 100644 index 0000000..05240e4 --- /dev/null +++ b/programs/euler.kl @@ -0,0 +1,78 @@ +// @todo: Add more stats in the preview +// @todo: for 0..1000(implicit i) and for i in 0..1000 +// @todo: Add blocks of stmts that you can simply define inside a function etc. + +entry :: () + print_str("\n") + euler1() + euler3() + +//----------------------------------------------------------------------------- +// Euler 2 +//----------------------------------------------------------------------------- +euler1 :: () + end := 1000 + result := 0 + for i := 0, i < end, i++ + if i % 3 == 0 + result += i + else if i % 5 == 0 + result += i + print_str("Euler1: ") + print_int(result) + print_str("\n") + + +//----------------------------------------------------------------------------- +// Euler 3 +//----------------------------------------------------------------------------- +int_sqrt :: (s: S64): S64 + result := sqrt(cast(s: F64)) + return cast(result: S64) + +// https://en.wikipedia.org/wiki/Integer_square_root +int_sqrt1 :: (s: S64): S64 + x0 := s / 2 + + if x0 != 0 + x1 := (x0 + s / x0) / 2 + for x1 < x0 + x0 = x1 + x1 = (x0 + s / x0) / 2 + return x0 + else + return s + +euler3 :: () + n := 600851475143 + results: [32]S64 + results_len := 0 + + // First search all 2's + for n % 2 == 0 + results[results_len++] = 2 + n /= 2 + + // Then search other primes, 3, 5, 7 etc. + for i := 3, i <= int_sqrt1(n), i += 2 + for n % i == 0 + results[results_len++] = i + n /= i + + // Then check if our number is a prime it self + if n > 2 + results[results_len++] = n + + print_str("Euler3: ") + + is_correct: S64 = 1 + for i := 0, i < results_len, i++ + is_correct = is_correct * results[i] + print_int(is_correct) + + print_str(":: ") + print_int(is_correct) + +sqrt :: (v: F64): F64 #foreign +print_int :: (i: S64) #foreign +print_str :: (i: String) #foreign \ No newline at end of file diff --git a/programs/globals.kl b/programs/globals.kl new file mode 100644 index 0000000..92a2f7e --- /dev/null +++ b/programs/globals.kl @@ -0,0 +1,91 @@ +lambdas :: #load "lambdas.kl" +Memory :: #load "enums.kl" +#load "lambdas.kl" +#load "lambdas.kl" +#import "order1.kl" +#import "order1.kl" +order :: #import "order1.kl" + + + +test_load :: () + new_types :: #load "new_types.kl" + new_types.basic_type_assignment() + arena__: order2.Arena + Arena_ :: order.order2.Arena + arena___: Arena_ +//----------------------------------------------------------------------------- +// Function types +//----------------------------------------------------------------------------- +test_function :: (thing: S64): *S64 +function_type: test_function +const_function_alias :: test_function +// null_function: (t: S64): *S64 = null + +//----------------------------------------------------------------------------- +// Booleans +//----------------------------------------------------------------------------- +Boolean: Bool = true + +//----------------------------------------------------------------------------- +// Nulls +//----------------------------------------------------------------------------- +// int_null: S64 = null +// str_null: String = null +// Bool_null: Bool = null + +//----------------------------------------------------------------------------- +// Compound expressions +//----------------------------------------------------------------------------- +array1: [4]S64 = {1,2,3,4} +imp_array := [5]S64{1,2} +// imp_array_a := [5]S64{1,2,3,4,5,6} +// imp_array_b: [5]S64 = {1,2,3,4,5,6} +imp_array_c: [5]S64 = {[0] = 1, [2] = 2, [1] = 0} // @todo this should be illegal +// without_size: []S64 = {} // @todo: this should be slice, converting from array should be implicit + +string: *char = "string" +first_letter := string[0] +decl_char: char = 55 + +//----------------------------------------------------------------------------- +// Pointers +//----------------------------------------------------------------------------- +pointer_decl : *S64 +variable_from_deref: S64 = *pointer_decl +pointer_from_var : *S64 = &variable_from_deref +Boolean_pointer := &Boolean + +//----------------------------------------------------------------------------- +// Implicit type +//----------------------------------------------------------------------------- +implicit_int :: 10 +implicit_str :: "Hello world" + +//----------------------------------------------------------------------------- +// Pointers +//----------------------------------------------------------------------------- +// pointer1: *S64 = 0 +// pointer2: *S64 = pointer1 +// pointer3: **S64 = 0 + +//----------------------------------------------------------------------------- +// String types +//----------------------------------------------------------------------------- +string1 :: "Test" +string2 :: string1 + +//----------------------------------------------------------------------------- +// Constant S64 variables +//----------------------------------------------------------------------------- +thing0 :: 10 +thing4 :: thing0 + 11 +thing3 :: thing4 + 20 +combin :: thing0 + thing4 + thing3 + +Some :: struct + len: S64 + cap: S64 + +some := Some{3, 2} +other := Some{len = 1, cap = 3} \ No newline at end of file diff --git a/programs/lambdas.kl b/programs/lambdas.kl new file mode 100644 index 0000000..9aea6d4 --- /dev/null +++ b/programs/lambdas.kl @@ -0,0 +1,64 @@ +Test :: struct + len: S64 + + +test: Test +member := test.len + +enum_val: Memory.Allocator_Kind = Memory.Allocator_Kind.Heap +other_enum_val: S64 = enum_val->S64 + +a_type :: S64 +pointer_type :: *S64 +// null_pointer: pointer_type = null + +if_stmt :: (cond: S64): type + CONSTANT :: 10 + thing := 10 + if i := thing + cond, cond > CONSTANT + return i + CONSTANT + else if cond < CONSTANT + return i - CONSTANT + else + return CONSTANT + + +for_stmt :: () + for i := 0, i < 10, i+=1 + out_of_order_resolving() + +out_of_order_resolving :: () + i := 10 + +add_10 :: (size: S64): S64 + add_20 :: (new_size: S64): S64 + return 20 + + add :: (a: S64, b: S64 = 10): S64 + return a + b + + constant :: 20; result := constant + 10 + + v3 := add(1,2) + // v2 := add(a = 1, b = 2) + // v1 := add(a = 1) + // // v_err := add([0] = 1) + // v4 := add(b = 1, a = 2) + // // v_err := add([0] = 1, [1] = 2) + // // v_err := add([0] = 1, 10) // illegal + // // v_err := add([1] = 1) // illegal + // // v_err := add() // illegal + v4 := constant + return v4 + +return_constant :: (): S64 + constant :: 10 + return constant + +returning_void :: (insert: S64) + // val1: S64 = return_constant() + // val2: S64 = add_10(val1) + return + + +type :: a_type \ No newline at end of file diff --git a/programs/main.kl b/programs/main.kl new file mode 100644 index 0000000..3a91ce3 --- /dev/null +++ b/programs/main.kl @@ -0,0 +1,9 @@ +#import "base.kl" +os :: #import "os.kl" + +main :: (argc: int, argv: **char): int + memory := os.reserve(size = 10000) + os.commit(&memory, 1000) + os.release(&memory) + os.print("Hello world") + diff --git a/programs/new_types.kl b/programs/new_types.kl new file mode 100644 index 0000000..6d8042e --- /dev/null +++ b/programs/new_types.kl @@ -0,0 +1,110 @@ + +unary_test :: () + int_val :: 141 + float_val :: 124.42 + conversion: F64 = -+int_val + float2 := -float_val + unsigned: S64 = -+-+-int_val + + not: Bool = !int_val + notf := !float_val + notb := !true + neg64: S64 = ~int_val + neg32: S32 = ~int_val + big_neg32: U32 = ~41512512->U32 + + var1: S64 + var2: S64 = 20 + var_bool: Bool = !var1 == !var2 + + pointer: *S64 + pointer += 10 + // pointer = pointer + pointer + + // uns: U64 = -int_val + // int_float: S64 = float_val + // string :: -"Thing" + // boolean :: -true + // var := -true + // var := +true + +// @note: Poggers +big_number1 :: 12512512512512521524242 +big_number2 :: 12512512512512521524242 +big_number3 :: 12512512512512521524242 +big_number4 :: 12512512512512521524242 +big_number5 :: 12512512512512521524242 + big_number1 * big_number1 +binary_test :: (thing: S32 = 442) + int_val :: 1000 + add :: int_val + 10 + 2.242 + 124 + mul :: 4 * 2 + bit_and :: 3 & 1 + bit_or :: 1 | 4 + bit_xor :: 8 ^ 7 + character :: 'รณ + +// ' + + boolean_equals :: true == false + boolean_var: Bool = boolean_equals + + cast_value :: 4242 + 32->S32 + 42->S32 + value: S32 = cast_value + + bvar2 := int_val > 1 + if int_val < 1 + if int_val > 1 + pass + +basic_type_assignment :: () + // custom_data1 := Custom_Data(thing = 23) + // custom_data2: Custom_Data + some_constant :: true + thing: Bool = some_constant + float_val :: 325.42 + float_var := float_val + + +for_loops :: () + for thing2 := 10 + pass + for thing1 := 10,,thing1+=1 + pass + for + pass + for i := 0, i == 4, i+=1 + pass + for j:=0, j < 10 + pass + +/* +Custom_Data :: struct + thing: S32 + +get_element_item :: (array: []Custom_Data, index: S64): Custom_Data + if index < array.len + return array[index] + else + return Custom_Data() + +get_element_pointer :: (array: []Custom_Data, index: S64): *Custom_Data + if index < array.len + return &array[index] + else + return 0 + +get_slice_len :: (array: []Custom_Data): S64 + return array.len + +array_test :: () + thing := []Custom_Data(Custom_Data(1), Custom_Data(2)) + reference := thing + length := reference.len + + item := get_element_item(thing, 1) + pointer := get_element_pointer(thing, 0) + len := get_slice_len(thing) + + with_size: [2]Custom_Data +*/ + // get_slice_len(with_size) \ No newline at end of file diff --git a/programs/order1.kl b/programs/order1.kl new file mode 100644 index 0000000..33cdc9f --- /dev/null +++ b/programs/order1.kl @@ -0,0 +1,21 @@ +order2 :: #load "order2.kl" + +recursive_lambda :: (thing: S64) + in_val := recursive_lambda + some_value := thing + const_in_lambda + +other_func :: () + a_val := recursive_lambda + +const_in_lambda :: 10 + +not_const := val + 10 +val := CONSTANT_VAL + +DEPENDENCE :: CONSTANT_VAL +CONSTANT_VAL :: 10 + +global_thing: a_type = 10 + +arena: *order2.Arena +order1_arena: order2.Arena diff --git a/programs/order2.kl b/programs/order2.kl new file mode 100644 index 0000000..0a5d575 --- /dev/null +++ b/programs/order2.kl @@ -0,0 +1,69 @@ + +Str16 :: String16 +arena_pointer: *Arena = 0 +order2_arena: Arena +no_type := order2_arena + +Arena :: struct + next: *Arena + data: *S64 + len : S64 + cap : S64 + +string16: Str16 + +String16 :: struct + data: *void + len : S64 + +thing := arena_pointer.len +with_type: Arena = order2_arena +test_dot := with_type.len +pointer := &with_type +deref := *pointer + + +test_assignments :: () + i := 0 + i += 4 + i -= 1 + i *= 2 + i /= 2 + i %= 2 + i = 2 + i &= 2 + i |= 2 + i >>= 2 + i <<= 2 + + boolean := i > 2 + CONST :: 23 == 23 + CONST_FLOAT :: 23.52 + + j: *S64 + *j = 1 + /* invalid + 8 = 32 + i + 4 = 32 + i += "String" + + */ + + +/* +for + pass +for i:=0, i < 10, i+=1 + pass +for array + pass +for it in array + pass +for it,i in array + pass +for i in 0..10 + pass + + + +*/ \ No newline at end of file diff --git a/programs/os.kl b/programs/os.kl new file mode 100644 index 0000000..14f0f62 --- /dev/null +++ b/programs/os.kl @@ -0,0 +1,44 @@ +#import "Windows.kl" +#import "base.kl" + +PAGE_SIZE :: 4096 +Memory :: struct + commit : SizeU + reserve: SizeU + data : *U8 + +reserve :: (size: SizeU): Memory + result := Memory{reserve=align_up(size, PAGE_SIZE)} + result.data = VirtualAlloc( + flProtect = PAGE_READWRITE, + dwSize = result.reserve, + flAllocationType = MEM_RESERVE, + lpAddress = 0)->*U8 + return result + +commit :: (m: *Memory, size: SizeU): Bool + commit_size := align_up(size, PAGE_SIZE) + total_commit := m.commit + commit_size + clamped_commit := clamp_top_sizeu(total_commit, m.reserve) + adjusted_commit := clamped_commit - m.commit + if adjusted_commit != 0 + result := VirtualAlloc( + lpAddress = (m.data + m.commit)->*void, + dwSize = adjusted_commit, + flAllocationType = MEM_COMMIT, + flProtect = PAGE_READWRITE, + ) + m.commit += adjusted_commit + return true + return false + +release :: (m: *Memory) + result := VirtualFree(m.data->*void, 0, MEM_RELEASE) + if result != 0 + m.data = 0 + m.commit = 0 + m.reserve = 0 + +print :: (string: String) + handle := GetStdHandle(STD_OUTPUT_HANDLE) + WriteConsoleA(handle, &string[0]->*void, length_of(string)->DWORD, 0, 0) diff --git a/programs/program.kl b/programs/program.kl new file mode 100644 index 0000000..c2ea5bb --- /dev/null +++ b/programs/program.kl @@ -0,0 +1,43 @@ +#foreign printf :: (str: String, ...) + +Token :: struct + kind: U8 + str: *U8 + len: S64 + + Kind :: enum + Number + + +kind_name :: (kind: U8): String + if kind == Token.Kind.Number + return "" + else + return "" + +is_numeric :: (c: U8): Bool + result := c >= '0 && c <= '9 + return result + +print_tokens :: (tokens: []Token) + for i := 0, i < tokens.len, i++ + t := &tokens[i] + printf("%d. %.*s", i, cast(t.len: S32), t.str) + +entry :: () + string_to_lex := "Identifier 2425525 Not_Number" + token_array: [32]Token + token_count: S64 = 0 + + t: Token + for i := 0, i < string_to_lex.len, i+=1 + if is_numeric(string_to_lex.str[i]) + t.kind = Token.Kind.Number + t.str = &string_to_lex.str[i] + t.len = i + for is_numeric(string_to_lex.str[i]) + i+=1 + t.len = i - t.len + token_array[token_count++] = t + print_tokens(token_array) +