dabeaz / sly

Sly Lex Yacc
Other
816 stars 107 forks source link

how to get contents of a variable? #75

Closed MEMESCOEP closed 2 years ago

MEMESCOEP commented 3 years ago

hello, I was wondering if there was a way to get a variable's contents from python. I've managed to get the contents after assignment, like this:

def walkTree(self, node):

        if isinstance(node, int):
            return node
        if isinstance(node, str):
            print(node)
            return node

but I can't get the contents otherwise.

dabeaz commented 3 years ago

I don't understand.

MEMESCOEP commented 3 years ago

Hello, some clarification: I am creating a programming language called AUSL.

Here's a snippet:

##AUSL Example Program, (C) xxxMEMESCOEPxxx 2021
##Make comments with "##"!
print(AUSL Example Program!)

##Simple Math problem
##These problems will be calculated and the results will be printed to console

6+9

l1 = "A"
l2 = "B"
l3 = "C"
l4 = "D"
l5 = "E"

##You can print a variable's output by using it's name
string = l1+l2+l3+l4+l5
string

##Simple Download Example
print(Downloading file from url...)
download(https://www.google.com, DownloadedFile.html)
print(done downloading!)

##Simple HTTP web server example! (REQUIRES ROOT || Uncomment to run)
##print(Simple HTTP server!)
##httpserver.startServer()

print(done)
end

I would like to know how to get the contents of the "string" variable from python.

MEMESCOEP commented 3 years ago

Ok, never mind. I figured out that I can get the variable data by doing:

if node[0] == 'var':
            try:

                print(str(self.env[node[1]]).replace('"', ''))
                return self.env[node[1]]
            except LookupError:
                print("Undefined variable '"+node[1]+"'")
                return 0

But, how do I get user input and assign it to a variable? I know that in standard python, you can do

usrinput = input(">>")

but how would I assign it to a sly variable?