Programming Language Template
AmazingMech2418 (1103)

Programming Language Template

Have you ever wanted to create your own programming language in Node.JS? If so, this template is right for you! Using the advanced custom constructor system in the new Adapt programming language, this template can allow you to define your own syntax for your programming language!

This language will technically be an "Adapt dialect" since it will use the Adapt interpreter, but is fully custom and removes the built-in constructors for language development on the running of your program in your custom language.

This template makes creating your own programming language easier than ever. So, you may be wondering how to use it now...


Syntax Definition

First, you must define your overall language syntax. Starting on line 278, the definition for the syntax object begins. There, you can define custom parentheses sets (currently only supports single-character parentheses), quote sets (again, only single-character for now), whitespace (recommended to leave alone), the character to end the line (by default just a new line character, but can be changed to semicolon or whatever), and indents (for Python-like syntax) if you want any. If you don't want indents, just set the parameter to false. If you do want indents, set the parameter to a string with whatever you want to use for indentation. By default it is two spaces. However, for indentation usage, the {} parenthesis set must be used since the interpreter will add those in place of the plain indentations, also with a space after the opening bracket. When defining a function that will use indentations, make sure to use the brackets instead in your constructor.

Programming Your Language

Intro to Language.Adapt

The language.adapt file is where you will program your language as an Adapt dialect. It uses the Adapt language syntax as will be further explained in the next few sections. From lines 1 through 5 are the definition for comments. If you edit the constructor (let's say to #$1 to be based on the Python comments), make sure to edit or remove the comment on line 7 or otherwise, it will throw an error.

Defining Native Functions

To define a native function, use the following format:

native function myFunction
  (ARGS) => {
    // JavaScript Code Here

In this case, myFunction is our function name. A native function is a function written in the host language (Node.JS), primarily for development purposes. The default native function constructor is removed on the running of your custom language's program, so if you want to use it within your language, you must redefine the constructor.

Creating Constructors

To create a constructor for a function (not to be confused with class constructors), use the following format:

constructor myFunction ({},{},{})

Inside the {}s, there are the constructor string, parameter map, and evaluation parameters list. The second two are in JSON while the first is just plain text.

Constructor Strings

A constructor string defines the syntax for your function. Make sure to use "$" followed by a character to denote the location of any parameters in your function. Remember that this constructor string is sensitive to whitespace besides where a parameter is located. In the constructor print($1), print( $1 ) will work while print ($1) will not. Additionally, any whitespace in the constructor string cannot be ignored and must be in the function calls.

Parameter Map

The parameter map is an array of arrays with the format `["character in constructor string","parameter name"] in JSON format. It links the parameters in the constructor string with parameters in the function in the ARGS object in the native function JavaScript code.

Evaluation Parameter List

The evaluation parameter list is simply an array in JSON format that defines which values to further evaluate. Parameters must be included in this list to be able to be held by variables.

Defining Non-Native Functions

To define non-native functions, those being functions that are written in your custom language, you must link the interpreter to a code parameter in a native function for function declarations. The interpreter code to include is as follows:

program = fs.readFileSync('index.adapt').toString();
_l = compileLines(program,syntax);
for(line of _l) {

The variables program, line, and _l may be changed, but should not conflict with variables already in the index.js file. Use the let keyword if you change one of the variables, or even preferably anyways so it doesn't conflict with the running of your program.


This documentation doesn't include everything needed for you to create your programming language, so please ask any questions in the comments.


The demo program in the template uses the print function from the THAIL programming language which is currently under development by myself, @LiamDonohue , @StudentFires , @CaptainAnon , @LizFoster , @MesyetiIsTaken , @GaneshaSharma , @SilentShadowBla , @Eleamonthe , @AdriaDonohue , @Codemonkey51 , @Coder100 , and @shaanbomb123 . (list gathered from multiplayer repl which everyone who is a part of the project is in) If you have any questions about THAIL, please ask @LiamDonohue , the leader of the project. The THAIL language is also currently planned to use the interpreter used within this template and may even use this template entirely. The Adapt programming language was an idea of mine for a while, but was recently revived when I became part of the THAIL team since the Adapt programming language can make it easier to develop new programming languages as Adapt dialects. Therefore, as an example program, I used the syntax that will be used in the THAIL programming language once complete. However, all code in this project is 100% by me.

You are viewing a single comment. View All
xxpertHacker (931)

There is only one last thing I have to recommend: if this were a real production language, the Adapt programmer shouldn't need to know JavaScript in order to change the syntax.

The syntax modification should be a separate file in a different format entirely. I don't know what format (preferably something easy to understand), but the index.js should read from the file and set-up the interpreter's syntax accordingly.

The syntax modification should have a clear and simple user-interface though. Ideally, usable by someone who knows no other coding languages, but they know English.
There should be translations of the lang too, ideally, but that's hard, so whatever.

AmazingMech2418 (1103)

@StudentFires It would be possible to put it in a JSON file, but it would add another level to trying to get a web-based version as well.