cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
  <div class="container">

  <h3>Input:</h3>
  <textarea id="language_input" type="text"/>
3 22 +</textarea>
  <h3>Results:</h3>
  <div class="results" id="language_output">5</div>

  <h3>Stack:</h3>
  <div id="stack_output"></div>

  <h4>A Simple Stack Based Language</h4>
  <p>Simple Stack based language in postfix notation.</p>

  <p>Ehem,  I'll say that again in English</p>

  <h4>A Backwards Calculator</h4>
  <p> a really simple calculator with a twist.</p>

  <p>My goal is to get to an easy place to experiment with various ways of visualizing the stack</p>

  <p>Postfix means instead of writing "2 + 3" you write "3 2 +". Make sure there is space between every number and every operator. Parentheses will not help you here.</p>

  <h5>Examples</h5>

  <pre>
3 22 +        ->        25

  </pre>

  <h5>How it works</h5>

  <p> It's kind of hard to explain. That's what I meant when I said I want to visualize this. I don't know exactly what it will look like when I'm satisfied, but I hope it helps someone understand this type of language (because it's really cool).</p>
</div>

            
          
!
            
              .container {
  float: left;
  margin: 0 60px 0 60px;
}

.details {
  margin-right:60px;
}
            
          
!
            
                

var selInput = d3.select('#language_input')
  .on('keyup', _.debounce(executeFromInput, 500))
var selOutput = d3.select('#language_output')

// Drive first one
executeFromInput();

function executeFromInput() {
  var input = selInput.node().value;
  var tokens = parse(input)
  var output = execute(tokens);
  
  selOutput
    .text(output);
}

function execute(tokens) {
  var stack = [];
  
  _.each(tokens, function (t, i) {
    
    if (stack.length > 10000) return userError('Stack overflow. That means something went horribly wrong on my end. Sorry boo.');
    
    switch (t.type) {
        
    
    // For values, just throw'm on to the stack.
    case "number":
     stack.push(t);
       break;
        
    // Operators
    case "operator":
        
     if (stack.length < 2) return userError('Stack underflow. That means you used an operator with not enough values on the stack.');

     // Take operatands off of stack.
     var a = stack.pop();
     var b = stack.pop();

     // Operator implementations
     switch(t.value) {
     case '+':
       log(a, b, t)
       a.value = a.value + b.value;
       break;
     case '-':
       a.value = a.value - b.value;
       break;
     case '*':
       a.value = a.value * b.value;
       break;
     case '/':
       a.value = a.value / b.value;
       break;
     }

     // Finally push new value back on stack;
     stack.push(a);
     break;
    }
  })
  
  return stack.map(function (t, i) { return t.value }).join(' ')
}

function parse(input) {
  var tokens = [];
  
  // Tokens are any-whitespace delimited
  // TODO: Can reduce this by using regex that matches any group of whitespace for split, instead of string
  _.each(input.split(' '), function (word, i) {
    // First match group is operators, second match group is values. Reserving a lot more than I'm implementing. See execute()
    var reTokens= /(\+|\-|\/|\*|\x|\^)|(\d+(.\d*)?(e\d*)?)/i;
    
    var matchTokens = word.match(reTokens);
    
    // Do nothing with words we don't understand
    if ( ! matchTokens ) return;
    
    var token = {
      type: 'BLANK_TOKEN_TYPE',
      value: 'BLANK_TOKEN_VALUE',
    }
    
    var op = matchTokens[1];
    var number = matchTokens[2];
    
    if (op) {
      token.type = 'operator';
      token.value = op
    } else if (number) {
      token.type = 'number';
      token.value = parseFloat(number, 10)
    }
    
    tokens.push(token);
  })
        
  return tokens;
}
         
function userError(message) {
    selOutput.text(message);
    throw new Error(message)
}

function log(mess,ag,es) { console.log.apply(console, arguments) }
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console