2021-01-06 09:37:29 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright (c) 2016-2018 Chung, Hyung-Hwan. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
2021-01-12 00:21:43 +00:00
|
|
|
notice, this list of conditions and the following disclaimer.
|
2021-01-06 09:37:29 +00:00
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
2021-01-12 00:21:43 +00:00
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
2021-01-06 09:37:29 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-01-09 16:53:35 +00:00
|
|
|
#include "hcl-prv.h"
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
static hcl_cnode_t* make_cnode (hcl_t* hcl, hcl_cnode_type_t type, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
|
|
|
hcl_cnode_t* cnode;
|
2021-01-19 14:07:42 +00:00
|
|
|
hcl_oocs_t empty;
|
|
|
|
hcl_ooch_t dummy;
|
2021-01-06 09:37:29 +00:00
|
|
|
|
2023-11-09 15:03:03 +00:00
|
|
|
if (!tok)
|
2021-01-19 14:07:42 +00:00
|
|
|
{
|
|
|
|
empty.ptr = &dummy;
|
|
|
|
empty.len = 0;
|
|
|
|
tok = ∅
|
|
|
|
}
|
|
|
|
cnode = hcl_callocmem(hcl, HCL_SIZEOF(*cnode) + HCL_SIZEOF(*tok->ptr) * (tok->len + 1));
|
2021-01-06 09:37:29 +00:00
|
|
|
if (HCL_UNLIKELY(!cnode)) return HCL_NULL;
|
|
|
|
|
2021-01-19 14:07:42 +00:00
|
|
|
cnode->cn_type = type;
|
|
|
|
cnode->cn_loc = *loc;
|
|
|
|
|
|
|
|
cnode->cn_tok.ptr = (hcl_ooch_t*)(cnode + 1);
|
|
|
|
cnode->cn_tok.len = tok->len;
|
|
|
|
hcl_copy_oochars (cnode->cn_tok.ptr, tok->ptr, tok->len);
|
|
|
|
cnode->cn_tok.ptr[tok->len] = '\0';
|
|
|
|
|
2021-01-06 09:37:29 +00:00
|
|
|
return cnode;
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodenil (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_NIL, loc, tok);
|
2021-01-06 09:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodetrue (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_TRUE, loc, tok);
|
2021-01-06 09:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodefalse (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_FALSE, loc, tok);
|
2021-01-06 09:37:29 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodeself (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2022-02-05 17:35:10 +00:00
|
|
|
{
|
|
|
|
return make_cnode(hcl, HCL_CNODE_SELF, loc, tok);
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodesuper (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2022-02-18 16:32:19 +00:00
|
|
|
{
|
|
|
|
return make_cnode(hcl, HCL_CNODE_SUPER, loc, tok);
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodeellipsis (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-05-07 08:53:01 +00:00
|
|
|
{
|
|
|
|
return make_cnode(hcl, HCL_CNODE_ELLIPSIS, loc, tok);
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodetrpcolons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-05-09 15:32:54 +00:00
|
|
|
{
|
|
|
|
return make_cnode(hcl, HCL_CNODE_TRPCOLONS, loc, tok);
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodedcstar (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2022-02-24 16:47:26 +00:00
|
|
|
{
|
|
|
|
return make_cnode(hcl, HCL_CNODE_DCSTAR, loc, tok);
|
|
|
|
}
|
2021-05-09 15:32:54 +00:00
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodecharlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, const hcl_ooch_t v)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CHARLIT, loc, tok);
|
2021-01-06 09:37:29 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
2021-01-19 14:07:42 +00:00
|
|
|
c->u.charlit.v = v;
|
2021-01-06 09:37:29 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodesymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-12 09:06:25 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SYMBOL, loc, tok);
|
2021-01-12 09:06:25 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
2021-01-19 14:07:42 +00:00
|
|
|
c->u.symbol.syncode = hcl_getsyncodebyoocs_noseterr(hcl, tok);
|
2021-01-12 09:06:25 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodedsymbol (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-17 17:45:39 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_DSYMBOL, loc, tok);
|
2021-01-17 17:45:39 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodestrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-12 09:06:25 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_STRLIT, loc, tok);
|
2021-01-12 09:06:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodenumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-09 16:53:35 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_NUMLIT, loc, tok);
|
2021-01-09 16:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnoderadnumlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-09 16:53:35 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_RADNUMLIT, loc, tok);
|
2021-01-09 16:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodefpdeclit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok)
|
2021-01-09 16:53:35 +00:00
|
|
|
{
|
2021-01-19 14:07:42 +00:00
|
|
|
return make_cnode(hcl, HCL_CNODE_FPDECLIT, loc, tok);
|
2021-01-09 16:53:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodesmptrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_oow_t v)
|
2021-01-06 09:37:29 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SMPTRLIT, loc, tok);
|
2021-01-06 09:37:29 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
2021-01-10 16:04:36 +00:00
|
|
|
c->u.smptrlit.v = v;
|
|
|
|
return c;
|
|
|
|
}
|
2021-01-06 09:37:29 +00:00
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodeerrlit (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_ooi_t v)
|
2021-01-10 16:04:36 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ERRLIT, loc, tok);
|
2021-01-10 16:04:36 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
2021-01-06 09:37:29 +00:00
|
|
|
c->u.errlit.v = v;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodecons (hcl_t* hcl, const hcl_loc_t* loc, const hcl_oocs_t* tok, hcl_cnode_t* car, hcl_cnode_t* cdr)
|
2021-01-10 16:04:36 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_CONS, loc, tok);
|
2021-01-10 16:04:36 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
|
|
|
c->u.cons.car = car;
|
|
|
|
c->u.cons.cdr = cdr;
|
|
|
|
return c;
|
2021-01-12 09:06:25 +00:00
|
|
|
}
|
2021-01-13 09:54:44 +00:00
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodeelist (hcl_t* hcl, const hcl_loc_t* loc, hcl_concode_t type)
|
2021-01-13 09:54:44 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_ELIST, loc, HCL_NULL);
|
2021-01-13 09:54:44 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
2021-01-25 15:23:24 +00:00
|
|
|
c->u.elist.concode = type;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2023-11-04 13:58:31 +00:00
|
|
|
hcl_cnode_t* hcl_makecnodeshell (hcl_t* hcl, const hcl_loc_t* loc, hcl_cnode_t* obj)
|
2021-01-25 15:23:24 +00:00
|
|
|
{
|
2023-11-12 12:54:17 +00:00
|
|
|
hcl_cnode_t* c = make_cnode(hcl, HCL_CNODE_SHELL, loc, HCL_NULL);
|
2021-01-25 15:23:24 +00:00
|
|
|
if (HCL_UNLIKELY(!c)) return HCL_NULL;
|
|
|
|
c->u.shell.obj = obj;
|
2021-01-13 09:54:44 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcl_freesinglecnode (hcl_t* hcl, hcl_cnode_t* c)
|
|
|
|
{
|
|
|
|
hcl_freemem (hcl, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void hcl_freecnode (hcl_t* hcl, hcl_cnode_t* c)
|
|
|
|
{
|
|
|
|
redo:
|
2021-01-19 14:07:42 +00:00
|
|
|
switch (c->cn_type)
|
2021-01-13 09:54:44 +00:00
|
|
|
{
|
|
|
|
case HCL_CNODE_CONS:
|
|
|
|
{
|
|
|
|
hcl_cnode_t* tmp1, * tmp2;
|
|
|
|
|
|
|
|
tmp1 = c->u.cons.car;
|
|
|
|
tmp2 = c->u.cons.cdr;
|
|
|
|
|
2021-01-15 09:12:28 +00:00
|
|
|
HCL_ASSERT (hcl, tmp1 != HCL_NULL);
|
2021-01-13 09:54:44 +00:00
|
|
|
hcl_freemem (hcl, c);
|
|
|
|
hcl_freecnode (hcl, tmp1); /* TODO: remove recursion? */
|
|
|
|
if (tmp2)
|
|
|
|
{
|
|
|
|
c = tmp2;
|
|
|
|
goto redo;
|
|
|
|
}
|
2021-01-15 09:12:28 +00:00
|
|
|
|
|
|
|
break;
|
2021-01-13 09:54:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:23:24 +00:00
|
|
|
case HCL_CNODE_SHELL:
|
|
|
|
{
|
|
|
|
hcl_cnode_t* tmp;
|
|
|
|
|
|
|
|
tmp = c->u.shell.obj;
|
|
|
|
hcl_freemem (hcl, c);
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
c = tmp;
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-13 09:54:44 +00:00
|
|
|
default:
|
|
|
|
hcl_freemem (hcl, c);
|
2021-01-15 09:12:28 +00:00
|
|
|
break;
|
2021-01-13 09:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 14:07:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
hcl_oow_t hcl_countcnodecons (hcl_t* hcl, hcl_cnode_t* cons)
|
|
|
|
{
|
|
|
|
/* this function ignores the last cdr */
|
|
|
|
hcl_oow_t count = 1;
|
|
|
|
|
|
|
|
HCL_ASSERT (hcl, HCL_CNODE_IS_CONS(cons));
|
|
|
|
do
|
|
|
|
{
|
|
|
|
cons = HCL_CNODE_CONS_CDR(cons);
|
|
|
|
if (!cons) break;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|