Silicon
Silicon

Reputation: 31

Optimizing nested list-based storage format for defining curly brackets scopes

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:

tokens = ['IF', 'a', '>', 'b', '{', ['PRINT', '(', '"', 'hello', '"', ')'], '}']
['PRINT', '(', '"', 'hello', '"', ')']
['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

Answers (0)

Related Questions