<commit_before>(function(yurt) {
  var ds = [];
  var ps = [];
  var tokens = [];
  function parse(text) {
    var first_char = text[0];
    switch (first_char) {
      case '[':
      case '{':
      case '"':
        tokens = JSON.parse(text);
        break;
      default:
        tokens = text.split(" ");
    }
  }
  function go() {
    var thisToken;
    while (tokens.length) {
      // consume this token
      thisToken = tokens.shift();
      if (isData(thisToken)) {
        ds.push(+thisToken);
      }
      else {
        execute(thisToken);
      }
    }
  }
  function isData(str) {
    // simple integer
    var dataPattern = new RegExp("-?[0-9]+");
    return dataPattern.test(''+str);
  }
  function execute(instruction) {
    var a, b;
    switch(instruction) {
      case "+":
        a = ds.pop();
        b = ds.pop();
        ds.push(a+b);
        break;
      case "-":
        a = ds.pop();
        b = ds.pop();
        ds.push(a-b);
        break;
      case "*":
        a = ds.pop();
        b = ds.pop();
        ds.push(a*b);
        break;
      case "/":
        a = ds.pop();
        b = ds.pop();
        ds.push(a/b);
        break;
      case "%":
        a = ds.pop();
        b = ds.pop();
        ds.push(a%b);
        break;
      case "fd":
        a = ds.pop();
        yurt.fd(a);
        break;
      case "tn":
        a = ds.pop();
        yurt.rt(a);
        break;
      case "dup":
        ds.push(ds[ds.length-1]);
        break;
      case "swap":
        a = ds.pop();
        b = ds.pop();
        ds.push(a);
        ds.push(b);
        break;
      case ".s":
        ds_ele.innerHTML += JSON.stringify(ds);
        break;
      default:
        a = ds.pop();
        ds.push(eval(instruction + '('+ a +');'));
    }
  }
  // testing

  var ds_ele = document.getElementById('ds');
  //parse('[3, -6, "+", ".s"]');
  //parse('3 -6 + .s');
  //parse('16 Math.sqrt .s');
  //parse('4 dup * Math.sqrt .s');
  //parse('4 3 swap .s');
  //parse('4 3 dup * swap dup * + Math.sqrt .s');
  parse('20 fd 72 tn 20 fd 108 tn 20 fd 72 tn 20 fd 108 tn .s');
  go();
})(yurt);<commit_msg>functions working for turtle-forth<commit_after>(function(yurt) {
  var ds = [];
  var ps = [];
  var heap = {};
  var funcDef = false, funcName = '';
  var tokens = [];
  function parse(text) {
    var first_char = text[0];
    switch (first_char) {
      case '[':
      case '{':
      case '"':
        tokens = JSON.parse(text);
        break;
      default:
        tokens = text.split(" ");
    }
  }
  function arrayCopy(a) {
    var i;
    var b = [];
    for (i = 0; i < a.length; i+=1) {
      b[i] = a[i];
    }
    return b;
  }
  function go() {
    var thisToken;
    while (tokens.length) {
      // consume this token
      thisToken = tokens.shift();
      if (funcDef) {
        if (!funcName) { funcName = thisToken; }
        else if (thisToken !== ';') {ps.push(thisToken);}
        else {
          funcDef = false;
          heap[funcName] = arrayCopy(ps);
          ps = [];
          funcName = '';
          funcDef = false;
        }
      }
      else if (isData(thisToken)) {
        ds.push(+thisToken);
      }
      else {
        execute(thisToken);
      }
    }
  }
  function isData(str) {
    // simple integer
    var dataPattern = new RegExp("^-?[0-9]+$");
    return dataPattern.test(''+str);
  }
  function execute(instruction) {
    var a, b;
    switch(instruction) {
      case ":":
        funcDef = true;
      break;
      case "+":
        a = ds.pop();
        b = ds.pop();
        ds.push(a+b);
        break;
      case "-":
        a = ds.pop();
        b = ds.pop();
        ds.push(a-b);
        break;
      case "*":
        a = ds.pop();
        b = ds.pop();
        ds.push(a*b);
        break;
      case "/":
        a = ds.pop();
        b = ds.pop();
        ds.push(a/b);
        break;
      case "%":
        a = ds.pop();
        b = ds.pop();
        ds.push(a%b);
        break;
      case "fd":
        a = ds.pop();
        yurt.fd(a);
        break;
      case "tn":
        a = ds.pop();
        yurt.rt(a);
        break;
      case "dup":
        ds.push(ds[ds.length-1]);
        break;
      case "swap":
        a = ds.pop();
        b = ds.pop();
        ds.push(a);
        ds.push(b);
        break;
      case ".s":
        ds_ele.innerHTML += JSON.stringify(ds);
        break;
      default:
        if (heap[instruction]) {
          tokens = heap[instruction].concat(tokens);
        }
        else {
          a = ds.pop();
          ds.push(eval(instruction + '('+ a +');'));
        }

    }
  }
  // testing

  var ds_ele = document.getElementById('ds');
  //parse('[3, -6, "+", ".s"]');
  //parse('3 -6 + .s');
  //parse('16 Math.sqrt .s');
  //parse('4 dup * Math.sqrt .s');
  //parse('4 3 swap .s');
  //parse('4 3 dup * swap dup * + Math.sqrt .s');
  //parse('20 fd 72 tn 20 fd 108 tn 20 fd 72 tn 20 fd 108 tn .s');
  parse(': times2 2 * ; 4 times2 .s');
  go();
})(yurt);
{'fraction_non_alphanumeric': 0.1876750700280112, 'fraction_numerical': 0.014405762304921969, 'mean_word_length': 1.4553045186640472, 'pattern_counts': {'":': 22, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '686763', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1737, 'n_words': 403}