Changeset 2339

Show
Ignore:
Timestamp:
12/03/07 16:43:25 (5 years ago)
Author:
nshuyo
Message:

lang/javascript/jsruby: added [] method parsing and string[nth] support

Location:
lang/javascript/jsruby/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/jsruby/trunk/src/builtinobjects.js

    r2328 r2339  
    8181    for(var i=self.str.length-1;i>=0;i--) st += self.str.charAt(i); 
    8282    return new RubyEngine.RubyObject.String(st); 
     83  }, 
     84  "[]": function(self, args, block) { 
     85    return new RubyEngine.RubyObject.Numeric(self.str.charCodeAt(args[0])); 
    8386  } 
    8487}; 
  • lang/javascript/jsruby/trunk/src/interpreter.js

    r2328 r2339  
    121121    return method.apply(this, [obj, node.args, node.block]); 
    122122  } else { 
    123     alert("undefined method :" + obj.toSource() + "." + nodetree[idx].toSource()); 
     123    alert("undefined method :" + obj.toSource() + "." + node[idx].toSource()); 
    124124  } 
    125125} 
  • lang/javascript/jsruby/trunk/src/parse.js

    r2337 r2339  
    134134} 
    135135 
    136 //  Primary : Primary2 ('.'Operation ('(' Args ')')? ('{' ('|'Varname'|')? CompStmt '}')? )*  
     136// Primary : Primary2 ( '[' Args ']' | '.'Operation ('(' Args ')')? ('{' ('|'Varname'|')? CompStmt '}')? )*  
     137// # for removing left recursions of Primary in BNF of Ruby 
    137138RubyEngine.Parser.prototype.primary = function() { 
    138139//console.log(this.body);console.trace();if(!confirm("continue?")) exit(); 
    139140        var prim = this.primary2(); 
    140141  while(prim != undefined) { 
    141                 if (!this.body.match(/^[ \t]*\./)) return prim; 
    142  
    143142    var y, z; 
    144143    var prebody = this.body; 
    145                 this.body = RegExp.rightContext; 
     144 
     145                if (!this.body.match(/^[ \t]*(\.|\[)/)) return prim; 
     146                this.body = RegExp.rightContext; 
     147 
     148    // '[' Args ']' 
     149    if (RegExp.$1=='[') { 
     150      y = this.args(); 
     151      if (y==undefined || !this.body.match(/^[ \t]*\]/)) { 
     152        this.body = prebody; 
     153        return prim; 
     154      } 
     155                this.body = RegExp.rightContext; 
     156                prim = new RubyEngine.Node.Method('[]', prim, y); 
     157      continue; 
     158    } 
    146159 
    147160    // '.'Operation 
  • lang/javascript/jsruby/trunk/tests/string.html

    r2328 r2339  
    3737} 
    3838 
     39function testBrackett() { 
     40        assertEquals("string[nth] 1", ruby.exec(' "ABC"[0] '), 65); 
     41        assertEquals("string[nth] 2", ruby.exec(' "ABC"[1] '), 66); 
     42        //assertEquals("string[nth] 3", ruby.exec(' "ABC"[-1] '), 67); 
     43} 
     44 
    3945</script> 
    4046</head>