inexorabletash / jsbasic

Applesoft BASIC in JavaScript
https://calormen.com/jsbasic
Other
187 stars 39 forks source link

quick minimal setup #39

Open javatlacati opened 2 years ago

javatlacati commented 2 years ago

A big portion of your repo's code is targeting your particular UI. In order to make it easily reusable consider checking program options for program.init to make it more accesible.

Example

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Applesoft BASIC in Javascript</h1>

<!-- Source -->
<div style="float: left; margin: 5px;">
  Enter code: &nbsp;&nbsp;
  <button id="btn_run">&#x25B6; Run</button

  <!-- Source code editor inserted here -->
  <textarea name="source" id="source"></textarea>
</div>

<!-- Screen -->
<div id="frame" class="jsb-frame" style="float: left; margin: 5px;" tabIndex="0">
  <div id="screen-wrapper" class="jsb-wrapper">
    <div id="screen" class="jsb-tty"></div>
  </div>
</div>

<script src="https://www.calormen.com/jsbasic/basic.js?2012-02-08"></script>
<script src="https://www.calormen.com/jsbasic/tty.js"></script>

JS

var stopped = true;
var frame = $('#frame');
var keyboard = frame;
var tty = new TTY($('#screen'), keyboard);
var program;
$('#btn_run').click(function(e) {
  e.preventDefault();
  try {
    program = basic.compile($('#source').val());
  } catch (e) {
    alert(e);
    return;
  }
  stopped = false;
  updateUI();

  program.init({
    tty: tty
  });
  setTimeout(driver, 0);
});

function updateUI() {
  $("#btn_run").disabled = stopped ? "disabled" : "";
}

function driver() {
  var state = basic.STATE_RUNNING;
  var statements = NUM_SYNCHRONOUS_STEPS;

  while (!stopped && state === basic.STATE_RUNNING && statements > 0) {

    try {
      state = program.step(driver);
    } catch (e) {
      console.log(e);
      alert(e.message ? e.message : e);
      stopped = true;
      updateUI();
      return;
    }

    statements -= 1;
  }

  if (state === basic.STATE_STOPPED || stopped) {
    stopped = true;
    updateUI();
  } else if (state === basic.STATE_BLOCKED) {
    // Fall out
  } else { // state === basic.STATE_RUNNING
    setTimeout(driver, 0); // Keep going
  }
}
inexorabletash commented 2 years ago

It's unclear what you're suggesting. The code can be used in other configurations, e.g. see:

https://github.com/inexorabletash/jsbasic/blob/main/script.md

At least historically it could run on the console on old versions of Windows Scripting Host and Mozilla Rhino, but that was removed.

Perhaps you could write a PR that explains the changes you'd like to see?

inexorabletash commented 2 years ago

(oops, I didn't mean to close this)