Merge pull request #4242 from laytan/caller-expression

add '#caller_expression'
This commit is contained in:
gingerBill
2024-09-16 17:36:46 +01:00
committed by GitHub
9 changed files with 133 additions and 13 deletions
+10
View File
@@ -2277,6 +2277,16 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
bd.name = name.text
return bd
case "caller_expression":
bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
bd.tok = tok
bd.name = name.text
if peek_token_kind(p, .Open_Paren) {
return parse_call_expr(p, bd)
}
return bd
case "location", "exists", "load", "load_directory", "load_hash", "hash", "assert", "panic", "defined", "config":
bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
bd.tok = tok
+8 -4
View File
@@ -105,9 +105,13 @@ cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
append(&t.cleanups, Internal_Cleanup{procedure, user_data, context})
}
expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
expect :: proc(t: ^T, ok: bool, msg := "", expr := #caller_expression(ok), loc := #caller_location) -> bool {
if !ok {
log.error(msg, location=loc)
if msg == "" {
log.errorf("expected %v to be true", expr, location=loc)
} else {
log.error(msg, location=loc)
}
}
return ok
}
@@ -119,10 +123,10 @@ expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_loc
return ok
}
expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location, value_expr := #caller_expression(value)) -> bool where intrinsics.type_is_comparable(T) {
ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
if !ok {
log.errorf("expected %v, got %v", expected, value, location=loc)
log.errorf("expected %v to be %v, got %v", value_expr, expected, value, location=loc)
}
return ok
}