Reputation: 31
I have made my own programming language in python, and as of right now, its using a system that, whenever detects a {
, it makes a list in front of it, somewhat like this:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
I have written a program to make use of this style of list:
def process_scopes(tokenized_output: list, condition: bool, position: int):
index = position
if condition == False:
for token in tokenized_output:
index += 1
if tokenized_output[index] == "}":
print(token)
break
else:
for token in tokenized_output:
index += 1
if isinstance(token, list):
index = process_scopes(token, condition, index)
else:
print(token)
position = index
return position
It takes a condition
argument, which is the output of a program that evaluates the conditions given to the if
statement (that is irrelevant). But the point is, it takes 3 arguments, all of which are crucial for the rest of the program to work. here is a description of each argument and what they do:
tokenized_output
, this is the list containing the info on the scopes, it should look somewhat like this:tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
condition
is a boolean value that evaluates on whether an if
statements conditions are true or not, if true, it executes the contents of the nested list inside the {}
, in this case, it should execute the:['PRINT', '(', '"', 'hello', '"', ')']
position
, this is a integer value that sppecifies with index of the tokenized_output
list it is on, and is also the reason I need a new proposal as to how to store these scopes, as when calling this function, the variable that is passed into the position
argument is the position of another list, which is the same as this scope list, but just that it dosent have any nested lists, and would look something like this:['IF', 'a', '>', 'b', '{', 'PRINT', '(', '"', 'hello', '"', ')', '}']
Meaning, unless I pass in the position of a index that is on the primary list (the first list, not nested in any other lists), and unless the position of that index is before the occurence of any nested lists (In this case, 'IF', 'a', '>', 'b', '{'
would all be before the occurence of the first nested list), the index would be all messed up.
Therefore I need help regarding how to "renew" this way that I use to define scopes, and instead request a way that works even when the index taken from the "non-nested list" is taken over to the other list (the list that the scopes are defined in), it would still give the same result.
Heres another example of the condition
argument:
(^ is the position pointer)
if condition
is False
:
from here:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
^
to here:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
^
skipping ['PRINT', '(', '"', 'hello', '"', ')']
if condition
is True
:
from here:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
^
execute all these:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
^ ^ ^ ^ ^ ^
to here:
tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
^
Meaning you need to return the value of position
after finish.
So i tried the code i listed before:
def process_scopes(tokenized_output: list, condition: bool, position: int):
index = position
if condition == False:
for token in tokenized_output:
index += 1
if tokenized_output[index] == "}":
print(token)
break
else:
for token in tokenized_output:
index += 1
if isinstance(token, list):
index = process_scopes(token, condition, index)
else:
print(token)
position = index
return position
And the problem with it is, sometimes it returns the index way beyond the length of the list, and sometimes it just exits the whole program without executing code that comes after the function is called.
Therefore I wish either for a solution, or another way to handle curly brackets and scopes in my program.
Upvotes: 0
Views: 30