tools: implement an -s option for v ast, to skip all nodes with default values like [], {}, 0, false; with it v ast -pts examples/hello_world.v | wc -l is 36 lines

This commit is contained in:
Delyan Angelov 2025-02-05 15:53:36 +02:00
parent eecaa64e9d
commit 94a97f6173
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 279 additions and 273 deletions

View File

@ -1,114 +1,75 @@
module main
import json
import json.cjson
struct UseJson {
x int
type Node = C.cJSON
fn as_n(p &cjson.Node) &Node {
return unsafe { &Node(p) }
}
fn suppress_json_warning() {
json.encode(UseJson{})
}
// struct C.cJSON {}
fn C.cJSON_CreateObject() &C.cJSON
fn C.cJSON_CreateArray() &C.cJSON
// fn C.cJSON_CreateBool(bool) &C.cJSON
fn C.cJSON_CreateTrue() &C.cJSON
fn C.cJSON_CreateFalse() &C.cJSON
fn C.cJSON_CreateNull() &C.cJSON
// fn C.cJSON_CreateNumber() &C.cJSON
// fn C.cJSON_CreateString() &C.cJSON
fn C.cJSON_CreateRaw(&u8) &C.cJSON
fn C.cJSON_IsInvalid(voidptr) bool
fn C.cJSON_IsFalse(voidptr) bool
// fn C.cJSON_IsTrue(voidptr) bool
fn C.cJSON_IsBool(voidptr) bool
fn C.cJSON_IsNull(voidptr) bool
fn C.cJSON_IsNumber(voidptr) bool
fn C.cJSON_IsString(voidptr) bool
fn C.cJSON_IsArray(voidptr) bool
fn C.cJSON_IsObject(voidptr) bool
fn C.cJSON_IsRaw(voidptr) bool
fn C.cJSON_AddItemToObject(voidptr, &u8, voidptr)
fn C.cJSON_AddItemToArray(voidptr, voidptr)
fn C.cJSON_Delete(voidptr)
fn C.cJSON_Print(voidptr) &u8
@[inline]
fn create_object() &C.cJSON {
return C.cJSON_CreateObject()
fn as_c(p &Node) &cjson.Node {
return unsafe { &cjson.Node(p) }
}
@[inline]
fn create_array() &C.cJSON {
return C.cJSON_CreateArray()
fn create_object() &Node {
return as_n(cjson.create_object())
}
@[inline]
fn create_string(val string) &C.cJSON {
return C.cJSON_CreateString(val.str)
fn create_array() &Node {
return as_n(cjson.create_array())
}
@[inline]
fn create_number(val f64) &C.cJSON {
return C.cJSON_CreateNumber(val)
fn create_string(val string) &Node {
return as_n(cjson.create_string(val))
}
@[inline]
fn create_bool(val bool) &C.cJSON {
return C.cJSON_CreateBool(val)
fn create_number(val f64) &Node {
return as_n(cjson.create_number(val))
}
@[inline]
fn create_true() &C.cJSON {
return C.cJSON_CreateTrue()
fn create_bool(val bool) &Node {
return as_n(cjson.create_bool(val))
}
@[inline]
fn create_false() &C.cJSON {
return C.cJSON_CreateFalse()
fn create_true() &Node {
return as_n(cjson.create_true())
}
@[inline]
fn create_null() &C.cJSON {
return C.cJSON_CreateNull()
fn create_false() &Node {
return as_n(cjson.create_false())
}
@[inline]
fn create_null() &Node {
return as_n(cjson.create_null())
}
@[inline]
fn delete(b voidptr) {
C.cJSON_Delete(b)
unsafe { cjson.delete(b) }
}
@[inline]
fn add_item_to_object(obj &C.cJSON, key string, item &C.cJSON) {
C.cJSON_AddItemToObject(obj, key.str, item)
fn add_item_to_object(mut obj Node, key string, item &Node) {
mut o := unsafe { &cjson.Node(obj) }
o.add_item_to_object(key, item)
}
@[inline]
fn add_item_to_array(obj &C.cJSON, item &C.cJSON) {
C.cJSON_AddItemToArray(obj, item)
fn add_item_to_array(mut obj Node, item &Node) {
mut o := as_c(obj)
o.add_item_to_array(item)
}
fn json_print(json_ &C.cJSON) string {
s := C.cJSON_Print(json_)
return unsafe { tos3(s) }
fn json_print(mut obj Node) string {
mut o := as_c(obj)
return o.print()
}

File diff suppressed because it is too large Load Diff

View File

@ -12,6 +12,18 @@ module cjson
#flag @VEXEROOT/thirdparty/cJSON/cJSON.o
#include "cJSON.h"
@[flag]
pub enum CJsonType {
t_false
t_true
t_null
t_number
t_string
t_array
t_object
t_raw
}
@[typedef]
pub struct C.cJSON {
pub:
@ -19,7 +31,7 @@ pub:
prev &C.cJSON
child &C.cJSON // An array or object item will have a child pointer pointing to a chain of the items in the array/object
type int // The type of the item, as above
type CJsonType // The type of the item, as above
valueint int // writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead
valuedouble f64 // The item's number, if type==cJSON_Number