1
0
Fork 0
forked from fun/fun

Added JSON support. (0.28.0)

This commit is contained in:
Johannes Findeisen 2025-11-25 00:51:48 +01:00
commit afb0e8cd3a
16 changed files with 521 additions and 3 deletions

View file

@ -141,6 +141,10 @@ static const char *opcode_name(OpCode op) {
case OP_SHR: return "SHR";
case OP_ROTL: return "ROTL";
case OP_ROTR: return "ROTR";
case OP_JSON_PARSE: return "JSON_PARSE";
case OP_JSON_STRINGIFY: return "JSON_STRINGIFY";
case OP_JSON_FROM_FILE: return "JSON_FROM_FILE";
case OP_JSON_TO_FILE: return "JSON_TO_FILE";
case OP_PCSC_ESTABLISH: return "PCSC_ESTABLISH";
case OP_PCSC_RELEASE: return "PCSC_RELEASE";
case OP_PCSC_LIST_READERS: return "PCSC_LIST_READERS";

View file

@ -141,6 +141,12 @@ typedef enum {
OP_ROTL, // pops s, a; pushes rotl32(a, s)
OP_ROTR, // pops s, a; pushes rotr32(a, s)
// JSON (json-c)
OP_JSON_PARSE, // pops text string; pushes value (or Nil on error)
OP_JSON_STRINGIFY, // pops pretty(bool), value; pushes string
OP_JSON_FROM_FILE, // pops path string; pushes value (or Nil)
OP_JSON_TO_FILE, // pops pretty(bool), value, path; pushes 1/0
// PCSC (smart card) opcodes
OP_PCSC_ESTABLISH, // returns context id (>0) or 0
OP_PCSC_RELEASE, // pops ctx id; returns 1/0

111
src/jsonc.c Normal file
View file

@ -0,0 +1,111 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-24
*/
/* json-c helpers and VM opcode cases (included from vm.c) */
#include "value.h"
#include "vm.h"
#ifdef FUN_WITH_JSON
#include <json-c/json.h>
#include <string.h>
#endif
/* --- Conversion helpers between json-c and Fun Value --- */
#ifdef FUN_WITH_JSON
static Value json_to_fun(json_object *j) {
if (!j) return make_nil();
enum json_type t = json_object_get_type(j);
switch (t) {
case json_type_null: return make_nil();
case json_type_boolean: return make_bool(json_object_get_boolean(j));
case json_type_double: return make_float(json_object_get_double(j));
case json_type_int: return make_int((int64_t)json_object_get_int64(j));
case json_type_string: return make_string(json_object_get_string(j));
case json_type_array: {
size_t n = json_object_array_length(j);
if (n == 0) {
return make_array_from_values(NULL, 0);
}
Value *vals = (Value*)malloc(sizeof(Value) * n);
if (!vals) return make_array_from_values(NULL, 0);
for (size_t i = 0; i < n; ++i) {
json_object *item = json_object_array_get_idx(j, (int)i);
vals[i] = json_to_fun(item);
}
Value arr = make_array_from_values(vals, (int)n);
for (size_t i = 0; i < n; ++i) free_value(vals[i]);
free(vals);
return arr;
}
case json_type_object: {
Value map = make_map_empty();
json_object_object_foreach(j, key, val) {
(void)map_set(&map, key, json_to_fun(val));
}
return map;
}
default:
return make_nil();
}
}
static json_object* fun_to_json(const Value *v) {
switch (v->type) {
case VAL_NIL: return json_object_new_null();
case VAL_BOOL: return json_object_new_boolean(v->i ? 1 : 0);
case VAL_INT: return json_object_new_int64(v->i);
case VAL_FLOAT: return json_object_new_double(v->d);
case VAL_STRING: return json_object_new_string(v->s ? v->s : "");
case VAL_ARRAY: {
json_object *arr = json_object_new_array();
int n = array_length(v);
for (int i = 0; i < n; ++i) {
Value item;
if (array_get_copy(v, i, &item)) {
json_object_array_add(arr, fun_to_json(&item));
free_value(item);
} else {
json_object_array_add(arr, json_object_new_null());
}
}
return arr;
}
case VAL_MAP: {
json_object *obj = json_object_new_object();
/* We don't have an iterator API; use keys() helper */
Value keys = map_keys_array(v);
int kn = array_length(&keys);
for (int i = 0; i < kn; ++i) {
Value k;
if (!array_get_copy(&keys, i, &k)) continue;
if (k.type == VAL_STRING && k.s) {
Value val;
if (map_get_copy(v, k.s, &val)) {
json_object_object_add(obj, k.s, fun_to_json(&val));
free_value(val);
} else {
json_object_object_add(obj, k.s, json_object_new_null());
}
}
free_value(k);
}
free_value(keys);
return obj;
}
default:
/* Fallback: stringify unsupported types */
return json_object_new_string("<unsupported>");
}
}
#endif /* FUN_WITH_JSON */
/* Note: The VM opcode case handlers are included from vm/vm switch via vm/json/ops.c */

View file

@ -739,6 +739,45 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
/* JSON builtins */
if (strcmp(name, "json_parse") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_parse expects (text)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after json_parse arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_JSON_PARSE, 0);
free(name);
return 1;
}
if (strcmp(name, "json_stringify") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_stringify expects (value, pretty)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "json_stringify expects (value, pretty)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_stringify expects (value, pretty)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after json_stringify args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_JSON_STRINGIFY, 0);
free(name);
return 1;
}
if (strcmp(name, "json_from_file") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_from_file expects (path)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after json_from_file arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_JSON_FROM_FILE, 0);
free(name);
return 1;
}
if (strcmp(name, "json_to_file") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_to_file expects (path, value, pretty)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "json_to_file expects (path, value, pretty)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_to_file expects (path, value, pretty)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "json_to_file expects (path, value, pretty)"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "json_to_file expects (path, value, pretty)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after json_to_file args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_JSON_TO_FILE, 0);
free(name);
return 1;
}
/* PCSC builtins */
if (strcmp(name, "pcsc_establish") == 0) {
(*pos)++; /* '(' */

View file

@ -12,6 +12,7 @@
#include "map.c"
#include "string.c"
#include "pcsc.c"
#include "jsonc.c"
#include "vm.h"
#include "value.h"
#include <stdio.h>
@ -660,6 +661,12 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/pcsc/disconnect.c"
#include "vm/pcsc/transmit.c"
/* JSON ops (implemented in jsonc.c, included above) */
#include "vm/json/parse.c"
#include "vm/json/stringify.c"
#include "vm/json/from_file.c"
#include "vm/json/to_file.c"
#include "vm/strings/find.c"
#include "vm/strings/regex_match.c"
#include "vm/strings/regex_search.c"

View file

@ -38,6 +38,7 @@ static const char *opcode_names[] = {
"TIME_NOW_MS","CLOCK_MONO_MS","DATE_FORMAT",
"THREAD_SPAWN","THREAD_JOIN","SLEEP_MS",
"BAND","BOR","BXOR","BNOT","SHL","SHR","ROTL","ROTR",
"JSON_PARSE","JSON_STRINGIFY","JSON_FROM_FILE","JSON_TO_FILE",
"PCSC_ESTABLISH","PCSC_RELEASE","PCSC_LIST_READERS","PCSC_CONNECT","PCSC_DISCONNECT","PCSC_TRANSMIT",
"SOCK_TCP_LISTEN","SOCK_TCP_ACCEPT","SOCK_TCP_CONNECT","SOCK_SEND","SOCK_RECV","SOCK_CLOSE","SOCK_UNIX_LISTEN","SOCK_UNIX_CONNECT",
"EXIT"

30
src/vm/json/from_file.c Normal file
View file

@ -0,0 +1,30 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-24
*/
/* JSON_FROM_FILE */
case OP_JSON_FROM_FILE: {
#ifdef FUN_WITH_JSON
Value vpath = pop_value(vm);
char *path = value_to_string_alloc(&vpath);
free_value(vpath);
if (!path) { push_value(vm, make_nil()); break; }
json_object *root = json_object_from_file(path);
free(path);
if (!root) { push_value(vm, make_nil()); break; }
Value v = json_to_fun(root);
push_value(vm, v);
json_object_put(root);
#else
Value vpath = pop_value(vm); free_value(vpath);
push_value(vm, make_nil());
#endif
break;
}

38
src/vm/json/parse.c Normal file
View file

@ -0,0 +1,38 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-24
*/
/* JSON_PARSE */
case OP_JSON_PARSE: {
#ifdef FUN_WITH_JSON
Value text = pop_value(vm);
char *s = value_to_string_alloc(&text);
free_value(text);
if (!s) { push_value(vm, make_nil()); break; }
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);
json_tokener_free(tok);
free(s);
if (jerr != json_tokener_success) {
push_value(vm, make_nil());
} else {
Value v = json_to_fun(root);
push_value(vm, v);
json_object_put(root);
}
#else
/* Fallback when JSON is disabled: consume arg, push Nil */
Value drop = pop_value(vm);
free_value(drop);
push_value(vm, make_nil());
#endif
break;
}

32
src/vm/json/stringify.c Normal file
View file

@ -0,0 +1,32 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-24
*/
/* JSON_STRINGIFY */
case OP_JSON_STRINGIFY: {
#ifdef FUN_WITH_JSON
Value vpretty = pop_value(vm);
Value any = pop_value(vm);
int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0;
json_object *j = fun_to_json(&any);
int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN;
const char *js = json_object_to_json_string_ext(j, flags);
push_value(vm, make_string(js ? js : ""));
json_object_put(j);
free_value(vpretty);
free_value(any);
#else
/* Fallback: consume two args, push "null" */
Value vpretty = pop_value(vm); free_value(vpretty);
Value any = pop_value(vm); free_value(any);
push_value(vm, make_string("null"));
#endif
break;
}

37
src/vm/json/to_file.c Normal file
View file

@ -0,0 +1,37 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-24
*/
/* JSON_TO_FILE */
case OP_JSON_TO_FILE: {
#ifdef FUN_WITH_JSON
Value vpretty = pop_value(vm);
Value any = pop_value(vm);
Value vpath = pop_value(vm);
char *path = value_to_string_alloc(&vpath);
int pretty = (vpretty.type == VAL_BOOL || vpretty.type == VAL_INT) ? (vpretty.i != 0) : 0;
free_value(vpretty);
free_value(vpath);
if (!path) { free_value(any); push_value(vm, make_int(0)); break; }
json_object *j = fun_to_json(&any);
int flags = pretty ? JSON_C_TO_STRING_PRETTY : JSON_C_TO_STRING_PLAIN;
int rc = json_object_to_file_ext(path, j, flags);
json_object_put(j);
free(path);
free_value(any);
push_value(vm, make_int(rc == 0 ? 1 : 0));
#else
Value vpretty = pop_value(vm); free_value(vpretty);
Value any = pop_value(vm); free_value(any);
Value vpath = pop_value(vm); free_value(vpath);
push_value(vm, make_int(0));
#endif
break;
}