use actual type for code generation instead of json type

This commit is contained in:
overflowerror 2021-05-04 15:55:07 +02:00
parent 7c433f61b5
commit 756ead18ab
4 changed files with 131 additions and 14 deletions

View file

@ -52,10 +52,30 @@ void _registerMarshaller(int namesCount, const char** names, jsonValue_t* (*mars
}
}
static jsonValue_t* json_marshall_char(void* value) {
return json_long(*((char*) value));
}
static jsonValue_t* json_marshall_short(void* value) {
return json_long(*((short*) value));
}
static jsonValue_t* json_marshall_int(void* value) {
return json_long(*((int*) value));
}
static jsonValue_t* json_marshall_long(void* value) {
return json_long(*((long*) value));
}
static jsonValue_t* json_marshall_long_long(void* value) {
return json_long(*((long long*) value));
}
static jsonValue_t* json_marshall_float(void* value) {
return json_long(*((float*) value));
}
static jsonValue_t* json_marshall_double(void* value) {
return json_long(*((double*) value));
}
@ -65,14 +85,24 @@ static jsonValue_t* json_marshall_string(void* value) {
}
static jsonValue_t* json_marshall_bool(void* value) {
return json_bool((const char*) value);
return json_bool(*((bool*) value));
}
jsonValue_t* _json_marshall_value(const char* type, void* value) {
if (value == NULL) {
return json_null();
} else if (strcmp(type, "char") == 0) {
return json_marshall_char(value);
} else if (strcmp(type, "short") == 0) {
return json_marshall_short(value);
} else if (strcmp(type, "int") == 0) {
return json_marshall_int(value);
} else if (strcmp(type, "long") == 0) {
return json_marshall_long(value);
} else if (strcmp(type, "long long") == 0) {
return json_marshall_long_long(value);
} else if (strcmp(type, "float") == 0) {
return json_marshall_float(value);
} else if (strcmp(type, "double") == 0) {
return json_marshall_double(value);
} else if (strcmp(type, "string") == 0) {
@ -91,10 +121,58 @@ char* _json_marshall(const char* type, void* value) {
return json_stringify(_json_marshall_value(type, value));
}
static void* json_unmarshall_char(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;
char* tmp = malloc(sizeof(char));
if (tmp == NULL)
return NULL;
*tmp = value->value.integer;
return tmp;
}
static void* json_unmarshall_short(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;
short* tmp = malloc(sizeof(short));
if (tmp == NULL)
return NULL;
*tmp = value->value.integer;
return tmp;
}
static void* json_unmarshall_int(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;
int* tmp = malloc(sizeof(int));
if (tmp == NULL)
return NULL;
*tmp = value->value.integer;
return tmp;
}
static void* json_unmarshall_long(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;
long* tmp = malloc(sizeof(long));
if (tmp == NULL)
return NULL;
*tmp = value->value.integer;
return tmp;
}
static void* json_unmarshall_long_long(jsonValue_t* value) {
if (value->type != JSON_LONG)
return NULL;
long long* tmp = malloc(sizeof(long long));
if (tmp == NULL)
return NULL;
@ -103,6 +181,21 @@ static void* json_unmarshall_long(jsonValue_t* value) {
return tmp;
}
static void* json_unmarshall_float(jsonValue_t* value) {
if (value->type != JSON_DOUBLE && value->type != JSON_LONG)
return NULL;
float* tmp = malloc(sizeof(float));
if (tmp == NULL)
return NULL;
if (value->type == JSON_DOUBLE)
*tmp = value->value.real;
else
*tmp = value->value.integer;
return tmp;
}
static void* json_unmarshall_double(jsonValue_t* value) {
if (value->type != JSON_DOUBLE && value->type != JSON_LONG)
return NULL;
@ -142,8 +235,18 @@ static void* json_unmarshall_string(jsonValue_t* value) {
void* _json_unmarshall_value(const char* type, jsonValue_t* value) {
if (value->type == JSON_NULL) {
return NULL;
} else if (strcmp(type, "char") == 0) {
return json_unmarshall_char(value);
} else if (strcmp(type, "short") == 0) {
return json_unmarshall_short(value);
} else if (strcmp(type, "int") == 0) {
return json_unmarshall_int(value);
} else if (strcmp(type, "long") == 0) {
return json_unmarshall_long(value);
} else if (strcmp(type, "long long") == 0) {
return json_unmarshall_long_long(value);
} else if (strcmp(type, "float") == 0) {
return json_unmarshall_float(value);
} else if (strcmp(type, "double") == 0) {
return json_unmarshall_double(value);
} else if (strcmp(type, "string") == 0) {

View file

@ -116,7 +116,7 @@ char* generateUnmarshallFunction(FILE* output, struct structinfo* info, char* su
fprintf(output, "\ttmp = _json_unmarshall_value(\"%s\", json_object_get(v, \"%s\"));\n", member->type->type, member->name);
if (strcmp(member->type->type, "string") == 0) {
fprintf(output, "\td->%s = (const char*) tmp;\n", member->name);
fprintf(output, "\td->%s = (char*) tmp;\n", member->name);
} else if (member->type->isPointer) {
fprintf(output, "\td->%s = (%s*) tmp;\n", member->name, member->type->type);
} else {

View file

@ -43,7 +43,7 @@ char* prefix(char* string, const char* prefix);
%type <declarsinfo> file
%token TYPEDEF STRUCT
%token LONG DOUBLE STRING
%token CHAR SHORT INT LONG LONG_LONG FLOAT DOUBLE STRING STDINT
%token POINTER
%token SEMICOLON OPEN_BRACES CLOSE_BRACES OPEN_BRACKETS CLOSE_BRACKETS
%token <id> ID
@ -117,7 +117,13 @@ structmember: type ID
}
;
type: LONG { $$ = newTypeInfo(false, "long"); }
type: STDINT { yyerror("stdint types are not yet supported"); YYERROR; }
| CHAR { $$ = newTypeInfo(false, "char"); }
| SHORT { $$ = newTypeInfo(false, "short"); }
| INT { $$ = newTypeInfo(false, "int"); }
| LONG { $$ = newTypeInfo(false, "long"); }
| LONG_LONG { $$ = newTypeInfo(false, "long long"); }
| FLOAT { $$ = newTypeInfo(false, "float"); }
| DOUBLE { $$ = newTypeInfo(false, "double"); }
| STRING { $$ = newTypeInfo(false, "string"); }
| STRUCT ID { $$ = newTypeInfo(false, prefix($2, "struct ")); }

View file

@ -5,15 +5,17 @@ dec [0-9]+
hex [0-9a-fA-F]+H
number {dec}|{hex}
int_types "long long"{whitespace}|"long"|"int"|"byte"|"short"
unsigned_int_types "unsigend "{int_types}
signed_int_types "signed "{int_types}
stdint_types_u "uint8_t"|"uint16_t"|"uint32_t"|"uint64_t"
stdint_types_s "int8_t"|"int16_t"|"int32_t"|"int64_t"
stdint_types {stdint_types_u}|{stdint_types_s}
int {int_types}|{unsigned_int_types}|{signed_int_types}|{stdint_types_u}|{stdint_types_s}
float "long double"|"double"|"float"
char "char"
short "short"({whitespace}"int")?
int "int"
long "long"({whitespace}"int")?
long_long "long long"{whitespace}"int"?
float "float"
double "double"
string "const "?"char"{whitespace}*"*"
@ -45,10 +47,16 @@ string "const "?"char"{whitespace}*"*"
<INITIAL>"typedef" { return TYPEDEF; }
<INITIAL>"struct" { return STRUCT; }
<INITIAL>{int} { return LONG; }
<INITIAL>{float} { return DOUBLE; }
<INITIAL>{string} { return STRING; }
<INITIAL>"*" { return POINTER; }
<INITIAL>{stdint_types} { return STDINT; }
<INITIAL>{char} { return CHAR; }
<INITIAL>{short} { return SHORT; }
<INITIAL>{int} { return INT; }
<INITIAL>{long} { return LONG; }
<INITIAL>{long_long} { return LONG_LONG; }
<INITIAL>{float} { return FLOAT; }
<INITIAL>{double} { return DOUBLE; }
<INITIAL>{string} { return STRING; }
<INITIAL>"*" { return POINTER; }
<INITIAL>";" { return SEMICOLON; }
<INITIAL>"{" { return OPEN_BRACES; }