From 963f162c4e0dfad8d744b956689b2ccd7ee1df27 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 10 Jan 2021 16:04:36 +0000 Subject: [PATCH] more code to implement a new object reader --- lib/cnode.c | 27 +++++++++++++++++++-------- lib/hcl-prv.h | 15 +++++++++++++-- lib/read.c | 2 +- lib/read2.c | 11 ++++------- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/cnode.c b/lib/cnode.c index 581c5bf..cc0c1bb 100644 --- a/lib/cnode.c +++ b/lib/cnode.c @@ -7,10 +7,10 @@ modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -61,8 +61,6 @@ hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl return c; } - - hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ooch_t* ptr, hcl_oow_t len) { hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_NUMLIT, loc, HCL_SIZEOF(*ptr) * (len + 1)); @@ -91,7 +89,6 @@ hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, const hcl_ioloc_t* loc, const hc { hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_FPDECLIT, loc, HCL_SIZEOF(*ptr) * (len + 1)); if (HCL_UNLIKELY(!c)) return HCL_NULL; - c->u.fpdeclit.ptr = (hcl_ooch_t*)(c + 1); c->u.fpdeclit.len = len; hcl_copy_oochars (c->u.fpdeclit.ptr, ptr, len); @@ -103,7 +100,6 @@ hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ { hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_STRLIT, loc, HCL_SIZEOF(*ptr) * (len + 1)); if (HCL_UNLIKELY(!c)) return HCL_NULL; - c->u.strlit.ptr = (hcl_ooch_t*)(c + 1); c->u.strlit.len = len; hcl_copy_oochars (c->u.strlit.ptr, ptr, len); @@ -111,12 +107,27 @@ hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ return c; } +hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_oow_t v) +{ + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, loc, 0); + if (HCL_UNLIKELY(!c)) return HCL_NULL; + c->u.smptrlit.v = v; + return c; +} + hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_ooi_t v) { hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, loc, 0); if (HCL_UNLIKELY(!c)) return HCL_NULL; - c->u.errlit.v = v; return c; } +hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_cnode_t* car, hcl_cnode_t* cdr) +{ + hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, loc, 0); + if (HCL_UNLIKELY(!c)) return HCL_NULL; + c->u.cons.car = car; + c->u.cons.cdr = cdr; + return c; +} \ No newline at end of file diff --git a/lib/hcl-prv.h b/lib/hcl-prv.h index 96b0416..50019e7 100644 --- a/lib/hcl-prv.h +++ b/lib/hcl-prv.h @@ -218,6 +218,7 @@ enum hcl_cnode_type_t typedef enum hcl_cnode_type_t hcl_cnode_type_t; /* NOTE: hcl_cnode_t used by the built-in compiler is not an OOP object */ +typedef struct hcl_cnode_t hcl_cnode_t; struct hcl_cnode_t { hcl_cnode_type_t type; @@ -250,12 +251,20 @@ struct hcl_cnode_t hcl_oow_t len; } fpdeclit; struct + { + hcl_oow_t v; + } smptrlit; + struct { hcl_ooi_t v; } errlit; + struct + { + hcl_cnode_t* car; + hcl_cnode_t* cdr; + } cons; } u; }; -typedef struct hcl_cnode_t hcl_cnode_t; /* NOTE: hcl_cframe_t used by the built-in compiler is not an OOP object */ struct hcl_cframe_t @@ -968,7 +977,7 @@ hcl_oop_t hcl_divints ( hcl_t* hcl, hcl_oop_t x, hcl_oop_t y, - int modulo, + int modulo, hcl_oop_t* rem ); @@ -1236,7 +1245,9 @@ hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ooch_t* ptr, hcl_oow_t len); hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ooch_t* ptr, hcl_oow_t len); hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, const hcl_ioloc_t* loc, const hcl_ooch_t* ptr, hcl_oow_t len); +hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_oow_t v); hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_ooi_t v); +hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, const hcl_ioloc_t* loc, hcl_cnode_t* car, hcl_cnode_t* cdr); #if defined(__cplusplus) } diff --git a/lib/read.c b/lib/read.c index 6d09945..d2a7040 100644 --- a/lib/read.c +++ b/lib/read.c @@ -1693,7 +1693,7 @@ static hcl_oop_t chain_to_list (hcl_t* hcl, hcl_oop_t obj) hcl_pushvolat (hcl, (hcl_oop_t*)&rsa); cons = hcl_makecons(hcl, obj, hcl->_nil); hcl_popvolat (hcl); - if (!cons) return HCL_NULL; + if (HCL_UNLIKELY(!cons)) return HCL_NULL; if (HCL_IS_NIL(hcl, rsa->slot[0])) { diff --git a/lib/read2.c b/lib/read2.c index 0cbd9e9..8a8fdbe 100644 --- a/lib/read2.c +++ b/lib/read2.c @@ -1635,7 +1635,7 @@ static HCL_INLINE void clear_comma_colon_flag (hcl_t* hcl) rsa->slot[2] = HCL_SMOOI_TO_OOP(flagv); } -static hcl_oop_t chain_to_list (hcl_t* hcl, hcl_oop_t obj) +static hcl_oop_t chain_to_list (hcl_t* hcl, hcl_cnode_t* obj) { hcl_oop_oop_t rsa; int flagv; @@ -1690,9 +1690,9 @@ static hcl_oop_t chain_to_list (hcl_t* hcl, hcl_oop_t obj) } hcl_pushvolat (hcl, (hcl_oop_t*)&rsa); - cons = hcl_makecons(hcl, obj, hcl->_nil); + cons = hcl_makecnodecons(hcl, &obj->loc, obj, HCL_NULL); hcl_popvolat (hcl); - if (!cons) return HCL_NULL; + if (HCL_UNLIKELY(!cons)) return HCL_NULL; if (HCL_IS_NIL(hcl, rsa->slot[0])) { @@ -2027,7 +2027,7 @@ static int read_object (hcl_t* hcl) return -1; } - obj = HCL_SMPTR_TO_OOP(v); + cnode = hcl_makecnodesmptrlit(hcl, TOKEN_LOC(hcl), v); break; } @@ -2058,17 +2058,14 @@ static int read_object (hcl_t* hcl) break; case HCL_IOTOK_NUMLIT: - //obj = string_to_num(hcl, TOKEN_NAME(hcl), TOKEN_TYPE(hcl) == HCL_IOTOK_RADNUMLIT); cnode = hcl_makecnodenumlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME_PTR(hcl), TOKEN_NAME_LEN(hcl)); break; case HCL_IOTOK_RADNUMLIT: - //obj = string_to_num(hcl, TOKEN_NAME(hcl), TOKEN_TYPE(hcl) == HCL_IOTOK_RADNUMLIT); cnode = hcl_makecnoderadnumlit(hcl, TOKEN_LOC(hcl), TOKEN_NAME_PTR(hcl), TOKEN_NAME_LEN(hcl)); break; case HCL_IOTOK_FPDECLIT: - //obj = string_to_fpdec(hcl, TOKEN_NAME(hcl), TOKEN_LOC(hcl)); cnode = hcl_makecnodefpdeclit(hcl, TOKEN_LOC(hcl), TOKEN_NAME_PTR(hcl), TOKEN_NAME_LEN(hcl)); break;