Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
parse.c File Reference

VM opcode snippet for parsing a JSON string into a Fun Value. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 free_value (text)
 if (!s)
 json_tokener_free (tok)
 free (s)
 if (jerr !=json_tokener_success)
 push_value (vm, v)
 json_object_put (root)

Variables

case OP_JSON_PARSE
char * s = value_to_string_alloc(&text)
struct json_tokener * tok = json_tokener_new()
json_object * root = json_tokener_parse_ex(tok, s, (int)strlen(s))
enum json_tokener_error jerr = json_tokener_get_error(tok)
 else
 break

Detailed Description

VM opcode snippet for parsing a JSON string into a Fun Value.

Implements the OP_JSON_PARSE instruction. Expects a string (or any value convertible to string) on the stack, parses it with json-c, converts the resulting json_object to a Fun Value and pushes it.

Build gating: compiled only when FUN_WITH_JSON is enabled. Otherwise the opcode consumes its argument and pushes Nil.

Stack effect (with FUN_WITH_JSON):

  • Pops: text (any; converted to string)
  • Pushes: Value converted from JSON, or Nil on error

Errors and edge cases:

  • If allocation fails, tokenization fails, or the text is not valid JSON, the opcode pushes Nil.
  • The temporary json-c objects are released after conversion; ownership of the pushed Fun Value follows normal VM semantics.

Definition in file parse.c.

Function Documentation

◆ free()

free ( s )

◆ free_value()

free_value ( text )

◆ if() [1/2]

if ( ! s)

Definition at line 38 of file parse.c.

◆ if() [2/2]

if ( jerr ! = json_tokener_success)

Definition at line 47 of file parse.c.

◆ json_object_put()

json_object_put ( root )

◆ json_tokener_free()

json_tokener_free ( tok )

◆ push_value()

push_value ( vm ,
v  )

Variable Documentation

◆ break

break

Definition at line 60 of file parse.c.

◆ else

else
Initial value:
{
Value v
Definition cast.c:22
json_object * root
Definition from_file.c:44
static Value json_to_fun(json_object *j)
Convert a json-c object into a Fun Value.
Definition json.c:46
Tagged union representing a Fun value.
Definition value.h:68

Definition at line 49 of file parse.c.

◆ jerr

enum json_tokener_error jerr = json_tokener_get_error(tok)

Definition at line 44 of file parse.c.

◆ OP_JSON_PARSE

case OP_JSON_PARSE

Definition at line 33 of file parse.c.

◆ root

json_object* root = json_tokener_parse_ex(tok, s, (int)strlen(s))

Definition at line 43 of file parse.c.

◆ s

Definition at line 36 of file parse.c.

◆ tok

struct json_tokener* tok = json_tokener_new()

Definition at line 42 of file parse.c.