101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
import re
|
|
|
|
|
|
class QLInterpreter:
|
|
def __init__(self):
|
|
self.output = []
|
|
self.functions = self._register_functions()
|
|
|
|
def _register_functions(self):
|
|
"""TUTAJ DODAJESZ NOWE FUNKCJE"""
|
|
return {
|
|
'print': self._func_print,
|
|
# DODAJ TU SWOJE FUNKCJE:
|
|
# 'powitanie': self._func_powitanie,
|
|
}
|
|
|
|
# ========== FUNKCJE WBUDOWANE ==========
|
|
def _func_print(self, *args):
|
|
"""Tylko dodaje do outputu - bez printowania"""
|
|
text = ' '.join(str(arg) for arg in args)
|
|
self.output.append(text)
|
|
|
|
def execute(self, code: str):
|
|
"""Wykonuje kod QL - NIE RUSZAJ TEJ FUNKCJI"""
|
|
self.output = []
|
|
|
|
for line in code.strip().split('\n'):
|
|
line = line.strip()
|
|
if not line or line.startswith(('//', '#')):
|
|
continue
|
|
|
|
# use lib; - ignoruj
|
|
if line.startswith('use '):
|
|
continue
|
|
|
|
# Wywołanie funkcji: nazwa(arg1, arg2, ...)
|
|
match = re.match(r'(\w+)\s*\((.*)\)', line.rstrip(';'))
|
|
if match:
|
|
func_name = match.group(1)
|
|
args_str = match.group(2)
|
|
|
|
if func_name in self.functions:
|
|
try:
|
|
args = self._parse_args(args_str)
|
|
self.functions[func_name](*args)
|
|
except Exception:
|
|
pass # Ignoruj błędy
|
|
|
|
return self.output
|
|
|
|
def _parse_args(self, args_str: str):
|
|
"""Parsuje argumenty - NIE RUSZAJ"""
|
|
args = []
|
|
current = ''
|
|
in_string = False
|
|
quote_char = None
|
|
|
|
for char in args_str:
|
|
if char in '"\'' and (quote_char is None or quote_char == char):
|
|
in_string = not in_string
|
|
quote_char = char if in_string else None
|
|
current += char
|
|
elif char == ',' and not in_string:
|
|
args.append(self._parse_arg(current.strip()))
|
|
current = ''
|
|
else:
|
|
current += char
|
|
|
|
if current.strip():
|
|
args.append(self._parse_arg(current.strip()))
|
|
|
|
return args
|
|
|
|
def _parse_arg(self, arg: str):
|
|
"""Parsuje pojedynczy argument - NIE RUSZAJ"""
|
|
arg = arg.strip()
|
|
|
|
# String "tekst" lub 'tekst'
|
|
if (arg.startswith('"') and arg.endswith('"')) or \
|
|
(arg.startswith("'") and arg.endswith("'")):
|
|
return arg[1:-1]
|
|
|
|
# Liczba
|
|
if arg.replace('.', '', 1).isdigit():
|
|
return float(arg) if '.' in arg else int(arg)
|
|
|
|
return arg
|
|
|
|
|
|
# ========== FUNKCJA URUCHAMIAJĄCA ==========
|
|
def run_ql_code(code: str):
|
|
"""Uruchamia kod QL - NIE RUSZAJ"""
|
|
interpreter = QLInterpreter()
|
|
output = interpreter.execute(code)
|
|
|
|
return {
|
|
'output': output, # TYLKO output z print()
|
|
'variables': {},
|
|
'functions': list(interpreter.functions.keys()),
|
|
'libraries': []
|
|
} |