Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
text.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 text.c
12 * @brief VM opcode snippet for retrieving the concatenated text of an XML node.
13 *
14 * Included by vm.c; implements OP_XML_TEXT.
15 *
16 * Opcode: OP_XML_TEXT
17 * - Stack effect: pop (int node_handle) → push (string text)
18 * - Behavior: Retrieves the node's textual content as produced by
19 * xmlNodeGetContent(), which concatenates all descendant text nodes.
20 * If the node is invalid or has no textual content, an empty string is
21 * pushed.
22 * - Gating: If FUN_WITH_XML2 is disabled, the input is discarded and an empty
23 * string is pushed.
24 *
25 * Notes
26 * - The returned string is copied into a VM string value; libxml2 buffers are
27 * freed immediately after use.
28 *
29 * Example
30 * - stack: [ node_handle ]
31 * - OP_XML_TEXT → [ "Hello world" ]
32 */
33
34/* OP_XML_TEXT: pops node handle; pushes string (concatenate text node children) */
36#ifdef FUN_WITH_XML2
37 Value vh = pop_value(vm);
38 int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
39 xmlNodePtr n = xml_node_get(h);
41 if (!n) {
42 push_value(vm, make_string(""));
43 break;
44 }
45 xmlChar *content = xmlNodeGetContent(n);
46 if (!content) {
47 push_value(vm, make_string(""));
48 break;
49 }
50 push_value(vm, make_string((const char *)content));
52#else
53 Value drop = pop_value(vm);
54 free_value(drop);
55 push_value(vm, make_string(""));
56#endif
57 break;
58}
@ OP_XML_TEXT
Definition bytecode.h:217
CURL * h
Definition download.c:59
Value vh
Definition get_bool.c:38
int n
Definition insert.c:41
Tagged union representing a Fun value.
Definition value.h:68
xmlChar * content
Definition text.c:45
free_value(vh)
xmlFree(content)
push_value(vm, make_string((const char *) content))
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
static xmlNodePtr xml_node_get(int h)
Retrieve a registered xmlNode by handle.
Definition xml2.c:120