Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
has_key.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file has_key.c
12 * @brief Implements the OP_HAS_KEY opcode for map key checking in the VM.
13 *
14 * This file handles the OP_HAS_KEY instruction, which checks if a map contains
15 * a specific key.
16 *
17 * Behavior:
18 * - Pops key and map from stack
19 * - Pushes 1 if key exists, 0 otherwise
20 *
21 * Error Handling:
22 * - Exits if arguments wrong types
23 */
24
26 Value key = pop_value(vm);
28 if (m.type != VAL_MAP || key.type != VAL_STRING) {
29 fprintf(stderr, "HAS_KEY expects (map, string)\n");
30 exit(1);
31 }
32 int ok = map_has(&m, key.s ? key.s : "");
35 push_value(vm, make_int(ok ? 1 : 0));
36 break;
37}
@ OP_HAS_KEY
Definition bytecode.h:133
int ok
Definition contains.c:38
const char * key
Definition get_bool.c:40
push_value(vm, make_int(ok ? 1 :0))
Value m
Definition has_key.c:27
free_value(m)
int map_has(const Value *vm, const char *key)
Check whether the map contains the specified key.
Definition map.c:130
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_MAP
Definition value.h:56
@ VAL_STRING
Definition value.h:53
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200
#define exit(code)
Definition vm.c:230