renamed fnblk to funblk, cblk to ctlblk.
All checks were successful
continuous-integration/drone/push Build is passing

enhancing the compiler code to handle 'var' inside 'class'
This commit is contained in:
2024-10-12 00:24:02 +09:00
parent 42009d3cce
commit 17550d44c5
12 changed files with 881 additions and 235 deletions

File diff suppressed because it is too large Load Diff

View File

@ -154,6 +154,7 @@ static const char* synerrstr[] =
"invalid class definition",
"invalid function definition",
"invalid variable declaration",
"elif without if",
"else without if",
"catch without try",

View File

@ -4102,8 +4102,6 @@ static int execute (hcl_t* hcl)
#if 0
hcl_logbfmt (hcl, HCL_LOG_STDERR, ">>>%O c->sc=%O sc=%O b2=%d b3=%d nivars=%d ncvars=%d<<<\n", class_obj, class_obj->superclass, superclass, b2, b3, (int)HCL_CLASS_SPEC_NAMED_INSTVARS(spec), (int)HCL_CLASS_SELFSPEC_CLASSVARS(spec));
#endif
hcl_logbfmt (hcl, HCL_LOG_STDERR, " spec %d %d | selfspec %d %d\n", expected_spec, spec, expected_selfspec, selfspec);
if (class_obj->superclass != superclass ||
expected_spec != spec ||
expected_selfspec != selfspec ||

View File

@ -1746,8 +1746,6 @@ oops:
int hcl_ignite (hcl_t* hcl, hcl_oow_t heapsize)
{
hcl_oow_t i;
if (!hcl->heap)
{
hcl->heap = hcl_makeheap(hcl, heapsize);

View File

@ -308,6 +308,7 @@ enum hcl_tok_type_t
HCL_TOK_CLASS,
HCL_TOK_FUN,
HCL_TOK_VAR,
HCL_TOK_DO,
HCL_TOK_IF,
HCL_TOK_ELIF,
@ -420,6 +421,7 @@ enum hcl_cnode_type_t
* these represent syntactical elements of the language only. */
HCL_CNODE_CLASS, /* first item for HCL_CNODE_IS_FOR_LANG */
HCL_CNODE_FUN,
HCL_CNODE_VAR,
HCL_CNODE_DO,
HCL_CNODE_IF,
HCL_CNODE_ELIF,
@ -684,28 +686,29 @@ struct hcl_cframe_t
unsigned int indexed_type;
hcl_loc_t start_loc;
hcl_cnode_t* cmd_cnode;
hcl_cnode_t* class_name_cnode;
} _class;
} u;
};
typedef struct hcl_cframe_t hcl_cframe_t;
enum hcl_cblk_type_t
enum hcl_ctlblk_type_t
{
HCL_CBLK_TYPE_LOOP,
HCL_CBLK_TYPE_TRY,
HCL_CBLK_TYPE_CLASS
HCL_CTLBLK_TYPE_LOOP,
HCL_CTLBLK_TYPE_TRY,
HCL_CTLBLK_TYPE_CLASS
};
typedef enum hcl_cblk_type_t hcl_cblk_type_t;
typedef enum hcl_ctlblk_type_t hcl_ctlblk_type_t;
/* control block information for the compiler */
struct hcl_cblk_info_t
struct hcl_ctlblk_info_t
{
hcl_cblk_type_t _type;
hcl_ctlblk_type_t _type;
};
typedef struct hcl_cblk_info_t hcl_cblk_info_t;
typedef struct hcl_ctlblk_info_t hcl_ctlblk_info_t;
/* function block information for the compiler */
struct hcl_fnblk_info_t
struct hcl_funblk_info_t
{
unsigned int fun_type;
@ -720,7 +723,7 @@ struct hcl_fnblk_info_t
hcl_oow_t make_inst_pos;
hcl_oow_t lfbase;
hcl_ooi_t cblk_base;
hcl_ooi_t ctlblk_base;
hcl_ooi_t clsblk_base;
hcl_ooi_t clsblk_top;
@ -728,19 +731,21 @@ struct hcl_fnblk_info_t
unsigned int access_outer: 1;
unsigned int accessed_by_inner: 1;
};
typedef struct hcl_fnblk_info_t hcl_fnblk_info_t;
typedef struct hcl_funblk_info_t hcl_funblk_info_t;
/* class block information for the compiler */
struct hcl_clsblk_info_t
{
hcl_cnode_t* class_name;
hcl_oow_t nivars;
hcl_oow_t ncvars;
hcl_ooch_t* ivars_str;
hcl_ooch_t* cvars_str;
hcl_oow_t spec; /* TODO: byte indexed, word indexed? */
hcl_ooi_t fnblk_base;
hcl_ooi_t funblk_base;
hcl_ooi_t class_start_inst_pos; /* the position of the first instruction in the class body after CLASS_ENTER */
};
typedef struct hcl_clsblk_info_t hcl_clsblk_info_t;
@ -998,16 +1003,16 @@ struct hcl_compiler_t
struct
{
hcl_ooi_t depth; /* signed because it starts with -1 */
hcl_cblk_info_t* info;
hcl_ctlblk_info_t* info;
hcl_oow_t info_capa;
} cblk; /* control block - loop, try-catch */
} ctlblk; /* control block - loop, try-catch */
struct
{
hcl_ooi_t depth; /* signed because it starts with -1 */
hcl_fnblk_info_t* info;
hcl_funblk_info_t* info;
hcl_oow_t info_capa;
} fnblk; /* function block */
} funblk; /* function block */
struct
{

View File

@ -158,6 +158,7 @@ enum hcl_synerrnum_t
HCL_SYNERR_CLASS, /* invalid class definition */
HCL_SYNERR_FUN, /* invalid function definition */
HCL_SYNERR_VAR, /* invalid variable declaration */
HCL_SYNERR_ELIF, /* elif without if */
HCL_SYNERR_ELSE, /* else without if */
HCL_SYNERR_CATCH, /* catch outside try */
@ -1577,7 +1578,7 @@ enum hcl_compile_flag_t
HCL_COMPILE_CLEAR_CODE = (1 << 0),
/* clear the top-level function block at the end of hcl_compile() */
HCL_COMPILE_CLEAR_FNBLK = (1 << 1)
HCL_COMPILE_CLEAR_FUNBLK = (1 << 1)
};
typedef enum hcl_compile_flag_t hcl_compile_flag_t;
#endif

View File

@ -60,6 +60,7 @@ static struct voca_t
{ 5, { 'c','l','a','s','s' } },
{ 3, { 'f','u','n' } },
{ 3, { 'v','a','r' } },
{ 2, { 'd','o' } },
{ 2, { 'i','f' } },
{ 4, { 'e','l','i','f' } },
@ -120,6 +121,7 @@ enum voca_id_t
VOCA_KW_CLASS,
VOCA_KW_FUN,
VOCA_KW_VAR,
VOCA_KW_DO,
VOCA_KW_IF,
VOCA_KW_ELIF,
@ -470,6 +472,7 @@ static hcl_tok_type_t classify_ident_token (hcl_t* hcl, const hcl_oocs_t* v)
{ VOCA_KW_CLASS, HCL_TOK_CLASS },
{ VOCA_KW_FUN, HCL_TOK_FUN },
{ VOCA_KW_VAR, HCL_TOK_VAR },
{ VOCA_KW_DO, HCL_TOK_DO },
{ VOCA_KW_IF, HCL_TOK_IF },
{ VOCA_KW_ELIF, HCL_TOK_ELIF },
@ -861,14 +864,7 @@ static HCL_INLINE int can_comma_list (hcl_t* hcl)
if (rstl->flagv & (COMMAED | COLONED | COLONEQED | BINOPED)) return 0;
cc = (hcl_concode_t)LIST_FLAG_GET_CONCODE(rstl->flagv);
if (cc == HCL_CONCODE_XLIST)
{
/* fun f(a :: b c) { b := (a + 10); c := (a + 20) }
* [x y] := (f 9)
* [x,y] := (f 9) */
LIST_FLAG_SET_CONCODE(rstl->flagv, HCL_CONCODE_ALIST);
}
else if (cc == HCL_CONCODE_DIC)
if (cc == HCL_CONCODE_DIC)
{
if (rstl->count & 1) return 0;
}
@ -1443,6 +1439,7 @@ static hcl_cnode_type_t kw_to_cnode_type (int tok_type)
HCL_CNODE_CLASS,
HCL_CNODE_FUN,
HCL_CNODE_VAR,
HCL_CNODE_DO,
HCL_CNODE_IF,
HCL_CNODE_ELIF,
@ -1882,6 +1879,7 @@ static int feed_process_token (hcl_t* hcl)
case HCL_TOK_CLASS:
case HCL_TOK_FUN:
case HCL_TOK_VAR:
case HCL_TOK_DO:
case HCL_TOK_IF:
case HCL_TOK_ELIF:
@ -3926,12 +3924,12 @@ static void fini_compiler_cb (hcl_t* hcl)
HCL_ASSERT (hcl, hcl->c->tv.capa == 0);
HCL_ASSERT (hcl, hcl->c->tv.wcount == 0);
if (hcl->c->cblk.info)
if (hcl->c->ctlblk.info)
{
hcl_freemem (hcl, hcl->c->cblk.info);
hcl->c->cblk.info = HCL_NULL;
hcl->c->cblk.info_capa = 0;
hcl->c->cblk.depth = -1;
hcl_freemem (hcl, hcl->c->ctlblk.info);
hcl->c->ctlblk.info = HCL_NULL;
hcl->c->ctlblk.info_capa = 0;
hcl->c->ctlblk.depth = -1;
}
if (hcl->c->clsblk.info)
@ -3942,12 +3940,12 @@ static void fini_compiler_cb (hcl_t* hcl)
hcl->c->clsblk.depth = -1;
}
if (hcl->c->fnblk.info)
if (hcl->c->funblk.info)
{
hcl_freemem (hcl, hcl->c->fnblk.info);
hcl->c->fnblk.info = HCL_NULL;
hcl->c->fnblk.info_capa = 0;
hcl->c->fnblk.depth = -1;
hcl_freemem (hcl, hcl->c->funblk.info);
hcl->c->funblk.info = HCL_NULL;
hcl->c->funblk.info_capa = 0;
hcl->c->funblk.depth = -1;
}
clear_sr_names (hcl);
@ -3999,9 +3997,9 @@ static int init_compiler (hcl_t* hcl)
hcl->c->r.e = hcl->_nil;
hcl->c->cfs.top = -1;
hcl->c->cblk.depth = -1;
hcl->c->ctlblk.depth = -1;
hcl->c->clsblk.depth = -1;
hcl->c->fnblk.depth = -1;
hcl->c->funblk.depth = -1;
init_feed (hcl);
hcl->c->cbp = cbp;

View File

@ -73,7 +73,7 @@ int hcl_marshalcode (hcl_t* hcl, const hcl_code_t* code, hcl_xchg_writer_t wrtr,
hcl_oow_t w;
hcl_xchg_hdr_t h;
lfbase = (hcl->option.trait & HCL_TRAIT_INTERACTIVE)? hcl->c->fnblk.info[hcl->c->fnblk.depth].lfbase: 0;
lfbase = (hcl->option.trait & HCL_TRAIT_INTERACTIVE)? hcl->c->funblk.info[hcl->c->funblk.depth].lfbase: 0;
/* start with a header */
h.ver = 1;