#๐ My own interpreter
119 messages ยท Page 1 of 1 (latest)
@noble crest
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.
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
whats your question?
hold up
let me think abuot it for a sec
*about
i think the question is how do i detect an operation and then execute it
do you mean in the input? you want to parse the operations?
what part isn't working the way you'd expect it to?
currently if i type an if statement it just wont work
cus there's nothing making it work
what do you mean by "wont work"? does it crash? what's the error? does it do nothing?
no it just wont do anything
where are you typing the if statement? is this the code your interpreter is executing, or in python?
win
speed 0
x = 10
frd x
right 10
up
frd x
right 10
down
repeat 6 18
that's the code
what do you mean by if statement?
ok, that answers my question
say hi
]
if 1 < 3
that's the way
i want it to be wrote down
and then if 1 < 3 then it'll execute all of the code in the sqare brackets
what are 1 and 3 in this context?
i'm still not really sure what you're asking
so you have code that's like [ say hi ]
yes
and your interpreter parses this code correctly
but you're having trouble executing it?
yes
having trouble with the logic
no
im calling [
a start point
and ] an end point
so what's wrong with [? what behavior do you expect to occur, and what happens instead?
how do i execite all the lines inside the [] when somthing is true
something the user dicideds
*decides
how familiar with interpreters are you already?
the way that i'd implement this is by using jumps
so how do i implement a jump in python
["x = 10", "frd x", "right 10", "up"]
are you familiar with lists?
and indexing them
of curse
*of curse
i do have a list of all the lines ever executed
but how do i check if a statement is true without like 100 lines of code
you can imagine your interpreter as operating over this list of instructions
with an index that points to the instruction currently being executed
after you execute an instruction, you increment your index by 1, which moves you to the next instruction
there are loops
a jump (also called jmp or goto) is just an instruction that lets you jump ahead or behind by some number
okay
so instead of index += 1, maybe it's index += 5 to skip over 5 instructions
so in your if statement
okay my problem is detecting if the statement is true
what problem are you having with that
where does the statement come from
a txt file
what does a statement look like
if you really want to do it simply, you can use a function called eval
!e ```py
my_vars = { "x": 5 }
x = my_vars["x"]
print(eval(f"{x} < 3"))
@serene marlin :white_check_mark: Your 3.12 eval job has completed with return code 0.
False
what?!?
otherwise, yes, you need to write a bit of code to do expression evaluation. similar to what you have in _math
that's like cheating for me, i never knew this existed
you shouldn't use it outside this context
thank you so much, let me try it
if you misuse it, it's possible to introduce remote code execution vulnerabilities
but in this case, you're the only one using your code so it's fine
but if you had a web server it would be bad
!e ```py
print(eval("True and False"))
print(eval("True and True"))
@serene marlin :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | False
002 | True
like omg thank you so much
๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐
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.