From a6f6147df3b46efefd9a6cf78f34fe06500a0a55 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 30 Sep 2022 16:36:55 +0200 Subject: [PATCH] Damn I didn't even know this language could do that, I guess an issue with namespacing fixed itself --- core_compiler.cpp | 2 +- core_globals.cpp | 2 +- core_typechecking.cpp | 2 +- examples/arrays_and_slices.core | 4 +-- examples/operator_overloading.core | 6 ++++- examples/raymarcher.core | 22 ++++++++-------- meta.py | 2 +- modules/Base.core | 6 ++--- modules/Math.core | 39 ---------------------------- modules/MathVec3.core | 41 ++++++++++++++++++++++++++++++ modules/Windows.core | 4 +-- 11 files changed, 69 insertions(+), 61 deletions(-) create mode 100644 modules/MathVec3.core diff --git a/core_compiler.cpp b/core_compiler.cpp index 29ffe96..76913c7 100644 --- a/core_compiler.cpp +++ b/core_compiler.cpp @@ -39,7 +39,7 @@ keyword_enum = l->intern("enum"_s); l->interns.first_keyword = keyword_struct.str; l->interns.last_keyword = keyword_enum.str; intern_sizeof = l->intern("SizeOf"_s); -intern_length = l->intern("Length"_s); +intern_len = l->intern("Len"_s); intern_alignof = l->intern("AlignOf"_s); intern_foreign = l->intern("foreign"_s); intern_strict = l->intern("strict"_s); diff --git a/core_globals.cpp b/core_globals.cpp index 709e564..86347e9 100644 --- a/core_globals.cpp +++ b/core_globals.cpp @@ -28,7 +28,7 @@ Intern_String keyword_else; Intern_String keyword_for; Intern_String keyword_enum; Intern_String intern_sizeof; -Intern_String intern_length; +Intern_String intern_len; Intern_String intern_alignof; Intern_String intern_foreign; Intern_String intern_strict; diff --git a/core_typechecking.cpp b/core_typechecking.cpp index aabecd8..56e7d9f 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1733,7 +1733,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ return operand_const_rvalue(v); } - else if(expr_atom_is_equal_intern(node->name, intern_length)){ + else if(expr_atom_is_equal_intern(node->name, intern_len)){ Ast_Expr *expr = unpack_ast_call_expr_for_builtin(node); Operand name = resolve_expr(expr, inherit_flag(flags, AST_CANT_BE_NULL)); diff --git a/examples/arrays_and_slices.core b/examples/arrays_and_slices.core index f4e41a4..ca2da39 100644 --- a/examples/arrays_and_slices.core +++ b/examples/arrays_and_slices.core @@ -16,7 +16,7 @@ main :: (): int static_array: [8]int // We can get size of array using Length builtin - #Assert(Length(static_array) == 8) + #Assert(Len(static_array) == 8) // Accessing values is like in C // Variables are zeroed by default @@ -40,7 +40,7 @@ main :: (): int slice: []int = static_array // We can't do a compile time Assert anymore - Assert(Length(slice) == 8) + Assert(Len(slice) == 8) Assert(slice[4] == 1) // After we loop and reassign slice values diff --git a/examples/operator_overloading.core b/examples/operator_overloading.core index 689575d..9f9491b 100644 --- a/examples/operator_overloading.core +++ b/examples/operator_overloading.core @@ -3,7 +3,11 @@ Vec3 :: struct;; x: F32; y: F32; z: F32 // We can define operator overloads for arbitrary types // these are just regular lambdas/functions -"+" :: (a: Vec3, b: Vec3): Vec3 ;; return {a.x+b.x, a.y+b.y, a.z+b.z} +"+" :: (a: Vec3, b: Vec3): Vec3 + return {a.x+b.x, a.y+b.y, a.z+b.z} + +// We can make a one liner out of these using ';;' operator +// which functions as a new line with indent "-" :: (a: Vec3, b: Vec3): Vec3 ;; return {a.x-b.x, a.y-b.y, a.z-b.z} "-" :: (a: Vec3): Vec3 ;; return {-a.x, -a.y, -a.z} diff --git a/examples/raymarcher.core b/examples/raymarcher.core index 8a192cb..b981b4a 100644 --- a/examples/raymarcher.core +++ b/examples/raymarcher.core @@ -1,4 +1,6 @@ #import "Math.core" +V3 :: #import "MathVec3.core" +Vec3 :: V3.Vec3 Epsilon :: 0.00001 Screen : *U32 @@ -8,13 +10,13 @@ TotalTime: F64 LightPos := Vec3{2,4,2} SphereSDF :: (pos: Vec3): F32 - result := Vec3_Length(pos) - 1.0 + result := V3.Length(pos) - 1.0 return result Raymarcher_Update :: () up := Vec3{0, 1, 0} forward := Vec3{0, 0, -1} - side := Vec3_Normalize(Vec3_Cross(forward, up)) + side := V3.Normalize(V3.Cross(forward, up)) LightPos.x = cosf(TotalTime->F32)*4 LightPos.y = sinf(TotalTime->F32)*4 @@ -32,7 +34,7 @@ Raymarcher_Update :: () for x := 0, x < X, x+=1 uv := Vec3{x->F32 * Xf * 2 - 1, y->F32 * Yf * 2 - 1, 1.0} uv.x *= ratio - dir := Vec3_Normalize(Vec3{Vec3_Dot(side, uv), Vec3_Dot(up, uv), Vec3_Dot(forward, uv)}) + dir := V3.Normalize(Vec3{V3.Dot(side, uv), V3.Dot(up, uv), V3.Dot(forward, uv)}) t: F32 end: F32 = 100.0 @@ -51,24 +53,24 @@ Raymarcher_Update :: () break if hit - normal := Vec3_Normalize(Vec3{ + normal := V3.Normalize(Vec3{ SphereSDF({p.x + Epsilon, p.y, p.z}) - SphereSDF({p.x - Epsilon, p.y, p.z}), SphereSDF({p.x, p.y + Epsilon, p.z}) - SphereSDF({p.x, p.y - Epsilon, p.z}), SphereSDF({p.x, p.y, p.z + Epsilon}) - SphereSDF({p.x, p.y, p.z - Epsilon}), }) - light_to_point := Vec3_Normalize(LightPos - p) - eye_to_point := Vec3_Normalize(eye - p) - reflected_light := Vec3_Normalize(Vec3_Reflect(Vec3_Negate(light_to_point), normal)) + light_to_point := V3.Normalize(LightPos - p) + eye_to_point := V3.Normalize(eye - p) + reflected_light := V3.Normalize(V3.Reflect(V3.Negate(light_to_point), normal)) ambient :: 0.2->F32 - diffuse := Vec3_Dot(normal, light_to_point) + diffuse := V3.Dot(normal, light_to_point) color := ambient_color*ambient->F32 if diffuse > Epsilon color = color + diffuse_color*diffuse - specular := Vec3_Dot(reflected_light, eye_to_point) + specular := V3.Dot(reflected_light, eye_to_point) if specular > Epsilon specular = specular*specular*specular*specular color = color + specular_color*specular*0.2->F32 @@ -78,7 +80,7 @@ Raymarcher_Update :: () color.x = sqrtf(color.x) color.y = sqrtf(color.y) color.z = sqrtf(color.z) - Screen[x + y*X] = Vec3_ConvertToARGB(color) + Screen[x + y*X] = V3.ConvertToARGB(color) else;; Screen[x + y*X] = 0 diff --git a/meta.py b/meta.py index 585f28b..c9082bc 100644 --- a/meta.py +++ b/meta.py @@ -117,7 +117,7 @@ keywords = [ interns = [ "SizeOf", - "Length", + "Len", "AlignOf", "foreign", "strict", diff --git a/modules/Base.core b/modules/Base.core index e808856..862b8bf 100644 --- a/modules/Base.core +++ b/modules/Base.core @@ -74,10 +74,10 @@ Utf32ToUtf16 :: (codepoint: U32): [2]U16, S64 StringToString16 :: (arena: *Arena, in: String): String16 in_str := &in[0] // @Note(Krzosa): Should be more then enough space - alloc_size := (Length(in)*2)+1 + alloc_size := (Len(in)*2)+1 result := String16{str = PushSize(arena, alloc_size->U64)} - for i := 0, i < Length(in) - s32, s32_len := Utf8ToUtf32(in_str + i, Length(in) - i) + for i := 0, i < Len(in) + s32, s32_len := Utf8ToUtf32(in_str + i, Len(in) - i) if s32_len != 0 i += s32_len s16, s16_len := Utf32ToUtf16(s32) diff --git a/modules/Math.core b/modules/Math.core index 334d77e..57fe6a5 100644 --- a/modules/Math.core +++ b/modules/Math.core @@ -4,45 +4,6 @@ sinf :: #foreign (value: F32): F32 Vec2I :: struct;; x: S64; y: S64 Vec2 :: struct;; x: F32; y: F32 -Vec3 :: struct;; x: F32; y: F32; z: F32 - -Vec3_Cross :: (a: Vec3, b: Vec3): Vec3 - result := Vec3{ - a.y * b.z - a.z * b.y, - a.z * b.x - a.x * b.z, - a.x * b.y - a.y * b.x, - } - return result - -Vec3_Normalize :: (a: Vec3): Vec3 - length := Vec3_Length(a) - result := a / length - return result - -Vec3_Reflect :: (a: Vec3, normal: Vec3): Vec3 - an := Vec3_Dot(a, normal)*2 - result := a - a * an - return result - -Vec3_ConvertToARGB :: (a: Vec3): U32 - a.x = F32_Clamp(0, a.x, 1) - a.y = F32_Clamp(0, a.y, 1) - a.z = F32_Clamp(0, a.z, 1) - r := (a.x * 255)->U32 << 16 - g := (a.y * 255)->U32 << 8 - b := (a.z * 255)->U32 << 0 - result := r | g | b - return result - -Vec3_Length :: (a: Vec3): F32 ;; return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z) -Vec3_Negate :: (a: Vec3): Vec3 ;; return Vec3{-a.x, -a.y, -a.z} -Vec3_Dot :: (a: Vec3, b: Vec3): F32 ;; return a.x*b.x + a.y*b.y + a.z*b.z -"*" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x*b.x, a.y*b.y, a.z*b.z} -"*" :: (a: Vec3, b: F32) : Vec3 ;; return Vec3{a.x*b, a.y*b, a.z*b} -"+" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x+b.x, a.y+b.y, a.z+b.z} -"/" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x/b.x, a.y/b.y, a.z/b.z} -"/" :: (a: Vec3, b: F32): Vec3 ;; return Vec3{a.x/b, a.y/b, a.z/b} -"-" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x-b.x, a.y-b.y, a.z-b.z} F32_Clamp :: (min: F32, value: F32, max: F32): F32 if value > max;; return max diff --git a/modules/MathVec3.core b/modules/MathVec3.core new file mode 100644 index 0000000..5667540 --- /dev/null +++ b/modules/MathVec3.core @@ -0,0 +1,41 @@ +#import "Math.core" + +Vec3 :: struct;; x: F32; y: F32; z: F32 + +Cross :: (a: Vec3, b: Vec3): Vec3 + result := Vec3{ + a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x, + } + return result + +Normalize :: (a: Vec3): Vec3 + length := Length(a) + result := a / length + return result + +Reflect :: (a: Vec3, normal: Vec3): Vec3 + an := Dot(a, normal)*2 + result := a - a * an + return result + +ConvertToARGB :: (a: Vec3): U32 + a.x = F32_Clamp(0, a.x, 1) + a.y = F32_Clamp(0, a.y, 1) + a.z = F32_Clamp(0, a.z, 1) + r := (a.x * 255)->U32 << 16 + g := (a.y * 255)->U32 << 8 + b := (a.z * 255)->U32 << 0 + result := r | g | b + return result + +Length :: (a: Vec3): F32 ;; return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z) +Negate :: (a: Vec3): Vec3 ;; return Vec3{-a.x, -a.y, -a.z} +Dot :: (a: Vec3, b: Vec3): F32 ;; return a.x*b.x + a.y*b.y + a.z*b.z +"*" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x*b.x, a.y*b.y, a.z*b.z} +"*" :: (a: Vec3, b: F32) : Vec3 ;; return Vec3{a.x*b, a.y*b, a.z*b} +"+" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x+b.x, a.y+b.y, a.z+b.z} +"/" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x/b.x, a.y/b.y, a.z/b.z} +"/" :: (a: Vec3, b: F32): Vec3 ;; return Vec3{a.x/b, a.y/b, a.z/b} +"-" :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x-b.x, a.y-b.y, a.z-b.z} diff --git a/modules/Windows.core b/modules/Windows.core index d26705e..c4f386f 100644 --- a/modules/Windows.core +++ b/modules/Windows.core @@ -101,9 +101,9 @@ Print :: (string: String, args: ..) buffer_len: S64 arg_counter := 0 - for i := 0, i < Length(string), i+=1 + for i := 0, i < Len(string), i+=1 if string[i] == '%' - Assert(arg_counter < Length(args), "Passing too many [%] to a print lambda") + Assert(arg_counter < Len(args), "Passing too many [%] to a print lambda") arg := args[arg_counter++] if arg.type == S64