调整版本并做测试

This commit is contained in:
2026-02-27 17:10:54 +08:00
parent fa673138f6
commit 31be9d0e97
77 changed files with 679 additions and 25 deletions

View File

@@ -0,0 +1,79 @@
#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
"""
JFinal Stat - Abstract Syntax Tree Base Class
"""
from typing import Any
from ..Scope import Scope
from ...Env import Env
class Stat:
"""Base class for statement AST nodes"""
def exec(self, env: Env, scope: Scope, writer) -> Any:
"""
Execute the statement
Args:
env: Template environment
scope: Execution scope
writer: Output writer
Returns:
Execution result
"""
raise NotImplementedError("Stat.exec() must be implemented by subclasses")
def get_stat_list(self) -> 'StatList':
"""Get as StatList if applicable"""
return None
def __repr__(self) -> str:
return f"Stat({self.__class__.__name__})"
class StatList(Stat):
"""Statement list containing multiple statements"""
def __init__(self):
"""Initialize statement list"""
self._stats: list = []
def add_stat(self, stat: Stat):
"""
Add statement to list
Args:
stat: Statement to add
"""
if stat:
self._stats.append(stat)
def exec(self, env: Env, scope: Scope, writer) -> Any:
"""
Execute all statements in list
Args:
env: Template environment
scope: Execution scope
writer: Output writer
Returns:
Last statement result or None
"""
result = None
for stat in self._stats:
result = stat.exec(env, scope, writer)
return result
def get_stats(self) -> list:
"""Get all statements"""
return self._stats.copy()
def __len__(self) -> int:
"""Get number of statements"""
return len(self._stats)
def __repr__(self) -> str:
return f"StatList({len(self._stats)} statements)"