music
OSdata.com: programming text book 

OSdata.com

stack check

summary

OSdata RSS
News Feed


OSdata blog RSS.

OSdata blog RSS - add to Bloglines
OSdata blog RSS - add to Google Reader.
OSdata blog RSS - add to My AOL
OSdata blog RSS - add to Newsgator.

    This section discusses adding sanity checks to our JavaScript/Forth stack.

    This is part of an instructional series on the building of a Forth programming environment in any standard browser using JavaScript.

    Threaded Interpreted Languages (TILs), including Forth, are designed for customization.

    In addition to writing your own Forth programs, please modify the underlying engine to meet your specific needs.

    Please have fun with this project. Make it your own.

    The most recent version is at stable release version or http://www.twiddledom.com/coder/js78.html

    Free Open Souce: Licensed under MIT license. License printed in full below.

previous page   next page
previous page
raw stack
  next page
ADD
Google

stack check

summary

    This section discusses adding sanity checks to our JavaScript/Forth stack.

important difference with real Forth

    In Forth, checks for stack overflow and underflow are normally performed after operations. Instead, we are going to check for stack overflow or underflow before performing any operation and abort on error. You can feel free to add your own code for error exception processing instead of a simple abort.

pop stack underflow check

    The new code occurs in the pop function and we simply check to see if the stack is already empty. If the stack is already empty (as indicated by a stack pointer value of zero), then we report the error and abort the function before popping anything.

if (topofstack == 0) {
  document.getElementById("response").innerHTML = "<p align=\"center\">no pop because stack is already empty</p>";
  return 0;
}

    http://www.twiddledom.com/coder/js9.html demonstrates this step.

showstack empty check

    We also change the ShowStack function to indicate when the stack is empty.

    First, we perform an if test and assign a temporary variable (stackcontent) with either the contents of the stack or the message that the stack is empty.

if (topofstack > 0) {
  stackcontent = datastack[topofstack];
}
else {
  stackcontent = "empty";
}

    And we modify the reporting routine to use the string from the if statement.

outputstring += " * and top of stack is " + topofstack + " and current stack item is " + stackcontent;

    Again, all of this page’s code can be found in context at http://www.twiddledom.com/coder/js9.html.

return to Forth in JavaScript
return to Outrageous Coder

previous page   next page
previous page
raw stack
  next page
ADD

contact
comments, suggestions, corrections, criticisms

because of ridiculous spam attacks,
contact through Twitter (@OutrageousCoder) will be more reliable than the contact form

please contact us

your name:
email address:
phone number:
message:

return to table of contents
free downloadable college text book

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under the MIT License.

    Copyright © 2014, 2015 Milo

    Licensed under the MIT License. You may obtain a copy of the License at

        http://opensource.org/licenses/MIT

    The MIT License (MIT)

    Copyright © 2014, 2015 Milo

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

previous page   next page
previous page
raw stack
  next page
ADD

Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2014 Milo

    Created: October 12, 2014

    Last Updated: October 12, 2014


return to table of contents
free downloadable college text book

previous page   next page
previous page
raw stack
  next page
ADD