1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-12-20 00:29:37 +03:00
parent b6fe2ebc0b
commit 6210984c97
54 changed files with 1757 additions and 1993 deletions

View File

@@ -1,9 +1,7 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module compiler
// TODO replace with comptime code generation.
// TODO remove cJSON dependency.
// OLD: User decode_User(string js) {
@@ -27,7 +25,7 @@ fn (p mut Parser) gen_json_for_type(typ Type) {
}
// println('gen_json_for_type( $typ.name )')
// Register decoder fn
mut dec_fn := Fn {
mut dec_fn := Fn{
mod: p.mod
typ: 'Option_$typ.name'
name: js_dec_name(t)
@@ -38,19 +36,19 @@ fn (p mut Parser) gen_json_for_type(typ Type) {
}
// decode_TYPE funcs receive an actual cJSON* object to decode
// cJSON_Parse(str) call is added by the compiler
arg := Var {
arg := Var{
typ: 'cJSON*'
}
dec_fn.args << arg
p.table.register_fn(dec_fn)
// Register encoder fn
mut enc_fn := Fn {
mut enc_fn := Fn{
mod: p.mod
typ: 'cJSON*'
name: js_enc_name(t)
}
// encode_TYPE funcs receive an object to encode
enc_arg := Var {
enc_arg := Var{
typ: t
}
enc_fn.args << enc_arg
@@ -85,28 +83,20 @@ string res = tos2("");
if field.attr == 'skip' {
continue
}
name := if field.attr.starts_with('json:') {
field.attr[5..]
} else {
field.name
}
name := if field.attr.starts_with('json:') { field.attr[5..] } else { field.name }
field_type := p.table.find_type(field.typ)
_typ := field.typ.replace('*', '')
enc_name := js_enc_name(_typ)
if field.attr == 'raw' {
dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' +
'js_get(root, "$name")));\n'
} else {
dec += ' res->$field.name = tos2(cJSON_PrintUnformatted(' + 'js_get(root, "$name")));\n'
}
else {
// Now generate decoders for all field types in this struct
// need to do it here so that these functions are generated first
p.gen_json_for_type(field_type)
dec_name := js_dec_name(_typ)
if is_js_prim(_typ) {
dec += ' res->$field.name = $dec_name (js_get(' +
'root, "$name"))'
dec += ' res->$field.name = $dec_name (js_get(' + 'root, "$name"))'
}
else {
dec += ' $dec_name (js_get(root, "$name"), & (res->$field.name))'
@@ -116,16 +106,13 @@ string res = tos2("");
enc += ' cJSON_AddItemToObject(o, "$name",$enc_name (val.$field.name)); \n'
}
// cJSON_delete
//p.cgen.fns << '$dec return opt_ok(res); \n}'
// p.cgen.fns << '$dec return opt_ok(res); \n}'
p.cgen.fns << '$dec return opt_ok(res, sizeof(*res)); \n}'
p.cgen.fns << '/*enc start*/ $enc return o;}'
}
fn is_js_prim(typ string) bool {
return typ == 'int' || typ == 'string' ||
typ == 'bool' || typ == 'f32' || typ == 'f64' ||
typ == 'i8' || typ == 'i16' || typ == 'i64' ||
typ == 'u16' || typ == 'u32' || typ == 'u64'
return typ == 'int' || typ == 'string' || typ == 'bool' || typ == 'f32' || typ == 'f64' || typ == 'i8' || typ == 'i16' || typ == 'i64' || typ == 'u16' || typ == 'u32' || typ == 'u64'
}
fn (p mut Parser) decode_array(array_type string) string {