Changeset 3261

Show
Ignore:
Timestamp:
12/18/07 12:46:51 (5 years ago)
Author:
nshuyo
Message:

lang/javascript/jsruby: added parsing 'def'

Location:
lang/javascript/jsruby/trunk/src
Files:
3 modified

Legend:

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

    r3087 r3261  
    4040RubyEngine.RESERVED = { 
    4141  "if":true, "then":true, "elsif":true, "else":true, "end":true, 
    42   "while":true, "unless":true, "until":true, 
     42  "while":true, "unless":true, "until":true, "def":true, 
    4343  "nil":true 
    4444} 
  • lang/javascript/jsruby/trunk/src/interpreter.js

    r3183 r3261  
    104104                } else if (RubyEngine.Node.Operator.prototype.isPrototypeOf(x)) { 
    105105                        switch (x.name) { 
    106                         case "neg": 
     106                        case "-@": 
    107107                                stk.push(stk.pop().neg()); 
    108108                                break; 
  • lang/javascript/jsruby/trunk/src/parse.js

    r3183 r3261  
    1212RubyEngine.Parser.prototype.compstmt = function() { 
    1313        var x; 
     14        while(this.term()); 
    1415        if ((x=this.stmt())==undefined) return undefined; 
    1516        var ret = [x]; 
     
    141142} 
    142143 
    143 // Primary : ('-'|'+') Primary | Primary2 ( '[' Args ']' | '.'Operation ('(' Args ')')? ('{' ('|'Varname'|')? CompStmt '}')? )* Args? 
    144 // # for removing left recursions of Primary in BNF of Ruby 
     144// Primary : ('-'|'+') Primary  
     145//      | Primary2 ( '['Args']' | '.'Operation ('('Args')')? ('{' ('|'Varname'|')? CompStmt '}')? )* Args? 
    145146RubyEngine.Parser.prototype.primary = function() { 
    146147//console.log(this.body);console.trace();if(!confirm("continue?")) exit(); 
     
    151152        if ((y=this.primary())!=undefined) { 
    152153      if (x=='+') return y; 
    153                 return new RubyEngine.Node.Expression([new RubyEngine.Node.Operator('neg'), y]); 
    154     } 
    155                 this.body = prebody; 
    156   } 
     154                return new RubyEngine.Node.Expression([new RubyEngine.Node.Operator('-@'), y]); 
     155    } 
     156                this.body = prebody; 
     157  } 
     158 
    157159        var prim = this.primary2(); 
    158160  while(prim != undefined) { 
     
    198200                        prebody = this.body; 
    199201                        this.body = RegExp.rightContext; 
    200                         y = this.blockvars(); 
    201                         while(this.term()); 
     202                        y=this.blockvars();  // it is maybe 'undefined' 
    202203      z=this.compstmt(); 
    203204      if (z==undefined) z=null; 
     
    219220 
    220221//  Primary2: '(' Expr ')' | Literal | Reference | '[' Args ']' 
    221 //         if Arg Then CompStmt (elsif Arg Then CompStmt)* (else CompStmt)? end 
     222//        | if Arg Then CompStmt (elsif Arg Then CompStmt)* (else CompStmt)? end 
     223//        | def Operation ArgDecl CompStmt end 
    222224//  Literal: / $INT:push | $JS_STRING:push /, 
    223225RubyEngine.Parser.prototype.primary2 = function() { 
     
    271273                x = RegExp.$1; 
    272274                if ((y = this.arg())!=undefined && this.then()) { 
    273                         while (this.term()); 
    274275                        if (z=this.compstmt()) { 
    275276                          var args = [y, z]; 
     
    278279                                        this.body = RegExp.rightContext; 
    279280                                        if ((y = this.arg()) && this.then()) { 
    280                                                 while (this.term()); 
    281281                                                if (!(z=this.compstmt())) { this.body = prebody2; break; } 
    282282                                                args.push(y, z) 
     
    286286                                        var prebody2 = this.body; 
    287287                                        this.body = RegExp.rightContext; 
    288                                         while (this.term()); 
    289288                                        if (z=this.compstmt()) {  
    290289                                                args.push(true, z) 
     
    301300                this.body = prebody; 
    302301        } 
    303         return undefined; 
     302 
     303  // def Fname ArgDecl CompStmt end 
     304        if (this.body.match(/^[ \t]*def/)) { 
     305                this.body=RegExp.rightContext; 
     306                x=this.operation(); 
     307                y=this.argdecl(); 
     308                z=this.compstmt(); 
     309                if(x!=undefined && z!=undefined && this.body.match(/^[ \s]*(end)/)) { 
     310                        this.body = RegExp.rightContext; 
     311                ret = new RubyEngine.Node.Method("def", null, [new RubyEngine.RubyObject.String(x)]); 
     312                ret.block = new RubyEngine.Node.Block(y, z); 
     313                return ret; 
     314    } 
     315                this.body = prebody; 
     316        } 
     317 
     318        return undefined; 
     319} 
     320 
     321// ArgDecl : `(' ArgList `)' | ArgList Term 
     322RubyEngine.Parser.prototype.argdecl = function() { 
     323//console.log(this.body);console.trace();if(!confirm("continue?")) exit(); 
     324  var x; 
     325        var prebody = this.body; 
     326        if (this.body.match(/^[ \t]*\(/)) { 
     327    this.body=RegExp.rightContext; 
     328    if ((x=this.arglist())!=undefined && this.body.match(/^[ \t]*\)/) ) { 
     329      this.body=RegExp.rightContext; 
     330      return x; 
     331    } 
     332  } else { 
     333    if ((x=this.arglist())!=undefined && this.term() ) return x; 
     334  } 
     335  this.body=prebody; 
     336        return undefined; 
     337} 
     338 
     339// ArgList : varname(`,'varname)*[`,'`*'[varname]][`,'`&'varname] | `*'varname[`,'`&'varname] | [`&'varname] 
     340RubyEngine.Parser.prototype.arglist = function() { 
     341  var x; 
     342  if ((x=this.varname())==undefined) return []; 
     343  var ret=[x], prebody=this.body; 
     344  while (this.body.match(/^[ \t]*,/)) { 
     345    prebody=this.body; 
     346    this.body=RegExp.rightContext; 
     347    if ((x=this.varname())==undefined) break; 
     348    ret.push(x); 
     349  } 
     350  this.body=prebody; 
     351  return ret; 
    304352} 
    305353