Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit eec5de0a4ff60b96d8264d3cce5538b5439d30ce @drewlesueur committed May 4, 2010
Showing with 175 additions and 0 deletions.
  1. +175 −0 index.html
@@ -0,0 +1,175 @@
+<!doctype html>
+<html>
+<head>
+<style>
+textarea {
+ width: 500px;
+ height: 400px;
+}
+</style>
+<script>
+var pc = 0;
+var scope = {};
+var types = {};
+var lines;
+function trim(str) {
+ match = str.match(/^[\s]*([^\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 variable = "[\\s]*([a-zA-Z\\']{1}[\\w\\.\\[\\]]*)[\\s]*";
+var number = "[\\s]*([\\d]+)[\\s]*";
+var other = number;
+var funcs = {
+ equal: function(a, b) {
+ return a == b;
+ }
+
+}
+var returns = [];
+function exec(line) {
+ if (match = line.match(new RegExp("^"+variable+"\\=" + number +"$"))) { //a = 12
+ var ee = match[1];
+ ee = get_var_name(ee);
+ var or = match[2];
+ assign(ee, or);
+ } else if (match = line.match(new RegExp("^" + variable + "\\=" + variable + "$"))) { // /^[\s]*([\w\.\[\]]+)[\s]*\=[\s]*([\a-zA-Z]{1}[\w\.\[\]]*)[\s]*$/)) { //houses = eight;
+ var ee = match[1];
+ ee = get_var_name(ee);
+ var or = match[2];
+ or = lookup(or);
+ assign(ee,or);
+ } else if (match = line.match(new RegExp("^" + variable + '\\=[\\s]*\\"(.*)\\"[\\s]*$'))) { // /^[\s]*([\w\.\[\]]+)[\s]*\=[\s]*\"(.*)\"[\s]*$/)) { //houses = "eight";
+ var ee = match[1];
+ ee = get_var_name(ee);
+ var or = match[2];
+ assign(ee,or);
+ // } else if (match = line.match(new RegExp("^" + variable + "$"))) {
+ // ret = lookup(match[1]);
+ // console.log(ret);
+ } else if (match = line.match(new RegExp("^" + variable + "\\=" + "[\\s]*\\[" + "(.*)" + "\\]$" ))) {
+ var ee = match[1];
+ var or = match[2];
+ var arr = or.split(" ");
+ for (var i in arr) {
+ assign(ee + "." + i, lookup(arr[i]));
+ }
+ console.log(match[2]);
+ } else if (match = line.match(/^([\s]*)([\w]+):/)) { print: //todo don't rememer parenthasized submatch
+ liner = match[0];
+ spaces = match[1].length;
+ func = match[2];
+ console.log(func)
+ var i = 0
+ var next = 0;
+ assign(func, pc + 1);
+ while (1) {
+ pc++;
+ if (pc >= lines.length) {
+ break;
+ }
+ next = lines[pc];
+ if (next.match(new RegExp("^[\\s]{"+spaces+"}[^\\s]{1}"))) {
+ pc--;
+ break;
+ } else {
+ }
+
+ }
+
+ /*while (1) {
+ pc++;
+ if (pc >= lines.length) {
+ break;
+ }
+ next = lines[pc];
+ if (next.match(new RegExp("^[\\s]{"+spaces+"}"))) {
+ break;
+ } else {
+ next_match = next.match(/^[\s]*(.*)$/)
+ assign(func + "." + i, next_match[1])
+ }
+ i++;
+ }
+ console.log(match);*/
+ } else if (match = line.match(new RegExp("^(" + variable + ")\\(\\)"))) { //print()
+ var to = match[1];
+ to = trim(to)
+ console.log("trimmed is " + to);
+
+ returns.push(pc)
+ pc = lookup(to) - 1;
+ console.log(pc)
+ //not used
+ } else if (match = line.match(/^[\s]*([\w]+)[\s]{1}([\w]+.*)$/)) { //print whatever you want
+ func = match[1];
+ if (func in funcs) {
+ return funcs[func].apply( )
+ }
+ } else if (line.match(/^[\s]*return/)){
+ ret = returns.pop();
+ pc = ret;
+ console.log(pc)
+ } else {
+ console.log("no");
+ }
+}
+
+function lookup(v) {
+
+ if (v.substr(0,1) == "'")
+ return v.substring(1);
+
+ v = get_var_name(v);
+ console.log("looking up " + v)
+ return scope[v];
+}
+
+function assign(ee,or){
+
+ for (var i in scope) {
+ if (i.match(new RegExp("^" + ee + "\\.*"))) {
+ delete scope[i];
+ }
+ }
+ scope[ee] = or
+ console.log(ee + "=" + or )
+
+ if (typeof or == "string" && or.length > 1)
+ for (var i = 0; i < or.length; i++) {
+ assign(ee + "." + i, or.charAt(i));
+ }
+}
+
+function parse(txt) {
+ lines = txt.split("\n");
+ var line;
+ var iters = 0;
+ while (pc < lines.length && iters < 25) {
+ iters++
+ line = lines[pc]
+ exec(line);
+ pc++;
+ }
+ pc = 0;
+
+}
+</script>
+</head>
+<body>
+<textarea id="t"></textarea>
+<br>
+<input type="button" onclick="parse(document.getElementById('t').value)" value="run">
+</body>
+</html>

0 comments on commit eec5de0

Please sign in to comment.