import re class BASEInterpreter: def __init__(self): self.variables = {} def run(self, filename): try: with open(filename, 'r') as f: lines = f.readlines() if self.check_structure(lines): self.execute(lines) else: print("Error: BASE program structure is incorrect.") except FileNotFoundError: print(f"Error: File '{filename}' not found.") def check_structure(self, lines): return lines[0].strip() == "START" and lines[-1].strip() == "END" def execute(self, lines): i = 1 while i < len(lines) - 1: line = lines[i].strip() if line.startswith("COMMENT"): i += 1 continue elif line.startswith("SET"): self.handle_set(line) elif line.startswith("SAY"): self.handle_say(line) elif line.startswith("WAIT"): self.handle_wait(line) elif line.startswith("IF"): condition = line[3:].strip() if self.evaluate_condition(condition): i += 1 while i < len(lines) - 1 and not lines[i].strip().startswith("ELSE") and not lines[i].strip().startswith("ENDIF"): self.execute_line(lines[i]) i += 1 if i < len(lines) - 1 and lines[i].strip().startswith("ELSE"): i += 1 while i < len(lines) - 1 and not lines[i].strip().startswith("ENDIF"): i += 1 else: while i < len(lines) - 1 and not lines[i].strip().startswith("ELSE") and not lines[i].strip().startswith("ENDIF"): i += 1 if i < len(lines) - 1 and lines[i].strip().startswith("ELSE"): i += 1 while i < len(lines) - 1 and not lines[i].strip().startswith("ENDIF"): self.execute_line(lines[i]) i += 1 i += 1 def evaluate_condition(self, condition): parts = re.split(r'(==|!=|>=|<=|>|<)', condition) var_name = parts[0].strip() operator = parts[1].strip() value_str = parts[2].strip() if var_name.startswith("var(") and var_name.endswith(")"): var_name = var_name[4:-1] var_value = self.variables.get(var_name) if var_value is None: return False try: value = float(value_str[:-1]) except ValueError: return False if operator == "==": return var_value == value elif operator == "!=": return var_value != value elif operator == ">=": return var_value >= value elif operator == "<=": return var_value <= value elif operator == ">": return var_value > value elif operator == "<": return var_value < value else: return False def execute_line(self, line): line = line.strip() if line.startswith("SET"): self.handle_set(line) elif line.startswith("SAY"): self.handle_say(line) elif line.startswith("WAIT"): self.handle_wait(line) elif line.startswith("ADD"): self.handle_math_operation("ADD", line) elif line.startswith("SUBTRACT"): self.handle_math_operation("SUBTRACT", line) elif line.startswith("MULTIPLY"): self.handle_math_operation("MULTIPLY", line) elif line.startswith("DIVIDE"): self.handle_math_operation("DIVIDE", line) def handle_set(self, line): _, rest = line.split(' ', 1) var_name, value = rest.split(' = ') var_name = var_name.strip() value = value.strip() self.variables[var_name] = value def handle_say(self, line): _, rest = line.split(' ', 1) if rest.startswith("text(") and rest.endswith(")"): text = rest[5:-1] parsed_text = self.parse_text(text) print(parsed_text) elif rest.startswith("num(") and rest.endswith(")"): num = rest[4:-1] print(num) else: parsed_text = self.parse_text(rest) print(parsed_text) def parse_text(self, text): if text.startswith("text(") and text.endswith(")"): return text[5:-1] if text.startswith("num(") and text.endswith(")"): return text[4:-1] def replace_var(match): var_name = match.group(1) return self.variables.get(var_name, f"Error: Variable '{var_name}' not found.") return re.sub(r'var\((\w+)\)', replace_var, text) def handle_wait(self, line): _, time_in_seconds = line.split(' ', 1) time_in_seconds = float(time_in_seconds.strip()) import time time.sleep(time_in_seconds) def handle_math_operation(self, operation, line): _, values = line.split(' ', 1) values = values.strip().split() operation_result = None if operation == "ADD": operation_result = sum(self.get_value(val) for val in values) elif operation == "SUBTRACT": operation_result = self.get_value(values[0]) for val in values[1:]: operation_result -= self.get_value(val) elif operation == "MULTIPLY": operation_result = 1 for val in values: operation_result *= self.get_value(val) elif operation == "DIVIDE": operation_result = self.get_value(values[0]) for val in values[1:]: operation_result /= self.get_value(val) first_var = values[0] self.variables[first_var] = str(operation_result) print(operation_result) def get_value(self, val): if val.isdigit() or (val[0] == '-' and val[1:].isdigit()): return float(val) elif val in self.variables: return float(self.variables[val]) else: return 0.0 if __name__ == "__main__": interpreter = BASEInterpreter() filename = input("Enter BASE file name to execute: ") interpreter.run(filename) input("Press Enter to exit...")