Changeset 2405

Show
Ignore:
Timestamp:
12/04/07 15:03:55 (6 years ago)
Author:
nshuyo
Message:

lang/javascript/jsruby: added parsing of substitute for []/property and string[nth]=int/str support

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

Legend:

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

    r2339 r2405  
    8484  "[]": function(self, args, block) { 
    8585    return new RubyEngine.RubyObject.Numeric(self.str.charCodeAt(args[0])); 
     86  }, 
     87  "[]=": function(self, args, block) { 
     88    var x = args[0].num; 
     89    if (RubyEngine.RubyObject.String.prototype.isPrototypeOf(args[1])) { 
     90      self.str = self.str.substr(0,x) + args[1].str + self.str.substr(x+1); 
     91      return args[1]; 
     92    } else if (RubyEngine.RubyObject.Numeric.prototype.isPrototypeOf(args[1])) { 
     93      self.str = self.str.substr(0,x) + String.fromCharCode(args[1].num) + self.str.substr(x+1); 
     94      return args[1]; 
     95    } else { 
     96      // TODO: error 
     97    } 
    8698  } 
    8799}; 
  • lang/javascript/jsruby/trunk/src/interpreter.js

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

    r2339 r2405  
    7575} 
    7676 
    77  
     77//  Lhs : VarName | Primary '[' Args ']' | Primary . IDENTIFIER 
    7878RubyEngine.Parser.prototype.lhs = function() { 
    79         return this.varname(); 
     79  var x; 
     80  var prebody = this.body; 
     81  if ((x=this.primary())!=undefined) { 
     82//console.log(this.body);console.dir(x);console.trace();if(!confirm("continue?")) exit(); 
     83    if (RubyEngine.Node.Method.prototype.isPrototypeOf(x) && x.block==undefined && (x.name=="[]" || x.args==undefined || x.args.length==0)) return x; 
     84    this.body = prebody; 
     85  } 
     86  if ((x=this.varname())!=undefined) return x; 
     87  return undefined; 
    8088} 
    8189 
     
    101109                if (this.body.match(/^[ \t]*\=/)) { 
    102110                        this.body = RegExp.rightContext; 
    103                         if ((y=this.arg())!=undefined) return new RubyEngine.Node.Method("*let", null, [x, y]); 
     111                        if ((y=this.arg())!=undefined) { 
     112        if (RubyEngine.Node.Method.prototype.isPrototypeOf(x)) { 
     113          x.name += "="; 
     114          if (x.args) { x.args.push(y); } else { x.args = [y]; } 
     115          return x; 
     116        } else { 
     117          return new RubyEngine.Node.Method("*let", null, [x, y]); 
     118        } 
     119      } 
    104120                } 
    105121                this.body=prebody; 
  • lang/javascript/jsruby/trunk/tests/string.html

    r2339 r2405  
    4343} 
    4444 
     45function testSubstituteBrackett() { 
     46        assertEquals("string[nth]= 1", ruby.exec(' a="abc"; a[1]=66; a '), "aBc"); 
     47        assertEquals("string[nth]= 2", ruby.exec(' a="abc"; a[1]="XYZ"; a '), "aXYZc"); 
     48} 
     49 
    4550</script> 
    4651</head>