Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
parse.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
31
32/* JSON_PARSE */
34#ifdef FUN_WITH_JSON
35 Value text = pop_value(vm);
36 char *s = value_to_string_alloc(&text);
37 free_value(text);
38 if (!s) {
39 push_value(vm, make_nil());
40 break;
41 }
42 struct json_tokener *tok = json_tokener_new();
43 json_object *root = json_tokener_parse_ex(tok, s, (int)strlen(s));
44 enum json_tokener_error jerr = json_tokener_get_error(tok);
45 json_tokener_free(tok);
46 free(s);
47 if (jerr != json_tokener_success) {
48 push_value(vm, make_nil());
49 } else {
50 Value v = json_to_fun(root);
51 push_value(vm, v);
52 json_object_put(root);
53 }
54#else
55 /* Fallback when JSON is disabled: consume arg, push Nil */
56 Value drop = pop_value(vm);
57 free_value(drop);
59#endif
60 break;
61}
@ OP_JSON_PARSE
Definition bytecode.h:167
Value v
Definition cast.c:22
push_value(vm, make_nil())
free_value(drop)
free(vals)
uint32_t s
Definition rol.c:31
Tagged union representing a Fun value.
Definition value.h:68
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641