Python Custom Programming Language [closed]

I’m creating a custom programming language in Python “FlexiCode” using a custom extension for it, “.flexi” I need help for the main code because it’s one file only. you can find the syntax of the programming language in the main.flexi, but ill give you a better explanation:

setline("Hello, World!"); # Is like a print

Here’s the variable types: <integer>, <string>, <bool>, <float>, <list>

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><integer> x = 10;
<string> y = "Hello";
<bool> z = True
<float> a = 51.5
<list> i = ['hi','hi2']
</code>
<code><integer> x = 10; <string> y = "Hello"; <bool> z = True <float> a = 51.5 <list> i = ['hi','hi2'] </code>
<integer> x = 10;
<string> y = "Hello";
<bool> z = True
<float> a = 51.5
<list> i = ['hi','hi2']

Here’s the “Main.flexi”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>setline("This is a message that will be printed.");
<integer> x = 10;
<integer> y = 10;
<integer> z = x + 5;
setline("The value of z is: " + str(z));
</code>
<code>setline("This is a message that will be printed."); <integer> x = 10; <integer> y = 10; <integer> z = x + 5; setline("The value of z is: " + str(z)); </code>
setline("This is a message that will be printed.");

<integer> x = 10;
<integer> y = 10;
<integer> z = x + 5;

setline("The value of z is: " + str(z));

Here’s the “main.py”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import tkinter as tk
from tkinter import filedialog as fd
import pathlib
import re
class FlexiCodeInterpreter:
def __init__(self):
self.code = ""
self.variables = {}
def OpenFile(self):
root = tk.Tk()
root.withdraw()
filename = fd.askopenfilename(title='Open a .flexi file', initialdir='/')
if filename:
with open(filename, 'r') as f:
self.code = f.read()
suffix = pathlib.Path(filename).suffix
if suffix != ".flexi":
print("Error: File must have .flexi extension.")
quit()
def Interpreter(self):
try:
lines = self.code.splitlines()
i = 0
while i < len(lines):
line = lines[i].strip()
if line.startswith('setline("'):
# Find the closing quote and append subsequent lines until the closing quote is found
end_quote_index = line.find('")')
while end_quote_index == -1:
i += 1
line += lines[i].strip()
end_quote_index = line.find('")') + 2 # include the closing quote and semicolon
message = line[8:end_quote_index-2] # extract message between quotes
print(f"Set Line Message: {message}")
elif any(type in line for type in ["<integer>", "<float>", "<bool>", "<string>", "<list>"]):
parts = line.split("=")
if len(parts) == 2:
declaration = parts[0].strip()
value_expr = parts[1].strip()
match = re.match(r'<(w+)> (w+)', declaration)
if match:
var_type = match.group(1)
var_name = match.group(2)
# Evaluate expression safely
value = eval(value_expr, self.variables.copy())
if var_type == "integer":
self.variables[var_name] = int(value)
elif var_type == "float":
self.variables[var_name] = float(value)
elif var_type == "bool":
self.variables[var_name] = bool(value)
elif var_type == "string":
self.variables[var_name] = str(value)
elif var_type == "list":
if isinstance(value, list):
self.variables[var_name] = value
else:
print(f"Error: Invalid list value for {var_name}")
else:
print(f"Error: Unknown variable type {var_type}")
else:
print(f"Invalid syntax: {line}")
elif line: # Handle empty lines gracefully
print(f"Unknown statement: {line}")
i += 1
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
flexi = FlexiCodeInterpreter()
flexi.OpenFile()
print("nAre you sure you want to compile this code?")
choice = input("YES/NO: ").strip().upper()
if choice == "YES":
flexi.Interpreter()
elif choice == "NO":
quit()
</code>
<code>import tkinter as tk from tkinter import filedialog as fd import pathlib import re class FlexiCodeInterpreter: def __init__(self): self.code = "" self.variables = {} def OpenFile(self): root = tk.Tk() root.withdraw() filename = fd.askopenfilename(title='Open a .flexi file', initialdir='/') if filename: with open(filename, 'r') as f: self.code = f.read() suffix = pathlib.Path(filename).suffix if suffix != ".flexi": print("Error: File must have .flexi extension.") quit() def Interpreter(self): try: lines = self.code.splitlines() i = 0 while i < len(lines): line = lines[i].strip() if line.startswith('setline("'): # Find the closing quote and append subsequent lines until the closing quote is found end_quote_index = line.find('")') while end_quote_index == -1: i += 1 line += lines[i].strip() end_quote_index = line.find('")') + 2 # include the closing quote and semicolon message = line[8:end_quote_index-2] # extract message between quotes print(f"Set Line Message: {message}") elif any(type in line for type in ["<integer>", "<float>", "<bool>", "<string>", "<list>"]): parts = line.split("=") if len(parts) == 2: declaration = parts[0].strip() value_expr = parts[1].strip() match = re.match(r'<(w+)> (w+)', declaration) if match: var_type = match.group(1) var_name = match.group(2) # Evaluate expression safely value = eval(value_expr, self.variables.copy()) if var_type == "integer": self.variables[var_name] = int(value) elif var_type == "float": self.variables[var_name] = float(value) elif var_type == "bool": self.variables[var_name] = bool(value) elif var_type == "string": self.variables[var_name] = str(value) elif var_type == "list": if isinstance(value, list): self.variables[var_name] = value else: print(f"Error: Invalid list value for {var_name}") else: print(f"Error: Unknown variable type {var_type}") else: print(f"Invalid syntax: {line}") elif line: # Handle empty lines gracefully print(f"Unknown statement: {line}") i += 1 except Exception as e: print(f"Error: {e}") if __name__ == "__main__": flexi = FlexiCodeInterpreter() flexi.OpenFile() print("nAre you sure you want to compile this code?") choice = input("YES/NO: ").strip().upper() if choice == "YES": flexi.Interpreter() elif choice == "NO": quit() </code>
import tkinter as tk
from tkinter import filedialog as fd
import pathlib
import re

class FlexiCodeInterpreter:
    def __init__(self):
        self.code = ""
        self.variables = {}

    def OpenFile(self):
        root = tk.Tk()
        root.withdraw()

        filename = fd.askopenfilename(title='Open a .flexi file', initialdir='/')
        if filename:
            with open(filename, 'r') as f:
                self.code = f.read()
                suffix = pathlib.Path(filename).suffix
                if suffix != ".flexi":
                    print("Error: File must have .flexi extension.")
                    quit()

    def Interpreter(self):
        try:
            lines = self.code.splitlines()
            i = 0
            while i < len(lines):
                line = lines[i].strip()
                if line.startswith('setline("'):
                    # Find the closing quote and append subsequent lines until the closing quote is found
                    end_quote_index = line.find('")')
                    while end_quote_index == -1:
                        i += 1
                        line += lines[i].strip()
                        end_quote_index = line.find('")') + 2  # include the closing quote and semicolon
                    
                    message = line[8:end_quote_index-2]  # extract message between quotes
                    print(f"Set Line Message: {message}")

                elif any(type in line for type in ["<integer>", "<float>", "<bool>", "<string>", "<list>"]):
                    parts = line.split("=")
                    if len(parts) == 2:
                        declaration = parts[0].strip()
                        value_expr = parts[1].strip()

                        match = re.match(r'<(w+)> (w+)', declaration)
                        if match:
                            var_type = match.group(1)
                            var_name = match.group(2)

                            # Evaluate expression safely
                            value = eval(value_expr, self.variables.copy())
                            
                            if var_type == "integer":
                                self.variables[var_name] = int(value)
                            elif var_type == "float":
                                self.variables[var_name] = float(value)
                            elif var_type == "bool":
                                self.variables[var_name] = bool(value)
                            elif var_type == "string":
                                self.variables[var_name] = str(value)
                            elif var_type == "list":
                                if isinstance(value, list):
                                    self.variables[var_name] = value
                                else:
                                    print(f"Error: Invalid list value for {var_name}")
                            else:
                                print(f"Error: Unknown variable type {var_type}")
                        else:
                            print(f"Invalid syntax: {line}")
                elif line:  # Handle empty lines gracefully
                    print(f"Unknown statement: {line}")

                i += 1

        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    flexi = FlexiCodeInterpreter()
    flexi.OpenFile()
    print("nAre you sure you want to compile this code?")
    choice = input("YES/NO: ").strip().upper()
    if choice == "YES":
        flexi.Interpreter()
    elif choice == "NO":
        quit()

I’m encountering problems for interpreting the lines of code in the .flexi file.

New contributor

Kesballo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

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