-
Notifications
You must be signed in to change notification settings - Fork 3
RFC: Programming language #1
Description
Overview
An interpreted, procedural, stack-based programming language that is similar to Forth and Lisp. Its expressions are in Reverse Polish Notation (RPN). Written in C++.
The Problem
Background & Motivation
Simple and elegant languages like Forth and Lisp are interesting to me. It would be cool to make one myself!
Solution
Goals
MVP
- The interpreter can print (to standard output) the results of basic arithmetic expressions (the operations "+", "-", "*", and "/", as well as the boolean operations shown below, act on integers to produce a single integer)
- The typical boolean operations "&&" (and), "||" (or), "!" (not) can also act on integers. "0" is falsy and all other integers are truthy
- The interpreter can use integer command line arguments as integers in the expressions
- The interpreter can initialize variables that can be used in the program
- The variables can be incremented, reassigned, etc.
- Functions
Stretch goals
-
New data types:
- Strings, with output, concatenation, etc. operations
- Arrays, with inserting, accessing, assigning, etc.
-
If statements
-
For and while loops
-
Make the language able to be compiled (probably transpile to C++)
Non-goals
- No OO support; it's procedural
- No variable scope; just global scope
Dependencies
We can use a lexer and/or a parser (shown below), but we can also just try to do it ourselves.
Alternatives & Prior Art
- Forth, shown above
- Lisp, shown above
Design
Example parsing:
Source code: 3 7 + 4 * print
Pre-define a few functions/operations (e.g. print, "+")
Go through each space separated value
Reading [0]: 3. it isn't a function/operation, add to stack. Stack: [3].
Reading [1]: 7. it isn't a function/operation, add to stack. Stack: [3, 7].
Reading [2]: +. it is a function/operation, evaluate the + function given n arguments in the stack (2 in this case, because + takes 2 args), replace past n values in stack with evaluated integer. Stack: [10].
Reading [3]: 4. it isn't a function/operation, add to stack. Stack: [4]
Reading [4]: *. it is a function/operation, evaluate the * function given n arguments in the stack (2 in this case, because * takes 2 args), replace past n values in stack with evaluated integer. Stack: [40].
Reading [5]: print. it is a function/operation, evaluate the print function given n arguments in the stack (1 in this case, because print takes 1 args), replace past n values in stack with evaluated integer. ("40" is printed to stdout) Stack: [].
End of program