cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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>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>
</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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console