Create Your Own ‘Language’

How To Create Your Own ‘Language’

This tutorial will show you how to create a programming language using Python 3.

  • Setup .py file
  • Create language in Python 3
  • Setup language within your own language

Setup .py File

To set up the file you can rename the file to the name of your language ex. main.z++ or main.u+ (the main part can be whatever you like, the file name like .py is your language name)

Create Language

To create your own language you have to write it I am using Python 3 to make it.

To actually make it you will change the names of things and add in things like modules.

To change the names of things like print('')
you will need to do this

<name of your thing> = <thing your trying to change>

an example would be output = print

You can also add in modules like import random and load them in with $ pip install. You can even change their names ran = random.randint

Setup Language

When you finish coding your language you need to set it up. First create a new file on the file tab, type in <whatever you want>.<name of language> and press return. Open your new file and you need to type from <name of language, ex. z++> import * that adds your language into your own language file. Now go into the .replit file and change the run to the name of your file `.. Now you can code away in your own language!

NOTE:

I would recommend putting your code for your language and your language file into a folder and naming it .<name of your language> to keep it tidy.

4 Likes

Wow, this is quite helpful. I have a question about the file naming, though. Will Replit recognise the language you make as something, or will it simply convert it into a text file?

For the file naming, the file must have .py at the end of it

1 Like

No, I meant when renaming it to .zz or something.

oh it will just convert into a text file

Hmm then I’m not sure if @SalladShooter 's code will work…

Do you wanna collab with me? I’m making something called TechCell OS and it uses Python, HTML and JavaScript

Sorry, but I’m not very good in other languages besides Python, Scratch, a bit of Flask and basic HTML (and very very very basic Javascript)…

Still, you could help! I’ll invite you right now!

Whoops I forgot to mention that you have to change the .replit file when running to the name of your language file so it can run that.

1 Like

I am wondering, is this not just python with different names? It could be a fun little project, but it will hard to find an experienced programmer who would consider this a programming language. Ig that is why the language part is in quotes.

2 Likes

Yes, and you can add functions that weren’t normally there and add modules. It’s basically a personalized version of that language.

An actual programming language made using Python would include the following in its development:

Tokens: Can include data types or some operations.

tokens = (
    "INT",
    "FLOAT",
    "STR",
    "PLUS",
    "SUB",
    "MUL",
    "DIV"
)

Something called the Lexer is used to add rules to these actions, and then the output is returned.

# lexer.py
# Associate with Regex

L_ADD = r"\+"

I don’t have time to give more examples, but that is the main idea. I found this article if you’re looking for more information:

4 Likes

As @bobastley thinks, it’s not a new programming language. It just new functions and new Python pattern, as .py, .pyw, etc. To make real programming language, you must:

  • choose: create interpreter for your language or compiler.
  • Then, create a lexer file what will classify all your tokens on the groups.
  • After, create a parser: program what takes lexer’s tokens and makes with it AST(Abstract Syntax Tree).
  • Then just create a program what will execute the AST to the bytecode.

I hope I’m right.

Note: create interpreter on Python isn’t a good idea because Python is a slow language, and make a big interpreter with lot’s of math work too on Python…
Use C++ instead. It’s faster, or use Crystal. I think it’s the best programming language because:
“As fast as C, as slick as Ruby…”

It’s easier to make compiler for your language. It’s the same as interpret, but only up to create AST. Then, you must translate your text to the machile language and then run it without any commands, just ./<machinelanguagefile>

3 Likes

I was wondering, if I was to make my own programing language, how to make an if statement or loops. I’ve tried doing this in one line but it doesn’t work…

Or you can do the hard way
heres an example:

from sly import Lexer, Parser

class BasicLexer(Lexer):
    tokens = { NAME, NUMBER, PLUS, MINUS, TIMES, DIVIDE, ASSIGN, LPAREN, RPAREN, NEWLINE, IF, ELSE, EQ, GT, LT }

    ignore = ' \t'

    NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
    NUMBER = r'\d+'

    PLUS = r'\+'
    MINUS = r'-'
    TIMES = r'\*'
    DIVIDE = r'/'
    ASSIGN = r'='
    LPAREN = r'\('
    RPAREN = r'\)'
    NEWLINE = r'\n+'
    EQ = r'=='
    GT = r'>'
    LT = r'<'

    IF = r'if'
    ELSE = r'else'

    def ignore_newline(self, t):
        self.lineno += t.value.count('\n')

    def error(self, t):
        print("Illegal character '%s'" % t.value[0])
        self.index += 1

class BasicParser(Parser):
    tokens = BasicLexer.tokens

    precedence = (
        ('left', PLUS, MINUS),
        ('left', TIMES, DIVIDE)
    )

    def __init__(self):
        self.vars = {}

    @_('statement')
    def expr(self, p):
        return p.statement

    @_('NAME ASSIGN expr')
    def statement(self, p):
        self.vars[p.NAME] = p.expr

    @_('expr')
    def statement(self, p):
        return p.expr

    @_('IF expr statement')
    def statement(self, p):
        if p.expr:
            return p.statement

    @_('IF expr statement ELSE statement')
    def statement(self, p):
        if p.expr:
            return p.statement0
        else:
            return p.statement1

    @_('expr PLUS expr', 'expr MINUS expr', 'expr TIMES expr', 'expr DIVIDE expr')
    def expr(self, p):
        if p[1] == '+':
            return p.expr0 + p.expr1
        elif p[1] == '-':
            return p.expr0 - p.expr1
        elif p[1] == '*':
            return p.expr0 * p.expr1
        elif p[1] == '/':
            return p.expr0 / p.expr1

    @_('expr EQ expr', 'expr GT expr', 'expr LT expr')
    def expr(self, p):
        if p[1] == '==':
            return p.expr0 == p.expr1
        elif p[1] == '>':
            return p.expr0 > p.expr1
        elif p[1] == '<':
            return p.expr0 < p.expr1

    @_('NAME')
    def expr(self, p):
        try:
            return self.vars[p.NAME]
        except LookupError:
            print(f"Undefined name '{p.NAME}'")
            return 0

    @_('NUMBER')
    def expr(self, p):
        return int(p.NUMBER)

    def error(self, p):
        if p:
            print(f"Syntax error at token '{p.value}'")
            self.parser.errok()

lexer = BasicLexer()
parser = BasicParser()

while True:
    try:
        text = input('basic > ')
    except EOFError:
        break
    if text:
        parser.parse(lexer.tokenize(text))

Its beautiful

3 Likes

May be ply instead of sly?

Yea I know, you cannit parse block statements in one stroke

This is like saying making C macros is a programming language.

I don’t recommend either. If you’re looking for an easy way, use EBNF, RegEx, SLY, YACC or something else rather than just redefining stuff.

1 Like

@anonymt @NataliaKazakiev We are getting off topic. If you want you can make a post about making a real language not a personalized one like this #resource . Have a great day :grinning:!