JAVASCRIPT   100

variable.js

Guest on 7th September 2021 05:53:11 PM

  1. (function (tree) {
  2.  
  3. tree.Variable = function (name, index, currentFileInfo) {
  4.     this.name = name;
  5.     this.index = index;
  6.     this.currentFileInfo = currentFileInfo || {};
  7. };
  8. tree.Variable.prototype = {
  9.     type: "Variable",
  10.     eval: function (env) {
  11.         var variable, name = this.name;
  12.  
  13.         if (name.indexOf('@@') === 0) {
  14.             name = '@' + new(tree.Variable)(name.slice(1)).eval(env).value;
  15.         }
  16.        
  17.         if (this.evaluating) {
  18.             throw { type: 'Name',
  19.                     message: "Recursive variable definition for " + name,
  20.                     filename: this.currentFileInfo.file,
  21.                     index: this.index };
  22.         }
  23.        
  24.         this.evaluating = true;
  25.  
  26.         variable = tree.find(env.frames, function (frame) {
  27.             var v = frame.variable(name);
  28.             if (v) {
  29.                 return v.value.eval(env);
  30.             }
  31.         });
  32.         if (variable) {
  33.             this.evaluating = false;
  34.             return variable;
  35.         } else {
  36.             throw { type: 'Name',
  37.                     message: "variable " + name + " is undefined",
  38.                     filename: this.currentFileInfo.filename,
  39.                     index: this.index };
  40.         }
  41.     }
  42. };
  43.  
  44. })(require('../tree'));

Raw Paste


Login or Register to edit or fork this paste. It's free.