Thursday, June 7, 2012

Your own Programming language - SCT

This is an article that gives a tutorial on redefining C++ syntax with SCT. The documentation is pretty straightforward, but this will show step-by-step how to make your own language in a few easy steps. Disclaimer: This is not really a programming language. You are just redefining some syntax here. If you TRULY want a programming language you should look into compiler design.

1) Download SCT
SCT is my own free product and I have it hosted here on Github. If you are on a debian-based system you can install the .deb (you may get a standard compliance warning if you use the Ubuntu software center. It's Okay to install this file. If you don't believe me, look at the source on Github). Otherwise, you will have to download the executable file ( in bin) and run it directly.

2) Make a langdefines.ldf
This is a type of file that holds a language. The syntax is old_new. You can use "$" for commenting, but not on its own line. For instance:
oldnew would not become part of this line. Here is how you could set it up to work with a hello world application for C++.


3) Make a code file
You can now make a code file. It is just a normal text file that adheres to the langdefines rules. However, you can include strings that the langdefines does not know; they will just not be translated. In fact, this is the only way to get some characters into code.

import <iostream>
using namespace txt;
int main(){
say <-- "Hi!" <--eol;

Of course, this is an overly simplistic example. Langdefines can contain up to 10,000 lines.

4) Run sct
You should run "sct" from the command line (if you used the .deb) or just run the executable. Here is what to do at each prompt:

What is the total file path to the language definition file?
$ /home/yourname/example.ldf

this one is pretty self explanatory. Where is your langdefines?

Would you like the langdefines automatically ordered? y or n
$ y
This one causes some confusion. Before I explain what this does, take an example.
You have a langdefines that reroutes || to or like this: ||_or. However, below it you have this: for_door. This will become "do||" in the code, causing a massive error. Ordering the langdefines by line size helps to prevent this. Ordering is not foolproof, however. Comments are DELIBERATELY COUNTED. If you want a particular line to be longer than the other for this reason, it is a good way to do it. Be careful with comments for this reason.

Are you using a Noran Make File? y/n
$ n
A Noran Make File is like a make file, but it specifies a list of files to translate at one time. The syntax is infile_outfile_magichar for every line. infile is the code, outfile is the output filepath, and magichar is what character you used for translation comments. Translation comments allow strings that you KNOW will be translated be preserved. In our above example, you could save "door" by doing $door$ and using $ as your magichar. $ is the standard.

If you did not use a Noran Make File, this is what the rest of the program will be:
 What is the total file path to the code?
$ /home/yourname/excode
This is where your code is located.

What is the file path to the output file?
$ /home/yourname/whatever.cpp

Finally, this is where to put the translated code.

If you would like to easily call this form other programs, it can run in argument mode, but it does not support iterative compiling.

Congratulations. You have now redefined C++ syntax and converted code that you wrote in your own custom language into runnable C++.

PS: Here is what my language looks like:

import <iostream>
use nspace std;
number program() (*
itl(number index is 0; index lthen 10; index`incra`)
say <-- "$Hello world, Iteration $"<--index<--eol;
mail 0;

No comments:

Post a Comment