0

Please Note That This Happened In 2019 So I picked up a few things from then

So please don't judge me.

So I have decided to try and make a very basic language in python.

And it is pretty basic right now but I want to improve it.

And one thing all languages need: variables And right now I have this:

 # Some other if statements up here
# neon is the name of the variable
# Something programmed real quickly to show example
elif neon[0:6] == ("addvar"):
    var = neon[7:]
elif neon.startswith("add"+ var):
    invar = neon.split("add"+ var , 1)[1]
elif neon.startswith("out"+ var):
    print(invar) 

YES it is very messy and horrid but I had to start with something and it is very buggy. I had to start working on math so I skipped out on the variables. I tried a few tests before e. g. Appending data to a list and whatnot.

My Problem:

It only lets me have one variable

But now I wonder if there is a way that finally goes through the lexer and parser

Please tell me if there is

7
  • 3
    use a dictionary whose keys are the variable names. Commented Jun 5, 2020 at 0:04
  • It would help if you showed examples of the language, and what the statements are supposed to do. Commented Jun 5, 2020 at 0:08
  • 1
    You might consider using regular expressions to parse the statements rather than slicing. Commented Jun 5, 2020 at 0:09
  • okay i am new i dont now what do really do sorry. 😂 Commented Jun 5, 2020 at 0:12
  • yes I will try to parse instead of slicing. This is just an example I coded quickly Commented Jun 5, 2020 at 0:13

1 Answer 1

3

Use a dictionary to hold all your variables.

variables = {}
# some other code here
elif neon.startswith("addvar "): # addvar variablename
    var = neon.split()[1]
    variables[var] = None # create empty variable
elif neon.startswith("add "): # add variablename value
    _, var, value = neon.split()
    variables[var] = value
elif neon.startswith("out "): # out variablename
    var = neon.split()[1]
    print(variables[var])
Sign up to request clarification or add additional context in comments.

8 Comments

Also just tested it out. How would you use the 'add'
also i am getting a ValueError for: 'To many values to unpack (expected 3)'
I fixed a lot of stuff but it still didnt work sorry :(
I've added comments showing the syntax I assumed. I did ask you to show examples of your language syntax. Since you haven't done that, I just made it up myself.
yes I know that. It is another day though and I have been working on it for a while now. I got it down to no errors but now im fixing where it prints the actual variable. it prints none instead of the variables so yeah debugging is fun. I actually saw at first your syntax but didn't understand it. I worked on it for a few hours and then I understood it. I am sorry if i didnt put any examples.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.