#๐Ÿ”’ Help making a programming language!

161 messages ยท Page 1 of 1 (latest)

lime hawkBOT
#

@pearl saffron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

pearl saffron
#

hey everybody
ive been trying to make a langguage in python recently
but im not very good at it
and i cant figure out how oto make if conditions and code blocks

#

and functions and inputs and stuff like taht

#

that link is the pastebin for the code so far

feral sky
pearl saffron
#

c++ i mean

feral sky
#

So like this? ```
if (condition) {
code
}

pearl saffron
feral sky
pearl saffron
#

sry brain fart

feral sky
pearl saffron
#

but

#

it didnt ork out well

feral sky
#

Can you send what you tried?

pearl saffron
#

i dont have it sadly

#

maybe

#

no sry wait i cnat find it

feral sky
#

That's fine, we can make it from the ground up

pearl saffron
#

alr!

feral sky
#

Actually, you would need to add curly braces to the tokenizer first

pearl saffron
#

hm

#

so should i add it at the bottom

feral sky
#

Anywhere above MISMATCH is fine

pearl saffron
#

alr

feral sky
#

but I would put it before COMMA with the other paired symbols

pearl saffron
#

ima update the paste bin

#

new link

feral sky
#

looks alright

#

(to be pedantic, LEFT_BRACE/RIGHT_BRACE would be more consistent but it doesn't really matter anyway)

pearl saffron
#

lol ok

feral sky
#

Alright, so the if statement is a statement

#

and you know a statement is one when it starts with if

pearl saffron
#

so do i add an if token?

feral sky
#

Since you're using IDENTIFIER for print anyway you can do the same for if

pearl saffron
#

ddo i change the token[] array to token[2] for if like i did token[1] for print

feral sky
#

why not try run it and find out?

pearl saffron
#

hey is it ok if we can contiue this in dms?

feral sky
#

any particular reason why?

#

I usually don't do DMs

pearl saffron
#

i was wondering if you wanted to vc, it would make the proccess a whole lot easier

feral sky
#

ah, unfortunately I'm already in a different vc

pearl saffron
#

hey

#

is this good?

#

def parse_statement(self):
token = self.current_token()
if token[0] == 'IDENTIFIER':
if self.peek_token() and self.peek_token()[0] == 'ASSIGN':
return self.parse_assignment()
elif token[1] == 'print':
self.eat('IDENTIFIER')
return ('PRINT', self.parse_expression())
elif token[2] == 'if':
self.eat('IDENTIFIER')
return ('IF', self.parse_expression())
raise SyntaxError(f"Unexpected token: {token}")

lime hawkBOT
#

Hey @pearl saffron!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
pearl saffron
#
        token = self.current_token()
        if token[0] == 'IDENTIFIER':
            if self.peek_token() and self.peek_token()[0] == 'ASSIGN':
                return self.parse_assignment()
            elif token[1] == 'print':
                self.eat('IDENTIFIER')
                return ('PRINT', self.parse_expression())
        elif token[2] == 'if':
                self.eat('IDENTIFIER')
                return ('IF', self.parse_expression())
        raise SyntaxError(f"Unexpected token: {token}")```
feral sky
#

Not quite, but it's close

pearl saffron
#

what do i do from here?

compact rain
pearl saffron
#

im not that great at python, sry lol

feral sky
#

so ||the elif should be indented inside if token[0] == 'IDENTIFIER':|| and ||the condition should use token[1]||

compact rain
#

unless the lexer parses if as a unique token

feral sky
#

(which it doesn't, for now)

compact rain
#

then i guess that, yeah

feral sky
compact rain
#

a

#

updated one

feral sky
#

as in the message?

pearl saffron
#

changed it to token[1]

#

the elif is already indented

compact rain
feral sky
#

can you break it down into specific tokens/expressions/statements?

pearl saffron
#

first we have the keyword, thats "if"

#

he have whitespce but that gets ignored

#

then we have brackets with arguments inside of it

#

an opening bracket

#

and then either integers or variables for the first argument

#

and strings

#

OH

compact rain
feral sky
pearl saffron
#

we need == <= etc

pearl saffron
#

we need to make tokens for == <= etc

feral sky
#

Yes, we might want to add conditional operators first

pearl saffron
#

ima do it

#

ima add it after the divide opperator

pearl saffron
#

for eq is this correct

#

('EQ', r'=='),

feral sky
#

try it and we'll see ;)

pearl saffron
#

alr

#

ima add the rest

pearl saffron
feral sky
#

LGTM, does it work?

pearl saffron
#

but i think it should

feral sky
pearl saffron
# feral sky perhaps test it with just tokenization

code = """
x = [1, 2, 3]
y = ["hello", "world"]
z = x + [4, 5]
a = y + ["!"]
print z
print a
augh = 3
alala = "huhuh"
print x[2]
print y[0]
print ((x[1] + x[2]) * 3) * augh
print "hahah " + alala

""" : ERROR!
Traceback (most recent call last):
File "<main.py>", line 208, in <module>
File "<main.py>", line 183, in run_minilang
File "<main.py>", line 69, in parse
File "<main.py>", line 80, in parse_statement
SyntaxError: Unexpected token: ('ASSIGN', '=')

lime hawkBOT
#

Hey @pearl saffron!

It looks like you are trying to paste code into this channel.

You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not """.

Here is an example of how it should look:
```
Hello, world!
```

This will result in the following:

Hello, world!```
You can **edit your original message** to correct your code block.
pearl saffron
#

i just typed == in the last line of the custom lang

#

and i got this error

feral sky
#

mhm, is that what you expected?

pearl saffron
#

no

feral sky
#

why do you think that might be?

compact rain
#
  File "<main.py>", line 208, in <module>
  File "<main.py>", line 183, in run_minilang
  File "<main.py>", line 69, in parse
  File "<main.py>", line 80, in parse_statement
                    ^^^^^^^     ^^^^^^^^^^^^^^^

and there you'll be able to see why it gave an error

pearl saffron
#

maybe cause it can only take in 1 char

#

or maybe i havent made any references anywhere in the code

#

ima try adding a return statemment if detectedin parse_factor()

feral sky
pearl saffron
feral sky
pearl saffron
# feral sky I would test with just the tokenizer so the parser doesn't get in the way

i did this but got same issue: def parse_statement(self):
token = self.current_token()
if token[0] == 'IDENTIFIER':
if self.peek_token() and self.peek_token()[0] == 'ASSIGN':
return self.parse_assignment()
elif token[1] == 'print':
self.eat('IDENTIFIER')
return ('PRINT', self.parse_expression())
if token[0] == 'EQ':
self.eat('EQ')
raise SyntaxError(f"Unexpected token: {token}")

#

again im sorry for takign up alot of ur time

#

im not rlly good at python thtat much

#

i know just enough

feral sky
#

no worries, we were all beginners at one point

feral sky
pearl saffron
#

code = """

"""

#

and this is the output

#

[('NEWLINE', '\n'), ('ASSIGN', '='), ('ASSIGN', '='), ('NEWLINE', '\n')]
ERROR!
Traceback (most recent call last):
File "<main.py>", line 197, in <module>
File "<main.py>", line 183, in run_minilang
File "<main.py>", line 69, in parse
File "<main.py>", line 80, in parse_statement
SyntaxError: Unexpected token: ('ASSIGN', '=')

feral sky
#

yup, so the == is being parsed as 2 =s

pearl saffron
#

yes...

#

maybe we should try something like this

#

like if it detects another = after one =

#

it will do ==

#

idk

#

actually

feral sky
#

I would consider the ordering of the tokens

pearl saffron
pearl saffron
#

ima try putting it above

#

u were right

#

[('NEWLINE', '\n'), ('EQ', '=='), ('NEWLINE', '\n')]
ERROR!
Traceback (most recent call last):
File "<main.py>", line 198, in <module>
File "<main.py>", line 184, in run_minilang
File "<main.py>", line 70, in parse
File "<main.py>", line 81, in parse_statement
SyntaxError: Unexpected token: ('EQ', '==')

feral sky
#

Essentially, higher rules get checked first so any = would always be consumed by ASSIGN before it reaches EQ

pearl saffron
#

im also put the lesst and moret lower

#

ok

#

so what now?

feral sky
#

parsing comparisons!

pearl saffron
#

sry in a rush

compact rain
pearl saffron
#

talk to u after 1 hr

#

see ya

#

sry botutbis again

#

basketball class

#

bye!

feral sky
#

no problem, you can ping me later when you come back

#

this thread will probably be closed by then so feel free to open another one

lime hawkBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.