Submit templates for repl.it/templates here.

← Back to all posts
Programming Language Template
AmazingMech2418 (661)

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...

Usage

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) {
  readFunction(line);
}

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.

Questions

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

Credits

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.

Commentshotnewtop
CSharpIsGud (415)

I was making something like this, but I didn't use regular expressions and did the full tokenizer -> parser combo but in a customizable way

Highwayman (1325)

Oh wait this is adapt!! :D awesome!!

xolyon (333)

Nice you finally completed it

Codemonkey51 (771)

YAY
also FYI I’m a designer lol

AmazingMech2418 (661)

@Codemonkey51 Also, I don't really know everyone's role so I just put everyone who is in the repl in the credits. LOL!

Codemonkey51 (771)

That’s fine I just didn’t code anything on it yet I may contribute some code later ¯_(ツ)_/¯ @AmazingMech2418

AmazingMech2418 (661)

@Codemonkey51 Yeah. Honestly, it is just now actually starting with the interpreter/compiler since I added the Adapt interpreter's base into the THAIL repl.

StudentFires (327)

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 (661)

@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.

xolyon (333)

is this related to the THAIL project?

AmazingMech2418 (661)

@xolyon Kind of... This will be used to create the THAIL programming language.

TheForArkLD (618)

Ohhhh myyy god it cool i making language too

TheForArkLD (618)

@AmazingMech2418 But i maked three templates...

AmazingMech2418 (661)

@TheForArkLD This is my second. I'm making a third soon too.

TheForArkLD (618)

@AmazingMech2418 Fibonacci In My Original Language “DefLang”

program main;
comment:Fibonacci in DefLang;
def:cont:1:normal;
def:cont2:1:normal;
echo:1;
echo:1;
for:i:1:998;
def:cont3:%cont%:normal;
def:cont:%cont%+%cont2%:calc;
def:cont2:%cont3%:normal;
echo:%cont%;
next;
end program
StudentFires (327)

If you can't make T.H.A.I.L. into Adapt, I'll try to help make it an extension to the language. Also, where's the print being used?

Also, you forgot "use strict";.

AmazingMech2418 (661)

@StudentFires The print function is used in the index.adapt file. If you run the repl, it runs the print function.

AmazingMech2418 (661)

@StudentFires What do you mean by "forgot "use strict";"?

StudentFires (327)

@AmazingMech2418 All JavaScript code should have it at the beginning of the file, it's highly recommended. I'm going to force THAIL to be developed under strict mode.

StudentFires (327)

@AmazingMech2418 The template looks okay, all you need now are comments for readability and you'll have a great chance at success.

AmazingMech2418 (661)

@StudentFires I just added strict mode. Also, thank you! Where should the comments be though? In the index.js file so that people can understand how it works?

StudentFires (327)

@AmazingMech2418 Ummm... everywhere; to explain how everything works?

Just try not to interfere with the readability of the actual code, I suck at that. Just make the comments really good and informative while being short and you're set.

AmazingMech2418 (661)

@StudentFires Okay. I'll start working on that tomorrow then.

StudentFires (327)

@AmazingMech2418 Oh, and btw: T.H.A.I.L. development hasn't even begun yet; Liam should be learning JavaScript, but I don't know.

AmazingMech2418 (661)

@StudentFires Well, I added the Adapt interpreter, so I think I started the actual development of THAIL...

xolyon (333)

@AmazingMech2418 lol now everyone's making their own languages!

can you work on the RPG?

AmazingMech2418 (661)

@xolyon Not right now. I have a meeting for school in 2 minutes... After about noon, I should though.

xolyon (333)

@AmazingMech2418 whens that lol you're in a different time zone!

AmazingMech2418 (661)

@xolyon Well, it's 8:37 AM right now.

StudentFires (327)

@AmazingMech2418 @xolyon What time zones are you two in!?

Sorry if I'm interrupting, you're commenting under my comment.