#๐ Help making a programming language!
161 messages ยท Page 1 of 1 (latest)
@pearl saffron
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.
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
Let's start with if statements. Do you already have a syntax in mind?
exacly like c
c++ i mean
So like this? ```
if (condition) {
code
}
Yup!
guess what, C and C++ have the same if syntax :p
Have you tried writing code which can parse it?
kindof
but
it didnt ork out well
Can you send what you tried?
That's fine, we can make it from the ground up
alr!
Actually, you would need to add curly braces to the tokenizer first
Anywhere above MISMATCH is fine
alr
but I would put it before COMMA with the other paired symbols
just did it
ima update the paste bin
new link
looks alright
(to be pedantic, LEFT_BRACE/RIGHT_BRACE would be more consistent but it doesn't really matter anyway)
lol ok
Alright, so the if statement is a statement
and you know a statement is one when it starts with if
ok
so do i add an if token?
Since you're using IDENTIFIER for print anyway you can do the same for if
ddo i change the token[] array to token[2] for if like i did token[1] for print
why not try run it and find out?
k
hey is it ok if we can contiue this in dms?
i was wondering if you wanted to vc, it would make the proccess a whole lot easier
ah, unfortunately I'm already in a different vc
ah ok
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}")
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.
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}")```
Not quite, but it's close
what do i do from here?
also things like that could use an enumeration just so you don't have to remember how you spell all the tokens
im not that great at python, sry lol
Remember, we're trying to look for if, which would be ('IDENTIFIER', 'if') when tokenized
so ||the elif should be indented inside if token[0] == 'IDENTIFIER':|| and ||the condition should use token[1]||
unless the lexer parses if as a unique token
(which it doesn't, for now)
then i guess that, yeah
^ here's OP's code for reference
ah, it doesn't load for me
a
updated one
as in the message?
done
changed it to token[1]
the elif is already indented
i was initially looking at their first link ๐
alright; now let's break down an if statement to see how we can parse it
can you break it down into specific tokens/expressions/statements?
well
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
to be more exact - there should be an expression inside
arguments?
we need == <= etc
i ment the condition lol
we need to make tokens for == <= etc
Yes, we might want to add conditional operators first
okok
ima do it
ima add it after the divide opperator
yo
for eq is this correct
('EQ', r'=='),
try it and we'll see ;)
('LESST', r'<'),
('MORET', r'>'),
('LQ', r'<='),
('GQ', r'>='),
('NOTQ', r'!='),
('AND', r'&&'),```
LGTM, does it work?
idk we havent done anything with em yet
but i think it should
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', '=')
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.
mhm, is that what you expected?
no
why do you think that might be?
take a look at the traceback
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
hmm idk
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()
but it can take identifiers which are multiple characters
yeah ye just noticed
I would test with just the tokenizer so the parser doesn't get in the way
kk
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
no worries, we were all beginners at one point
Start from the simplest case, with just tokenization: ```py
code = "=="
print(tokenize(code))
this is the custom lang code
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', '=')
yup, so the == is being parsed as 2 =s
yes...
maybe we should try something like this
like if it detects another = after one =
it will do ==
idk
actually
I would consider the ordering of the tokens
actually, could it be that we didnt add any parser code for it?
hmm
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', '==')
Essentially, higher rules get checked first so any = would always be consumed by ASSIGN before it reaches EQ
alr ima put the rest above it
im also put the lesst and moret lower
ok
so what now?
parsing comparisons!
making something like print 1 == 1 work
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
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.