diff options
-rw-r--r-- | ChangeLog | 38 | ||||
-rw-r--r-- | array.c | 4 | ||||
-rw-r--r-- | class.c | 55 | ||||
-rw-r--r-- | dict.c | 1 | ||||
-rw-r--r-- | dir.c | 3 | ||||
-rw-r--r-- | error.c | 2 | ||||
-rw-r--r-- | eval.c | 53 | ||||
-rw-r--r-- | node.h | 4 | ||||
-rw-r--r-- | object.c | 24 | ||||
-rw-r--r-- | parse.y | 208 | ||||
-rw-r--r-- | process.c | 6 | ||||
-rw-r--r-- | re.c | 13 | ||||
-rw-r--r-- | ruby.1 | 24 | ||||
-rw-r--r-- | ruby.c | 82 | ||||
-rw-r--r-- | sample/Artistic | 117 | ||||
-rw-r--r-- | sample/MANIFEST | 3 | ||||
-rw-r--r-- | sample/attr.rb | 2 | ||||
-rw-r--r-- | sample/cbreak.rb | 24 | ||||
-rw-r--r-- | sample/const.rb | 16 | ||||
-rw-r--r-- | sample/evaldef.rb | 4 | ||||
-rw-r--r-- | sample/export.rb | 2 | ||||
-rw-r--r-- | sample/getopts.rb | 16 | ||||
-rwxr-xr-x | sample/getopts.test | 2 | ||||
-rwxr-xr-x | sample/less.rb | 2 | ||||
-rw-r--r-- | sample/opt_x.rb | 10 | ||||
-rw-r--r-- | sample/opt_x.test | 10 | ||||
-rw-r--r-- | sample/ruby-mode.el | 16 | ||||
-rw-r--r-- | sample/svr.rb | 2 | ||||
-rw-r--r-- | sample/tt.rb | 2 | ||||
-rw-r--r-- | spec | 218 | ||||
-rw-r--r-- | string.c | 12 | ||||
-rw-r--r-- | variable.c | 60 | ||||
-rw-r--r-- | version.h | 4 |
33 files changed, 500 insertions, 539 deletions
@@ -1,3 +1,41 @@ +Mon Feb 20 16:10:14 1995 Yukihiro Matsumoto (matz@ix-02) + + * parse.y(yylex): 定数を`%識別子'から,第1文字が大文字の識別子に変 + 更.それにともないクラスは定数となった. + + * eval.c: クラス定義内のselfがクラス定義外部のthe_classだった. + + * variable.c(rb_name_class): クラス名をインスタンス変数に格納する. + +Thu Feb 16 15:36:17 1995 Yukihiro Matsumoto (matz@ix-02) + + * parse.y: BLOCKをbraceで表現する文法に変更したものを作ってみる. + MLに提示してみるが反応がない. + + * object.c(do,forever): なくした. + +Wed Feb 15 13:20:49 1995 Yukihiro Matsumoto (matz@ix-02) + + * re.c(new): 第2引数が与えられて,かつnilでないときだけ設定するよ + うに(以前はnilの時にも設定を行なっていた). + + * parse.y(parse_regexp): 正規表現リテラルで大文字小文字を無視する + かどうか指定できるように. + +Tue Feb 14 00:55:33 1995 Yukihiro Matsumoto (matz@dyna) + + * parse.y: (compexpr) -> (expr). + +Fri Feb 10 16:30:00 1995 Yukihiro Matsumoto (matz@ix-02) + + * ruby.c(load_file): scriptを読み込む時だけ"#!"の解析を行うように. + + * ruby.c(readin): ファイル読み込み時に先頭に"#!"があり,rubyに引数 + が与えられていれば,その引数も有効になる. + + * parse.y(yylex): コメント行の終りが`\'であった時,次の行に継続し + ているとみなすようにした. + Thu Feb 9 16:18:37 1995 Yukihiro Matsumoto (matz@ix-02) * parse.y: protectをbeginに変更.begin..endは例外処理だけでなく, @@ -262,9 +262,7 @@ range_beg_end(range, begp, lenp, len) if (beg > end) { int tmp; - if (verbose) { - Warning("start %d is bigger than end %d", beg, end); - } + Warning("start %d is bigger than end %d", beg, end); tmp = beg; beg = end; end = tmp; } *begp = beg; *lenp = end - beg + 1; @@ -16,6 +16,7 @@ #include "st.h" struct st_table *new_idhash(); +extern st_table *rb_class_tbl; extern VALUE C_Class; extern VALUE C_Module; @@ -81,7 +82,6 @@ rb_define_class_id(id, super) struct RClass *cls = (struct RClass*)class_new(super); rb_name_class(cls, id); - /* make metaclass */ RBASIC(cls)->class = single_class_new(super?super->class:C_Class); @@ -93,7 +93,29 @@ rb_define_class(name, super) char *name; VALUE super; { - return rb_define_class_id(rb_intern(name), super); + VALUE class; + ID id; + + id = rb_intern(name); + class = rb_define_class_id(id, super); + st_add_direct(rb_class_tbl, id, class); + + return class; +} + +rb_define_class_under(under, name, super) + VALUE under; + char *name; + VALUE super; +{ + VALUE class; + ID id; + + id = rb_intern(name); + class = rb_define_class_id(id, super); + rb_const_set(under, id, class); + + return class; } VALUE @@ -112,9 +134,11 @@ VALUE rb_define_module_id(id) ID id; { + extern st_table *rb_class_tbl; struct RClass *mdl = (struct RClass*)module_new(); rb_name_class(mdl, id); + return (VALUE)mdl; } @@ -122,7 +146,28 @@ VALUE rb_define_module(name) char *name; { - return rb_define_module_id(rb_intern(name)); + VALUE module; + ID id; + + id = rb_intern(name); + module = rb_define_module_id(id); + st_add_direct(rb_class_tbl, id, module); + + return module; +} + +rb_define_module_under(under, name) + VALUE under; + char *name; +{ + VALUE module; + ID id; + + id = rb_intern(name); + module = rb_define_module_id(id); + rb_const_set(under, id, module); + + return module; } static struct RClass * @@ -189,9 +234,7 @@ rb_add_method(class, mid, node, noex) if (class == Qnil) class = (struct RClass*)C_Object; if (st_lookup(class->m_tbl, mid, &body)) { - if (verbose) { - Warning("redefine %s", rb_id2name(mid)); - } + Warning("redefine %s", rb_id2name(mid)); rb_clear_cache(body); } body = NEW_METHOD(node, noex); @@ -579,7 +579,6 @@ Init_Dict() hash = rb_intern("hash"); C_Dict = rb_define_class("Dict", C_Object); - rb_name_class(C_Dict, rb_intern("Hash")); /* alias */ rb_include_module(C_Dict, M_Enumerable); @@ -241,8 +241,7 @@ Init_Dir() { extern VALUE M_Enumerable; - C_Dir = rb_define_class("Directory", C_Object); - rb_name_class(C_Dir, rb_intern("Dir")); /* alias */ + C_Dir = rb_define_class("Dir", C_Object); rb_include_module(C_Dir, M_Enumerable); @@ -85,6 +85,8 @@ Warning(fmt, va_alist) char buf[BUFSIZ]; va_list args; + if (!verbose) return; + sprintf(buf, "warning: %s", fmt); va_start(args); @@ -115,9 +115,7 @@ rb_alias(class, name, def) } if (st_lookup(class->m_tbl, name, &old)) { - if (verbose) { - Warning("redefine %s", rb_id2name(name)); - } + Warning("redefine %s", rb_id2name(name)); rb_clear_cache(old->nd_body); } @@ -1017,11 +1015,7 @@ rb_eval(node) Bug("unexpected local variable"); return the_scope->local_vars[node->nd_cnt]; - case NODE_GVAR: - return rb_gvar_get(node->nd_entry); - case NODE_IVAR: - return rb_ivar_get(node->nd_vid); - case NODE_MVAR: + case NODE_LVAR2: if (the_scope->flags & SCOPE_MALLOCED) { ID id = node->nd_vid, *tbl = the_scope->local_tbl; int i, len = tbl[0]; @@ -1034,7 +1028,13 @@ rb_eval(node) return the_scope->local_vars[i]; } } - return rb_mvar_get(node->nd_vid); + Warning("local var %s not initialized", rb_id2name(node->nd_vid)); + return Qnil; + + case NODE_GVAR: + return rb_gvar_get(node->nd_entry); + case NODE_IVAR: + return rb_ivar_get(node->nd_vid); case NODE_CVAR: { @@ -1189,56 +1189,55 @@ rb_eval(node) else { super = C_Object; } - if (class = rb_id2class(node->nd_cname)) { - if (verbose) { - Warning("redefine class %s", rb_id2name(node->nd_cname)); - } + if (verbose && rb_id2class(node->nd_cname)) { + Warning("redefine class %s", rb_id2name(node->nd_cname)); } - PUSH_SELF((VALUE)the_class); + class = rb_define_class_id(node->nd_cname, super); + rb_const_set(the_class, node->nd_cname, class); PUSH_CLASS(); - the_class = (struct RClass*) - rb_define_class_id(node->nd_cname, super); + the_class = (struct RClass*)class; + PUSH_SELF((VALUE)the_class); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { rb_eval(node->nd_body); } POP_TAG(); - POP_CLASS(); POP_SELF(); + POP_CLASS(); if (state) JUMP_TAG(state); + return class; } - return Qnil; case NODE_MODULE: { VALUE module; - if (module = rb_id2class(node->nd_cname)) { - if (verbose) { - Warning("redefine module %s", rb_id2name(node->nd_cname)); - } + if (verbose && rb_id2class(node->nd_cname)) { + Warning("redefine module %s", rb_id2name(node->nd_cname)); } - PUSH_SELF((VALUE)the_class); + module = rb_define_module_id(node->nd_cname); + rb_const_set(the_class, node->nd_cname, module); PUSH_CLASS(); - the_class = (struct RClass*)rb_define_module_id(node->nd_cname); + the_class = (struct RClass*)module; + PUSH_SELF((VALUE)the_class); PUSH_TAG(); if ((state = EXEC_TAG()) == 0) { rb_eval(node->nd_body); } POP_TAG(); - POP_CLASS(); POP_SELF(); + POP_CLASS(); if (state) JUMP_TAG(state); + return module; } - return Qnil; case NODE_INC: { struct RClass *module; - module = (struct RClass*)rb_id2class(node->nd_modl); + module = (struct RClass*)rb_const_get(node->nd_modl); if (module == Qnil) { Fail("undefined module %s", rb_id2name(node->nd_modl)); } @@ -51,9 +51,9 @@ enum node_type { NODE_FAIL, NODE_YIELD, NODE_LVAR, + NODE_LVAR2, NODE_GVAR, NODE_IVAR, - NODE_MVAR, NODE_CVAR, NODE_CONST, NODE_LIT, @@ -211,8 +211,8 @@ typedef struct RNode { #define NEW_OP_ASGN2(r,i,val) newnode(NODE_OP_ASGN1,r,val,i) #define NEW_GVAR(v) newnode(NODE_GVAR,v,Qnil,rb_global_entry(v)) #define NEW_LVAR(v) newnode(NODE_LVAR,v,Qnil,local_cnt(v)) +#define NEW_LVAR2(v) newnode(NODE_LVAR2,v,Qnil,local_cnt(v)) #define NEW_IVAR(v) newnode(NODE_IVAR,v,Qnil,Qnil) -#define NEW_MVAR(v) newnode(NODE_MVAR,v,Qnil,Qnil) #define NEW_CVAR(v) newnode(NODE_CVAR,v,Qnil,Qnil) #define NEW_LIT(l) newnode(NODE_LIT,l,Qnil,Qnil) #define NEW_STR(s) newnode(NODE_STR,s,Qnil,Qnil) @@ -356,24 +356,15 @@ VALUE boot_defclass(name, super) char *name; VALUE super; { + extern st_table *rb_class_tbl; struct RClass *obj = (struct RClass*)class_new(super); + ID id = rb_intern(name); - rb_name_class(obj, rb_intern(name)); + rb_name_class(obj, id); + st_add_direct(rb_class_tbl, id, obj); return (VALUE)obj; } -Fdo() -{ - return rb_yield(Qnil); -} - -Fforever() -{ - for (;;) { - rb_yield(Qnil); - } -} - VALUE TopSelf; VALUE TRUE = 1; @@ -437,9 +428,6 @@ Init_Object() rb_define_private_method(C_Kernel, "sprintf", Fsprintf, -1); rb_define_alias(C_Kernel, "format", "sprintf"); - rb_define_private_method(C_Kernel, "do", Fdo, 0); - rb_define_private_method(C_Kernel, "forever", Fforever, 0); - rb_define_private_method(C_Object, "init_object", Fobj_init_object, -1); rb_define_method(C_Object, "clone", Fobj_clone, 0); @@ -478,8 +466,8 @@ Init_Object() TRUE = obj_alloc(C_Object); rb_define_single_method(TRUE, "to_s", Ftrue_to_s, 0); - rb_define_const(C_Kernel, "%TRUE", TRUE); - rb_define_const(C_Kernel, "%FALSE", FALSE); + rb_define_const(C_Kernel, "TRUE", TRUE); + rb_define_const(C_Kernel, "FALSE", FALSE); init_object = rb_intern("init_object"); } @@ -326,11 +326,11 @@ lhs : variable $$ = attrset($1, $3, Qnil); } -inc_list : IDENTIFIER +inc_list : CONSTANT { $$ = NEW_INC($1); } - | inc_list comma IDENTIFIER + | inc_list comma CONSTANT { $$ = block_append($1, NEW_INC($3)); } @@ -346,6 +346,7 @@ inc_list : IDENTIFIER } fname : IDENTIFIER + | CONSTANT | op { lex_state = EXPR_END; @@ -718,7 +719,7 @@ primary : literal | primary '{' opt_iter_var '|' compexpr rbrace { if (nd_type($1) == NODE_LVAR - || nd_type($1) == NODE_MVAR) { + || nd_type($1) == NODE_CVAR) { $1 = NEW_CALL(Qnil, $1->nd_vid, Qnil); } $$ = NEW_ITER($3, $1, $5); @@ -775,14 +776,17 @@ primary : literal $$ = NEW_BEGIN($2, $3, $4); } } - | LPAREN compexpr rparen + | LPAREN expr + opt_nl + rparen { $$ = $2; } - | CLASS IDENTIFIER superclass + | CLASS CONSTANT superclass { - if (cur_class || cur_mid || in_single) - Error("nested class definition"); + if (cur_mid || in_single) + Error("class definition in method body"); + cur_class = $2; push_local(); } @@ -793,10 +797,10 @@ primary : literal pop_local(); cur_class = Qnil; } - | MODULE IDENTIFIER + | MODULE CONSTANT { - if (cur_class != Qnil) - Error("nested module definition"); + if (cur_mid || in_single) + Error("module definition in method body"); cur_class = $2; in_module = 1; push_local(); @@ -877,12 +881,7 @@ case_body : WHEN args then } cases : opt_else - | WHEN args then - compexpr - cases - { - $$ = NEW_WHEN($2, $4, $5); - } + | case_body resque : /* none */ { @@ -916,7 +915,6 @@ literal : numeric symbol : fname | IVAR | GVAR - | CONSTANT numeric : INTEGER | FLOAT @@ -947,7 +945,7 @@ superclass : term { lex_state = EXPR_BEG; } - IDENTIFIER + CONSTANT { $$ = $3; } @@ -1069,6 +1067,9 @@ assoc : arg ASSOC arg opt_term : /* none */ | term +opt_nl : /* none */ + | nl + term : sc | nl @@ -1158,6 +1159,7 @@ static int parse_regx() { register int c; + int casefold = 0; int in_brack = 0; int re_start = sourceline; NODE *list = Qnil; @@ -1199,6 +1201,13 @@ parse_regx() if (in_brack) break; + if ('i' == nextc()) { + casefold = 1; + } + else { + pushback(); + } + tokfix(); lex_state = EXPR_END; if (list) { @@ -1207,11 +1216,13 @@ parse_regx() list_append(list, NEW_STR(ss)); } nd_set_type(list, NODE_DREGX); + if (casefold) list->nd_cflag = 1; yylval.node = list; return DREGEXP; } else { yylval.val = regexp_new(tok(), toklen()); + if (casefold) FL_SET(yylval.val, FL_USER1); return REGEXP; } case -1: @@ -1347,7 +1358,6 @@ yylex() { register int c; struct kwtable *low = kwtable, *mid, *high = LAST(kwtable); - int last; retry: switch (c = nextc()) { @@ -1366,6 +1376,10 @@ retry: while ((c = nextc()) != '\n') { if (c == -1) return 0; + if (c == '\\') { /* skip a char */ + c = nextc(); + if (c == '\n') sourceline++; + } } /* fall through */ case '\n': @@ -1801,22 +1815,13 @@ retry: return '\\'; case '%': - if (lex_state == EXPR_BEG || lex_state == EXPR_MID) { - /* class constant */ - newtok(); - tokadd('%'); - c = nextc(); - break; - } - else { - lex_state = EXPR_BEG; - if (nextc() == '=') { - yylval.id = '%'; - return OP_ASGN; - } - pushback(); - return c; + lex_state = EXPR_BEG; + if (nextc() == '=') { + yylval.id = '%'; + return OP_ASGN; } + pushback(); + return c; case '$': newtok(); @@ -1844,14 +1849,16 @@ retry: case '>': /* $>: default output handle */ case '"': /* $": already loaded files */ tokadd(c); - tokadd('\0'); - goto id_fetch; + tokfix(); + yylval.id = rb_intern(tok()); + lex_state = EXPR_END; + return GVAR; default: - if (is_identchar(c)) - break; - pushback(); - return tok()[0]; + if (!is_identchar(c)) { + pushback(); + return '$'; + } } break; @@ -1886,51 +1893,55 @@ retry: pushback(); tokfix(); - /* See if it is a reserved word. */ - while (low <= high) { - mid = low + (high - low)/2; - if (( c = strcmp(mid->name, tok())) == 0) { - enum lex_state state = lex_state; - lex_state = mid->state; - if (state != EXPR_BEG) { - if (mid->id == IF) return IF_MOD; - if (mid->id == WHILE) return WHILE_MOD; - } - return mid->id; - } - else if (c < 0) { - low = mid + 1; - } - else { - high = mid - 1; - } - } - - id_fetch: { - enum lex_state state = lex_state; + int result; - lex_state = EXPR_END; - yylval.id = rb_intern(tok()); switch (tok()[0]) { - case '%': - return CONSTANT; case '$': - return GVAR; + result = GVAR; + break; case '@': - return IVAR; + result = IVAR; + break; default: - if (state == EXPR_FNAME) { + /* See if it is a reserved word. */ + while (low <= high) { + mid = low + (high - low)/2; + if (( c = strcmp(mid->name, tok())) == 0) { + enum lex_state state = lex_state; + lex_state = mid->state; + if (state != EXPR_BEG) { + if (mid->id == IF) return IF_MOD; + if (mid->id == WHILE) return WHILE_MOD; + } + return mid->id; + } + else if (c < 0) { + low = mid + 1; + } + else { + high = mid - 1; + } + } + + if (lex_state == EXPR_FNAME) { if ((c = nextc()) == '=') { - yylval.id &= ~ID_SCOPE_MASK; - yylval.id |= ID_ATTRSET; + tokadd(c); } else { pushback(); } } - return IDENTIFIER; + if (isupper(tok()[0])) { + result = CONSTANT; + } + else { + result = IDENTIFIER; + } } + lex_state = EXPR_END; + yylval.id = rb_intern(tok()); + return result; } } @@ -1949,7 +1960,7 @@ var_extend(list, term) tokadd('#'); pushback(); return list; - case '@': case '%': + case '@': t = nextc(); pushback(); if (!is_identchar(t)) { @@ -1998,7 +2009,7 @@ var_extend(list, term) goto fetch_id; } /* through */ - case '@': case '%': + case '@': tokadd(c); c = nextc(); break; @@ -2370,7 +2381,7 @@ gettable(id) if (local_id(id)) return NEW_LVAR(id); else - return NEW_MVAR(id); + return NEW_LVAR2(id); } else if (is_global_id(id)) { return NEW_GVAR(id); @@ -2510,9 +2521,7 @@ cond(node) case NODE_GASGN: case NODE_IASGN: case NODE_CASGN: - if (verbose) { - Warning("asignment in condition"); - } + Warning("asignment in condition"); break; } @@ -2771,11 +2780,6 @@ rb_intern(name) case '@': id |= ID_INSTANCE; break; - case '%': - if (name[1] != '\0') { - id |= ID_CONST; - break; - } /* fall through */ default: if (name[0] != '_' && !isalpha(name[0]) && !ismbchar(name[0])) { @@ -2792,6 +2796,7 @@ rb_intern(name) if (id == Qnil) Bug("Unknown operator `%s'", name); break; } + last = strlen(name)-1; if (name[last] == '=') { /* attribute asignment */ @@ -2803,6 +2808,9 @@ rb_intern(name) id &= ~ID_SCOPE_MASK; id |= ID_ATTRSET; } + else if (isupper(name[0])) { + id |= ID_CONST; + } else { id |= ID_LOCAL; } @@ -2861,40 +2869,6 @@ rb_id2name(id) return find_ok; } -char * -rb_class2name(class) - struct RClass *class; -{ - extern st_table *rb_class_tbl; - - find_ok = Qnil; - - switch (TYPE(class)) { - case T_ICLASS: - class = (struct RClass*)RBASIC(class)->class; - break; - case T_CLASS: - case T_MODULE: - break; - default: - Fail("0x%x is not a class/module", class); - } - - while (FL_TEST(class, FL_SINGLE)) { - class = (struct RClass*)class->super; - } - - while (TYPE(class) == T_ICLASS) { - class = (struct RClass*)class->super; - } - - st_foreach(rb_class_tbl, id_find, class); - if (find_ok) { - return rb_id2name((ID)find_ok); - } - Bug("class 0x%x not named", class); -} - static int const_check(id, val, class) ID id; @@ -508,9 +508,9 @@ Init_process() rb_define_module_function(M_Process, "getpriority", Fproc_getpriority, 2); rb_define_module_function(M_Process, "setpriority", Fproc_setpriority, 3); - rb_define_const(M_Process, "%PRIO_PROCESS", INT2FIX(PRIO_PROCESS)); - rb_define_const(M_Process, "%PRIO_PGRP", INT2FIX(PRIO_PGRP)); - rb_define_const(M_Process, "%PRIO_USER", INT2FIX(PRIO_USER)); + rb_define_const(M_Process, "PRIO_PROCESS", INT2FIX(PRIO_PROCESS)); + rb_define_const(M_Process, "PRIO_PGRP", INT2FIX(PRIO_PGRP)); + rb_define_const(M_Process, "PRIO_USER", INT2FIX(PRIO_USER)); rb_define_module_function(M_Process, "uid", Fproc_getuid, 0); rb_define_module_function(M_Process, "uid=", Fproc_setuid, 1); @@ -128,8 +128,9 @@ research(reg, str, start, ignorecase) { int result; - if (FL_TEST(reg, FL_USER1)) { /* case-flag set for the object */ - ignorecase = FL_TEST(reg, FL_USER2); /* case-flag */ + /* case-flag set for the object */ + if (FL_TEST(reg, FL_USER1)) { + ignorecase = 1; } if (ignorecase) reg->ptr->pat.translate = casetable; @@ -387,14 +388,8 @@ Sreg_new(argc, argv, self) Check_Type(src, T_STRING); } - if (argc == 2) { + if (argc == 2 && argv[1]) { FL_SET(reg, FL_USER1); - if (argv[1]) { - FL_SET(reg, FL_USER2); - } - else { - FL_UNSET(reg, FL_USER2); - } } return Qnil; @@ -164,7 +164,29 @@ ruby \- オブジェクト指向スクリプト言語 .TP 5 .B \-S スクリプト名が`/'で始まっていない場合, 環境変数`PATH'の値を -使ってスクリプトを探す. +使ってスクリプトを探す. これは、#! をサポートしていないマシ +ンで、#! による実行をエミュレートするために、以下のようにし +て使うことができる: + + #! /usr/local/bin/ruby + # This line makes the next one a comment in ruby \ + eval "exec /usr/local/bin/ruby -S $0 $*" + +システムは最初の行を無視し,スクリプトを`/bin/sh'に渡す. +`/bin/sh'はrubyスクリプトをシェルスクリプトとして実行しよう +とする.シェルは2行目だけをコメントであると解釈し,3行目を通 +常のシェルコマンドとして実行し,rubyインタプリタを起動する. + +システムによっては`$0'は必ずしもフルパスを含まないので,`-S' +を用いてrubyに必要に応じてスクリプトを探すように指示する. +rubyがスクリプトを見つけると、これらの行の解析を始めるが, +rubyは2行目の行末にあるバックスラッシュにより,2行目のコメン +トが3行目まで継続するとみなして,3行目を無視する. + +ファイル名に含まれるスペースなどを正しく扱うには,`$*'よりも +`${1+"$@"}'のほうがよいだろうが,cshが解釈する場合には動作し +ない. + .TP 5 .B \-v, \-\-verbose 冗長モード. 起動時にバージョン番号の表示を行い, システム変数 @@ -52,7 +52,7 @@ char *rb_dln_argv0; #endif static void load_stdin(); -void rb_load_file(); +static void load_file(); static int do_loop = FALSE, do_print = FALSE; static int do_check = FALSE, do_line = FALSE; @@ -210,7 +210,7 @@ proc_options(argcp, argvp) script = dln_find_file(script, getenv("PATH")); if (!script) script = argv[optind]; } - rb_load_file(script); + load_file(script, 1); optind++; } } @@ -242,12 +242,13 @@ proc_options(argcp, argvp) } static void -readin(fd, fname) +readin(fd, fname, script) int fd; char *fname; + int script; { struct stat st; - char *ptr, *p, *pend; + char *ptr, *p, *pend, *s; if (fstat(fd, &st) < 0) rb_sys_fail(fname); if (!S_ISREG(st.st_mode)) @@ -259,39 +260,62 @@ readin(fd, fname) rb_sys_fail(fname); } pend = p + st.st_size; - if (xflag) { - char *s = p; + *pend = '\0'; + + if (script) { + if (xflag) { + xflag = FALSE; + while (p < pend) { + if (p[0] == '#' && p[1] == '!') { + char *s = p; + while (s < pend && *s != '\n') s++; + if (*s == '\n') { + *s = '\0'; + if (strstr(p, "ruby")) { + *s = '\n'; + goto start_read; + } + } + p = s + 1; + } + else { + while (p < pend && *p++ != '\n') + ; + if (p >= pend) break; + } + } + free(ptr); + Fail("No Ruby script found in input"); + } + + start_read: + if (p[0] == '#' && p[1] == '!') { + char *s = p, *q; - *pend = '\0'; - xflag = FALSE; - while (p < pend) { while (s < pend && *s != '\n') s++; - if (*s != '\n') break; - *s = '\0'; - if (p[0] == '#' && p[1] == '!' && strstr(p, "ruby")) { - if (p = strstr(p, "ruby -")) { + if (*s == '\n') { + *s = '\0'; + if (q = strstr(p, "ruby -")) { int argc; char *argv[2]; char **argvp = argv; - argc = 2; argv[0] = Qnil; argv[1] = p + 5; + argc = 2; argv[0] = Qnil; argv[1] = q + 5; proc_options(&argc, &argvp); + p = s + 1; + } + else { + *s = '\n'; } - xflag = TRUE; - p = s + 1; - goto start_read; } - p = s + 1; } - free(ptr); - Fail("No Ruby script found in input"); } - start_read: lex_setsrc(fname, p, pend - p); yyparse(); free(ptr); } -void -rb_load_file(fname) +static void +load_file(fname, script) char *fname; + int script; { int fd; char *ptr; @@ -303,10 +327,17 @@ rb_load_file(fname) fd = open(fname, O_RDONLY, 0); if (fd < 0) rb_sys_fail(fname); - readin(fd, fname); + readin(fd, fname, script); close(fd); } +void +rb_load_file(fname) + char *fname; +{ + load_file(fname, 0); +} + static void load_stdin() { @@ -324,9 +355,6 @@ load_stdin() putc(c, f); } fclose(f); - - if (fd < 0) rb_sys_fail(buf); - readin(fd, "-"); } diff --git a/sample/Artistic b/sample/Artistic index fbf7989775..e69de29bb2 100644 --- a/sample/Artistic +++ b/sample/Artistic @@ -1,117 +0,0 @@ - - - - - The "Artistic License" - - Preamble - -The intent of this document is to state the conditions under which a -Package may be copied, such that the Copyright Holder maintains some -semblance of artistic control over the development of the package, -while giving the users of the package the right to use and distribute -the Package in a more-or-less customary fashion, plus the right to make -reasonable modifications. - -Definitions: - - "Package" refers to the collection of files distributed by the - Copyright Holder, and derivatives of that collection of files - created through textual modification. - - "Standard Version" refers to such a Package if it has not been - modified, or has been modified in accordance with the wishes - of the Copyright Holder. - - "Copyright Holder" is whoever is named in the copyright or - copyrights for the package. - - "You" is you, if you're thinking about copying or distributing - this Package. - - "Reasonable copying fee" is whatever you can justify on the - basis of media cost, duplication charges, time of people involved, - and so on. (You will not be required to justify it to the - Copyright Holder, but only to the computing community at large - as a market that must bear the fee.) - - "Freely Available" means that no fee is charged for the item - itself, though there may be fees involved in handling the item. - It also means that recipients of the item may redistribute it - under the same conditions they received it. - -1. You may make and give away verbatim copies of the source form of the -Standard Version of this Package without restriction, provided that you -duplicate all of the original copyright notices and associated disclaimers. - -2. You may apply bug fixes, portability fixes and other modifications -derived from the Public Domain or from the Copyright Holder. A Package -modified in such a way shall still be considered the Standard Version. - -3. You may otherwise modify your copy of this Package in any way, provided -that you insert a prominent notice in each changed file stating how and -when you changed that file, and provided that you do at least ONE of the -following: - - a) place your modifications in the Public Domain or otherwise make them - Freely Available, such as by posting said modifications to Usenet or - an equivalent medium, or placing the modifications on a major archive - site such as uunet.uu.net, or by allowing the Copyright Holder to include - your modifications in the Standard Version of the Package. - - b) use the modified Package only within your corporation or organization. - - c) rename any non-standard executables so the names do not conflict - with standard executables, which must also be provided, and provide - a separate manual page for each non-standard executable that clearly - documents how it differs from the Standard Version. - - d) make other distribution arrangements with the Copyright Holder. - -4. You may distribute the programs of this Package in object code or -executable form, provided that you do at least ONE of the following: - - a) distribute a Standard Version of the executables and library files, - together with instructions (in the manual page or equivalent) on where - to get the Standard Version. - - b) accompany the distribution with the machine-readable source of - the Package with your modifications. - - c) accompany any non-standard executables with their corresponding - Standard Version executables, giving the non-standard executables - non-standard names, and clearly documenting the differences in manual - pages (or equivalent), together with instructions on where to get - the Standard Version. - - d) make other distribution arrangements with the Copyright Holder. - -5. You may charge a reasonable copying fee for any distribution of this -Package. You may charge any fee you choose for support of this Package. -You may not charge a fee for this Package itself. However, -you may distribute this Package in aggregate with other (possibly -commercial) programs as part of a larger (possibly commercial) software -distribution provided that you do not advertise this Package as a -product of your own. - -6. The scripts and library files supplied as input to or produced as -output from the programs of this Package do not automatically fall -under the copyright of this Package, but belong to whomever generated -them, and may be sold commercially, and may be aggregated with this -Package. - -7. C subroutines supplied by you and linked into this Package in order -to emulate subroutines and variables of the language defined by this -Package shall not be considered part of this Package, but are the -equivalent of input as in Paragraph 6, provided these subroutines do -not change the language in any way that would cause it to fail the -regression tests for the language. - -8. The name of the Copyright Holder may not be used to endorse or promote -products derived from this software without specific prior written permission. - -9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR -IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED -WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. - - The End diff --git a/sample/MANIFEST b/sample/MANIFEST index c54adf31ef..c91c078e39 100644 --- a/sample/MANIFEST +++ b/sample/MANIFEST @@ -1,5 +1,4 @@ MANIFEST -Artistic aset.rb attr.rb biorhythm.rb @@ -39,7 +38,7 @@ occur.pl occur.rb occur2.rb opt_s.rb -opt_x.rb +opt_x.test parsearg.rb rcs.awk rcs.dat diff --git a/sample/attr.rb b/sample/attr.rb index 83265295f3..1d329ea06a 100644 --- a/sample/attr.rb +++ b/sample/attr.rb @@ -4,7 +4,7 @@ # #<Foo: @test=10> class Foo - attr "test", %TRUE + attr "test", TRUE end foo = Foo.new diff --git a/sample/cbreak.rb b/sample/cbreak.rb index 6d3d551569..5d2d849512 100644 --- a/sample/cbreak.rb +++ b/sample/cbreak.rb @@ -1,31 +1,31 @@ # ioctl example works on Sun -%CBREAK = 0x00000002 -%ECHO = 0x00000008 -%TIOCGETP = 0x40067408 -%TIOCSETP = 0x80067409 +CBREAK = 0x00000002 +ECHO = 0x00000008 +TIOCGETP = 0x40067408 +TIOCSETP = 0x80067409 def cbreak () - set_cbreak(%TRUE) + set_cbreak(TRUE) end def cooked () - set_cbreak(%FALSE) + set_cbreak(FALSE) end def set_cbreak (on) tty = "\0" * 256 - $stdin.ioctl(%TIOCGETP, tty) + $stdin.ioctl(TIOCGETP, tty) ttys = tty.unpack("C4 S") if on - ttys[4] |= %CBREAK - ttys[4] &= ~%ECHO + ttys[4] |= CBREAK + ttys[4] &= ~ECHO else - ttys[4] &= ~%CBREAK - ttys[4] |= %ECHO + ttys[4] &= ~CBREAK + ttys[4] |= ECHO end tty = ttys.pack("C4 S") - $stdin.ioctl(%TIOCSETP, tty) + $stdin.ioctl(TIOCSETP, tty) end cbreak(); diff --git a/sample/const.rb b/sample/const.rb index 783965cfd5..50780407b8 100644 --- a/sample/const.rb +++ b/sample/const.rb @@ -2,23 +2,23 @@ # output: # 1234 # 1268 -%test1 = 1 -%test2 = 2 +TEST1 = 1 +TEST2 = 2 module Const - %test3 = 3 - %test4 = 4 + TEST3 = 3 + TEST4 = 4 end module Const2 - %test3 = 6 - %test4 = 8 + TEST3 = 6 + TEST4 = 8 end include Const -print(%test1,%test2,%test3,%test4,"\n") +print(TEST1,TEST2,TEST3,TEST4,"\n") include Const2 -print(%test1,%test2,%test3,%test4,"\n") +print(TEST1,TEST2,TEST3,TEST4,"\n") diff --git a/sample/evaldef.rb b/sample/evaldef.rb index 1d77a3c008..a705300a94 100644 --- a/sample/evaldef.rb +++ b/sample/evaldef.rb @@ -3,7 +3,7 @@ # bar # (eval):26: method `baz' not available for "#<foo: 0xbfc5c>"(foo) -class foo +class Foo def foo eval(" def baz @@ -12,7 +12,7 @@ end") end end -class bar:foo +class Bar : Foo def bar baz() end diff --git a/sample/export.rb b/sample/export.rb index 03b9492e5a..a6dfa8e40e 100644 --- a/sample/export.rb +++ b/sample/export.rb @@ -3,7 +3,7 @@ # foobar # foo -class foo +class Foo export :printf end diff --git a/sample/getopts.rb b/sample/getopts.rb index 01eddcea98..37fd3dc69d 100644 --- a/sample/getopts.rb +++ b/sample/getopts.rb @@ -28,7 +28,7 @@ # # IvVw, $OPT_?? non-nil , I # vVZbg. -# -f -> $OPT_f = %TRUE +# -f -> $OPT_f = TRUE # --geometry 300x400 -> $OPT_geometry = 300x400 # # - -- , ~, SIvV. @@ -50,7 +50,8 @@ def getopts(single_opts, *opts) end end end - + + opts = {} count = 0 while ($ARGV.length != 0) compare = nil @@ -68,11 +69,13 @@ def getopts(single_opts, *opts) return nil end eval("$OPT_" + compare + " = " + '$ARGV[1]') + opts[compare] = TRUE $ARGV.shift count += 1 break elsif (option == compare) - eval("$OPT_" + compare + " = %TRUE") + eval("$OPT_" + compare + " = TRUE") + opts[compare] = TRUE count += 1 break end @@ -82,16 +85,19 @@ def getopts(single_opts, *opts) for index in 1..($ARGV[0].length - 1) compare = $ARGV[0][index, 1] if (single_opts && compare =~ "[" + single_opts + "]") - eval("$OPT_" + compare + " = %TRUE") + eval("$OPT_" + compare + " = TRUE") + opts[compare] = TRUE count += 1 elsif (single_colon != "" && compare =~ "[" + single_colon + "]") if ($ARGV[0][index..-1].length > 1) eval("$OPT_" + compare + " = " + '$ARGV[0][(index + 1)..-1]') + opts[compare] = TRUE count += 1 elsif ($ARGV.length <= 1) return nil else eval("$OPT_" + compare + " = " + '$ARGV[1]') + opts[compare] = TRUE $ARGV.shift count = count + 1 end @@ -103,7 +109,7 @@ def getopts(single_opts, *opts) end $ARGV.shift - if (!defined("$OPT_" + compare)) + if (!opts.includes(compare)) return nil end end diff --git a/sample/getopts.test b/sample/getopts.test index cdb818d390..adef7628db 100755 --- a/sample/getopts.test +++ b/sample/getopts.test @@ -1,4 +1,4 @@ -#! /mp/free/bin/ruby -- -*- ruby -*- +#! /usr/local/bin/ruby load("parsearg.rb") diff --git a/sample/less.rb b/sample/less.rb index 6960b9c03d..b0906d5d22 100755 --- a/sample/less.rb +++ b/sample/less.rb @@ -1,4 +1,4 @@ -#! /usr/local/bin/ruby -- -*- ruby -*- +#! /usr/local/bin/ruby ZCAT = "/usr/local/bin/zcat" LESS = "/usr/local/bin/less" diff --git a/sample/opt_x.rb b/sample/opt_x.rb index 47a67f6cfa..e69de29bb2 100644 --- a/sample/opt_x.rb +++ b/sample/opt_x.rb @@ -1,10 +0,0 @@ -test for option `-x' - -this is a forwarding header -this is a header too. - -from here script starts -#! ./ruby -v -print("tt\n") -__END__ -this is a trailer diff --git a/sample/opt_x.test b/sample/opt_x.test new file mode 100644 index 0000000000..47a67f6cfa --- /dev/null +++ b/sample/opt_x.test @@ -0,0 +1,10 @@ +test for option `-x' + +this is a forwarding header +this is a header too. + +from here script starts +#! ./ruby -v +print("tt\n") +__END__ +this is a trailer diff --git a/sample/ruby-mode.el b/sample/ruby-mode.el index 4396b1098d..20e1abebc9 100644 --- a/sample/ruby-mode.el +++ b/sample/ruby-mode.el @@ -8,7 +8,7 @@ ;;; (defconst ruby-block-beg-re - "class\\|module\\|def\\|if\\|unless\\|case\\|while\\|until\\|for\\|begin" + "class\\|module\\|def\\|if\\|case\\|while\\|for\\|begin" ) (defconst ruby-block-mid-re @@ -170,10 +170,14 @@ The variable ruby-indent-level controls the amount of indentation. ((or (string= "\"" w) ;skip string (string= "'" w) (string= "`" w)) - (if (search-forward w indent-point t) - nil + (cond + ((string= w (char-to-string (char-after (point)))) + (forward-char 1)) + ((re-search-forward (format "[^\\]%s" w) indent-point t) + nil) + (t (goto-char indent-point) - (setq in-string t))) + (setq in-string t)))) ((or (string= "/" w) (string= "<" w)) (if (string= "<" w) (setq w ">")) @@ -189,7 +193,9 @@ The variable ruby-indent-level controls the amount of indentation. (and (eq c ?w) (save-excursion (forward-word -1) - (looking-at ruby-block-beg-re)))) + (or + (looking-at ruby-block-beg-re) + (looking-at ruby-block-mid-re))))) (if (search-forward w indent-point t) nil (goto-char indent-point) diff --git a/sample/svr.rb b/sample/svr.rb index 1c0104cc40..91faa56dd8 100644 --- a/sample/svr.rb +++ b/sample/svr.rb @@ -5,7 +5,7 @@ gs = TCPserver.open(0) printf("server port is on %d\n", gs.port) socks = [gs] -while %TRUE +while TRUE nsock = select(socks); if nsock == nil; continue end for s in nsock[0] diff --git a/sample/tt.rb b/sample/tt.rb index c53ec39d68..4afa2022e6 100644 --- a/sample/tt.rb +++ b/sample/tt.rb @@ -43,7 +43,7 @@ if offset = (ttt =~ /this ([^ ]*) (.*)/) println("2 = ", $2); end -class Fib:Object +class Fib : Object print("in Fib:Object\n") def Fib.test(*args) @@ -54,9 +54,10 @@ tab), CR(carriage return),改頁(form feed)である.改行(newline)は明ら 識別子の長さに制限はない.現在の実装は識別子としてマルチバイトコード (EUC,SJIS)も通すが勧められない. -グローバル変数名は"$"に続く識別子または記号1文字,インスタンス変数は -"@"に続く識別子,クラス定数は"%"に続く識別子である.メソッド名,ローカ -ル変数名とクラス名は単なる識別子を用いる. +グローバル変数名は"$"に続く識別子または記号1文字,インスタンス変数は +"@"に続く識別子,定数には大文字で始まる識別子,ローカル変数名は小文字 +で始まる識別子である.メソッド名には単なる識別子を用いる(メソッド名は +大文字でも小文字でも始められる). ** コメント @@ -126,12 +127,15 @@ Rubyではnilが偽,それ以外が真と評価される.CやPerl などとは異なり,0や \M-c メタ文字(c|0x80) \それ以外 文字そのもの +文字列式は毎回新しい文字列オブジェクトを生成するので,文字列の内容を変 +更してももともとの文字列は変わらない. + 変数展開 ダブルクォート(`"')で囲まれた文字列と正規表現の中では `#{変数名}'とい -う形式で変数の内容を展開することができる.変数が変数記号(`$',`@',`%') - で始まる場合には`#変数名'という形式でも展開できる.文字`#'に続く文字 -が `{',`$',`@',`%'でなければ,そのまま`#'として解釈される. +う形式で変数の内容を展開することができる.変数が変数記号(`$',`@')で始 +まる場合には`#変数名'という形式でも展開できる.文字`#'に続く文字が +`{',`$',`@'でなければ,そのまま`#'として解釈される. ** コマンド出力 @@ -142,7 +146,7 @@ Rubyではshのようにコマンドの実行結果を文字列リテラルのように使うことが ** 正規表現式 - /.../ + /.../[i] ^ 行頭 $ 行末 @@ -164,7 +168,8 @@ Rubyではshのようにコマンドの実行結果を文字列リテラルのように使うことが | 選択 ( ) 正規表現をまとめる -その他に文字列と同じバックスラッシュ記法や変数展開も有効である. +その他に文字列と同じバックスラッシュ記法や変数展開も有効である.正規表 +現の後ろにiがついた時には大文字小文字の違いを無視する. ** ワイルドカード式 @@ -226,57 +231,32 @@ Rubyの変数はスコープ(有効範囲)と寿命(有効期限)によって4種類に分類され, メソッドから参照できる.スコープはメソッド内であり,その寿命はオブジェ クトの寿命に等しい. -*** クラス名/モジュール名/ローカル変数 +*** クラス定数 例: - Array - Math - foobar + FOOBAR -アルファベットまたは`_'で始まる変数は識別子とも呼ばれ,ローカル変数, -クラス名またはモジュール名である. +大文字で始まる識別子ははクラス定数へのアクセスであり,そのクラスと全て +のサブクラスのインスタンスから参照できる.この定数へはトップレベル,す +なわちメソッドが定義できるレベルでのみ代入可能である.この変数はクラス +間で値が共有され,一度代入すると値を変更することができない.クラス定数 +の寿命はクラスの寿命と等しい. -初期状態では識別子はクラス/モジュール名とみなされるが(該当するクラスが -存在しない場合の値はnil),代入式の左辺に現れた識別子は,そのスコープ内 -ではローカル変数として見なされ,同名のクラスやモジュールは隠される.こ -の意味で識別子への代入は宣言としての働きも持つ. +クラス定義は自動的に定数を定義するので,クラス名は定数である. - Array # 配列クラス Array - Array = 15 # 代入.以後Arrayはローカル変数 - print Array, "\n" # `15'が出力される +*** ローカル変数 -この宣言としての代入の解釈はコンパイル時に行なわれるため,識別子への代 -入式が実際に実行されてもされなくても,以降のスコープ内ではその識別子は -ローカル変数とみなされる. +例: - Array # 配列クラス Array - if %FALSE - Array = 15 # このコードは実行されないが, - # 以降Arrayはローカル変数とみなされる. - end - print Array, "\n" # `nil'が出力される + foobar -このルールは一見複雑だが,クラス/モジュール名とローカル変数名が重複し -ない限り,未初期化のローカル変数の値はnilであると考えても差し支えはな -い.Rubyの組み込みクラスは大文字のアルファベットで始まる名前がついて -おり,ユーザもクラス/モジュール名には大文字で始まる識別子を,ローカル -変数名には小文字または`_'で始まる識別子を使うことを強く推奨する. +小文字または`_'で始まる識別子はローカル変数へのアクセスである.初期化 +されないローカル変数の値はnilである -ローカル変数のスコープも寿命もそのブロックの終りまで(トップレベルのロー +ローカル変数のスコープは寿命もそのブロックの終りまで(トップレベルのロー カル変数はプログラムの終了まで)である. -*** クラス定数 - -例: - - %foobar - -`%'で始まる変数はクラス定数であり,そのクラスと全てのサブクラスのイン -スタンスから参照できる.この定数へはトップレベル,すなわちメソッドが定 -義できるレベルでのみ代入可能である.この変数はクラス間で値が共有され, -一度代入すると値を変更することができない. - *** 疑似変数 通常の変数以外に疑似変数と呼ばれる特殊な変数が4つある. @@ -300,14 +280,6 @@ Rubyの変数はスコープ(有効範囲)と寿命(有効期限)によって4種類に分類され, `(' 式 `)' -括弧の中には単なる式だけではなく,式の並び(=プログラム)を置くことが出 -来る. - - `(' 式 `;' 式.. `)' - -式の並びの値は最後に評価した式の値である.つまりの値は最後に評価した式 -の値になる. - ** 配列式 例: @@ -365,10 +337,13 @@ Rubyの変数はスコープ(有効範囲)と寿命(有効期限)によって4種類に分類され, メッセージ式で,レシーバがselfの場合,レシーバを省略して通常のプログラ ミング言語における関数のような形式でメソッドを呼び出すことができる. -メソッド呼び出しでは曖昧さがない時には括弧を省略できる.曖昧さがある時 -とは第一引数が以下の文字または予約語で始まる場合である. +メソッド呼び出しの引数の周りの括弧を省略できるが,第一引数となる式が以 +下の文字または予約語で始まる場合は,優先順位の関係で予想通りの結果が得 +られない場合がある. - (, [, {, <, /, %, +, -, if, while + (, [, {, <, /, +, -, if, while + +どのように評価されるか曖昧な場合には括弧をつける事. 例: foo bar+baz # メソッド呼び出しfoo(bar+baz) @@ -416,31 +391,31 @@ Rubyの変数はスコープ(有効範囲)と寿命(有効期限)によって4種類に分類され, foo[0] = bar foo.bar = baz -代入には変数に対する代入(真の代入)と,プログラムを簡単にするためのシン -タックスシュガーとしての代入がある.真の代入は以下の形式である. +代入式は変数などに値を設定するために用いられる.代入式は演算子形式をとっ +ているが,メソッドではないので再定義することはできない.左辺になること +が出来るのは以下の3種類の式である. - 変数 `=' 式 +変数(`$'識別子 | `@'識別子 | 識別子) -これは式を評価し,変数の値として代入する.クラスやモジュールや疑似変数 -には代入できない.クラスやモジュールの定義を変更するためにはclass式, -module式を用いる.代入式は演算子形式をとっているが,メソッドではないの -で再定義することはできない. + 変数 `=' 式 -シンタックスシュガーとしての代入式は以下のものがある. +変数への代入は右辺の式を評価して得られた値を左辺で指定された変数に代入 +する. -配列要素への代入 +配列参照(式[式..]) 式1`[' 式2.. `]' `=' 式n -式1を評価して得られるオブジェクトに,式2から式nまでを引数として,"[]=" -というメソッドを呼び出す. +配列参照式への代入は,式1を評価して得られるオブジェクトに,式2から式n +までを引数として,"[]=" というメソッドを呼び出す. -属性代入 +属性参照(式`.'識別子) 式1 `.' 識別子 `=' 式2 -式1を評価して得られるオブジェクト(レシーバが省略された場合は`self')に -対して,"識別子="というメソッドを式 2を引数として呼び出す. +属性参照(引数なしのメソッド呼び出し)への代入は,式1を評価して得られる +オブジェクト(レシーバが省略された場合は`self')に対して,"識別子="とい +うメソッドを式 2を引数として呼び出す. ** 自己代入 @@ -472,11 +447,12 @@ module式を用いる.代入式は演算子形式をとっているが,メソッドではないの 左辺 `,' [左辺 `,'..] [`*' 左辺]= 式 [, 式..] -右辺の式が一つしかない場合は,その値を配列として(必要ならばto_aメソッ -ドで配列に変換して),要素をそれぞれ左辺に代入する.それ以外の場合には, -それぞれの式の値が左辺に代入される.左辺の数と右辺の要素の数が合わない -時には足りない変数には nilが代入され,余った要素は無視される.多重代入 -の最後の要素の前に`*'がある場合,残りの全て引数が配列として代入される. +左辺には代入式の3種類の式が来る.右辺の式が一つしかない場合は,その値 +を配列として(必要ならばto_aメソッドで配列に変換して),要素をそれぞれ左 +辺に代入する.それ以外の場合には,それぞれの式の値が左辺に代入される. +左辺の数と右辺の要素の数が合わない時には足りない変数には nilが代入され, +余った要素は無視される.多重代入の最後の要素の前に`*'がある場合,残り +の全て引数が配列として代入される. foo, bar = [1, 2] # foo = 1; bar = 2 foo, bar = 1, 2 # foo = 1; bar = 2 @@ -690,7 +666,7 @@ whileの条件判断部の式では文字列と正規表現リテラルは式「$_=~ リテラル」 イテレータとは制御構造(特にループ)の抽象化のために用いられるメソッドの 一種である.イテレータの呼び出しは以下の構文で行なわれる. - 式 `{' 変数.. `|' 式.. `}' + 式 `{' 左辺式.. `|' 式.. `}' 「式」をブロックとして設定し,「式」のメソッドをイテレータとして評価す る.「式」のトップレベルのメソッドだけがイテレータとして呼び出され, @@ -709,13 +685,13 @@ Enumerableモジュールのgrepメソッドのようにイテレータとして呼ばれた時と オブジェクトの各要素に対して操作を行なうための形式も提供されている.形 式は以下の通り. - for 変数.. in 式 + for 左辺式.. in 式 式 end 式の各要素に対し式を実行する.これは以下の式と等価である. - (式).each `{' 変数.. `|' 式 `}' + (式).each `{' 左辺式.. `|' 式 `}' よって式の値のオブジェクトがメソッドeachを持たない場合,forを実行する と例外が発生する. @@ -908,13 +884,6 @@ Rubyには厳密な意味では関数はないがKernelクラスの関数メソッドは(全ての exit()とは違って,例外処理などは一切行なわない.fork()の後,子 プロセスを終了させる時などに用いる. - do() - - ブロックを1度だけ実行するイテレータ.ブロックをまとめるためだ - けに存在する.使い方の例: - - do { foobar() } while (baz()) - eof() コマンドラインからの入力がEOFに到達している場合,真を返す. @@ -1048,8 +1017,8 @@ Rubyには厳密な意味では関数はないがKernelクラスの関数メソッドは(全ての fileをロードする.loadとの動作の違いはrequireはロードしたファ イルのフルパスを変数`$"'に覚えていて,既にロードしたファイルは - 再ロードしない点である.実際にロードした時には%TRUE,既にロー - ドされている時には%FALSEを返す. + 再ロードしない点である.実際にロードした時にはTRUE,既にロード + されている時にはFALSEを返す. select(reads[, writes[, execpts[, timeout]]]) @@ -1224,23 +1193,23 @@ Rubyには厳密な意味では関数はないがKernelクラスの関数メソッドは(全ての $VERSION rubyのバージョンを示す文字列 - %TRUE t - %FALSE nil + TRUE t + FALSE nil それぞれ真偽値を表す.条件判断はnilを偽,それ以外の全ての値を - 真として判断するため,%TRUEの値は代表的な真の値という以上の意 - 味を持たない.よって,あるメソッドの返値が真であるということと, - それが%TRUEを返すということは厳密には同じではない(述語的に用い - られるメソッドは大抵真の値として%TRUEを返すようにはなっている - が).つまり + 真として判断するため,TRUEの値は代表的な真の値という以上の意味 + を持たない.よって,あるメソッドの返値が真であるということと, + それがTRUEを返すということは厳密には同じではない(述語的に用い + られるメソッドは大抵真の値としてTRUEを返すようにはなっているが). + つまり if some.method() then ... else ... end と - if some.method() == %TRUE then ... else ... end + if some.method() == TRUE then ... else ... end - は完全には同義ではない.%FALSEに関しては,このような問題は生じ + は完全には同義ではない.FALSEに関しては,このような問題は生じ ない. * 組み込みクラスとモジュール @@ -1708,8 +1677,8 @@ Single Methods: ** Dict(クラス) 辞書あるいは連想配列.任意のオブジェクトを添字とできる配列のクラスであ -る.Hashという名前でもアクセスできる.連想配列オブジェクトの生成は一般 -的には連想配列式``{a=>b,..}'' で行なわれる. +る.連想配列オブジェクトの生成は一般的には連想配列式``{a=>b,..}'' で行 +なわれる. SuperClass: Object @@ -1796,10 +1765,9 @@ Single Methods: 新しい(空の)辞書オブジェクトを返す. -** Directory(クラス) +** Dir(クラス) ディレクトリ内の要素を順に返すディレクトリストリーム操作のためのクラス. -Dirという名前でもアクセスできる. SuperClass: Object @@ -2681,7 +2649,7 @@ Private Methods: 省略可能な第2引数publicが与えられて,かつその値がnilでない時に はその属性には属性設定メソッドも用意され,外部から代入可能にな - る.attr("attr", %TRUE)はクラス定義に以下のコードを追加するの + る.attr("attr", TRUE)はクラス定義に以下のコードを追加するの とほぼ同義である. def attr; @attr; end @@ -2690,7 +2658,7 @@ Private Methods: 属性を構成するメソッドを再定義することによって,アクセス時の動 作を変更できる.例えば - attr("test", %TRUE) + attr("test", TRUE) def test=(val) print("test was ", @test, "\n") print("and now is ", @test = val, "\n") @@ -2843,8 +2811,8 @@ Single Methods: whichとwhoで指定されるプロセス,プロセスグループ,ユーザの現在 の優先順位を返す.詳細はgetpriority(2)を参照.Processモジュー - ルではwhichとして指定できる定数%PRIO_PROCESS,%PRIO_PGRP, - %PRIO_USERが定義されている. + ルではwhichとして指定できる定数PRIO_PROCESS,PRIO_PGRP, + PRIO_USERが定義されている. gid @@ -2893,9 +2861,9 @@ Single Methods: しかし,この場合は以下の方が速い. - do 1.upto(5) + 1.upto(5) { ... - end + } 範囲オブジェクトを生成する`..'演算子の両辺はComparableを含むクラスのイ ンスタンスであれば何でも構わない.範囲は始点と終点を含むことに注意する @@ -3025,10 +2993,9 @@ Single Methods: new(string[, casefold]) 文字列を正規表現に変換したオブジェクトを返す.省略可能な第2引 - 数が与えられた時には,生成される正規表現オブジェクトはシステム - 変数`$='の値に関わらず,真の時は大文字小文字を無視し,偽の時は - 区別する.設定されない場合はマッチを行なった時点のシステム変数 - `$='の値によって区別するかしないかが決定される. + 数が与えられ,その値がnilでない時には,生成された正規表現オブ + ジェクトはシステム変数`$='の値に関わらず,マッチする時に大文字 + 小文字の違いを無視する. quote(str) @@ -3456,7 +3423,7 @@ TCP/IPストリーム型接続のサーバ側のソケットのクラス.このクラスによって gs = TCPserver.open(4444) socks = [gs] - while %TRUE + while TRUE nsock = select(socks); if nsock == nil; continue end for s in nsock[0] @@ -3766,7 +3733,8 @@ ARG : LHS `=' ARG | ARG `||' ARG | PRIMARY -PRIMARY : LITERAL +PRIMARY : `(' EXPR `)' + | LITERAL | VARIABLE | super `(' [CALL_ARGS] `)' | super @@ -3807,7 +3775,6 @@ PRIMARY : LITERAL [resque COMPEXPR] [ensure COMPEXPR] end - | `(' COMPEXPR `)' | class identifier `:' identifier COMPEXPR end @@ -3852,8 +3819,7 @@ ASSOCS : ASSOC (`,' ASSOC)* ASSOC : ARG `=>' ARG -VARIABLE : identifier - | VARNAME +VARIABLE : VARNAME | nil | self | `__FILE__' @@ -3880,7 +3846,7 @@ FNAME : identifier | `::' | `..' | `|' | `^' | `&' VARNAME : GLOBAL | `@'identifier - | `%'identifier + | identifier GLOBAL : `$'identifier | `$'any_char @@ -3889,18 +3855,22 @@ STRING : `"' any_char* `"' | `'' any_char* `'' | ``' any_char* ``' -REGEXP : `/' any_char* `/' +REGEXP : `/' any_char* `/'[i] GLOB : `<' any_char* `>' * 謝辞 -Rubyの言語仕様はC, Perl, Eiffelの各言語にこの順に影響を受けている. そ -の他に影響を受けた言語としてはtcl, AWK, bourne shell, CLU, Sather, -Icon, Smalltalk, Emacs Lispなどがある. またrubyの言語仕様を決定するた -めに協力して下さった方々を以下にあげる(敬称略): 石塚圭樹,大庭康生,伊 -藤純一郎,中村@NEC.関根@日本DEC,たなか@赤坂.富士通. +Rubyの言語仕様は数多くの言語の影響を受けている. 以下にあげるのはその主 +な言語である. + + C, Perl, CLU, Sather, CLOS, Eiffel, Icon, tcl, AWK, bourne shell, + Smalltalk, Emacs Lisp. + +またrubyの言語仕様を決定するために協力して下さった方々を以下にあげる + 石塚圭樹,大庭康生,伊藤純一郎,中村@NEC.関根@日本DEC, + たなか@赤坂.富士通(敬称略). ------------------------------------------------------- Local variables: fill-column: 70 @@ -209,9 +209,7 @@ str_subseq(str, beg, end) if (beg > end) { int tmp; - if (verbose) { - Warning("start %d is bigger than end %d", beg, end); - } + Warning("start %d is bigger than end %d", beg, end); tmp = beg; beg = end; end = tmp; } @@ -606,9 +604,7 @@ Fstr_aref_internal(str, indx) if (beg > end) { int tmp; - if (verbose) { - Warning("start %d is bigger than end %d", beg, end); - } + Warning("start %d is bigger than end %d", beg, end); tmp = beg; beg = end; end = tmp; } @@ -743,9 +739,7 @@ Fstr_aset_internal(str, indx, val) if (beg > end) { int tmp; - if (verbose) { - Warning("start %d is bigger than end %d", beg, end); - } + Warning("start %d is bigger than end %d", beg, end); tmp = beg; beg = end; end = tmp; } diff --git a/variable.c b/variable.c index bdfca0a2cf..9ac538c37e 100644 --- a/variable.c +++ b/variable.c @@ -38,11 +38,40 @@ rb_name_class(class, id) { VALUE body; - if (st_lookup(class_tbl, id, &body)) { - Bug("%s %s already exists", - TYPE(body)==T_CLASS?"class":"module", rb_id2name(id)); + rb_ivar_set_1(class, rb_intern("__classname__"), INT2FIX(id)); +} + +char * +rb_class2name(class) + struct RClass *class; +{ + int name; + + switch (TYPE(class)) { + case T_ICLASS: + class = (struct RClass*)RBASIC(class)->class; + break; + case T_CLASS: + case T_MODULE: + break; + default: + Fail("0x%x is not a class/module", class); + } + + while (FL_TEST(class, FL_SINGLE)) { + class = (struct RClass*)class->super; + } + + while (TYPE(class) == T_ICLASS) { + class = (struct RClass*)class->super; } - st_add_direct(class_tbl, id, class); + + name = rb_ivar_get_1(class, rb_intern("__classname__")); + if (name) { + name = FIX2INT(name); + return rb_id2name((ID)name); + } + Bug("class 0x%x not named", class); } struct global_entry { @@ -203,8 +232,7 @@ rb_gvar_get(entry) default: break; } - if (verbose) - Warning("global var %s not initialized", rb_id2name(entry->id)); + Warning("global var %s not initialized", rb_id2name(entry->id)); return Qnil; } @@ -248,18 +276,6 @@ rb_gvar_set2(name, val) } VALUE -rb_mvar_get(id) - ID id; -{ - VALUE val; - - if (st_lookup(class_tbl, id, &val)) return val; - if (verbose) - Warning("local var %s not initialized", rb_id2name(id)); - return Qnil; -} - -VALUE rb_ivar_get_1(obj, id) struct RObject *obj; ID id; @@ -278,9 +294,7 @@ rb_ivar_get_1(obj, id) rb_class2name(CLASS_OF(obj))); break; } - if (verbose) { - Warning("instance var %s not initialized", rb_id2name(id)); - } + Warning("instance var %s not initialized", rb_id2name(id)); return Qnil; } @@ -333,6 +347,10 @@ rb_const_get(id) } class = class->super; } + + /* pre-defined class */ + if (st_lookup(class_tbl, id, &value)) return value; + Fail("Uninitialized constant %s", rb_id2name(id)); /* not reached */ } @@ -1,2 +1,2 @@ -#define RUBY_VERSION "0.66" -#define VERSION_DATE "95/02/09" +#define RUBY_VERSION "0.67" +#define VERSION_DATE "95/02/21" |