git @ Cat's Eye Technologies yoob.js / master eg / repl.html
master

Tree @master (Download .tar.gz)

repl.html @masterraw · history · blame

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>yoob REPL Demo</title>
  <style>
#terminal {
    background: black;
}
  </style>
</head>
<body>

<h1>REPL Demo</h1>

<canvas id="terminal" width="400" height="400" tabindex="0">
  Your browser doesn't support displaying an HTML5 canvas.
</canvas>

</body>
<script src="../src/yoob/playfield.js"></script>
<script src="../src/yoob/cursor.js"></script>
<script src="../src/yoob/playfield-canvas-view.js"></script>
<script src="../src/yoob/text-terminal.js"></script>
<script src="../src/yoob/line-input-buffer.js"></script>
<script src="../src/yoob/tree.js"></script>
<script src="../src/yoob/scanner.js"></script>
<script src="../src/yoob/sexp-parser.js"></script>
<script>
  var element = document.getElementById('terminal');
  var t = new yoob.TextTerminal().init(80, 25);
  var view = t.createPlayfieldCanvasView(element, 12, 18);

  t.write("yoob REPL\n");
  var p = new yoob.SexpParser();
  var ast = null;
  var quit = new yoob.Tree('list', [(new yoob.Tree('atom')).setValue('quit')]);
  var pat = new yoob.Tree('list', [
    (new yoob.Tree('atom')).setValue('cmd'),
    (new yoob.Tree('var')).setVariable('X')
  ]);
  var equ = new yoob.Tree('list', [
    (new yoob.Tree('atom')).setValue('eq'),
    (new yoob.Tree('var')).setVariable('X'),
    (new yoob.Tree('var')).setVariable('X')
  ]);
  var ib = new yoob.LineInputBuffer().init(element, t);
  ib.onupdate = function(str) { view.draw(); };
  ib.onenter = function(str) {
    p.init(str);
    ast = p.parse();
    if (ast !== undefined) {
      if (ast.equals(quit)) {
        t.write("You can't give up yet!");
      } else {
        var unifier = equ.match(ast);
        if (unifier) {
          ast = pat.subst(unifier);
        }
        t.write(ast.toString());
      }
      t.write("\n");
    } else {
      t.write("*** parse error\n");
    }
    view.draw();
  };
  view.draw();
  element.focus();
</script>