Ask coding questions

← Back to all posts
# How would I add if statements to my coding language
angrydoge (305)

So...

I made this cool simple coding language, but i dont know how I would add if statements.

Help would be really appreciated

kthxbye

Commentshotnewtop
fuzzyastrocat (1461)

Don't make a language this way! I'm not trying to downplay what you've done here, but this just isn't how a programming language works. (That's why you're having trouble adding if-statements, and it will only get harder.)

I'd suggest reading https://craftinginterpreters.com/. It's a great tutorial that will walk you through how to properly build a programming language. It'll teach you all the theory behind how coding languages work, but it won't bore you with it since it always applies it practically.

angrydoge (305)

@fuzzyastrocat dude, all these turorials usually import 7 different modules and its super advanced, and usually the site uses 100 different cookies, and has 10 ads on each page, so thats why im doing a split language

fuzzyastrocat (1461)

@dabombdgdzjr Did you even look at the tutorial I linked? It uses 0 modules (only the C standard library), the site uses no cookies, there's no ads... that's why I linked it. And it literally builds from no lang experience — totally from the ground up!

angrydoge (305)

@fuzzyastrocat Ik, dude im just not ready for that mucha dvanced. I also didnt say that site has those. I said the sites in general. Also i am terrible at using any type of other language inside of python

fuzzyastrocat (1461)

@dabombdgdzjr Well, I wouldn't call it advanced. It walks you through literally everything, giving step-by-step code bits. Additionally, since the site doesn't have those, it's not really relevant. If you're bad at langs outside of Python, then I highly encourage you to try other langs! Python is an unescapable pit if you learn Python first and never transition to another language. Python's not bad, it's just that if you don't try to learn another language now it'll be nearly impossible later.

angrydoge (305)

Ik @fuzzyastrocat I know a few other languages: tiniest bit of cpp, html, js, some css.

RayhanADev (1215)

@fuzzyastrocat uwu you gave me my next project thank you!!!

xxpertHacker (649)

Well, since you gave absolutely no information whatsoever, I recommend just emitting hardware branching instructions. Seems simple enough, just write the operation codes into a file, what part are you struggling with specifically, writing into the file, finding the proper codes, validation?

Okay, seriously, I suggest checking out @CSharpIsGud's parser tutorials in Python before you make your life any harder than it has to be: How To Make A True Coding Language Part 1.

angrydoge (305)

I gave some information, If you want more, Its a split based language, I need to know how to add if statements, This is my First coding language @xxpertHacker

xxpertHacker (649)

@dabombdgdzjr I've never heard of a split-based language.

But where did you give extra information regarding your language?

Is it interpreted or compiled?
If it's interpreted, are you interpreting text or bytecode?
If it's compiled, is it AoT or JiT compiled?

Which frameworks, libraries, and toolchains are you using? LLVM is pretty good, from what I've heard.
I see you attached a Repl in Python, surely that's not the actual language that it will be developed in, right?

What is the motivation behind the language? What does it fix? What does it do better?

Is it a low-level, medium-level, or high-level programming language? Or wait... is it even a programming language, or is it something else, like a scripting language or markup language?

What are you taking inspiration from? I presume you must have been looking at C-family languages since you want an "if statement," and those are honestly pretty lame and cliche at this point. How about a "match statement"?

What paradigm is your language, object-orientated, functional, etc?

How do you even want your "if statement" to work? For example, Wasm and Rust have very similar if statements, as they can execute code and return values, putting them somewhere in between a C "conditional operator" and C "if statement", where does yours fall into?

What part of adding them do you need help with? Parsing them? Lexing them? compiling them? Executing them?

As you can see, there's a lot that we don't know about your language, we just know that it's a language and that you're trying to make it.

angrydoge (305)

0_0 i didnt think there was much else i needed to add

xxpertHacker (649)

@dabombdgdzjr Well, in that case, the link I originally gave is still up: How To Make A True Coding Language Part 1 :).

angrydoge (305)

It feels like everyones calling my language not a "true" coding language :/ @xxpertHacker

xxpertHacker (649)

@dabombdgdzjr That's... not by coincidence.

There are a lot of different ways that you can create a coding language, and there are a lot of ways that have yet to be created... but, this isn't one of them.

How does a real language do stuff?

Take, for instance, using the word "computer," as you had done in your "program."

Most languages would do a lookup in the nearest lexical scope, checking for a declared variable by the name of "computer," searching the next outer scope until it has been found.

If it was not found, usually an error is thrown, whether it be dynamic, like an ECMAScript "ReferenceError," or compile-time, like a C compiler error.

If it was found, it will continue executing/compiling the expression, in this case, it would do a property lookup with the name "startup," if the object has it, and it's not private (depending on where the code is being parsed/executed from), it gets read and the expression continues, otherwise, an error is thrown or something of the sort.

How did you do it? You hardcoded it into an if statement :/

  elif "computer" in read:
    if ".startup" in read:

The language itself can be a "true coding language," but the implementation sucks.

You're going to go through hell trying to add variables.
You will suffer trying to add simple "if" "else" statements.
Your life will be made miserable, even daring to add a simple looping construct.
Don't make me mention functions, they're oftentimes bound to a name, not unlike a variable.
And don't think of doing anything cool and unique in your language, because, at that point, you will have given up on coding in its entirety.

Now, what if I had told you, that all of that could be avoided? How about you open that link, or take the advice of others here? We're only trying to push you in the right direction.
You don't have to follow the same path that every other language implementation follows, but it can't be what it currently is, you'll know when you're on the right track.

angrydoge (305)

@xxpertHacker ... honestly lexers are horrible for me so i thought this was a way to do it but people are just keeping giving me links to how to make an "actual" coding language instead of encouraging this experiment

xxpertHacker (649)

@dabombdgdzjr To anyone who hardcodes their programs, I have no sympathy for, sorry.

angrydoge (305)

its just my first coding language, its simple, if your gonna be like that (no offense) please dont comment @xxpertHacker

Coder100 (12434)

wait you are using a split-based language?
Well you are going to have soo much fun with adding strings and comments...

angrydoge (305)

adding comments easy doe @Coder100

Coder100 (12434)

@dabombdgdzjr Why are you checking for in when you should be checking letter by letter?

Coder100 (12434)

@dabombdgdzjr well that means like it's going to ignore erroneous commands/keywords and if there were errors, it would get super confused

angrydoge (305)

i notcied thats happening for if statements for me, I tired something but it is not work @Coder100

Coder100 (12434)

@dabombdgdzjr yeah, you should probably learn how to make a real programming language
try this
just search up 'how to make a programming language'

angrydoge (305)

"yeah you should probably learn how to make a real programming language"

D':
ok @Coder100

Coder100 (12434)

yeah sorry, but making a programming language isn't super easy, I've tried it before, and it's not easy! @dabombdgdzjr

RYANTADIPARTHI (3231)

You could put if statements like:

var = 1
if var == 1:
  print("1")
else:
  print("not 1")

But in your programming language, you could put something like

if var (==) 1 {
  write("1")
} else {
  write("not 1")
} 
RYANTADIPARTHI (3231)

@dabombdgdzjr, well, you can put actual if statements checking the condition of if the user's if statements equal your if statements, then do this.

angrydoge (305)

um could you explain a bit more in depth @RYANTADIPARTHI

RYANTADIPARTHI (3231)

@dabombdgdzjr ok, so now you have your OWN if statements, and the real python if statements.

user= input("write if statement")
if user == "if var (==):
 # this is your if statement, which python is checking. 

so python if statements check if the user is correctly giving YOUR OWN if statement

angrydoge (305)

But im tryna make the if statements in the file and not the console @RYANTADIPARTHI

RYANTADIPARTHI (3231)

@dabombdgdzjr , then you can do the same thing, but by opening the file with the open()

angrydoge (305)

... i dont think that would help bc it would be if the file line equals if var (==): and you couldnt add any args to it @RYANTADIPARTHI

RYANTADIPARTHI (3231)

@dabombdgdzjr no, i'm just giving an example of what type of if statements you could put. You can put your own.

angrydoge (305)

yeah, Idk what to do with this srry @RYANTADIPARTHI

RYANTADIPARTHI (3231)

@dabombdgdzjr oh, ok, but if i helped you get this a bit, please mark my answer. Your choice though. Thanks!