Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:07:01+00:00 2026-06-16T01:07:01+00:00

I am interested in extending my knowledge of computer programming by implementing a stack-based

  • 0

I am interested in extending my knowledge of computer programming by implementing a stack-based programming language. I am seeking out advice on where to begin, as I intend for it to have functions like “pushint 1” which would push an integer with value 1 on to the top of the stack and flow-control via labels like “L01: jump L01:“.

So far I have made a C# implementation of what I want my language to act like (wanted to link to it but IDEOne is blocked), but it is very messy and needs optimization. It translates the input to XML and then parses it. My goals are to go to a lower level language, (perhaps C/C++) but my issues are implementing a stack that can hold various data types and does not have a fixed size.

Eventually I would also like to implement arrays and functions. In addition, I think that I need to have a better Lexer and I am wondering if a parsing-tree would be a good idea for such a simplistic language.

Any advice/criticism is welcome, and please consider that I am still reasonably new to programming (I have just recently completed AP CompSci I). Also, links to open-source stack-based languages are welcome.

Here is a basic program that I would like to try and interpret/compile (where [this is a comment]):

[Hello World!]
pushchar    '\n'
pushstring  "Hello World!"
print
[Count to 5 and then count down!]
pushint     1
setlocal    0
L01:
pushchar    '\n'
getlocal    0
print           [print x + '\n']
getlocal    0
increment
setlocal    0   [x = x + 1]
pushint     5
getlocal    0
lessthan        [x < 5]
iftrue      L01
L02:
pushchar    '\n'
getlocal    0
print           [print x + '\n']
getlocal    0
decrement
setlocal    0   [x = x - 1]
pushint     0
getlocal    0
greaterthan     [x > 0]
iftrue      L02

The expected output would be:

Hello World!
1
2
3
4
5
4
3
2
1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:07:02+00:00Added an answer on June 16, 2026 at 1:07 am

    A stack based language such as Factor has the following syntax:

    2 3 + 5 - print
    

    This is equivalent to the following C style code:

    print(2 + 3 - 5);
    

    The advantage of using a stack based language is that it’s simple to implement. In addition if the language uses reverse polish notation, as most stack based languages do, then all you need for the front end of your language is a lexer. You don’t need to parse the tokens into a syntax tree as there’s only one way to decode the stream of tokens.

    What you’re trying to create is not a stack based programming language, but a stack based virtual machine. Application virtual machines can be either stack based or register based. For example, the Java Virtual Machine is stack based. It executes Java bytecode (which is what you’re creating – bytecode for a virtual machine). However the programming languages that compile to this bytecode (e.g. Java, Erlang, Groovy, etc.) are not stack based.

    What you’re trying to create is like the assembly level language of your own virtual machine, which happens to be stack based. That being said it’ll be fairly easy to do so – stack based virtual machines are easier to implement that register based virtual machines. Again, all you need is a lexer such as flex. Here’s a small example in JavaScript using a library called lexer:

    var program = "[print(2 + 3)]";
    program += "\n push 2";
    program += "\n push 3";
    program += "\n add";
    program += "\n print";
    
    lexer.setInput(program);
    
    var token;
    var stack = [];
    var push = false;
    
    while (token = lexer.lex()) {
        switch (token) {
        case "NUMBER":
            if (push) stack.push(lexer.yytext);
            else alert("Unexpected number.");
            break;
        case "ADD":
            if (push) alert("Expected number.");
            else stack.push(stack.pop() + stack.pop());
            break;
        case "PRINT":
            if (push) alert("Expected number.");
            else alert(stack.pop());
            break;
        }
    
        push = token === "PUSH";
    }
    <script src="https://rawgit.com/aaditmshah/lexer/master/lexer.js"></script>
    <script>
    var lexer = new Lexer;
    
    lexer.addRule(/\s+/, function () {
        // matched whitespace - discard it
    });
    
    lexer.addRule(/\[.*\]/, function () {
        // matched a comment - discard it
    });
    
    lexer.addRule(/\d+/, function (lexeme) {
        this.yytext = parseInt(lexeme);
        return "NUMBER";
    });
    
    lexer.addRule(/push/, function () {
        return "PUSH";
    });
    
    lexer.addRule(/add/, function () {
        return "ADD";
    });
    
    lexer.addRule(/print/, function () {
        return "PRINT";
    });
    </script>

    It’s really simple. You can fiddle with the program and modify it to your needs. Best of luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of classes extending an abstract Base class. Each subclass takes
I am an intermediate javaScript programmer, and i am interested in expanding my knowledge
I am interested in extending my application with plug-ins. I made the Application to
I am interested changing the ISO values of my Android Phone Camera. I have
I have exercise below: The Lavin Interactive Company, which has developed the turn-based strategy
We are interested in extending the session duration for guests that visit our Magento
I'm interested in extending the behavior of jqueryUI's dialog widget. In the past I've
i have an ajax engine based on jQuery. And after receiving the response i
I already know some Python and got interested in extending Blender using Python scripts.
Interested in upgrading JAVA VERSION from JAVA 1.5 to JAVA 1.6 [oracle@server301 /]$ java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.