From 0050abe1905682bf43ec01bd9ae71ff5fe93a7b7 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 28 Sep 2022 10:49:48 +0200 Subject: [PATCH] Fix error where it would show wrong function in error --- core_ast.cpp | 8 ++------ core_parsing.cpp | 29 +++++++++++++++++++++++++---- core_typechecking.cpp | 17 +++++++++++++++-- core_typechecking.h | 2 ++ examples/raymarcher.kl | 20 ++++++++++++++++---- modules/math.kl | 25 +++++++------------------ 6 files changed, 67 insertions(+), 34 deletions(-) diff --git a/core_ast.cpp b/core_ast.cpp index 83b98f2..e217d87 100644 --- a/core_ast.cpp +++ b/core_ast.cpp @@ -109,12 +109,14 @@ struct Ast_Call_Item: Ast_Expr{ Intern_String resolved_name; }; +struct Ast_Lambda; struct Ast_Call: Ast_Expr{ union{ Ast_Expr *name; Ast_Expr *typespec; }; Array exprs; + Ast_Decl *resolved_decl; }; struct Ast_Var_Unpack: Ast_Expr{ @@ -259,16 +261,10 @@ enum Ast_Decl_State{ DECL_RESOLVING, }; -typedef S32 Register_Index; struct Ast_Decl: Ast{ Ast_Decl_State state; Intern_String name; - // Bytecode - void *bytecode_data_position; - Register_Index register_index; - - Ast_Scope *scope; Ast_Expr *typespec; union{ diff --git a/core_parsing.cpp b/core_parsing.cpp index 105f9b2..7c1d7da 100644 --- a/core_parsing.cpp +++ b/core_parsing.cpp @@ -30,21 +30,42 @@ print_token_context(Token *token){ print_token_line(token); } +function void +compiler_error(Token *token1, Token *token2, const char *str, ...){ + Scratch scratch; + STRING_FMT(scratch, str, string); + + printf("\n%s", string.str); + if(token1){ + if(token1->kind == TK_Error){ + printf("\nToken Error: %.*s", (int)token1->error_val.len, token1->error_val.str); + } + print_token_context(token1); + } + + if(token2){ + if(token2->kind == TK_Error){ + printf("\nToken Error: %.*s", (int)token2->error_val.len, token2->error_val.str); + } + print_token_context(token2); + } + + __debugbreak(); +} + function void compiler_error(Token *token, const char *str, ...){ Scratch scratch; STRING_FMT(scratch, str, string); - // @Note(Krzosa): Print nice error message - printf("\nError :: %s", string.str); + printf("\n%s", string.str); if(token){ if(token->kind == TK_Error){ - printf("Token Error: %.*s", (int)token->error_val.len, token->error_val.str); + printf("\nToken Error: %.*s", (int)token->error_val.len, token->error_val.str); } print_token_context(token); } - __debugbreak(); } diff --git a/core_typechecking.cpp b/core_typechecking.cpp index f0f021d..301060a 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1290,12 +1290,24 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ if(name.type->kind != TYPE_LAMBDA){ compiler_error(node->pos, "Calling %Q which is not a [Lambda]", typestring(name.type)); } + if(!name.resolved_decl){ + compiler_error(node->pos, "Internal compiler error: Failed to propagate a resolved lambda declaration from atom resolution"); + } + node->resolved_decl = name.resolved_decl; Scratch scratch; Array items = {scratch}; S64 was_name_indexed = false; S64 default_iter = 0; + /* + @warning + We only have one instance of a given Lambda type for example (a: Vec3): Vec3. + Even though we might have multiple functions with this signature. + This creates a problem cause sometimes we can refer to the wrong AST. + + @todo: array of decls in Ast_Type + */ Ast_Lambda *lambda = (Ast_Lambda *)name.type->ast; For_Named(lambda->args, lambda_arg){ assert(lambda_arg->type); @@ -1340,8 +1352,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){ } else{ - if(!lambda_arg->expr) - compiler_error(lambda_arg->pos, "Required value: %Q in lambda call was not passed", lambda_arg->name); + if(!lambda_arg->expr){ + compiler_error(node->pos, node->resolved_decl->pos, "Required value: %Q in lambda call was not passed", lambda_arg->name); + } // @note: default values are typechecked when they get resolved Ast_Call_Item *call_item = ast_call_item(lambda_arg->expr->pos, 0, 0, lambda_arg->expr); diff --git a/core_typechecking.h b/core_typechecking.h index 81a470b..dacf0f0 100644 --- a/core_typechecking.h +++ b/core_typechecking.h @@ -1,6 +1,7 @@ struct Operand{ INLINE_VALUE_FIELDS; + Ast_Decl *resolved_decl; U8 is_const : 1; U8 is_lvalue: 1; U8 pound_strict: 1; @@ -40,6 +41,7 @@ operand(Ast_Decl *decl){ result.is_const = decl->kind != AST_VAR ? true : false; result.is_lvalue= decl->kind == AST_CONST ? false : true; // Cant assign to const values result.value = decl->value; + result.resolved_decl = decl; if(decl->kind == AST_LAMBDA){ result.is_const = false; } diff --git a/examples/raymarcher.kl b/examples/raymarcher.kl index 391788f..9f580eb 100644 --- a/examples/raymarcher.kl +++ b/examples/raymarcher.kl @@ -14,6 +14,9 @@ Raymarcher_Update :: () forward := Vec3{0, 0, -1} side := Vec3_Normalize(Vec3_Cross(forward, up)) + light_pos := Vec3{0, 4, 0} + + Xf := 1 / X->F32 Yf := 1 / Y->F32 ratio := X->F32 / Y->F32 @@ -22,13 +25,14 @@ Raymarcher_Update :: () 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)}) - pos := Vec3{0, 0, 5} + pos := Vec3{0, 0, 2} t: F32 end: F32 = 100.0 hit := true + p: Vec3 for i := 0, i < 255, i+=1 - p := Vec3_Add(pos, Vec3_MulF32(dir, t)) + p = Vec3_Add(pos, Vec3_MulF32(dir, t)) distance := SphereSDF(p) if distance < Epsilon @@ -40,8 +44,16 @@ Raymarcher_Update :: () break if hit - Screen[x + y*X] = Vec3_ConvertToARGB({1, uv.y, 0}) - + normal := Vec3_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}), + }) + + diffuse := Vec3_Dot(normal, Vec3_Negate()) + + Screen[x + y*X] = Vec3_ConvertToARGB(normal) + else;; Screen[x + y*X] = 0 diff --git a/modules/math.kl b/modules/math.kl index 607f237..64f6475 100644 --- a/modules/math.kl +++ b/modules/math.kl @@ -35,24 +35,13 @@ Vec3_ConvertToARGB :: (a: Vec3): U32 result := r | g | b return result -Vec3_Dot :: (a: Vec3, b: Vec3): F32 - result := a.x*b.x + a.y*b.y + a.z*b.z - return result -Vec3_Mul :: (a: Vec3, b: Vec3): Vec3 - result := Vec3{a.x*b.x, a.y*b.y, a.z*b.z} - return result -Vec3_MulF32 :: (a: Vec3, b: F32): Vec3 - result := Vec3{a.x*b, a.y*b, a.z*b} - return result -Vec3_Add :: (a: Vec3, b: Vec3): Vec3 - result := Vec3{a.x+b.x, a.y+b.y, a.z+b.z} - return result -Vec3_Div :: (a: Vec3, b: Vec3): Vec3 - result := Vec3{a.x/b.x, a.y/b.y, a.z/b.z} - return result -Vec3_Sub :: (a: Vec3, b: Vec3): Vec3 - result := Vec3{a.x-b.x, a.y-b.y, a.z-b.z} - return result +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 +Vec3_Mul :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x*b.x, a.y*b.y, a.z*b.z} +Vec3_MulF32 :: (a: Vec3, b: F32): Vec3 ;; return Vec3{a.x*b, a.y*b, a.z*b} +Vec3_Add :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x+b.x, a.y+b.y, a.z+b.z} +Vec3_Div :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x/b.x, a.y/b.y, a.z/b.z} +Vec3_Sub :: (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