Fix error where it would show wrong function in error

This commit is contained in:
Krzosa Karol
2022-09-28 10:49:48 +02:00
parent 1fa5e1a26b
commit 0050abe190
6 changed files with 67 additions and 34 deletions

View File

@@ -109,12 +109,14 @@ struct Ast_Call_Item: Ast_Expr{
Intern_String resolved_name; Intern_String resolved_name;
}; };
struct Ast_Lambda;
struct Ast_Call: Ast_Expr{ struct Ast_Call: Ast_Expr{
union{ union{
Ast_Expr *name; Ast_Expr *name;
Ast_Expr *typespec; Ast_Expr *typespec;
}; };
Array<Ast_Call_Item *> exprs; Array<Ast_Call_Item *> exprs;
Ast_Decl *resolved_decl;
}; };
struct Ast_Var_Unpack: Ast_Expr{ struct Ast_Var_Unpack: Ast_Expr{
@@ -259,16 +261,10 @@ enum Ast_Decl_State{
DECL_RESOLVING, DECL_RESOLVING,
}; };
typedef S32 Register_Index;
struct Ast_Decl: Ast{ struct Ast_Decl: Ast{
Ast_Decl_State state; Ast_Decl_State state;
Intern_String name; Intern_String name;
// Bytecode
void *bytecode_data_position;
Register_Index register_index;
Ast_Scope *scope; Ast_Scope *scope;
Ast_Expr *typespec; Ast_Expr *typespec;
union{ union{

View File

@@ -30,21 +30,42 @@ print_token_context(Token *token){
print_token_line(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 function void
compiler_error(Token *token, const char *str, ...){ compiler_error(Token *token, const char *str, ...){
Scratch scratch; Scratch scratch;
STRING_FMT(scratch, str, string); STRING_FMT(scratch, str, string);
// @Note(Krzosa): Print nice error message printf("\n%s", string.str);
printf("\nError :: %s", string.str);
if(token){ if(token){
if(token->kind == TK_Error){ 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); print_token_context(token);
} }
__debugbreak(); __debugbreak();
} }

View File

@@ -1290,12 +1290,24 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
if(name.type->kind != TYPE_LAMBDA){ if(name.type->kind != TYPE_LAMBDA){
compiler_error(node->pos, "Calling %Q which is not a [Lambda]", typestring(name.type)); 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; Scratch scratch;
Array<Ast_Call_Item *> items = {scratch}; Array<Ast_Call_Item *> items = {scratch};
S64 was_name_indexed = false; S64 was_name_indexed = false;
S64 default_iter = 0; 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; Ast_Lambda *lambda = (Ast_Lambda *)name.type->ast;
For_Named(lambda->args, lambda_arg){ For_Named(lambda->args, lambda_arg){
assert(lambda_arg->type); assert(lambda_arg->type);
@@ -1340,8 +1352,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context){
} }
else{ else{
if(!lambda_arg->expr) if(!lambda_arg->expr){
compiler_error(lambda_arg->pos, "Required value: %Q in lambda call was not passed", lambda_arg->name); 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 // @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); Ast_Call_Item *call_item = ast_call_item(lambda_arg->expr->pos, 0, 0, lambda_arg->expr);

View File

@@ -1,6 +1,7 @@
struct Operand{ struct Operand{
INLINE_VALUE_FIELDS; INLINE_VALUE_FIELDS;
Ast_Decl *resolved_decl;
U8 is_const : 1; U8 is_const : 1;
U8 is_lvalue: 1; U8 is_lvalue: 1;
U8 pound_strict: 1; U8 pound_strict: 1;
@@ -40,6 +41,7 @@ operand(Ast_Decl *decl){
result.is_const = decl->kind != AST_VAR ? true : false; result.is_const = decl->kind != AST_VAR ? true : false;
result.is_lvalue= decl->kind == AST_CONST ? false : true; // Cant assign to const values result.is_lvalue= decl->kind == AST_CONST ? false : true; // Cant assign to const values
result.value = decl->value; result.value = decl->value;
result.resolved_decl = decl;
if(decl->kind == AST_LAMBDA){ if(decl->kind == AST_LAMBDA){
result.is_const = false; result.is_const = false;
} }

View File

@@ -14,6 +14,9 @@ Raymarcher_Update :: ()
forward := Vec3{0, 0, -1} forward := Vec3{0, 0, -1}
side := Vec3_Normalize(Vec3_Cross(forward, up)) side := Vec3_Normalize(Vec3_Cross(forward, up))
light_pos := Vec3{0, 4, 0}
Xf := 1 / X->F32 Xf := 1 / X->F32
Yf := 1 / Y->F32 Yf := 1 / Y->F32
ratio := X->F32 / 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 := Vec3{x->F32 * Xf * 2 - 1, y->F32 * Yf * 2 - 1, 1.0}
uv.x *= ratio uv.x *= ratio
dir := Vec3_Normalize(Vec3{Vec3_Dot(side, uv), Vec3_Dot(up, uv), Vec3_Dot(forward, uv)}) 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 t: F32
end: F32 = 100.0 end: F32 = 100.0
hit := true hit := true
p: Vec3
for i := 0, i < 255, i+=1 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) distance := SphereSDF(p)
if distance < Epsilon if distance < Epsilon
@@ -40,7 +44,15 @@ Raymarcher_Update :: ()
break break
if hit 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 else;; Screen[x + y*X] = 0

View File

@@ -35,24 +35,13 @@ Vec3_ConvertToARGB :: (a: Vec3): U32
result := r | g | b result := r | g | b
return result return result
Vec3_Dot :: (a: Vec3, b: Vec3): F32 Vec3_Negate :: (a: Vec3): Vec3 ;; return Vec3{-a.x, -a.y, -a.z}
result := a.x*b.x + a.y*b.y + a.z*b.z Vec3_Dot :: (a: Vec3, b: Vec3): F32 ;; return a.x*b.x + a.y*b.y + a.z*b.z
return result Vec3_Mul :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x*b.x, a.y*b.y, a.z*b.z}
Vec3_Mul :: (a: Vec3, b: Vec3): Vec3 Vec3_MulF32 :: (a: Vec3, b: F32): Vec3 ;; return Vec3{a.x*b, a.y*b, a.z*b}
result := Vec3{a.x*b.x, a.y*b.y, a.z*b.z} Vec3_Add :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x+b.x, a.y+b.y, a.z+b.z}
return result Vec3_Div :: (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 Vec3_Sub :: (a: Vec3, b: Vec3): Vec3 ;; return Vec3{a.x-b.x, a.y-b.y, a.z-b.z}
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
F32_Clamp :: (min: F32, value: F32, max: F32): F32 F32_Clamp :: (min: F32, value: F32, max: F32): F32
if value > max;; return max if value > max;; return max