Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
len.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
33
34case OP_LEN: {
35 Value a = pop_value(vm);
36 int len = 0;
37 if (a.type == VAL_STRING) {
38 len = (int)(a.s ? (int)strlen(a.s) : 0);
39 } else if (a.type == VAL_ARRAY) {
41 if (len < 0) len = 0;
42 } else {
43 /* Be lenient: for non-array/non-string, treat length as 0 */
44 push_value(vm, make_int(0));
45 }
48 break;
49}
Value a
Definition add.c:37
@ OP_LEN
Definition bytecode.h:84
size_t len
Definition input_line.c:102
free_value(a)
push_value(vm, make_int(len))
Tagged union representing a Fun value.
Definition value.h:68
int array_length(const Value *v)
Get the element count of an array Value.
Definition value.c:176
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_ARRAY
Definition value.h:55
@ VAL_STRING
Definition value.h:53