Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
to_file.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
30
31/* JSON_TO_FILE */
33#ifdef FUN_WITH_JSON
34 Value vpretty = pop_value(vm);
35 Value any = pop_value(vm);
36 Value vpath = pop_value(vm);
38 int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0;
39 free_value(vpretty);
41 if (!path) {
43 push_value(vm, make_int(0));
44 break;
45 }
46 json_object *j = fun_to_json(&any);
47 int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN;
48 int rc = json_object_to_file_ext(path, j, flags);
49 json_object_put(j);
50 free(path);
52 push_value(vm, make_int(rc == 0 ? 1 : 0));
53#else
54 Value vpretty = pop_value(vm);
55 free_value(vpretty);
56 Value any = pop_value(vm);
58 Value vpath = pop_value(vm);
61#endif
62 break;
63}
@ OP_JSON_TO_FILE
Definition bytecode.h:170
int rc
free(vals)
Value any
Definition stringify.c:48
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
void vpath
Definition stubs.c:23
push_value(vm, make_int(0))
free_value(vpretty)
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
@ VAL_BOOL
Definition value.h:52
@ VAL_INT
Definition value.h:51
Value path
Definition write_file.c:33