import re

class AHOInterpreter:
    def __init__(self):
        self.vars = {}
        self.funcs = {}

    def parse_value(self, raw):
        """型サフィックスに基づき値を変換"""
        if raw.endswith("_n"):
            return int(re.findall(r"'(.*?)'", raw)[0])
        elif raw.endswith("_t"):
            return str(re.findall(r"'(.*?)'", raw)[0])
        elif raw.endswith("_L"):
            return eval(re.findall(r"'(.*?)'", raw)[0])
        elif raw.endswith("_R"):
            return re.findall(r"'(.*?)'", raw)[0]
        else:
            return raw

    def execute(self, code):
        lines = [l.strip() for l in code.split("\n") if l.strip() and not l.strip().startswith("--")]
        i = 0
        while i < len(lines):
            line = lines[i]

            # 代入
            if "=" in line and not line.startswith("if") and not line.endswith("}_R"):
                name, val = [x.strip() for x in line.split("=", 1)]
                if val.startswith("'"):
                    self.vars[name] = self.parse_value(val)
                elif val.endswith("()"):  # 関数呼び出し
                    fname = val[:-2]
                    self.call_function(fname)
                else:
                    self.vars[name] = val

            # Show({A}+{B})
            elif line.startswith("Show"):
                parts = re.findall(r"\{(.*?)\}", line)
                result = ""
                for p in parts:
                    result += str(self.vars.get(p, p))
                print(result)

            # 関数定義
            elif re.match(r"\w+\(.*\)\s*=\s*\{", line):
                fname = line.split("(")[0].strip()
                block = []
                i += 1
                while not lines[i].startswith("}'"):
                    block.append(lines[i])
                    i += 1
                ret_line = lines[i]
                ret_val = re.findall(r"'(.*?)'_R", ret_line)[0] if "_R" in ret_line else None
                self.funcs[fname] = (block, ret_val)

            # if構文
            elif line.startswith("if "):
                cond_part = line.split("->")[0][3:].strip()
                cond_result = eval(cond_part, {}, self.vars)

                # o{...}, x{...}
                block_o = re.findall(r"o\{(.*?)\}", line)
                block_x = re.findall(r"x\{(.*?)\}", line)

                chosen = block_o if cond_result else block_x
                for stmt in chosen:
                    self.execute(stmt)

            i += 1

    def call_function(self, name):
        if name not in self.funcs:
            print(f"未定義関数: {name}")
            return
        block, ret = self.funcs[name]
        for stmt in block:
            self.execute(stmt)
        return ret


# -------------------------
# 実行テスト
# -------------------------
code = """
A = '10'_n
Z = '5'_n

if A > Z ->
    o{cls = l()},
    x{cls = t()}

l() = {
    g='大きい'_t
    Show({g})
}'()'_R

t() = {
    g='小さい'_t
    Show({g})
}'()'_R
"""

interpreter = AHOInterpreter()
interpreter.execute(code)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: