Skip to content

Commit

Permalink
Merge pull request #45 from Kray-G/develop
Browse files Browse the repository at this point in the history
fixed #22: added JSON parser.
  • Loading branch information
Kray-G authored Nov 7, 2019
2 parents 34f51b3 + 3f7acc9 commit 01edb10
Show file tree
Hide file tree
Showing 10 changed files with 2,824 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ test.c
result.txt
timeit.dat
sample.db
kccrt/libsrc/kcc/ext_json.c
json.output
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ EXTSRC = \
src/_extdll/lib/sqlite3/sqlite3.c \
src/_extdll/lib/zip/miniz.c

all: $(TARGET)
all: ext_json.c $(TARGET)

$(TARGET): bin/bootstrap/kcc bin/bootstrap/kccbltin.so bin/bootstrap/kccjit.so bin/bootstrap/kccext.so
mkdir -p $(TARGETDIR)
Expand All @@ -93,6 +93,13 @@ $(TARGET): bin/bootstrap/kcc bin/bootstrap/kccbltin.so bin/bootstrap/kccjit.so b
cp -f bin/bootstrap/kccjit.so .
cp -f bin/bootstrap/kccext.so .

ext_json.c: myacc kccrt/libsrc/kcc/json.y
./myacc -y __json_yy -Y JSON_YY kccrt/libsrc/kcc/json.y
mv -f y.tab.c kccrt/libsrc/kcc/ext_json.c

myacc:
$(CC) $(CFLAGS) -o myacc utility/myacc.c

bin/bootstrap/kcc: bin/bootstrap/libkcc.so
@mkdir -p $(@D)
$(CC) $(MAINSRC) -o $@ -Wl,-rpath,'$$ORIGIN' -L$(@D) -lkcc
Expand Down
9 changes: 8 additions & 1 deletion Makefile.msc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ EXTOBJ = \
.SUFFIXES :
.SUFFIXES : .c .obj

all: libs $(NAME).exe $(NAME)bltin.dll $(NAME)jit.dll kccext.dll
all: ext_json.c libs $(NAME).exe $(NAME)bltin.dll $(NAME)jit.dll kccext.dll

test: all
test\test-8cc\test.cmd
Expand All @@ -104,6 +104,13 @@ test: all
test\test-picoc\test.cmd -j
test\test-picoc\csmith.cmd -j

ext_json.c: myacc.exe kccrt\libsrc\kcc\json.y
myacc -y __json_yy -Y JSON_YY kccrt\libsrc\kcc\json.y
move /y y.tab.c kccrt\libsrc\kcc\ext_json.c

myacc.exe:
$(CC) $(CFLAGS) /Femyacc.exe utility\myacc.c

libs: onig_s.lib

onig_s.lib:
Expand Down
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
6 changes: 5 additions & 1 deletion kccrt/include/_builtin.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#ifndef KCC__BUILTIN_H
#define KCC__BUILTIN_H

#define NULL ((void*)0)
typedef unsigned long long uint64_t;
#if defined(__KCC__)
typedef unsigned long size_t;
typedef int ptrdiff_t;
#endif
#if !defined(NULL)
#define NULL ((void*)0)
#endif

void __kcc_builtin_onstart(void);
void __kcc_builtin_onexit(void);
Expand Down
110 changes: 110 additions & 0 deletions kccrt/include/kcc/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#ifndef JSON_H
#define JSON_H

#include <_ext.h>
#include <stdio.h>
#include <stdint.h>
#include <kcc/ext.h>

typedef enum {
JSON_UNKNWON,
JSON_OBJECT,
JSON_ARRAY,
JSON_PAIR,
JSON_TEXT,
JSON_BOOLEAN,
JSON_INTEGER,
JSON_REAL,
JSON_NULL
} __json_type_t;

struct __json_object_;

typedef union __json_value_ {
int b; /* boolean */
int64_t i; /* integer */
double d; /* real */
string_t t; /* text */
struct __json_object_ *o; /* object/array */
} __json_value_t;

typedef struct __json_object_ {
__json_type_t type;
struct __json_object_ *link; /* for object management */
struct __json_object_ *root; /* easy to access to the root */
struct __json_object_ *mgr; /* easy to access to the manager */

struct __json_object_ *prop; /* next element of object */
struct __json_object_ *lobj; /* last element of object */
struct __json_object_ *next; /* next element of array */
struct __json_object_ *lary; /* last element of array */

string_t key; /* key if exists */
__json_value_t value; /* value of json object */
} __json_object_t;

extern __json_object_t *__json_parse(const char *str);
extern __json_object_t *__json_parse_file(const char *filename);
extern const char *__json_error_message(void);
extern void __json_pretty_print_all(__json_object_t *j);

extern __json_object_t *__json_set_top(__json_object_t *j);
extern __json_object_t *__json_gen_object(void);
extern __json_object_t *__json_append_pair(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_gen_pair(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_gen_array(void);
extern __json_object_t *__json_append_value(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_gen_double_object(double d);
extern __json_object_t *__json_gen_integer_object(int64_t i);
extern __json_object_t *__json_gen_text_object(string_t s);
extern __json_object_t *__json_make_boolean(int);
extern __json_object_t *__json_make_null(void);

/* extension of calculation */
extern __json_object_t *__json_bit_or(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_bit_xor(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_bit_and(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_add(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_sub(__json_object_t *j1, __json_object_t *j2);
extern __json_object_t *__json_mul(__json_object_t *j1, __json_object_t *j2);
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_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__)
#include <../libsrc/kcc/ext_json.c>
#endif
#endif

#endif /* JSON_H */
Loading

0 comments on commit 01edb10

Please sign in to comment.