Ask coding questions

← Back to all posts
Help me create a programing language in python
CodinUser (7)

WTSLang

So yeah. I need help creating a programing language. I have done some amount of the work, but yeah
See baisc.wts for an basic idea line by line

Features

  • Python like syntax
  • Simple and basic programing concepts

The first line

#!wts:script $bind #root %main%

This line is called the header, it is used to initlaize a program and is required. Basicly what the header says is

[1] Use WTS Script
[2] Bind a main function to the entire program

This header basicly makes you not need to do something like..

#wts:script
function main () {
  ...
}

So $bind does that, bind #root (which is the entire file excluding the header)

This header turns None if its there, and InvalidHeaderError if it doesnt exist or is incorrect

So since we are working on math for now. its mainly math functions that are included

Progress

  • Create some sample scripts
  • Work on finding the header and return error
  • Create a method to call the Lexer LINE by LINE
    Line By Line is the syntax, but I think it will b bad for functions classes and but idk if there even going to arrive. Most likely will come in a reconstruction in 5/10 years lol
  • Ignore comments.
    limitations : #comment is considered as a keyword, not a comment, so you need to type
# COMMENT TEXT
NOT
#COMMENT TEXT
Answered by fuzzyastrocat (1867) [earned 5 cycles]
View Answer
Comments
hotnewtop
fuzzyastrocat (1867)

Creating a programming language is a big project. Step 1 is to not expect that you'll get anything done quickly! Instead, take it slow and do things step by step.

The link @DynamicSquid provided (craftinginterpreters.com) is a great link to get started. It is done in Java (first half) and C (second half), but you can probably transpose it to Python if you're willing to do a little extra. Since that resource includes all the details of what designing a language entails, I'll mention how to avoid a few pitfalls before you even get started.

Even if you don't plan to use the book, definitely check out http://craftinginterpreters.com/a-map-of-the-territory.html. It is extremely important to understand how a programming language works before you try to build your own, or else you'll start making something that doesn't really work and end up having to start all over again! Most people start this way (me included) and it's horrible!

This is where the "take it slow" part comes in. Since there are many different components to a programming language, it's best to work on them one-by-one. First, decide on a basic syntax for the language — it looks like you've got some ideas already, but try to put all your thoughts out on paper (err, in a file) so that you know what you're working towards. This isn't final or anything, but it will be way harder to make your language if you don't know exactly what you're trying to achieve. Think about the features you want your language to have, and write them out so you can remember them. It doesn't have to add up to a working program or anything (better yet, just make many small demo programs), but get it out there! (I'm stressing this part because it's one of the least emphasized in language design, and most people skip over it which leads to headaches later on.)

Next, start the first component of the language: the lexer. (This is explained in the link above.) Once you've got that working, move to the parser. Then optimization. Then bytecode/intermediate language generation. And so on and so on, but the point is that you need to make sure each step is solid before you move on to the next. Once again, this can lead to some major headaches if you don't.

Best of luck on your language design journey! It may seem a bit daunting at first, but don't worry, it's very doable if you're willing to stick with it!

CodinUser (7)

@fuzzyastrocat Alright thanks! and ill have a look at the links
thanks for the tips :)

I am first working on the main file reading sections, error messages part....

fuzzyastrocat (1867)

@CodinUser Great!

By the way, I notice you say the lexer goes line-by-line, but actually lexers work best when they go word-by-word (counting numbers, parentheses, etc as "words"). All the lexer should do is convert the program into a list of known "symbols" (so "x = 5 * 3" becomes [VAR x, SYM <equals>, NUM 5, SYM <times>, NUM 3] for example), which is actually simpler when you don't do it line-by-line.

CodinUser (7)

@fuzzyastrocat Thanks!
But i decided not to make this project because it wont really be usefull after its developed due to its limitations

DynamicSquid (5029)

Nice question!

For your first step, try making a calculator. It should take in user input as a string like this (you don't have to worry about brackets for now):

5 +8 / 2*5 + 14

And return an array like this:

[5] [+] [8] [/] [2] [*] [5] [+] [14]

Then from that array, "parse" it to get a single value (remember order of operations matters!):

39

That was my first step when I made my language. Hope that helps :)

CodinUser (7)

@DynamicSquid Yeah but i was wondering how to do print() like functions
but good tip anyway :)

Also how do we parse multiple lines?
like

write "Hello world"
#output : ['write', 'Hello world']
DynamicSquid (5029)

@CodinUser Well, making a calculator is only the first step. It teaches you about some important concepts in language design.

DynamicSquid (5029)

@CodinUser If you can make a calculator, you can basically make a language as well :)

CodinUser (7)

@DynamicSquid Alright :)
I will have a look at it :)

Thanks!

Also is there any good sites i can look for some ideas?

DynamicSquid (5029)

@CodinUser This is basically the best website to learn from: https://craftinginterpreters.com/

But it's done in Java

CodinUser (7)

@DynamicSquid Thanks will have a look :)
EDIT : I think ill maybe not work on it.. Its too big i think.