Skip to content

Commit

Permalink
Merge pull request #44 from Kray-G/feature/issue#22/support-json-parser
Browse files Browse the repository at this point in the history
fixed #22: updated for JSON parser.
  • Loading branch information
Kray-G authored Nov 7, 2019
2 parents f714b93 + 28b4ce9 commit e2ea6e1
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 84 deletions.
138 changes: 69 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,50 +277,49 @@ so it shows the fib function only.
$ kcc -J fib.c
...(omitted)...
fib
0000BCE6: 55 pushq %rbp
0000BCE7: 48 89 E5 movq %rsp, %rbp
0000BCEA: 53 pushq %rbx
0000BCEB: 41 54 pushq %r12
0000BCED: 41 55 pushq %r13
0000BCEF: 41 56 pushq %r14
0000BCF1: 48 83 EC 10 subq $16, %rsp
0000BCF5: 89 7D D8 movl %edi, -40(%rbp)
.L2343
0000BCF8: 83 7D D8 03 cmpl $3, -40(%rbp)
0000BCFC: 0F 8D 10 00 00 00 jge .L2345
.L2344
0000BD02: 8B 45 D8 movl -40(%rbp), %eax
0000BD05: 48 8D 65 E0 leaq -32(%rbp), %rsp
0000BD09: 41 5E popq %r14
0000BD0B: 41 5D popq %r13
0000BD0D: 41 5C popq %r12
0000BD0F: 5B popq %rbx
0000BD10: C9 leave
0000BD11: C3 ret
.L2345
0000BD12: 8B 45 D8 movl -40(%rbp), %eax
0000BD15: B9 02 00 00 00 movl $2, %ecx
0000BD1A: 29 C8 subl %ecx, %eax
0000BD1C: 89 C3 movl %eax, %ebx
0000BD1E: 89 DF movl %ebx, %edi
0000BD20: E8 C1 FF FF FF call fib
0000BD25: 41 89 C4 movl %eax, %r12d
0000BD28: 8B 45 D8 movl -40(%rbp), %eax
0000BD2B: B9 01 00 00 00 movl $1, %ecx
0000BD30: 29 C8 subl %ecx, %eax
0000BD32: 41 89 C5 movl %eax, %r13d
0000BD35: 44 89 EF movl %r13d, %edi
0000BD38: E8 A9 FF FF FF call fib
0000BD3D: 41 89 C6 movl %eax, %r14d
0000BD40: 44 89 F0 movl %r14d, %eax
0000BD43: 44 01 E0 addl %r12d, %eax
0000BD46: 48 8D 65 E0 leaq -32(%rbp), %rsp
0000BD4A: 41 5E popq %r14
0000BD4C: 41 5D popq %r13
0000BD4E: 41 5C popq %r12
0000BD50: 5B popq %rbx
0000BD51: C9 leave
0000BD52: C3 ret
0000C2F3: 55 pushq %rbp
0000C2F4: 48 89 E5 movq %rsp, %rbp
0000C2F7: 53 pushq %rbx
0000C2F8: 41 54 pushq %r12
0000C2FA: 41 55 pushq %r13
0000C2FC: 41 56 pushq %r14
0000C2FE: 48 83 EC 10 subq $16, %rsp
0000C302: 89 7D D8 movl %edi, -40(%rbp)
.L2453
0000C305: 83 7D D8 03 cmpl $3, -40(%rbp)
0000C309: 0F 8D 10 00 00 00 jge .L2455
.L2454
0000C30F: 8B 45 D8 movl -40(%rbp), %eax
0000C312: 48 8D 65 E0 leaq -32(%rbp), %rsp
0000C316: 41 5E popq %r14
0000C318: 41 5D popq %r13
0000C31A: 41 5C popq %r12
0000C31C: 5B popq %rbx
0000C31D: C9 leave
0000C31E: C3 ret
.L2455
0000C31F: 8B 45 D8 movl -40(%rbp), %eax
0000C322: FF C8 decl %eax
0000C324: FF C8 decl %eax
0000C326: 89 C3 movl %eax, %ebx
0000C328: 89 DF movl %ebx, %edi
0000C32A: E8 C4 FF FF FF call fib
0000C32F: 41 89 C4 movl %eax, %r12d
0000C332: 8B 45 D8 movl -40(%rbp), %eax
0000C335: FF C8 decl %eax
0000C337: 41 89 C5 movl %eax, %r13d
0000C33A: 44 89 EF movl %r13d, %edi
0000C33D: E8 B1 FF FF FF call fib
0000C342: 41 89 C6 movl %eax, %r14d
0000C345: 44 89 F0 movl %r14d, %eax
0000C348: 44 01 E0 addl %r12d, %eax
0000C34B: 48 8D 65 E0 leaq -32(%rbp), %rsp
0000C34F: 41 5E popq %r14
0000C351: 41 5D popq %r13
0000C353: 41 5C popq %r12
0000C355: 5B popq %rbx
0000C356: C9 leave
0000C357: C3 ret
...(omitted)...
```

Expand All @@ -331,30 +330,29 @@ $ kcc -X fib.c
...(omitted)...
----------------------------------------------------------------
fib
7856: enter 32
.L2295
7857: push(32) 3 (0x3)
7858: push [BP-24] : n(i32)
7859: gt (i32)
7860: jnz * +15 <.L2296>
.L2297
7861: push [BP-24] : n(i32)
7862: push(32) 2 (0x2)
7863: sub (i32)
7864: call * 7856 <fib>
7865: cleanup (8)
7866: pop [BP+8] : .t849(i32)
7867: push [BP-24] : n(i32)
7868: push(32) 1 (0x1)
7869: sub (i32)
7870: call * 7856 <fib>
7871: cleanup (8)
7872: push [BP+8] : .t849(i32)
7873: add (i32)
7874: ret (4)
.L2296
7875: push [BP-24] : n(i32)
7876: ret (4)
7979: enter 32
.L2405
7980: push(32) 3 (0x3)
7981: push [BP-24] : n(i32)
7982: gt (i32)
7983: jnz * +14 <.L2406>
.L2407
7984: push [BP-24] : n(i32)
7985: dec (i32)
7986: dec (i32)
7987: call * 7979 <fib>
7988: cleanup (8)
7989: pop [BP+8] : .t945(i32)
7990: push [BP-24] : n(i32)
7991: dec (i32)
7992: call * 7979 <fib>
7993: cleanup (8)
7994: push [BP+8] : .t945(i32)
7995: add (i32)
7996: ret (4)
.L2406
7997: push [BP-24] : n(i32)
7998: ret (4)
...(omitted)...
```

Expand All @@ -366,9 +364,11 @@ I have a plan to do the followings when I have a time.

* [ ] Providing all of Standard C Library.
* [ ] Adding a library of XML Parser.
* [ ] Adding a library of JSON Parser.
* [ ] Adding a library with libCurl.
* [ ] Supporting encryption of Zip/Unzip.
* [ ] Adding [JSONPath][] for JSON parser.

[JSONPath]: http://goessner.net/articles/JsonPath/

## License

Expand Down
34 changes: 28 additions & 6 deletions kccrt/include/kcc/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,35 @@ extern __json_object_t *__json_div(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_mod(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_neg(__json_object_t *j);

/* accessors */
extern int __json_get_type(__json_object_t *j);
extern __json_object_t *__json_get_property(__json_object_t *j, const char *key);
extern int __json_get_property_count(__json_object_t *j);
extern __json_object_t *__json_get_element(__json_object_t *j, int index);
extern int __json_get_element_count(__json_object_t *j);
extern int __json_get_boolean(__json_object_t *j);
extern int64_t __json_get_integer(__json_object_t *j);
extern double __json_get_real(__json_object_t *j);
extern string_t* __json_get_key(__json_object_t *j);
extern string_t* __json_get_string(__json_object_t *j);

typedef __json_object_t json_object_t;
#define json_yyin __json_yyin
#define json_parse __json_parse
#define json_parse_file __json_parse_file
#define json_pretty_print __json_pretty_print_all
#define json_free_all __json_free_all
#define json_error_message __json_error_message
#define json_yyin __json_yyin
#define json_parse __json_parse
#define json_parse_file __json_parse_file
#define json_pretty_print __json_pretty_print_all
#define json_free_all __json_free_all
#define json_error_message __json_error_message

#define json_get_property __json_get_property
#define json_get_property_count __json_get_property_count
#define json_get_element __json_get_element
#define json_get_element_count __json_get_element_count
#define json_get_boolean __json_get_boolean
#define json_get_integer __json_get_integer
#define json_get_real __json_get_real
#define json_get_key __json_get_key
#define json_get_string __json_get_string

#ifndef KCC_NO_IMPORT
#if defined(__KCC_JIT__) || defined(__KCC__)
Expand Down
112 changes: 108 additions & 4 deletions kccrt/libsrc/kcc/json.y
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,9 @@ static void __json_pretty_print(__json_object_t *j, int indent, int comma, int c
if (n) {
printf("\n");
while (n) {
__json_object_t *prop = n->prop;
__json_pretty_print(n, indent+1, prop ? 1 : 0, 0);
n = prop;
__json_object_t *next = n->prop;
__json_pretty_print(n, indent+1, next ? 1 : 0, 0);
n = next;
}
__json_print_indent(indent);
}
Expand Down Expand Up @@ -1000,7 +1000,7 @@ __json_object_t *__json_parse_file(const char *filename)
return NULL;
}

const char *__json_status_message(void)
const char *__json_error_message(void)
{
if (__json_status) {
static char buf[256] = {0};
Expand All @@ -1009,3 +1009,107 @@ const char *__json_status_message(void)
}
return "No errors.";
}

int __json_get_type(__json_object_t *j)
{
return j ? j->type : JSON_UNKNWON;
}

__json_object_t *__json_get_property(__json_object_t *j, const char *key)
{
if (j && j->type == JSON_OBJECT) {
json_object_t *n = j->prop;
while (n) {
if (strcmp(n->key.cstr, key) == 0) {
return n;
}
n = n->prop;
}
}
return NULL;
}

int __json_get_property_count(__json_object_t *j)
{
int count = 0;
if (j && j->type == JSON_OBJECT) {
json_object_t *n = j->prop;
while (n) {
++count;
n = n->prop;
}
}
return count;
}

__json_object_t *__json_get_element(__json_object_t *j, int index)
{
if (j && j->type == JSON_ARRAY) {
json_object_t *n = j->next;
for (int i = 0; n; ++i) {
if (i == index) {
return n;
}
n = n->next;
}
}
return NULL;
}

int __json_get_element_count(__json_object_t *j)
{
int count = 0;
if (j && j->type == JSON_ARRAY) {
json_object_t *n = j->next;
while (n) {
++count;
n = n->next;
}
}
return count;
}

int __json_get_boolean(__json_object_t *j)
{
if (j && j->type == JSON_BOOLEAN) {
return j->value.b;
}
return 0;
}

int64_t __json_get_integer(__json_object_t *j)
{
if (j && j->type == JSON_INTEGER) {
return j->value.i;
}
return 0;
}

double __json_get_real(__json_object_t *j)
{
if (j && j->type == JSON_REAL) {
return j->value.d;
}
return 0;
}

string_t* __json_get_key(__json_object_t *j)
{
if (j && j->type == JSON_PAIR) {
return &j->key;
}
return 0;
}

string_t* __json_get_string(__json_object_t *j)
{
if (j) {
if (j->type == JSON_TEXT) {
return &j->value.t;
}
if (j->type == JSON_PAIR) {
return __json_get_string(j->value.o);
}
}
return 0;
}
9 changes: 7 additions & 2 deletions samples/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ int main()
json_object_t *j = json_parse_file("samples/sample.json");
if (j) {
json_pretty_print(j);
int count = json_get_element_count(j);
printf("element count = %d\n", count);
json_object_t *e = json_get_element(j, 1);
json_object_t *p = json_get_property(e, "path");
string_t *s = json_get_string(p);
printf("json[1].path = %s\n", s ? s->cstr : "<not found..>");
json_free_all(j);
} else {
printf("Parse Error: %s\n", __json_status_message());
printf("Parse Error: %s\n", json_error_message());
}
return 0;
}

13 changes: 10 additions & 3 deletions samples/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
"path": "/Applications"
},
{
"x": 130,
"y": 150,
"type": "file"
"p1": {
"x": 130,
"y": 150,
"type": "file"
},
"p2": {
"x": 230,
"y": 90,
"type": "directory"
}
}
]

0 comments on commit e2ea6e1

Please sign in to comment.