Concept: a basic programming language that should allow basic calculations, variables, method definitions and method calls, compile program to binary.
For a while I wanted to get into creating my very own programming language. Implementing a full blown C style language like C#, C++ or Java would be an undertaking requiring hundreds of years of my own time. Since I’m only human I thought I’d rather learn about programming languages and compilers by creating a language that has a very simple syntax, literally just two parts per statement. An example set of statements that would allow you to create an integer variable and initialise it to the value 1928 would be:
value 1928
assign_to num1
Keyword Reference
Keyword | Description | Example |
value | Declare an value to be used in some context, e.g. variable declaration and initialisation. | value 1928 assign_to num1 |
assign_to | Assigns a value to a named variable. The value should be declared as a literal or as another variable name. The value should use the “value” keyword as a statement before the assign_to call. | value 1928 assign_to num1 |
call | Invokes a standard or user defined method. If the method requires arguments then one or more “param” statements need to follow the method invocation. | call fn_print param 123 |
param | Declares the value or variable name containing the value to be passed as an argument to the method invocation. | call fn_print param 123 |
// | Double forward slashes would indicate a comment line. Any text can follow this and it will be ignored by the compiler | // This is a small comment |
add | Mathematical addition operation | add 1 |
sub | Mathematical subtraction operation | sub 1 |
mul | Mathematical multiplication operation | mul 5 |
div | Mathematical division operation | div 3 |
pow | Mathematical power operation | pow 2 |
root | Mathematical root operation | root 2 |
include | This keyword copies the contents of another two part file at the location where it’s used. | include “file-2.tp” |
Example TwoPart Program
include "another-tp-file.tp"
call fn_print
param 123
call fn_print
param 456
call fn_print
param 789
call fn_print
param "hello world"
value 1928
assign_to num1
call fn_print
param num1
value "Adhir"
assign_to name
// you can even include files right here at this position.
include "middle-file.tp"
//print with var
call fn_print
param name
//comment
if true
call fn_branch1
ifnot true
call fn_branch2
// some weird calculation
value 2
add 2
mul 4
div 2
sub 9
pow 3
root 2
assign_to result
// pythagoras a^2 = b^2 + c^2
value 0
assign_to a
value 3
pow 2
assign_to b
value 1
pow 2
assign_to c
value b
add c
root 2
assign_to a