From 1c0b23fa97d2ddd427c90055cc095a28619864b9 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Wed, 17 Jul 2019 09:52:26 +0000 Subject: [PATCH] started working on parsing labels --- moo/kernel/test-003.moo | 2 + moo/lib/comp.c | 150 +++++++++++++++++++++++++++++++--------- moo/lib/gc.c | 2 +- moo/lib/moo-prv.h | 2 +- 4 files changed, 123 insertions(+), 33 deletions(-) diff --git a/moo/kernel/test-003.moo b/moo/kernel/test-003.moo index 22dd24b..376c91d 100644 --- a/moo/kernel/test-003.moo +++ b/moo/kernel/test-003.moo @@ -91,6 +91,8 @@ class MyObject(Object) | tb | tb := tc at: idx. System log(System.Log.INFO, idx asString, (if (tb value) { " PASS" } else { " FAIL" }), "\n"). +/*a: +b: c:*/ ]. Exception signal: 'experiment with exception signalling'. diff --git a/moo/lib/comp.c b/moo/lib/comp.c index e751300..e63b6f1 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -984,7 +984,6 @@ static MOO_INLINE int is_token_keyword (moo_t* moo, voca_id_t id) return TOKEN_TYPE(moo) == MOO_IOTOK_KEYWORD && does_token_name_match(moo, id); } - static MOO_INLINE int add_token_str (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len) { moo_oocs_t tmp; @@ -4819,7 +4818,7 @@ static int compile_block_expression (moo_t* moo) moo_cunit_class_t* cc = (moo_cunit_class_t*)moo->c->cunit; moo_oow_t jump_inst_pos; moo_oow_t saved_tmpr_count, saved_tmprs_len; - moo_oow_t block_arg_count, block_tmpr_count; + moo_oow_t block_arg_count, block_tmpr_count, code_start; moo_ioloc_t block_loc, colon_loc, tmpr_loc; /* @@ -4931,6 +4930,7 @@ static int compile_block_expression (moo_t* moo) if (emit_single_param_instruction(moo, BCODE_JUMP_FORWARD_0, MAX_CODE_JUMP, &block_loc) <= -1) return -1; /* compile statements inside a block */ +#if 0 if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) { /* the block is empty */ @@ -4938,25 +4938,83 @@ static int compile_block_expression (moo_t* moo) } else { + int empty = 1, n; + while (TOKEN_TYPE(moo) != MOO_IOTOK_EOF) { - if (compile_block_statement(moo) <= -1) return -1; - - if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; - else if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + n = compile_block_statement(moo); + if (n <= -1) return -1; + if (n == 8888) { - moo_ioloc_t period_loc = *TOKEN_LOC(moo); - GET_TOKEN (moo); - if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; - if (emit_byte_instruction(moo, BCODE_POP_STACKTOP, &period_loc) <= -1) return -1; + /* compile_block_statement() processed non-statement item like a jump label. */ + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) + { + if (empty && emit_byte_instruction(moo, BCODE_PUSH_NIL, TOKEN_LOC(moo)) <= -1) return -1; + break; + } } else { - moo_setsynerr (moo, MOO_SYNERR_RBRACK, TOKEN_LOC(moo), TOKEN_NAME(moo)); - return -1; + /* a proper statement has been processed in compile_block_statemnt */ + empty = 0; + + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; + else if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + { + moo_ioloc_t period_loc = *TOKEN_LOC(moo); + GET_TOKEN (moo); + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; + if (emit_byte_instruction(moo, BCODE_POP_STACKTOP, &period_loc) <= -1) return -1; + } + else + { + moo_setsynerr (moo, MOO_SYNERR_RBRACK, TOKEN_LOC(moo), TOKEN_NAME(moo)); + return -1; + } } } } +#else + code_start = cc->mth.code.len; + if (TOKEN_TYPE(moo) != MOO_IOTOK_RBRACK) + { + while (TOKEN_TYPE(moo) != MOO_IOTOK_EOF) + { + int n; + + n = compile_block_statement(moo); + if (n <= -1) return -1; + if (n == 8888) + { + /* compile_block_statement() processed non-statement item like a jump label. */ + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; + } + else + { + /* a proper statement has been processed in compile_block_statemnt */ + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; + else if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + { + moo_ioloc_t period_loc = *TOKEN_LOC(moo); + GET_TOKEN (moo); + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK) break; + if (emit_byte_instruction(moo, BCODE_POP_STACKTOP, &period_loc) <= -1) return -1; + } + else + { + moo_setsynerr (moo, MOO_SYNERR_RBRACK, TOKEN_LOC(moo), TOKEN_NAME(moo)); + return -1; + } + } + } + } + + if (cc->mth.code.len == code_start) + { + /* the block is empty */ + if (emit_byte_instruction(moo, BCODE_PUSH_NIL, TOKEN_LOC(moo)) <= -1) return -1; + } +#endif if (emit_byte_instruction(moo, BCODE_RETURN_FROM_BLOCK, TOKEN_LOC(moo)) <= -1) return -1; @@ -5996,20 +6054,30 @@ static int compile_braced_block (moo_t* moo) { while (TOKEN_TYPE(moo) != MOO_IOTOK_EOF) { - if (compile_block_statement(moo) <= -1) return -1; + int n; - if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; - else if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + n = compile_block_statement(moo); + if (n <= -1) return -1; + if (n == 8888) { - moo_ioloc_t period_loc = *TOKEN_LOC(moo); - GET_TOKEN (moo); - if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; - if (emit_byte_instruction(moo, BCODE_POP_STACKTOP, &period_loc) <= -1) return -1; + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; } else { - moo_setsynerr (moo, MOO_SYNERR_RBRACE, TOKEN_LOC(moo), TOKEN_NAME(moo)); - return -1; + + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; + else if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + { + moo_ioloc_t period_loc = *TOKEN_LOC(moo); + GET_TOKEN (moo); + if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; + if (emit_byte_instruction(moo, BCODE_POP_STACKTOP, &period_loc) <= -1) return -1; + } + else + { + moo_setsynerr (moo, MOO_SYNERR_RBRACE, TOKEN_LOC(moo), TOKEN_NAME(moo)); + return -1; + } } } } @@ -6652,6 +6720,14 @@ static int compile_special_statement (moo_t* moo) inject_continue_to_loop(moo, &start_loc): /* in a do-while loop, the position to the conditional is not known yet */ emit_backward_jump_instruction(moo, BCODE_JUMP_BACKWARD_0, cc->mth.code.len - cc->mth.loop->startpos, &start_loc); } + else if (TOKEN_TYPE(moo) == MOO_IOTOK_KEYWORD) + { + /* 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)); + GET_TOKEN (moo); + return 8888; /* indicates that non-statement has been seen and processed.*/ + } return 9999; /* to indicate that no special statement has been seen and processed */ } @@ -6744,24 +6820,36 @@ static int compile_method_statements (moo_t* moo) { do { - if (compile_method_statement(moo) <= -1) return -1; + int n; - if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + n = compile_method_statement(moo); + if (n <= -1) return -1; + if (n == 8888) { - /* period after a statement */ - GET_TOKEN (moo); - + /* a non-statement such as label has been processed */ if (TOKEN_TYPE(moo) == MOO_IOTOK_EOF || TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; } else { - if (TOKEN_TYPE(moo) == MOO_IOTOK_EOF || - TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; + /* a proper statement has been processed */ + if (TOKEN_TYPE(moo) == MOO_IOTOK_PERIOD) + { + /* period after a statement */ + GET_TOKEN (moo); - /* not a period, EOF, nor } */ - moo_setsynerr (moo, MOO_SYNERR_PERIOD, TOKEN_LOC(moo), TOKEN_NAME(moo)); - return -1; + if (TOKEN_TYPE(moo) == MOO_IOTOK_EOF || + TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; + } + else + { + if (TOKEN_TYPE(moo) == MOO_IOTOK_EOF || + TOKEN_TYPE(moo) == MOO_IOTOK_RBRACE) break; + + /* not a period, EOF, nor } */ + moo_setsynerr (moo, MOO_SYNERR_PERIOD, TOKEN_LOC(moo), TOKEN_NAME(moo)); + return -1; + } } } while (1); diff --git a/moo/lib/gc.c b/moo/lib/gc.c index 39f05fb..9c1fdd4 100644 --- a/moo/lib/gc.c +++ b/moo/lib/gc.c @@ -1059,7 +1059,7 @@ void moo_gc (moo_t* moo) /* invalidate method cache. TODO: GCing entries on the method cache is also one way instead of full invalidation */ moo_clearmethodcache (moo); - + /* TODO: include some gc statstics like number of live objects, gc performance, etc */ MOO_LOG4 (moo, MOO_LOG_GC | MOO_LOG_INFO, "Finished GC curheap base %p ptr %p newheap base %p ptr %p\n", diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 2c306ef..93d453a 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -315,7 +315,7 @@ enum moo_iotok_type_t MOO_IOTOK_IDENT, MOO_IOTOK_IDENT_DOTTED, MOO_IOTOK_BINSEL, - MOO_IOTOK_KEYWORD, + MOO_IOTOK_KEYWORD, /* word ending with : such as basicAt:*/ MOO_IOTOK_ASSIGN, /* := */ MOO_IOTOK_COLON, /* : */ MOO_IOTOK_DHASH, /* ## */