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:
<integer> x = 10;
<string> y = "Hello";
<bool> z = True
<float> a = 51.5
<list> i = ['hi','hi2']
Here’s the “Main.flexi”
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”
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.
Kesballo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1