Code coverage report for sc/lang/compiler/codegen/scope.js

Statements: 100% (36 / 36)      Branches: 100% (8 / 8)      Functions: 100% (12 / 12)      Lines: 100% (36 / 36)      Ignored: none     

All files » sc/lang/compiler/codegen/ » scope.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 711     1   1 118 118 118     1   89 84   89     54       1 143 143     1 210         210     1 92 92     1 95 95     1 248     1 96 139       1 84 84 39   45 45   84     1    
(function(sc) {
  "use strict";
 
  require("../compiler");
 
  function Scope() {
    this.stack = [];
    this.tempVarId = 0;
    this.begin();
  }
 
  var delegate = {
    var: function addToVariable(scope, name) {
      if (!scope.vars[name]) {
        addVariableToStatement(scope, name);
      }
      scope.vars[name] = true;
    },
    arg: function addToArguments(scope, name) {
      scope.args[name] = true;
    }
  };
 
  Scope.prototype.add = function(type, name, scope) {
    delegate[type](scope || this.peek(), name);
    return this;
  };
 
  Scope.prototype.begin = function() {
    this.stack.unshift({
      vars: {},
      args: {},
      stmt: { head: [], vars: [], tail: [] }
    });
    return this;
  };
 
  Scope.prototype.end = function() {
    this.stack.shift();
    return this;
  };
 
  Scope.prototype.toVariableStatement = function() {
    var stmt = this.peek().stmt;
    return [ stmt.head, stmt.vars, stmt.tail ];
  };
 
  Scope.prototype.peek = function() {
    return this.stack[0];
  };
 
  Scope.prototype.find = function(name) {
    return this.stack.some(function(scope) {
      return scope.vars[name] || scope.args[name];
    });
  };
 
  function addVariableToStatement(scope, name) {
    var stmt = scope.stmt;
    if (stmt.vars.length) {
      stmt.vars.push(", ");
    } else {
      stmt.head.push("var ");
      stmt.tail.push(";");
    }
    stmt.vars.push(name.replace(/^(?![_$])/, "$"));
  }
 
  sc.lang.compiler.Scope = Scope;
})(sc);