Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8cd29a0f57
Fetching contributors…

Cannot retrieve contributors at this time

file 259 lines (228 sloc) 5.555 kb
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
<!doctype html>
<html>
<head>
<style>
textarea {
  width: 500px;
  height: 200px;
}

* {
padding: 0;
margin: 0;
}
#bar{
  width: 300px;
  height: 16px;
  background-color: blue;
  opacity: 0.25;
  position: absolute;
  left: 0px;
}
</style>
<script>

function _s(val, start, end) {
  var need_to_join = false;
  var ret = []
  if (typeof val == "string") {
    val = val.split("")
    need_to_join = true;
  }
  if (start >= 0) {
  } else {
      start = val.length + start
  }
  
  if (end == null) {
     ret = val.slice(start)
  } else {
      if (end < 0) {
        end = val.length + end;
      } else {
        end = end + start
      }
      ret = val.slice(start, end)
  }
    
  if (need_to_join) {
      return ret.join("")
  } else {
      return ret;
  }
}

var pc = 0;
var scope = {};
var types = {};
var lines;
var on_jump = false;
function trim(str) {
      match = str.match(/^[\s]*(.*)[\s]*$/);
      if (match) {
          return match[1];
      }
}

function replacer(match, inner, offset, s) {
    inner = inner.substring(1).substring(0,inner.length-2)
    return lookup(inner);
}

function get_var_name(str) {
  str = str.replace(/(\[[^\]]+\])/g, replacer);
  return str;
}

var funcs = {
  equal: function(a, b) {
     return a == b;
  },
  print: function(tokens, line) {
    console.log(lookup(tokens[0]))
  }
}
var returns = [];
var last_if = false;


function tokenize(line) {
    
    line = line.split("");
    i = 0;
    var state = "out";
    var cur_token = []
    var tokens = [];
    var last_chr = "";
   
    while (i < line.length) {
        chr = line[i];
        if (chr == " " && state == "out") {
            tokens.push(cur_token.join(""))
            cur_token = []
        } else if (chr == '"' && state == "out") {
            state = "in";
            cur_token.push("'")
        } else if (chr == '"' && state == "in" && last_chr != "\\") {
            state = "out";
        } else if (chr.match(/:/) && state == "out") {
           tokens.push(cur_token.join(""))
           tokens.push(chr)
           cur_token = []
        } else {
             cur_token.push(chr)
        }
        
        last_chr = chr;
        i++
    }
    if (cur_token.length > 0) {
        tokens.push(cur_token.join(""))
    }
    return tokens
}

function exec(tokens, line) {
  
  if (tokens[1] == "=") {
    tokens_after_equals = tokens.slice(2)
    line_after_equals = line.substr(line.indexOf(" =") + 2);
    exec_after_equal = exec(tokens_after_equals, line_after_equals);
    assign(tokens[0], exec_after_equal)
  } else if (tokens[0] == "if"){
      
  } else if (tokens[tokens.length-1] == ":") {
      assign(tokens[0], pc + 1);
  } else if (tokens.length == 1 && _s(tokens[0],-2) == "()") {
      var func = _s(tokens[0],0,-2)
      line = lookup(func);
      returns.push(pc + 1)
      pc = line - 1
      on_jump = true;
  } else if (tokens.length == 1) {
      return lookup(tokens[0]);
  } else { //normal fuction call
      var func = tokens[0];
      if (func in funcs) {
          sub_token = _s(tokens,1);
          sub_line = _s(line, tokens[0].length)
          return funcs[func](sub_token, sub_line)
      } else if (func in scope) {
          assign("args", _s(tokens, 1))
          assign("raw_args", _s(line, tokens[0].length))
          new_pc = lookup(func);
          returns.push(pc + 1)
          pc = new_pc - 1
          on_jump = true;
      }
  }
 
}

function lookup(v) {
  
  if (v.match(/^[\d]/)) {
      return v;
  }
  if (v.substr(0,1) == "'")
  return v.substring(1);
  
  v = get_var_name(v);
  
  if (v in scope) {
      return scope[v];
  } else {
    return v
  }
  
}

function assign(ee,or){
  ee = get_var_name(ee);
  for (var i in scope) {
      if (i.match(new RegExp("^" + ee + "\\.*"))) {
          delete scope[i];
      }
  }
  scope[ee] = or
  if (typeof or == "string" && or.length > 1) {
      for (var i = 0; i < or.length; i++) {
              assign(ee + "." + i, or.charAt(i));
      }
  }
  if (typeof or == "object") {
    for (var i in or) {
        assign(ee + "." + i, or[i])
    }
  }
  
}

function parse(txt) {
  lines = txt.split("\n");
  var line;
  var iters = 0;
  var next_len;
  
  //while (pc < lines.length && iters < 25) { //uncomment this
  function while2() { //comment this out
      iters++
      line = lines[pc]
      document.getElementById("bar").style.top = 16 * pc + "px"
      match = line.match(/^[\s]*/)
      space_len = match[0];
      tokens = tokenize(trim(line))
           
      exec(tokens,trim(line));
      pc = pc + 1
      
      if (!on_jump) {
        on_jump = false;
        next_pc = pc
        while (next_pc < lines.length) {
            next_line = lines[next_pc];
            match = next_line.match(/^[\s]*/)
            next_space_len = match[0]
            if (next_space_len > space_len) {
                //skip
            } else if (next_space_len < space_len) {
               
               pc = returns.pop()
               break;
            } else if (next_space_len == space_len) {
                pc = next_pc;
                
                break;
            }
            next_pc++
        }
      } else {
          on_jump = false;
      }
      
      //comment this out
      if (iters < 25 && pc < lines.length) {
        setTimeout(while2, 500)
      }
  }
  pc = 0;
  while2(); //comment this out
}
</script>
</head>
<body>
<div id="bar"> </div>
<textarea id="t"></textarea>
<br>
<input type="button" onclick="parse(document.getElementById('t').value)" value="run">
</body>
</html>
Something went wrong with that request. Please try again.