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:

  • 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật