From 05837aef28832d1074e3196d24ddf27b492d830f Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 18 Jul 2019 15:51:55 +0000 Subject: [PATCH] more goto-label handling code --- moo/kernel/test-003.moo | 3 ++- moo/lib/comp.c | 33 +++++++++++++++++++++++++++++++++ moo/lib/moo-prv.h | 19 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/moo/kernel/test-003.moo b/moo/kernel/test-003.moo index dae2b14..73e5569 100644 --- a/moo/kernel/test-003.moo +++ b/moo/kernel/test-003.moo @@ -93,8 +93,9 @@ class MyObject(Object) System log(System.Log.INFO, idx asString, (if (tb value) { " PASS" } else { " FAIL" }), "\n"). ]. (if (true) { a: 10. b: 1p1000. 20000 }) dump. - [a: 10. b: 1000. ] value class dump. + [A01: 10. B02: 1000. ] value class dump. +TODO: Exception signal: 'experiment with exception signalling'. diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 99b6dec..d749f1a 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -6690,6 +6690,38 @@ oops: return -1; } +static int add_label (moo_t* moo, const moo_oocs_t* name) +{ + moo_cunit_class_t* cc = (moo_cunit_class_t*)moo->c->cunit; + moo_label_t* lab; + moo_ooch_t* nptr; + +/* TODO: name must not have trailing colon? */ + lab = cc->mth._label; + while (lab) + { + nptr = (moo_ooch_t*)(lab + 1); + if (moo_comp_oochars_oocstr(name->ptr, name->len, nptr) == 0) + { + /* duplicate label name */ +/* TODO: there is a duplicate label... */ + return -1; + } + } + + lab = (moo_label_t*)moo_allocmem(moo, MOO_SIZEOF(*lab) + (name->len + 1) * MOO_SIZEOF(moo_ooch_t)); + if (!lab) return -1; + + nptr = (moo_ooch_t*)(lab + 1); + moo_copy_oochars (nptr, name->ptr, name->len); + nptr[name->len] = '\0'; + + lab->next = cc->mth._label; + cc->mth._label = lab; + + return 0; +} + static int compile_special_statement (moo_t* moo) { moo_cunit_class_t* cc = (moo_cunit_class_t*)moo->c->cunit; @@ -6752,6 +6784,7 @@ static int compile_special_statement (moo_t* moo) /* this is a label */ /* remember the label location with the block depth */ MOO_DEBUG2 (moo, "LABEL => %.*js\n", TOKEN_NAME_LEN(moo), TOKEN_NAME_PTR(moo)); + /* TODO: if (add_label(moo, TOKEN_NAME(moo), level???) <= -1) return -1; */ GET_TOKEN (moo); return 8888; /* indicates that non-statement has been seen and processed.*/ } diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index e16e29c..0a645eb 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -399,6 +399,22 @@ struct moo_loop_t moo_loop_t* next; }; +typedef struct moo_label_t moo_label_t; +struct moo_label_t +{ + moo_ooch_t* name; + moo_oow_t level; + moo_label_t* next; +}; + +typedef struct moo_goto_t moo_goto_t; +struct moo_goto_t +{ + moo_ooch_t* label_name; + moo_oow_t level; + moo_goto_t* next; +}; + typedef struct moo_oopbuf_t moo_oopbuf_t; struct moo_oopbuf_t { @@ -532,6 +548,9 @@ struct moo_method_data_t /* information about loop constructs */ moo_loop_t* loop; + moo_goto_t* _goto; + moo_label_t* _label; + /* byte code */ moo_code_t code; };