From 215b487cefbb0fc77269ac55437d18912894fd1e Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 30 Sep 2005 12:19:00 +0000 Subject: [PATCH] *** empty log message *** --- ase/stx/bytecode.c | 7 +-- ase/stx/bytecode.h | 3 +- ase/stx/interp.c | 30 ++++++++++-- ase/stx/parser.c | 111 +++++++++++++++++++++++++----------------- ase/stx/stx.h | 10 +++- ase/test/stx/parser.c | 10 +++- ase/test/stx/test.st | 2 +- ase/test/stx/test1.st | 3 +- 8 files changed, 119 insertions(+), 57 deletions(-) diff --git a/ase/stx/bytecode.c b/ase/stx/bytecode.c index 56575566..acdceadd 100644 --- a/ase/stx/bytecode.c +++ b/ase/stx/bytecode.c @@ -1,5 +1,5 @@ /* - * $Id: bytecode.c,v 1.13 2005-09-13 11:15:41 bacon Exp $ + * $Id: bytecode.c,v 1.14 2005-09-30 12:19:00 bacon Exp $ */ #include #include @@ -121,7 +121,8 @@ static int __decode2 (xp_stx_t* stx, XP_TEXT("push_active_context"), XP_TEXT("push_nil"), XP_TEXT("push_true"), - XP_TEXT("push_false") + XP_TEXT("push_false"), + XP_TEXT("push_receiver") }; static const xp_char_t* return_opcode_names[] = @@ -151,7 +152,7 @@ static int __decode2 (xp_stx_t* stx, xp_printf (XP_TEXT("%s %d\n"), stack_opcode_names[code & 0x0F], next); } - else if (code >= 0x67 && code <= 0x6C) { + else if (code >= 0x67 && code <= 0x6D) { /* stack special */ xp_printf (XP_TEXT("%s\n"), stack_special_opcode_names[code - 0x67]); diff --git a/ase/stx/bytecode.h b/ase/stx/bytecode.h index 5b735ebd..0559ba12 100644 --- a/ase/stx/bytecode.h +++ b/ase/stx/bytecode.h @@ -1,5 +1,5 @@ /* - * $Id: bytecode.h,v 1.10 2005-08-18 15:28:18 bacon Exp $ + * $Id: bytecode.h,v 1.11 2005-09-30 12:19:00 bacon Exp $ */ #ifndef _XP_STX_BYTECODE_H_ @@ -27,6 +27,7 @@ #define PUSH_NIL 0x6A #define PUSH_TRUE 0x6B #define PUSH_FALSE 0x6C +#define PUSH_RECEIVER 0x6D #define SEND_TO_SELF 0x70 #define SEND_TO_SUPER 0x71 diff --git a/ase/stx/interp.c b/ase/stx/interp.c index 0726c927..ba2c0619 100644 --- a/ase/stx/interp.c +++ b/ase/stx/interp.c @@ -1,5 +1,5 @@ /* - * $Id: interp.c,v 1.15 2005-09-13 15:56:23 bacon Exp $ + * $Id: interp.c,v 1.16 2005-09-30 12:19:00 bacon Exp $ */ #include @@ -42,6 +42,7 @@ struct process_t xp_word_t stack_base; xp_word_t stack_top; + xp_word_t receiver; xp_word_t method; xp_word_t pc; @@ -93,6 +94,7 @@ xp_printf (XP_TEXT("out of memory in xp_stx_interp\n")); proc.argcount = XP_STX_FROM_SMALLINT(mthobj->argcount); proc.tmpcount = XP_STX_FROM_SMALLINT(mthobj->tmpcount); + proc.receiver = receiver; proc.method = method; proc.pc = 0; @@ -143,10 +145,25 @@ static int __run_process (xp_stx_t* stx, process_t* proc) int what = code >> 4; int index = code & 0x0F; __store_from_stack (stx, proc, code >> 4, code & 0x0F); - } - /* more here .... */ + /* TODO: more here .... */ + + else if (code == 0x6A) { + proc->stack[proc->stack_top++] = stx->nil; + } + else if (code == 0x6B) { + proc->stack[proc->stack_top++] = stx->true; + } + else if (code == 0x6C) { + proc->stack[proc->stack_top++] = stx->false; + } + else if (code == 0x6D) { + /* push receiver */ + proc->stack[proc->stack_top++] = proc->receiver; + } + + /* TODO: more here .... */ else if (code == 0x70) { next = proc->bytecodes[proc->pc++]; @@ -267,6 +284,7 @@ xp_printf (XP_TEXT("cannot find the method....\n")); proc->stack_base = proc->stack_top - 3 - tmpcount - argcount - 1; xp_assert (proc->stack_base > 0); + proc->receiver = receiver; proc->method = method; proc->pc = 0; @@ -293,13 +311,17 @@ static int __return_from_message (xp_stx_t* stx, process_t* proc) method = proc->stack[proc->stack_base + 1 + proc->tmpcount + proc->argcount + 1]; pc = proc->stack[proc->stack_base + 1 + proc->tmpcount + proc->argcount]; - mthobj = (xp_stx_method_t*)XP_STX_OBJECT(stx,method); xp_assert (mthobj != XP_NULL); + /* return value is located on top of the previous stack */ + proc->stack[proc->stack_base - 1] = proc->stack[proc->stack_top - 1]; + + /* restore the stack pointers */ proc->stack_top = proc->stack_base; proc->stack_base = stack_base; + proc->receiver = proc->stack[stack_base]; proc->method = method; proc->pc = pc; diff --git a/ase/stx/parser.c b/ase/stx/parser.c index b3d82567..5f782d35 100644 --- a/ase/stx/parser.c +++ b/ase/stx/parser.c @@ -1,5 +1,5 @@ /* - * $Id: parser.c,v 1.72 2005-09-13 11:15:41 bacon Exp $ + * $Id: parser.c,v 1.73 2005-09-30 12:19:00 bacon Exp $ */ #include @@ -33,15 +33,19 @@ static int __parse_assignment ( static int __parse_basic_expression ( xp_stx_parser_t* parser, const xp_char_t* ident); static int __parse_primary ( - xp_stx_parser_t* parser, const xp_char_t* ident); + xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super); static int __parse_primary_ident ( - xp_stx_parser_t* parser, const xp_char_t* ident); + xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super); static int __parse_block_constructor (xp_stx_parser_t* parser); -static int __parse_message_continuation (xp_stx_parser_t* parser); -static int __parse_keyword_message (xp_stx_parser_t* parser); -static int __parse_binary_message (xp_stx_parser_t* parser); -static int __parse_unary_message (xp_stx_parser_t* parser); +static int __parse_message_continuation ( + xp_stx_parser_t* parser, xp_bool_t is_super); +static int __parse_keyword_message ( + xp_stx_parser_t* parser, xp_bool_t is_super); +static int __parse_binary_message ( + xp_stx_parser_t* parser, xp_bool_t is_super); +static int __parse_unary_message ( + xp_stx_parser_t* parser, xp_bool_t is_super); static int __get_token (xp_stx_parser_t* parser); static int __get_ident (xp_stx_parser_t* parser); @@ -860,11 +864,12 @@ static int __parse_basic_expression ( /* * ::= [ ] */ + xp_bool_t is_super; - if (__parse_primary(parser, ident) == -1) return -1; + if (__parse_primary(parser, ident, &is_super) == -1) return -1; if (parser->token.type != XP_STX_TOKEN_END && parser->token.type != XP_STX_TOKEN_PERIOD) { - if (__parse_message_continuation(parser) == -1) return -1; + if (__parse_message_continuation(parser, is_super) == -1) return -1; } return 0; } @@ -912,7 +917,8 @@ static int __parse_assignment ( return -1; } -static int __parse_primary (xp_stx_parser_t* parser, const xp_char_t* ident) +static int __parse_primary ( + xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super) { /* * ::= @@ -927,7 +933,8 @@ static int __parse_primary (xp_stx_parser_t* parser, const xp_char_t* ident) xp_word_t literal; if (parser->token.type == XP_STX_TOKEN_IDENT) { - if (__parse_primary_ident(parser, parser->token.name.buffer) == -1) return -1; + if (__parse_primary_ident(parser, + parser->token.name.buffer, is_super) == -1) return -1; GET_TOKEN (parser); } else if (parser->token.type == XP_STX_TOKEN_CHARLIT) { @@ -984,26 +991,30 @@ static int __parse_primary (xp_stx_parser_t* parser, const xp_char_t* ident) } else { /*if (__parse_primary_ident(parser, parser->token.name.buffer) == -1) return -1;*/ - if (__parse_primary_ident(parser, ident) == -1) return -1; + if (__parse_primary_ident(parser, ident, is_super) == -1) return -1; } return 0; } -static int __parse_primary_ident (xp_stx_parser_t* parser, const xp_char_t* ident) +static int __parse_primary_ident ( + xp_stx_parser_t* parser, const xp_char_t* ident, xp_bool_t* is_super) { xp_word_t i; xp_stx_t* stx = parser->stx; -/* - if (xp_strcmp(token->name.buffer, XP_TEXT("self")) == 0) { - EMIT_CODE (parser, PUSH_SELF); + *is_super = xp_false; + + if (xp_strcmp(ident, XP_TEXT("self")) == 0) { + EMIT_CODE (parser, PUSH_RECEIVER); + return 0; } - else if (xp_strcmp(token->name.buffer, XP_TEXT("super")) == 0) { - EMIT_CODE (parser, PUSH_SUPER); + else if (xp_strcmp(ident, XP_TEXT("super")) == 0) { + *is_super = xp_true; + EMIT_CODE (parser, PUSH_RECEIVER); + return 0; } -*/ - if (xp_strcmp(ident, XP_TEXT("nil")) == 0) { + else if (xp_strcmp(ident, XP_TEXT("nil")) == 0) { EMIT_CODE (parser, PUSH_NIL); return 0; } @@ -1097,7 +1108,8 @@ static int __parse_block_constructor (xp_stx_parser_t* parser) return 0; } -static int __parse_message_continuation (xp_stx_parser_t* parser) +static int __parse_message_continuation ( + xp_stx_parser_t* parser, xp_bool_t is_super) { /* * ::= @@ -1106,21 +1118,22 @@ static int __parse_message_continuation (xp_stx_parser_t* parser) * * ::= (';' )* */ + xp_bool_t dummy; - if (__parse_keyword_message(parser) == -1) return -1; + if (__parse_keyword_message(parser, is_super) == -1) return -1; while (parser->token.type == XP_STX_TOKEN_SEMICOLON) { EMIT_CODE_TEST (parser, XP_TEXT("DoSpecial(DUP_RECEIVER(CASCADE))"), XP_TEXT("")); GET_TOKEN (parser); - if (__parse_keyword_message (parser) == -1) return -1; + if (__parse_keyword_message (parser, &dummy) == -1) return -1; EMIT_CODE_TEST (parser, XP_TEXT("DoSpecial(POP_TOP)"), XP_TEXT("")); } return 0; } -static int __parse_keyword_message (xp_stx_parser_t* parser) +static int __parse_keyword_message (xp_stx_parser_t* parser, xp_bool_t is_super) { /* * ::= (keyword )+ @@ -1129,9 +1142,10 @@ static int __parse_keyword_message (xp_stx_parser_t* parser) xp_stx_name_t name; xp_word_t pos; - int nargs = 0; + xp_bool_t is_super2; + int nargs = 0, n; - if (__parse_binary_message (parser) == -1) return -1; + if (__parse_binary_message (parser, is_super) == -1) return -1; if (parser->token.type != XP_STX_TOKEN_KEYWORD) return 0; if (xp_stx_name_open(&name, 0) == XP_NULL) { @@ -1147,12 +1161,12 @@ static int __parse_keyword_message (xp_stx_parser_t* parser) } GET_TOKEN (parser); - if (__parse_primary(parser, XP_NULL) == -1) { + if (__parse_primary(parser, XP_NULL, &is_super2) == -1) { xp_stx_name_close (&name); return -1; } - if (__parse_binary_message(parser) == -1) { + if (__parse_binary_message(parser, is_super2) == -1) { xp_stx_name_close (&name); return -1; } @@ -1161,32 +1175,35 @@ static int __parse_keyword_message (xp_stx_parser_t* parser) /* TODO: check if it has too many arguments.. */ } while (parser->token.type == XP_STX_TOKEN_KEYWORD); - /* TODO: SEND_TO_SUPER */ pos = __add_symbol_literal (parser, name.buffer, name.size); if (pos == -1) { xp_stx_name_close (&name); return -1; } - /*EMIT_SEND_TO_SELF (parser, nargs, pos);*/ - if (__emit_send_to_self(parser,nargs,pos) == -1) { + + n = (is_super)? + __emit_send_to_super(parser,nargs,pos): + __emit_send_to_self(parser,nargs,pos); + if (n == -1) { xp_stx_name_close (&name); return -1; } xp_stx_name_close (&name); - return 0; } -static int __parse_binary_message (xp_stx_parser_t* parser) +static int __parse_binary_message (xp_stx_parser_t* parser, xp_bool_t is_super) { /* * ::= binarySelector * ::= * */ xp_word_t pos; + xp_bool_t is_super2; + int n; - if (__parse_unary_message (parser) == -1) return -1; + if (__parse_unary_message (parser, is_super) == -1) return -1; while (parser->token.type == XP_STX_TOKEN_BINARY) { xp_char_t* op = xp_stx_token_yield (&parser->token, 0); @@ -1196,24 +1213,26 @@ static int __parse_binary_message (xp_stx_parser_t* parser) } GET_TOKEN (parser); - if (__parse_primary(parser, XP_NULL) == -1) { + if (__parse_primary(parser, XP_NULL, &is_super2) == -1) { xp_free (op); return -1; } - if (__parse_unary_message(parser) == -1) { + if (__parse_unary_message(parser, is_super2) == -1) { xp_free (op); return -1; } - /* TODO: SEND_TO_SUPER */ pos = __add_symbol_literal (parser, op, xp_strlen(op)); if (pos == -1) { xp_free (op); return -1; } - /*EMIT_SEND_TO_SELF (parser, 2, pos);*/ - if (__emit_send_to_self(parser,2,pos) == -1) { + + n = (is_super)? + __emit_send_to_super(parser,2,pos): + __emit_send_to_self(parser,2,pos); + if (n == -1) { xp_free (op); return -1; } @@ -1224,21 +1243,23 @@ static int __parse_binary_message (xp_stx_parser_t* parser) return 0; } -static int __parse_unary_message (xp_stx_parser_t* parser) +static int __parse_unary_message (xp_stx_parser_t* parser, xp_bool_t is_super) { /* ::= unarySelector */ xp_word_t pos; + int n; while (parser->token.type == XP_STX_TOKEN_IDENT) { - /* TODO: SEND_TO_SUPER */ - /*EMIT_SEND_TO_SELF (parser, 0, index of parser->token.name.buffer);*/ - /*EMIT_SEND_TO_SELF (parser, 0, 0);*/ - pos = __add_symbol_literal (parser, parser->token.name.buffer, parser->token.name.size); if (pos == -1) return -1; - EMIT_SEND_TO_SELF (parser, 0, pos); + + n = (is_super)? + __emit_send_to_super(parser,0,pos): + __emit_send_to_self(parser,0,pos); + if (n == -1) return -1; + GET_TOKEN (parser); } diff --git a/ase/stx/stx.h b/ase/stx/stx.h index 6cc25133..3ae83492 100644 --- a/ase/stx/stx.h +++ b/ase/stx/stx.h @@ -1,5 +1,5 @@ /* - * $Id: stx.h,v 1.43 2005-08-18 15:28:18 bacon Exp $ + * $Id: stx.h,v 1.44 2005-09-30 12:19:00 bacon Exp $ */ #ifndef _XP_STX_STX_H_ @@ -134,12 +134,20 @@ struct xp_stx_t #define XP_STX_CHAR_OBJECT(stx,idx) \ ((xp_stx_char_object_t*)XP_STX_OBJECT(stx,idx)) +/* #define XP_STX_WORD_AT(stx,idx,n) \ (((xp_word_t*)(XP_STX_OBJECT(stx,idx) + 1))[n]) #define XP_STX_BYTE_AT(stx,idx,n) \ (((xp_byte_t*)(XP_STX_OBJECT(stx,idx) + 1))[n]) #define XP_STX_CHAR_AT(stx,idx,n) \ (((xp_char_t*)(XP_STX_OBJECT(stx,idx) + 1))[n]) +*/ +#define XP_STX_WORD_AT(stx,idx,n) \ + (XP_STX_WORD_OBJECT(stx,idx)->data[n]) +#define XP_STX_BYTE_AT(stx,idx,n) \ + (XP_STX_BYTE_OBJECT(stx,idx)->data[n]) +#define XP_STX_CHAR_AT(stx,idx,n) \ + (XP_STX_CHAR_OBJECT(stx,idx)->data[n]) #ifdef __cplusplus extern "C" { diff --git a/ase/test/stx/parser.c b/ase/test/stx/parser.c index 94079448..0dc4dc2d 100644 --- a/ase/test/stx/parser.c +++ b/ase/test/stx/parser.c @@ -171,12 +171,18 @@ int xp_main (int argc, xp_char_t* argv[]) xp_stx_parser_error_string (&parser)); } - xp_printf (XP_TEXT("== Decoded Methods ==\n")); + xp_printf (XP_TEXT("\n== Decoded Methods ==\n")); if (xp_stx_decode(&stx, n) == -1) { xp_printf (XP_TEXT("parser error <%s>\n"), xp_stx_parser_error_string (&parser)); } + xp_printf (XP_TEXT("\n== Decoded Methods for Symbol ==\n")); + if (xp_stx_decode(&stx, stx.class_symbol) == -1) { + xp_printf (XP_TEXT("parser error <%s>\n"), + xp_stx_parser_error_string (&parser)); + } + xp_printf (XP_TEXT("== Running the main method ==\n")); m = xp_stx_lookup_method (&stx, n, XP_TEXT("main")); if (m == stx.nil) { @@ -196,6 +202,7 @@ exit_program: muntrace (); #endif +/* #ifdef __linux { char buf[1000]; @@ -203,6 +210,7 @@ exit_program: system (buf); } #endif +*/ return 0; } diff --git a/ase/test/stx/test.st b/ase/test/stx/test.st index b2617bc1..d3dbeb00 100644 --- a/ase/test/stx/test.st +++ b/ase/test/stx/test.st @@ -10,4 +10,4 @@ main a := #abc print: 123 and: 2345. #abc print: a and: a. 1234567. - ^nil + ^nil. diff --git a/ase/test/stx/test1.st b/ase/test/stx/test1.st index be6acbaf..c3974a73 100644 --- a/ase/test/stx/test1.st +++ b/ase/test/stx/test1.st @@ -4,4 +4,5 @@ print: a1 and: a2 t1 := #abcdefg. "a1 := 2341 arguments are not assignable" t1 prim2: a2. - ^9999. + self prim2: 2189. + ^67891.