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