Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
cast.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
18
19case OP_CAST: {
20 /* pop type then value (args pushed in this order: value, typeName) */
21 Value t = pop_value(vm);
22 Value v = pop_value(vm);
23
24 const char *tn = (t.type == VAL_STRING && t.s) ? t.s : NULL;
26
27 /* Normalize target name to lowercase into a small buffer */
28 char target[32];
29 int k = 0;
30 if (tn) {
31 const char *p = tn;
32 while (*p && k < (int)sizeof(target) - 1) {
33 char c = *p++;
34 if (c >= 'A' && c <= 'Z') c = (char)(c - 'A' + 'a');
35 target[k++] = c;
36 }
37 }
38 target[k] = '\0';
39
40 if (!tn) {
41 out = make_nil();
42 } else if (strcmp(target, "number") == 0) {
43 if (v.type == VAL_INT) {
44 out = make_int(v.i);
45 } else if (v.type == VAL_STRING) {
46 const char *s = v.s ? v.s : "";
47 const char *p = s;
48 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
49 p++;
50 char *endp = NULL;
51 long long parsed = strtoll(p, &endp, 10);
52 while (endp && (*endp == ' ' || *endp == '\t' || *endp == '\r' || *endp == '\n'))
53 endp++;
54 if (endp && *endp != '\0')
55 out = make_int(0);
56 else
57 out = make_int((int64_t)parsed);
58 } else {
59 out = make_int(0);
60 }
61 } else if (strcmp(target, "string") == 0) {
62 char *s = value_to_string_alloc(&v);
63 out = make_string(s ? s : "");
64 if (s) free(s);
65 } else if (strcmp(target, "array") == 0) {
66 if (v.type == VAL_ARRAY) {
67 out = copy_value(&v);
68 } else {
69 Value tmp = deep_copy_value(&v);
71 free_value(tmp);
72 }
73 } else if (strcmp(target, "map") == 0) {
74 if (v.type == VAL_MAP) {
75 out = copy_value(&v);
76 } else {
78 }
79 } else if (strcmp(target, "nil") == 0) {
80 out = make_nil();
81 } else if (strcmp(target, "function") == 0) {
82 out = (v.type == VAL_FUNCTION) ? copy_value(&v) : make_nil();
83 } else if (strcmp(target, "boolean") == 0) {
84 out = make_int(value_is_truthy(&v) ? 1 : 0);
85 } else {
86 out = make_nil();
87 }
88
92 break;
93}
Value out
Definition apop.c:38
@ OP_CAST
Definition bytecode.h:95
free_value(t)
Value v
Definition cast.c:22
const char * tn
Definition cast.c:24
push_value(vm, out)
char target[32]
Definition cast.c:28
int k
Definition cast.c:29
Value c
Definition load_const.c:31
free(vals)
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
const char * p
Definition read_file.c:37
uint32_t s
Definition rol.c:31
long t
Definition sleep_ms.c:32
Tagged union representing a Fun value.
Definition value.h:68
int value_is_truthy(const Value *v)
Evaluate a Value's truthiness according to Fun language rules.
Definition value.c:610
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
Value deep_copy_value(const Value *v)
Deep copy a Value, recursively copying arrays and maps.
Definition value.c:463
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142
Value copy_value(const Value *v)
Shallow copy a Value.
Definition value.c:415
@ VAL_ARRAY
Definition value.h:55
@ VAL_MAP
Definition value.h:56
@ VAL_STRING
Definition value.h:53
@ VAL_FUNCTION
Definition value.h:54
@ VAL_INT
Definition value.h:51