#๐Ÿ”’ My own interpreter

119 messages ยท Page 1 of 1 (latest)

low knotBOT
#

@noble crest

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.

grim lily
#

!paste

low knotBOT
#
Pasting large amounts of code

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.

noble crest
#

here's the code:

serene marlin
#

whats your question?

noble crest
#

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

serene marlin
#

do you mean in the input? you want to parse the operations?

noble crest
#

i guess so

#

but its not with a parser

serene marlin
#

it looks like you're already sort of doing that

#

did you write this code?

noble crest
#

yes

#

my uncle helped me improve it

serene marlin
#

what part isn't working the way you'd expect it to?

noble crest
#

currently if i type an if statement it just wont work

#

cus there's nothing making it work

serene marlin
#

what do you mean by "wont work"? does it crash? what's the error? does it do nothing?

noble crest
#

no it just wont do anything

serene marlin
#

where are you typing the if statement? is this the code your interpreter is executing, or in python?

noble crest
#

what?

#

its an interpreter i wrote

serene marlin
#

sure

#

but what kind of if statement is this

noble crest
#

win
speed 0

x = 10

frd x
right 10
up
frd x
right 10
down
repeat 6 18

#

that's the code

serene marlin
#

what do you mean by if statement?

noble crest
#

that the interpreter is reading

#

[

serene marlin
#

ok, that answers my question

noble crest
#

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

serene marlin
#

what are 1 and 3 in this context?

noble crest
#

suppuse its a variable and a number

#

*supose

serene marlin
#

i'm still not really sure what you're asking

#

so you have code that's like [ say hi ]

noble crest
#

yes

serene marlin
#

and your interpreter parses this code correctly

#

but you're having trouble executing it?

noble crest
#

yes

serene marlin
#

oh you're calling [ an if statement in your code

#

i see

noble crest
#

having trouble with the logic

#

no

#

im calling [

#

a start point

#

and ] an end point

serene marlin
#

so what's wrong with [? what behavior do you expect to occur, and what happens instead?

noble crest
#

how do i execite all the lines inside the [] when somthing is true

#

something the user dicideds

#

*decides

serene marlin
#

how familiar with interpreters are you already?

noble crest
#

not realy

#

i know that's not how interpreters work

serene marlin
#

the way that i'd implement this is by using jumps

noble crest
#

what's a jump?

#

like in python

serene marlin
#

it's more of an assembly concept

#

so you have a list of instructions

noble crest
#

so how do i implement a jump in python

serene marlin
#

["x = 10", "frd x", "right 10", "up"]

#

are you familiar with lists?

#

and indexing them

noble crest
#

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

serene marlin
#

you can imagine your interpreter as operating over this list of instructions

#

with an index that points to the instruction currently being executed

noble crest
#

i allready implemented that

#

with a repeat

serene marlin
#

after you execute an instruction, you increment your index by 1, which moves you to the next instruction

noble crest
#

there are loops

serene marlin
#

a jump (also called jmp or goto) is just an instruction that lets you jump ahead or behind by some number

noble crest
#

okay

serene marlin
#

so instead of index += 1, maybe it's index += 5 to skip over 5 instructions

#

so in your if statement

noble crest
#

okay my problem is detecting if the statement is true

serene marlin
#

what problem are you having with that

noble crest
#

with the logic

#

of how od i come up with an algorithm to do it

serene marlin
#

where does the statement come from

noble crest
#

a txt file

serene marlin
#

what does a statement look like

noble crest
#

[

#

say hi

#

]

#

if x < 3

serene marlin
#

ok the x < 3 is the important part

#

where does x come from

noble crest
#

yes

#

i have variables implemented

serene marlin
#

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"))

low knotBOT
#

@serene marlin :white_check_mark: Your 3.12 eval job has completed with return code 0.

False
noble crest
#

what?!?

serene marlin
#

otherwise, yes, you need to write a bit of code to do expression evaluation. similar to what you have in _math

noble crest
#

that's like cheating for me, i never knew this existed

serene marlin
#

you shouldn't use it outside this context

noble crest
#

thank you so much, let me try it

serene marlin
#

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

noble crest
#

does it work with and/or/not

#

?

serene marlin
#

!e ```py
print(eval("True and False"))
print(eval("True and True"))

low knotBOT
#

@serene marlin :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | False
002 | True
noble crest
#

like omg thank you so much

#

๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™ ๐Ÿ™

low knotBOT
#
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.