@furongjun1999/dsh-memory 0.9.6 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/policy.json +38 -38
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +29 -6
- package/lib/index.js +11 -2
- package/lib/lib/hook_audit.js +38 -2
- package/lib/lib/prompt_safety.js +14 -3
- package/lib/tools.d.ts +7 -0
- package/lib/tools.js +22 -1
- package/md_cg/_guard_kit.py +121 -121
- package/md_cg/anchor.py +114 -114
- package/md_cg/audit.py +15 -2
- package/md_cg/chain.py +3 -2
- package/md_cg/config_registry_guard.py +487 -487
- package/md_cg/conformance.py +747 -747
- package/md_cg/consolidate.py +1793 -1793
- package/md_cg/crypto.py +794 -794
- package/md_cg/hyperedge.py +262 -262
- package/md_cg/interface_schema.py +146 -146
- package/md_cg/lifecycle.py +426 -426
- package/md_cg/mdcos.py +12 -8
- package/md_cg/predict.py +1103 -1103
- package/md_cg/protect.py +654 -654
- package/md_cg/provenance.py +606 -606
- package/md_cg/queue_health.py +130 -130
- package/md_cg/refine.py +613 -613
- package/md_cg/retrieve.py +128 -128
- package/md_cg/revise.py +140 -140
- package/md_cg/rollback.py +657 -657
- package/md_cg/rollback_cli.py +124 -124
- package/md_cg/rotate.py +346 -346
- package/md_cg/routing.py +490 -490
- package/md_cg/self_surface.py +50 -50
- package/md_cg/semantic/canonical.py +144 -144
- package/md_cg/semantic/zh_en_atoms.py +141 -141
- package/md_cg/sources.py +1091 -1091
- package/md_cg/stg.py +3 -2
- package/md_cg/test_audit_rotate_parity.py +279 -279
- package/md_cg/test_autonomy_modes.py +2013 -2013
- package/md_cg/test_cn_recall_grams.py +206 -206
- package/md_cg/test_code_kind_forbidden.py +101 -0
- package/md_cg/test_condaccept.py +156 -156
- package/md_cg/test_condgate.py +141 -141
- package/md_cg/test_heartbeat_ledger.py +557 -557
- package/md_cg/test_i50e_readside_protection.py +658 -658
- package/md_cg/test_issue111_gate_order.py +76 -76
- package/md_cg/test_issue111_scoring_contract.py +86 -86
- package/md_cg/test_issue121_calibration_bounds.py +293 -293
- package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
- package/md_cg/test_issue147_pack_id_escape.py +378 -378
- package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
- package/md_cg/test_issue149_accel_default_face.py +248 -248
- package/md_cg/test_issue150_empty_query_terms.py +328 -328
- package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
- package/md_cg/test_issue152_sealed_postings.py +309 -309
- package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
- package/md_cg/test_issue157b_index_remainder.py +344 -344
- package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
- package/md_cg/test_issue191_import_trust.py +382 -382
- package/md_cg/test_issue193_write_scope.py +378 -378
- package/md_cg/test_issue212_writeface_gaps.py +349 -349
- package/md_cg/test_issue214_reject_sensitivity.py +266 -266
- package/md_cg/test_issue215_condition_source_gap.py +272 -272
- package/md_cg/test_issue216_clash_routes.py +214 -214
- package/md_cg/test_issue217_slot_routes.py +248 -248
- package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
- package/md_cg/test_issue258_pack_sensitivity.py +468 -468
- package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
- package/md_cg/test_issue274_read_node_id_none.py +341 -341
- package/md_cg/test_issue275_layer_param.py +250 -250
- package/md_cg/test_issue277_node_id_traversal.py +384 -384
- package/md_cg/test_issue278_neg_separated_tension.py +271 -271
- package/md_cg/test_issue278_neg_window.py +294 -294
- package/md_cg/test_issue278_negative_layering.py +83 -83
- package/md_cg/test_issue284_skip_not_pass.py +291 -291
- package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
- package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
- package/md_cg/test_issue303_design_boundaries.py +262 -262
- package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
- package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
- package/md_cg/test_issue321_mcp_deep_json.py +304 -304
- package/md_cg/test_issue322_node_write_lock.py +727 -727
- package/md_cg/test_issue324_write_size_fields.py +214 -214
- package/md_cg/test_issue325_audit_poison.py +229 -229
- package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
- package/md_cg/test_issue326_key_perms.py +355 -355
- package/md_cg/test_issue326_twin_parser_parity.py +323 -323
- package/md_cg/test_issue326_vm_call_frame.py +569 -569
- package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
- package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
- package/md_cg/test_issue348_insight_verify_op.py +199 -199
- package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
- package/md_cg/test_issue350_casefold_collision.py +235 -235
- package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
- package/md_cg/test_issue352_imgaudit.py +418 -418
- package/md_cg/test_issue352_pathsjson.py +537 -537
- package/md_cg/test_issue352_refindex.py +412 -412
- package/md_cg/test_issue352_rollback.py +604 -604
- package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
- package/md_cg/test_issue354_rust_index_containment.py +571 -571
- package/md_cg/test_issue354_rust_wal_containment.py +606 -606
- package/md_cg/test_issue358_truncation_observable.py +247 -247
- package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
- package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
- package/md_cg/test_issue362_neg_condition.py +468 -468
- package/md_cg/test_issue363_lexical_idf.py +275 -275
- package/md_cg/test_issue370_readcache_assembly.py +488 -488
- package/md_cg/test_issue376_heartbeat_failed.py +293 -293
- package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
- package/md_cg/test_issue378_protected_floor.py +310 -310
- package/md_cg/test_issue383_state_event_write_gate.py +462 -462
- package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
- package/md_cg/test_issue398_toolface_op_parity.py +511 -511
- package/md_cg/test_issue399_hive_json_shape.py +317 -317
- package/md_cg/test_issue401_wisdom_codegen.py +295 -295
- package/md_cg/test_issue404_heal_receipt.py +389 -389
- package/md_cg/test_issue409_compact_index_completeness.py +509 -509
- package/md_cg/test_issue410_verify_demote_write.py +366 -366
- package/md_cg/test_issue411_scrub_idempotence.py +575 -575
- package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
- package/md_cg/test_issue413_preimage_failclosed.py +239 -239
- package/md_cg/test_issue413_preimage_twin.py +339 -339
- package/md_cg/test_issue415_deps_gate_probe.py +357 -357
- package/md_cg/test_issue416_entry_key_subset.py +525 -525
- package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
- package/md_cg/test_issue425_ingest_library_path.py +331 -331
- package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
- package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
- package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
- package/md_cg/test_issue427_serve_status_shape.py +191 -191
- package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
- package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
- package/md_cg/test_issue434_435_float_gate.py +90 -90
- package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
- package/md_cg/test_issue43_default_policy.py +874 -874
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
- package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
- package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
- package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
- package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
- package/md_cg/test_issue451_audit_actor.py +313 -313
- package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
- package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
- package/md_cg/test_issue466_basis_registry.py +469 -469
- package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
- package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
- package/md_cg/test_issue470_wal_event_durable.py +427 -427
- package/md_cg/test_issue471_identity_readable.py +136 -136
- package/md_cg/test_issue472_audit_chain.py +90 -90
- package/md_cg/test_issue472_prune_ledger.py +105 -105
- package/md_cg/test_issue472_rollback_degraded.py +81 -81
- package/md_cg/test_issue491_compose_sandbox.py +460 -460
- package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
- package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
- package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
- package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
- package/md_cg/test_issue525_read_null_semantics.py +52 -52
- package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
- package/md_cg/test_issue547_audit_durable.py +96 -96
- package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
- package/md_cg/test_issue559_refine_prefix.py +69 -69
- package/md_cg/test_issue562_retired_faces.py +150 -0
- package/md_cg/test_issue71_queue_backlog.py +376 -376
- package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
- package/md_cg/test_issue82_credential_rules.py +271 -271
- package/md_cg/test_issue91_verifier_sandbox.py +314 -314
- package/md_cg/test_issue93_remember_redaction.py +282 -282
- package/md_cg/test_logref.py +1141 -1141
- package/md_cg/test_md_access_parity.py +330 -330
- package/md_cg/test_md_cg_no_hive_dep.py +139 -139
- package/md_cg/test_mutation_residue.py +119 -119
- package/md_cg/test_mutation_rollback.py +835 -835
- package/md_cg/test_n123_derive_expiry_chain.py +210 -210
- package/md_cg/test_n209_verify_write_face_gates.py +466 -466
- package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
- package/md_cg/test_neg_tail_honesty.py +733 -733
- package/md_cg/test_p21_tokens.py +294 -294
- package/md_cg/test_p27_docindex.py +792 -792
- package/md_cg/test_p28_refcheck.py +1053 -1053
- package/md_cg/test_p2_six_elements.py +476 -476
- package/md_cg/test_p33_ccg_wiring.py +300 -300
- package/md_cg/test_p3_legacy_closure.py +438 -438
- package/md_cg/test_p40_refine_worklist.py +244 -244
- package/md_cg/test_p4_freshness.py +781 -781
- package/md_cg/test_p9c_dedup_hints.py +288 -288
- package/md_cg/test_policy_required_ccg.py +444 -444
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_state_event_op.py +521 -521
- package/md_cg/test_stgidx_index_parity.py +1024 -1024
- package/md_cg/test_sustain_bounded.py +652 -652
- package/md_cg/test_trust_edge_reachability.py +405 -405
- package/md_cg/test_w7_redlines.py +659 -659
- package/md_cg/test_write_return_shape.py +919 -919
- package/md_cg/test_writepipe.py +228 -228
- package/md_cg/trust.py +1114 -1114
- package/md_cg/twophase.py +303 -303
- package/md_cg/whitebox.py +536 -536
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
- package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
- package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
- package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
- package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
- package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
- package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
- package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
- package/md_cg/writepipe.py +1337 -1337
- package/package.json +3 -2
- package/scripts/workspace_index.py +328 -0
- package/src/hooks.ts +30 -6
- package/src/index.ts +14 -5
- package/src/lib/hook_audit.ts +282 -256
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
|
@@ -1,1082 +1,1082 @@
|
|
|
1
|
-
"""
|
|
2
|
-
parser.py · 语法分析器 v2.0
|
|
3
|
-
将 Token 序列构建为抽象语法树(AST)
|
|
4
|
-
支持:条件语句、指令语句、九章算术结构
|
|
5
|
-
|
|
6
|
-
v2.0 变更:
|
|
7
|
-
- 新增 _merge_identifiers:合并紧密相连的 IDENTIFIER/NUMBER 序列
|
|
8
|
-
- 修复"道 新信任路径"被切成多个操作数的问题
|
|
9
|
-
- 支持"止 X 于 Y"结构中的多词 X
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
from enum import Enum, auto
|
|
13
|
-
from dataclasses import dataclass, field
|
|
14
|
-
from typing import List, Optional, Union, Any, Tuple
|
|
15
|
-
from .lexer import Token, TokenType
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# =============================================================================
|
|
19
|
-
# AST 节点类型
|
|
20
|
-
# =============================================================================
|
|
21
|
-
|
|
22
|
-
class NodeType(Enum):
|
|
23
|
-
"""AST 节点类型"""
|
|
24
|
-
|
|
25
|
-
# === 顶层 ===
|
|
26
|
-
PROGRAM = auto() # 整个程序
|
|
27
|
-
|
|
28
|
-
# === 九章算术结构 ===
|
|
29
|
-
WENYUE = auto() # 问曰
|
|
30
|
-
DAYUE = auto() # 答曰
|
|
31
|
-
SHUYUE = auto() # 术曰
|
|
32
|
-
STEP = auto() # 步骤(序号.操作)
|
|
33
|
-
|
|
34
|
-
# === 语句 ===
|
|
35
|
-
CONDITION_STMT = auto() # 条件语句(若...则...)
|
|
36
|
-
LOOP_STMT = auto() # 循环语句(当...执行...)
|
|
37
|
-
BLOCK = auto() # 语句块(多条顺序语句)
|
|
38
|
-
FUNC_DEF = auto() # 函数定义(定义 名(参数):语句)
|
|
39
|
-
RETURN_STMT = auto() # 返回语句(返回 表达式)
|
|
40
|
-
ASSIGN_STMT = auto() # 赋值语句
|
|
41
|
-
INSTRUCTION_STMT = auto() # 指令语句(道德经助记符)
|
|
42
|
-
OPERATION_STMT = auto() # 操作语句
|
|
43
|
-
|
|
44
|
-
# === 表达式 ===
|
|
45
|
-
BINARY_EXPR = auto() # 二元表达式
|
|
46
|
-
UNARY_EXPR = auto() # 一元表达式
|
|
47
|
-
IDENTIFIER = auto() # 标识符
|
|
48
|
-
LITERAL = auto() # 字面量
|
|
49
|
-
CALL_EXPR = auto() # 调用表达式
|
|
50
|
-
|
|
51
|
-
# === 条件 ===
|
|
52
|
-
COMPARISON = auto() # 比较表达式
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
# =============================================================================
|
|
56
|
-
# AST 节点定义
|
|
57
|
-
# =============================================================================
|
|
58
|
-
|
|
59
|
-
@dataclass
|
|
60
|
-
class ASTNode:
|
|
61
|
-
"""AST 节点基类"""
|
|
62
|
-
type: NodeType
|
|
63
|
-
line: int = 1
|
|
64
|
-
column: int = 1
|
|
65
|
-
children: List['ASTNode'] = field(default_factory=list)
|
|
66
|
-
value: Any = None
|
|
67
|
-
attributes: dict = field(default_factory=dict)
|
|
68
|
-
|
|
69
|
-
def add_child(self, child: 'ASTNode'):
|
|
70
|
-
"""追加子节点(None 直接忽略)。"""
|
|
71
|
-
if child is not None:
|
|
72
|
-
self.children.append(child)
|
|
73
|
-
|
|
74
|
-
def __repr__(self) -> str:
|
|
75
|
-
"""AST 节点调试表示:类型与子节点概览。"""
|
|
76
|
-
return f"ASTNode({self.type.name}, value={self.value!r}, children={len(self.children)})"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
@dataclass
|
|
80
|
-
class ProgramNode(ASTNode):
|
|
81
|
-
"""程序根节点"""
|
|
82
|
-
def __init__(self, line: int = 1, column: int = 1):
|
|
83
|
-
"""构造 ProgramNode:绑定本节点的声明字段与子结构。"""
|
|
84
|
-
super().__init__(NodeType.PROGRAM, line, column)
|
|
85
|
-
self.statements: List[ASTNode] = []
|
|
86
|
-
|
|
87
|
-
def add_statement(self, stmt: ASTNode):
|
|
88
|
-
"""追加语句并同时挂入子树。"""
|
|
89
|
-
if stmt is not None:
|
|
90
|
-
self.statements.append(stmt)
|
|
91
|
-
self.add_child(stmt)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
@dataclass
|
|
95
|
-
class WenyueNode(ASTNode):
|
|
96
|
-
"""问曰节点"""
|
|
97
|
-
def __init__(self, question: str, line: int = 1, column: int = 1):
|
|
98
|
-
"""构造 WenyueNode:绑定本节点的声明字段与子结构。"""
|
|
99
|
-
super().__init__(NodeType.WENYUE, line, column)
|
|
100
|
-
self.question = question
|
|
101
|
-
self.value = question
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
@dataclass
|
|
105
|
-
class DayueNode(ASTNode):
|
|
106
|
-
"""答曰节点"""
|
|
107
|
-
def __init__(self, answer: str, line: int = 1, column: int = 1):
|
|
108
|
-
"""构造 DayueNode:绑定本节点的声明字段与子结构。"""
|
|
109
|
-
super().__init__(NodeType.DAYUE, line, column)
|
|
110
|
-
self.answer = answer
|
|
111
|
-
self.value = answer
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
@dataclass
|
|
115
|
-
class ShuyueNode(ASTNode):
|
|
116
|
-
"""术曰节点"""
|
|
117
|
-
def __init__(self, line: int = 1, column: int = 1):
|
|
118
|
-
"""构造 ShuyueNode:绑定本节点的声明字段与子结构。"""
|
|
119
|
-
super().__init__(NodeType.SHUYUE, line, column)
|
|
120
|
-
self.steps: List['StepNode'] = []
|
|
121
|
-
|
|
122
|
-
def add_step(self, step: 'StepNode'):
|
|
123
|
-
"""追加步骤节点并挂入子树。"""
|
|
124
|
-
self.steps.append(step)
|
|
125
|
-
self.add_child(step)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
@dataclass
|
|
129
|
-
class StepNode(ASTNode):
|
|
130
|
-
"""步骤节点"""
|
|
131
|
-
def __init__(self, step_num: int, statement: ASTNode, line: int = 1, column: int = 1):
|
|
132
|
-
"""构造 StepNode:绑定本节点的声明字段与子结构。"""
|
|
133
|
-
super().__init__(NodeType.STEP, line, column)
|
|
134
|
-
self.step_num = step_num
|
|
135
|
-
self.statement = statement
|
|
136
|
-
self.add_child(statement)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
@dataclass
|
|
140
|
-
class ConditionStmtNode(ASTNode):
|
|
141
|
-
"""条件语句:若 [条件] 则 [操作] [否则 [操作]](body 可为语句列表)"""
|
|
142
|
-
def __init__(self, condition: ASTNode, then_body, else_body=None,
|
|
143
|
-
line: int = 1, column: int = 1):
|
|
144
|
-
"""构造 ConditionStmtNode:绑定本节点的声明字段与子结构。"""
|
|
145
|
-
super().__init__(NodeType.CONDITION_STMT, line, column)
|
|
146
|
-
self.condition = condition
|
|
147
|
-
self.then_body = then_body
|
|
148
|
-
self.else_body = else_body
|
|
149
|
-
self.add_child(condition)
|
|
150
|
-
self.add_child(then_body)
|
|
151
|
-
if else_body is not None:
|
|
152
|
-
self.add_child(else_body)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
@dataclass
|
|
156
|
-
class LoopStmtNode(ASTNode):
|
|
157
|
-
"""循环语句:当 [条件] 执行 [操作](while 语义,body 可为语句列表)"""
|
|
158
|
-
def __init__(self, condition: ASTNode, body, line: int = 1, column: int = 1):
|
|
159
|
-
"""构造 LoopStmtNode:绑定本节点的声明字段与子结构。"""
|
|
160
|
-
super().__init__(NodeType.LOOP_STMT, line, column)
|
|
161
|
-
self.condition = condition
|
|
162
|
-
self.body = body
|
|
163
|
-
self.add_child(condition)
|
|
164
|
-
self.add_child(body)
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
@dataclass
|
|
168
|
-
class BlockNode(ASTNode):
|
|
169
|
-
"""语句块:多条顺序执行的语句(循环体/条件体多语句支持)"""
|
|
170
|
-
def __init__(self, statements, line: int = 1, column: int = 1):
|
|
171
|
-
"""构造 BlockNode:绑定本节点的声明字段与子结构。"""
|
|
172
|
-
super().__init__(NodeType.BLOCK, line, column)
|
|
173
|
-
self.statements = statements
|
|
174
|
-
for s in statements:
|
|
175
|
-
self.add_child(s)
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
@dataclass
|
|
179
|
-
class FuncDefNode(ASTNode):
|
|
180
|
-
"""函数定义:定义 名(参数):语句(body 可为语句列表)"""
|
|
181
|
-
def __init__(self, name: str, params: List[str], body, line: int = 1, column: int = 1):
|
|
182
|
-
"""构造 FuncDefNode:绑定本节点的声明字段与子结构。"""
|
|
183
|
-
super().__init__(NodeType.FUNC_DEF, line, column)
|
|
184
|
-
self.name = name
|
|
185
|
-
self.params = params
|
|
186
|
-
self.body = body
|
|
187
|
-
self.add_child(body)
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
@dataclass
|
|
191
|
-
class ReturnStmtNode(ASTNode):
|
|
192
|
-
"""返回语句:返回 [表达式]"""
|
|
193
|
-
def __init__(self, value: Optional[ASTNode], line: int = 1, column: int = 1):
|
|
194
|
-
"""构造 ReturnStmtNode:绑定本节点的声明字段与子结构。"""
|
|
195
|
-
super().__init__(NodeType.RETURN_STMT, line, column)
|
|
196
|
-
self.value = value
|
|
197
|
-
if value:
|
|
198
|
-
self.add_child(value)
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
@dataclass
|
|
202
|
-
class CallExprNode(ASTNode):
|
|
203
|
-
"""函数调用:名(参数1,参数2)"""
|
|
204
|
-
def __init__(self, name: str, args: List[ASTNode], line: int = 1, column: int = 1):
|
|
205
|
-
"""构造 CallExprNode:绑定本节点的声明字段与子结构。"""
|
|
206
|
-
super().__init__(NodeType.CALL_EXPR, line, column)
|
|
207
|
-
self.name = name
|
|
208
|
-
self.args = args
|
|
209
|
-
for a in args:
|
|
210
|
-
self.add_child(a)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
@dataclass
|
|
214
|
-
class InstructionStmtNode(ASTNode):
|
|
215
|
-
"""指令语句:道德经助记符 + 操作数"""
|
|
216
|
-
def __init__(self, instruction: TokenType, operands: List[ASTNode] = None,
|
|
217
|
-
line: int = 1, column: int = 1):
|
|
218
|
-
"""构造 InstructionStmtNode:绑定本节点的声明字段与子结构。"""
|
|
219
|
-
super().__init__(NodeType.INSTRUCTION_STMT, line, column)
|
|
220
|
-
self.instruction = instruction
|
|
221
|
-
self.operands = operands or []
|
|
222
|
-
for op in self.operands:
|
|
223
|
-
self.add_child(op)
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
@dataclass
|
|
227
|
-
class AssignStmtNode(ASTNode):
|
|
228
|
-
"""赋值语句:标识符 = 值"""
|
|
229
|
-
def __init__(self, target: str, value: ASTNode, line: int = 1, column: int = 1):
|
|
230
|
-
"""构造 AssignStmtNode:绑定本节点的声明字段与子结构。"""
|
|
231
|
-
super().__init__(NodeType.ASSIGN_STMT, line, column)
|
|
232
|
-
self.target = target
|
|
233
|
-
self.value_node = value
|
|
234
|
-
self.add_child(value)
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
@dataclass
|
|
238
|
-
class BinaryExprNode(ASTNode):
|
|
239
|
-
"""二元表达式:左 操作符 右"""
|
|
240
|
-
def __init__(self, left: ASTNode, operator: str, right: ASTNode,
|
|
241
|
-
line: int = 1, column: int = 1):
|
|
242
|
-
"""构造 BinaryExprNode:绑定本节点的声明字段与子结构。"""
|
|
243
|
-
super().__init__(NodeType.BINARY_EXPR, line, column)
|
|
244
|
-
self.left = left
|
|
245
|
-
self.operator = operator
|
|
246
|
-
self.right = right
|
|
247
|
-
self.add_child(left)
|
|
248
|
-
self.add_child(right)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
@dataclass
|
|
252
|
-
class ComparisonNode(ASTNode):
|
|
253
|
-
"""比较表达式"""
|
|
254
|
-
def __init__(self, left: ASTNode, op: str, right: ASTNode,
|
|
255
|
-
line: int = 1, column: int = 1):
|
|
256
|
-
"""构造 ComparisonNode:绑定本节点的声明字段与子结构。"""
|
|
257
|
-
super().__init__(NodeType.COMPARISON, line, column)
|
|
258
|
-
self.left = left
|
|
259
|
-
self.op = op
|
|
260
|
-
self.right = right
|
|
261
|
-
self.add_child(left)
|
|
262
|
-
self.add_child(right)
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
@dataclass
|
|
266
|
-
class IdentifierNode(ASTNode):
|
|
267
|
-
"""标识符"""
|
|
268
|
-
def __init__(self, name: str, line: int = 1, column: int = 1):
|
|
269
|
-
"""构造 IdentifierNode:绑定本节点的声明字段与子结构。"""
|
|
270
|
-
super().__init__(NodeType.IDENTIFIER, line, column)
|
|
271
|
-
self.name = name
|
|
272
|
-
self.value = name
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
@dataclass
|
|
276
|
-
class LiteralNode(ASTNode):
|
|
277
|
-
"""字面量"""
|
|
278
|
-
def __init__(self, value: Union[str, float, int], literal_type: str,
|
|
279
|
-
line: int = 1, column: int = 1):
|
|
280
|
-
"""构造 LiteralNode:绑定本节点的声明字段与子结构。"""
|
|
281
|
-
super().__init__(NodeType.LITERAL, line, column)
|
|
282
|
-
self.literal_value = value
|
|
283
|
-
self.literal_type = literal_type # "number", "string"
|
|
284
|
-
self.value = value
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
# =============================================================================
|
|
288
|
-
# 语法分析器
|
|
289
|
-
# =============================================================================
|
|
290
|
-
|
|
291
|
-
class Parser:
|
|
292
|
-
"""
|
|
293
|
-
语法分析器 v2.0
|
|
294
|
-
将 Token 序列构建为 AST
|
|
295
|
-
"""
|
|
296
|
-
|
|
297
|
-
# 指令助记符集合
|
|
298
|
-
INSTRUCTION_TOKENS = {
|
|
299
|
-
TokenType.DAO, TokenType.DE, TokenType.ZIRAN,
|
|
300
|
-
TokenType.WUWEI, TokenType.GU, TokenType.PIN,
|
|
301
|
-
TokenType.ROU, TokenType.PU, TokenType.ZHI, TokenType.ZHIZU,
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
def __init__(self, tokens: List[Token], errors: List[str] = None):
|
|
305
|
-
"""构造 Parser:绑定本节点的声明字段与子结构。"""
|
|
306
|
-
self.tokens = tokens
|
|
307
|
-
self.pos = 0
|
|
308
|
-
self.errors: List[str] = errors or []
|
|
309
|
-
self.current_token = self.tokens[0] if tokens else None
|
|
310
|
-
|
|
311
|
-
def parse(self) -> ProgramNode:
|
|
312
|
-
"""解析整个程序"""
|
|
313
|
-
program = ProgramNode()
|
|
314
|
-
|
|
315
|
-
while not self._is_at_end():
|
|
316
|
-
if self._match(TokenType.NEWLINE, TokenType.SEMICOLON, TokenType.COMMA):
|
|
317
|
-
continue
|
|
318
|
-
|
|
319
|
-
stmt = self._parse_statement()
|
|
320
|
-
if stmt:
|
|
321
|
-
program.add_statement(stmt)
|
|
322
|
-
# DEBUG
|
|
323
|
-
# print(f" [parse] Added: type={stmt.type.name} value={stmt.value!r}")
|
|
324
|
-
# if hasattr(stmt, 'operands'):
|
|
325
|
-
# for j, op in enumerate(stmt.operands):
|
|
326
|
-
# print(f" op{j}: {op.type.name} value={op.value!r}")
|
|
327
|
-
else:
|
|
328
|
-
# print(f" [parse] None at {self.current_token}")
|
|
329
|
-
self._advance()
|
|
330
|
-
|
|
331
|
-
return program
|
|
332
|
-
|
|
333
|
-
# ---- 语句解析 ----
|
|
334
|
-
|
|
335
|
-
def _parse_statement(self) -> Optional[ASTNode]:
|
|
336
|
-
"""解析单个语句"""
|
|
337
|
-
token = self.current_token
|
|
338
|
-
|
|
339
|
-
if token is None or token.type == TokenType.EOF:
|
|
340
|
-
return None
|
|
341
|
-
|
|
342
|
-
# 问曰 → 答曰 → 术曰(九章算术结构)
|
|
343
|
-
if token.type == TokenType.WENYUE:
|
|
344
|
-
return self._parse_wenyue_block()
|
|
345
|
-
|
|
346
|
-
# 术曰 → 术曰块(可以独立出现)
|
|
347
|
-
if token.type == TokenType.SHUYUE:
|
|
348
|
-
return self._parse_shuyue_block()
|
|
349
|
-
|
|
350
|
-
# 若 → 条件语句
|
|
351
|
-
if token.type == TokenType.RUO:
|
|
352
|
-
return self._parse_condition()
|
|
353
|
-
|
|
354
|
-
# 当 → 循环语句(白箱循环语法:当 条件 执行 操作)
|
|
355
|
-
if token.type == TokenType.DANG:
|
|
356
|
-
return self._parse_loop()
|
|
357
|
-
|
|
358
|
-
# 定义 → 函数定义(定义 名(参数):语句)
|
|
359
|
-
if token.type == TokenType.DINGYI:
|
|
360
|
-
return self._parse_func_def()
|
|
361
|
-
|
|
362
|
-
# 返回 → 返回语句(返回 表达式)
|
|
363
|
-
if token.type == TokenType.FANHUI:
|
|
364
|
-
self._advance()
|
|
365
|
-
val = self._parse_expression() if self.current_token else None
|
|
366
|
-
return ReturnStmtNode(val,
|
|
367
|
-
line=token.line, column=token.column)
|
|
368
|
-
|
|
369
|
-
# 道德经助记符 → 指令语句
|
|
370
|
-
if token.type in self.INSTRUCTION_TOKENS:
|
|
371
|
-
return self._parse_instruction()
|
|
372
|
-
|
|
373
|
-
# 标识符 → 可能是赋值
|
|
374
|
-
if token.type == TokenType.IDENTIFIER:
|
|
375
|
-
return self._parse_assign_or_call()
|
|
376
|
-
|
|
377
|
-
# 句号结束
|
|
378
|
-
if token.type == TokenType.PERIOD:
|
|
379
|
-
self._advance()
|
|
380
|
-
return None
|
|
381
|
-
|
|
382
|
-
# 无法识别
|
|
383
|
-
self.errors.append(
|
|
384
|
-
f"L{token.line}:C{token.column} 无法解析的语句开头: '{token.value}' ({token.type.name})"
|
|
385
|
-
)
|
|
386
|
-
self._advance()
|
|
387
|
-
return None
|
|
388
|
-
|
|
389
|
-
def _parse_shuyue_block(self) -> Optional[ASTNode]:
|
|
390
|
-
"""
|
|
391
|
-
解析术曰块(独立形式):术曰:1。... 2。...
|
|
392
|
-
不依赖前面的问曰/答曰
|
|
393
|
-
"""
|
|
394
|
-
self._consume(TokenType.SHUYUE, "期望 '术曰'")
|
|
395
|
-
|
|
396
|
-
shuyue = ShuyueNode(line=self.current_token.line if self.current_token else 1,
|
|
397
|
-
column=self.current_token.column if self.current_token else 1)
|
|
398
|
-
|
|
399
|
-
# 解析步骤序列
|
|
400
|
-
while not self._is_at_end():
|
|
401
|
-
# 跳过分隔符
|
|
402
|
-
while (self.current_token and
|
|
403
|
-
self.current_token.type in (TokenType.SEMICOLON,
|
|
404
|
-
TokenType.COMMA,
|
|
405
|
-
TokenType.COLON)):
|
|
406
|
-
self._advance()
|
|
407
|
-
|
|
408
|
-
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
409
|
-
step_num = int(float(self.current_token.value))
|
|
410
|
-
self._advance()
|
|
411
|
-
|
|
412
|
-
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
413
|
-
self._advance()
|
|
414
|
-
|
|
415
|
-
step_content = self._parse_step_content()
|
|
416
|
-
if step_content:
|
|
417
|
-
shuyue.add_step(StepNode(step_num, step_content,
|
|
418
|
-
line=step_content.line,
|
|
419
|
-
column=step_content.column))
|
|
420
|
-
else:
|
|
421
|
-
break
|
|
422
|
-
|
|
423
|
-
return shuyue
|
|
424
|
-
|
|
425
|
-
def _parse_wenyue_block(self) -> Optional[ASTNode]:
|
|
426
|
-
"""解析 问曰:... 答曰:... 术曰:... 结构"""
|
|
427
|
-
# 问曰
|
|
428
|
-
self._consume(TokenType.WENYUE, "期望 '问曰'")
|
|
429
|
-
question_parts = self._collect_until(TokenType.DAYUE)
|
|
430
|
-
question = "".join(p.value for p in question_parts).strip()
|
|
431
|
-
|
|
432
|
-
# 答曰
|
|
433
|
-
self._consume(TokenType.DAYUE, "期望 '答曰'")
|
|
434
|
-
answer_parts = self._collect_until(TokenType.SHUYUE)
|
|
435
|
-
answer = "".join(p.value for p in answer_parts).strip()
|
|
436
|
-
|
|
437
|
-
# 术曰
|
|
438
|
-
self._consume(TokenType.SHUYUE, "期望 '术曰'")
|
|
439
|
-
|
|
440
|
-
# 用独立的术曰解析逻辑
|
|
441
|
-
shuyue = ShuyueNode(line=self.current_token.line if self.current_token else 1,
|
|
442
|
-
column=self.current_token.column if self.current_token else 1)
|
|
443
|
-
|
|
444
|
-
while not self._is_at_end():
|
|
445
|
-
# 跳过分隔符
|
|
446
|
-
while (self.current_token and
|
|
447
|
-
self.current_token.type in (TokenType.SEMICOLON,
|
|
448
|
-
TokenType.COMMA,
|
|
449
|
-
TokenType.COLON)):
|
|
450
|
-
self._advance()
|
|
451
|
-
|
|
452
|
-
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
453
|
-
step_num = int(float(self.current_token.value))
|
|
454
|
-
self._advance()
|
|
455
|
-
|
|
456
|
-
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
457
|
-
self._advance()
|
|
458
|
-
|
|
459
|
-
step_content = self._parse_step_content()
|
|
460
|
-
if step_content:
|
|
461
|
-
shuyue.add_step(StepNode(step_num, step_content,
|
|
462
|
-
line=step_content.line,
|
|
463
|
-
column=step_content.column))
|
|
464
|
-
else:
|
|
465
|
-
break
|
|
466
|
-
|
|
467
|
-
shuyue.attributes["question"] = question
|
|
468
|
-
shuyue.attributes["answer"] = answer
|
|
469
|
-
|
|
470
|
-
return shuyue
|
|
471
|
-
|
|
472
|
-
def _parse_step_content(self) -> Optional[ASTNode]:
|
|
473
|
-
"""解析步骤内容"""
|
|
474
|
-
if self.current_token and self.current_token.type == TokenType.RUO:
|
|
475
|
-
return self._parse_condition()
|
|
476
|
-
elif self.current_token and self.current_token.type == TokenType.DANG:
|
|
477
|
-
return self._parse_loop()
|
|
478
|
-
elif self.current_token and self.current_token.type == TokenType.DINGYI:
|
|
479
|
-
return self._parse_func_def()
|
|
480
|
-
elif self.current_token and self.current_token.type == TokenType.FANHUI:
|
|
481
|
-
self._advance()
|
|
482
|
-
val = self._parse_expression() if self.current_token else None
|
|
483
|
-
return ReturnStmtNode(val, line=1, column=1)
|
|
484
|
-
elif self.current_token and self.current_token.type in self.INSTRUCTION_TOKENS:
|
|
485
|
-
return self._parse_instruction()
|
|
486
|
-
elif self.current_token and self.current_token.type == TokenType.IDENTIFIER:
|
|
487
|
-
# 可能是单个标识符或合并后的多词短语
|
|
488
|
-
#
|
|
489
|
-
# issue #326-②(2026-10-14):本函数是 compiler/parser.py 的**孪生副本**,
|
|
490
|
-
# 而该处修复(2026-09-14)长期**未回移**——两条链对同一中文源码产出
|
|
491
|
-
# **不同字节码**:本副本原先无条件走 _merge_identifiers()(多词短语
|
|
492
|
-
# 合并),从不探视其后是否紧跟 = 或 (,于是「1。计数 = 0;」只留下裸
|
|
493
|
-
# 标识符 计数、`= 0` 被丢掉,且**不报错**——只降级为一条 warning
|
|
494
|
-
# 「L2 未编译语句类型: IDENTIFIER」,即静默错值(比崩溃更危险)。
|
|
495
|
-
# 实测复现:compiler/ 14 条指令(含 PUSH_CONST 0.0; STORE_NAME 计数),
|
|
496
|
-
# 本副本 12 条且初始化缺失。
|
|
497
|
-
# 回移 lookahead(与 compiler/parser.py 同款):下一个 token 是 = 或 (
|
|
498
|
-
# 则走 _parse_assign_or_call,否则保持原多词合并行为(「道 新信任路径」
|
|
499
|
-
# 等短语不受影响)。双链字节码等价性由守卫
|
|
500
|
-
# md_cg/test_issue326_twin_parser_parity.py 回归守护。
|
|
501
|
-
_nxt = self._peek_next()
|
|
502
|
-
if _nxt is not None and _nxt.type in (TokenType.EQUALS,
|
|
503
|
-
TokenType.LPAREN):
|
|
504
|
-
return self._parse_assign_or_call()
|
|
505
|
-
merged = self._merge_identifiers()
|
|
506
|
-
return merged
|
|
507
|
-
else:
|
|
508
|
-
parts = []
|
|
509
|
-
while (self.current_token and
|
|
510
|
-
self.current_token.type not in (TokenType.NUMBER, TokenType.PERIOD) and
|
|
511
|
-
not self._is_at_end()):
|
|
512
|
-
parts.append(self.current_token.value)
|
|
513
|
-
self._advance()
|
|
514
|
-
if parts:
|
|
515
|
-
return LiteralNode("".join(parts).strip(), "string",
|
|
516
|
-
line=self.current_token.line if self.current_token else 1,
|
|
517
|
-
column=self.current_token.column if self.current_token else 1)
|
|
518
|
-
return None
|
|
519
|
-
|
|
520
|
-
def _parse_condition(self) -> Optional[ASTNode]:
|
|
521
|
-
"""解析条件语句:若 [条件] 则 [操作] [否则 [操作]]"""
|
|
522
|
-
start_line = self.current_token.line if self.current_token else 1
|
|
523
|
-
start_col = self.current_token.column if self.current_token else 1
|
|
524
|
-
|
|
525
|
-
self._consume(TokenType.RUO, "期望 '若'")
|
|
526
|
-
|
|
527
|
-
condition = self._parse_comparison()
|
|
528
|
-
|
|
529
|
-
self._skip_punctuation_before(TokenType.ZE)
|
|
530
|
-
|
|
531
|
-
self._consume(TokenType.ZE, "期望 '则'")
|
|
532
|
-
|
|
533
|
-
then_body = self._parse_single_statement()
|
|
534
|
-
|
|
535
|
-
else_body = None
|
|
536
|
-
# 跳过 then 与 否则 之间的分隔符(逗号/分号等)
|
|
537
|
-
self._skip_punctuation_before(TokenType.FOUZE)
|
|
538
|
-
if self.current_token and self.current_token.type == TokenType.FOUZE:
|
|
539
|
-
self._advance()
|
|
540
|
-
else_body = self._parse_single_statement()
|
|
541
|
-
|
|
542
|
-
return ConditionStmtNode(condition, then_body, else_body, start_line, start_col)
|
|
543
|
-
|
|
544
|
-
def _parse_loop(self) -> Optional[ASTNode]:
|
|
545
|
-
"""解析循环语句:当 [条件] 执行 [操作](while 语义)"""
|
|
546
|
-
start_line = self.current_token.line if self.current_token else 1
|
|
547
|
-
start_col = self.current_token.column if self.current_token else 1
|
|
548
|
-
|
|
549
|
-
self._consume(TokenType.DANG, "期望 '当'")
|
|
550
|
-
|
|
551
|
-
condition = self._parse_comparison()
|
|
552
|
-
|
|
553
|
-
self._skip_punctuation_before(TokenType.ZHIXING)
|
|
554
|
-
|
|
555
|
-
self._consume(TokenType.ZHIXING, "期望 '执行'")
|
|
556
|
-
|
|
557
|
-
body = self._parse_statement_or_block()
|
|
558
|
-
|
|
559
|
-
return LoopStmtNode(condition, body, start_line, start_col)
|
|
560
|
-
|
|
561
|
-
def _parse_func_def(self) -> Optional[ASTNode]:
|
|
562
|
-
"""解析函数定义:定义 名(参数1,参数2):语句
|
|
563
|
-
参数在( )内,逗号分隔;返回 FuncDefNode(body 可为块)"""
|
|
564
|
-
start_line = self.current_token.line if self.current_token else 1
|
|
565
|
-
start_col = self.current_token.column if self.current_token else 1
|
|
566
|
-
|
|
567
|
-
self._consume(TokenType.DINGYI, "期望 '定义'")
|
|
568
|
-
|
|
569
|
-
# 函数名(标识符)
|
|
570
|
-
if not (self.current_token and self.current_token.type == TokenType.IDENTIFIER):
|
|
571
|
-
self.errors.append("定义后期望函数名")
|
|
572
|
-
return None
|
|
573
|
-
name = self.current_token.value
|
|
574
|
-
self._advance()
|
|
575
|
-
|
|
576
|
-
# 参数列表( )
|
|
577
|
-
params = []
|
|
578
|
-
if self.current_token and self.current_token.type == TokenType.LPAREN:
|
|
579
|
-
self._advance()
|
|
580
|
-
while (self.current_token and
|
|
581
|
-
self.current_token.type != TokenType.RPAREN and
|
|
582
|
-
not self._is_at_end()):
|
|
583
|
-
if self.current_token.type == TokenType.IDENTIFIER:
|
|
584
|
-
params.append(self.current_token.value)
|
|
585
|
-
self._advance() # 跳过标识符/逗号/空格
|
|
586
|
-
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
587
|
-
self._advance()
|
|
588
|
-
|
|
589
|
-
# 冒号(可选分隔)
|
|
590
|
-
if self.current_token and self.current_token.type == TokenType.COLON:
|
|
591
|
-
self._advance()
|
|
592
|
-
|
|
593
|
-
body = self._parse_single_statement()
|
|
594
|
-
|
|
595
|
-
return FuncDefNode(name, params, body, start_line, start_col)
|
|
596
|
-
|
|
597
|
-
def _skip_punctuation_before(self, *target_types: TokenType):
|
|
598
|
-
"""跳过标点符号"""
|
|
599
|
-
while self.current_token and self.current_token.type in (
|
|
600
|
-
TokenType.COMMA, TokenType.PERIOD, TokenType.SEMICOLON,
|
|
601
|
-
TokenType.COLON, TokenType.QUESTION, TokenType.EXCLAM,
|
|
602
|
-
):
|
|
603
|
-
self._advance()
|
|
604
|
-
|
|
605
|
-
def _parse_statement_or_block(self) -> Any:
|
|
606
|
-
"""解析语句或块:返回语句列表(单语句=[stmt];分号/句号分隔多条)
|
|
607
|
-
块内多条语句:`则 语句1;语句2;...`(支持循环体/条件体多语句)"""
|
|
608
|
-
stmts = []
|
|
609
|
-
while True:
|
|
610
|
-
stmt = self._parse_single_statement()
|
|
611
|
-
if stmt is not None:
|
|
612
|
-
stmts.append(stmt)
|
|
613
|
-
# 分隔符:分号/句号 → 继续收下一条;否则块结束
|
|
614
|
-
if self.current_token and self.current_token.type in (
|
|
615
|
-
TokenType.SEMICOLON, TokenType.PERIOD, TokenType.COMMA):
|
|
616
|
-
self._advance()
|
|
617
|
-
# 分隔符后若是步骤号/块边界 → 块结束(九章算术步骤边界 1。…2。…)
|
|
618
|
-
if (self.current_token and
|
|
619
|
-
self.current_token.type in (TokenType.NUMBER, TokenType.SHUYUE)):
|
|
620
|
-
break
|
|
621
|
-
continue
|
|
622
|
-
break
|
|
623
|
-
if not stmts:
|
|
624
|
-
return None
|
|
625
|
-
if len(stmts) == 1:
|
|
626
|
-
return stmts[0]
|
|
627
|
-
return BlockNode(stmts, line=stmts[0].line, column=stmts[0].column)
|
|
628
|
-
|
|
629
|
-
def _parse_single_statement(self) -> Optional[ASTNode]:
|
|
630
|
-
"""解析单条语句"""
|
|
631
|
-
if self.current_token and self.current_token.type == TokenType.RUO:
|
|
632
|
-
return self._parse_condition()
|
|
633
|
-
elif self.current_token and self.current_token.type == TokenType.DANG:
|
|
634
|
-
return self._parse_loop()
|
|
635
|
-
elif self.current_token and self.current_token.type == TokenType.DINGYI:
|
|
636
|
-
return self._parse_func_def()
|
|
637
|
-
elif self.current_token and self.current_token.type == TokenType.FANHUI:
|
|
638
|
-
self._advance()
|
|
639
|
-
val = self._parse_expression() if self.current_token else None
|
|
640
|
-
return ReturnStmtNode(val, line=1, column=1)
|
|
641
|
-
elif self.current_token and self.current_token.type in self.INSTRUCTION_TOKENS:
|
|
642
|
-
return self._parse_instruction()
|
|
643
|
-
elif self.current_token and self.current_token.type == TokenType.IDENTIFIER:
|
|
644
|
-
return self._parse_assign_or_call()
|
|
645
|
-
else:
|
|
646
|
-
parts = []
|
|
647
|
-
# 停止条件:标点符号 + 语句开头关键字
|
|
648
|
-
_STOP = (
|
|
649
|
-
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
650
|
-
TokenType.WENYUE, TokenType.DAYUE, TokenType.SHUYUE,
|
|
651
|
-
TokenType.RUO, TokenType.FOUZE, TokenType.DANG,
|
|
652
|
-
TokenType.ZHIXING,
|
|
653
|
-
)
|
|
654
|
-
while (self.current_token and
|
|
655
|
-
self.current_token.type not in _STOP and
|
|
656
|
-
not self._is_at_end()):
|
|
657
|
-
parts.append(self.current_token.value)
|
|
658
|
-
self._advance()
|
|
659
|
-
text = "".join(parts).strip()
|
|
660
|
-
if text:
|
|
661
|
-
return LiteralNode(text, "string")
|
|
662
|
-
return None
|
|
663
|
-
|
|
664
|
-
def _parse_instruction(self) -> Optional[ASTNode]:
|
|
665
|
-
"""
|
|
666
|
-
解析指令语句 v2.0
|
|
667
|
-
|
|
668
|
-
关键改进:使用 _merge_identifiers 合并紧密相连的
|
|
669
|
-
IDENTIFIER/NUMBER 序列为一个逻辑操作数。
|
|
670
|
-
|
|
671
|
-
例:道 新信任路径 → 道 + [ID(新信任路径)]
|
|
672
|
-
例:柔 响应强度 → 柔 + [ID(响应强度)]
|
|
673
|
-
例:止情感权重于0.15 → 止 + [ID(情感权重)] + [Lit(0.15)]
|
|
674
|
-
"""
|
|
675
|
-
instr_token = self.current_token
|
|
676
|
-
instr_type = instr_token.type
|
|
677
|
-
self._advance()
|
|
678
|
-
|
|
679
|
-
operands = []
|
|
680
|
-
|
|
681
|
-
# 停止条件:标点符号 + 语句开头关键字(防止跨语句吞噬)
|
|
682
|
-
_INST_STOP = (
|
|
683
|
-
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
684
|
-
TokenType.EOF,
|
|
685
|
-
# 语句开头关键字 —— 遇到这些说明指令操作数已结束
|
|
686
|
-
TokenType.WENYUE, # 问曰 → 新语句
|
|
687
|
-
TokenType.DAYUE, # 答曰 → 新语句
|
|
688
|
-
TokenType.SHUYUE, # 术曰 → 新语句
|
|
689
|
-
TokenType.RUO, # 若 → 条件语句
|
|
690
|
-
TokenType.FOUZE, # 否则 → 条件语句
|
|
691
|
-
TokenType.DAO, # 道 → 指令(但不在操作数位置)
|
|
692
|
-
TokenType.DE, # 德 → 指令
|
|
693
|
-
TokenType.ZIRAN, # 自然 → 指令
|
|
694
|
-
TokenType.WUWEI, # 无为 → 指令
|
|
695
|
-
TokenType.GU, # 谷 → 指令
|
|
696
|
-
TokenType.PIN, # 牝 → 指令
|
|
697
|
-
TokenType.ROU, # 柔 → 指令
|
|
698
|
-
TokenType.PU, # 朴 → 指令
|
|
699
|
-
TokenType.ZHI, # 止 → 指令
|
|
700
|
-
TokenType.ZHIZU, # 知足 → 指令
|
|
701
|
-
)
|
|
702
|
-
|
|
703
|
-
while (self.current_token and
|
|
704
|
-
self.current_token.type not in _INST_STOP and
|
|
705
|
-
not self._is_at_end()):
|
|
706
|
-
|
|
707
|
-
tok = self.current_token
|
|
708
|
-
|
|
709
|
-
if tok.type == TokenType.YU:
|
|
710
|
-
# "于" → 后面跟数值
|
|
711
|
-
self._advance()
|
|
712
|
-
value = self._parse_numeric_value()
|
|
713
|
-
if value is not None:
|
|
714
|
-
operands.append(value)
|
|
715
|
-
continue
|
|
716
|
-
|
|
717
|
-
elif tok.type == TokenType.IDENTIFIER:
|
|
718
|
-
# 合并后续紧密相连的 IDENTIFIER/NUMBER
|
|
719
|
-
merged = self._merge_identifiers()
|
|
720
|
-
operands.append(merged)
|
|
721
|
-
|
|
722
|
-
elif tok.type == TokenType.NUMBER:
|
|
723
|
-
operands.append(LiteralNode(float(tok.value), "number", tok.line, tok.column))
|
|
724
|
-
self._advance()
|
|
725
|
-
|
|
726
|
-
elif tok.type == TokenType.STRING:
|
|
727
|
-
operands.append(LiteralNode(tok.value, "string", tok.line, tok.column))
|
|
728
|
-
self._advance()
|
|
729
|
-
|
|
730
|
-
else:
|
|
731
|
-
self._advance()
|
|
732
|
-
|
|
733
|
-
return InstructionStmtNode(instr_type, operands,
|
|
734
|
-
line=instr_token.line,
|
|
735
|
-
column=instr_token.column)
|
|
736
|
-
|
|
737
|
-
def _merge_identifiers(self) -> ASTNode:
|
|
738
|
-
"""
|
|
739
|
-
合并从当前位置开始的紧密相连的 IDENTIFIER/NUMBER 序列
|
|
740
|
-
|
|
741
|
-
例:ID(新) ID(信任) ID(路径) → IdentifierNode("新信任路径")
|
|
742
|
-
例:ID(响应) ID(强度) → IdentifierNode("响应强度")
|
|
743
|
-
例:ID(累积) ID(信任值) → IdentifierNode("累积信任值")
|
|
744
|
-
|
|
745
|
-
判断标准:同一行、列号连续
|
|
746
|
-
"""
|
|
747
|
-
start_tok = self.current_token
|
|
748
|
-
parts = [start_tok.value]
|
|
749
|
-
line = start_tok.line
|
|
750
|
-
col = start_tok.column
|
|
751
|
-
self._advance()
|
|
752
|
-
|
|
753
|
-
while (self.current_token and
|
|
754
|
-
self.current_token.line == line and
|
|
755
|
-
self.current_token.type in (TokenType.IDENTIFIER, TokenType.NUMBER)):
|
|
756
|
-
parts.append(self.current_token.value)
|
|
757
|
-
self._advance()
|
|
758
|
-
|
|
759
|
-
merged_name = "".join(parts)
|
|
760
|
-
return IdentifierNode(merged_name, line, col)
|
|
761
|
-
|
|
762
|
-
def _parse_numeric_value(self) -> Optional[ASTNode]:
|
|
763
|
-
"""解析数值(可能跨多个 token)"""
|
|
764
|
-
if self.current_token is None:
|
|
765
|
-
return None
|
|
766
|
-
|
|
767
|
-
if self.current_token.type == TokenType.NUMBER:
|
|
768
|
-
val = float(self.current_token.value)
|
|
769
|
-
line, col = self.current_token.line, self.current_token.column
|
|
770
|
-
self._advance()
|
|
771
|
-
return LiteralNode(val, "number", line, col)
|
|
772
|
-
|
|
773
|
-
if self.current_token.type == TokenType.IDENTIFIER:
|
|
774
|
-
prefix = self.current_token.value
|
|
775
|
-
line, col = self.current_token.line, self.current_token.column
|
|
776
|
-
self._advance()
|
|
777
|
-
|
|
778
|
-
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
779
|
-
self._advance()
|
|
780
|
-
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
781
|
-
val = float(prefix + "." + self.current_token.value)
|
|
782
|
-
self._advance()
|
|
783
|
-
return LiteralNode(val, "number", line, col)
|
|
784
|
-
else:
|
|
785
|
-
try:
|
|
786
|
-
return LiteralNode(float(prefix), "number", line, col)
|
|
787
|
-
except ValueError:
|
|
788
|
-
return IdentifierNode(prefix, line, col)
|
|
789
|
-
elif self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
790
|
-
val = float(prefix + self.current_token.value)
|
|
791
|
-
self._advance()
|
|
792
|
-
return LiteralNode(val, "number", line, col)
|
|
793
|
-
else:
|
|
794
|
-
return IdentifierNode(prefix, line, col)
|
|
795
|
-
|
|
796
|
-
return None
|
|
797
|
-
|
|
798
|
-
def _parse_assign_or_call(self) -> Optional[ASTNode]:
|
|
799
|
-
"""解析赋值或调用
|
|
800
|
-
|
|
801
|
-
若后面紧跟 = 或 = → 赋值语句
|
|
802
|
-
若后面紧跟 ( → 函数调用表达式(名(参数))
|
|
803
|
-
否则 → 标识符表达式
|
|
804
|
-
"""
|
|
805
|
-
ident = self.current_token.value
|
|
806
|
-
line = self.current_token.line
|
|
807
|
-
col = self.current_token.column
|
|
808
|
-
self._advance()
|
|
809
|
-
|
|
810
|
-
# 检查是否是赋值
|
|
811
|
-
if self.current_token and self.current_token.type == TokenType.EQUALS:
|
|
812
|
-
self._advance() # 跳过 =
|
|
813
|
-
value_node = self._parse_expression()
|
|
814
|
-
return AssignStmtNode(
|
|
815
|
-
target=ident,
|
|
816
|
-
value=value_node,
|
|
817
|
-
line=line,
|
|
818
|
-
column=col
|
|
819
|
-
)
|
|
820
|
-
|
|
821
|
-
# 函数调用:名(参数1,参数2)
|
|
822
|
-
if self.current_token and self.current_token.type == TokenType.LPAREN:
|
|
823
|
-
self._advance()
|
|
824
|
-
args = []
|
|
825
|
-
while (self.current_token and
|
|
826
|
-
self.current_token.type != TokenType.RPAREN and
|
|
827
|
-
not self._is_at_end()):
|
|
828
|
-
# 参数可为表达式(n 减 1)/标识符/数值
|
|
829
|
-
if self.current_token.type in (TokenType.IDENTIFIER,
|
|
830
|
-
TokenType.NUMBER):
|
|
831
|
-
args.append(self._parse_expression())
|
|
832
|
-
else:
|
|
833
|
-
self._advance()
|
|
834
|
-
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
835
|
-
self._advance()
|
|
836
|
-
return CallExprNode(ident, args, line, col)
|
|
837
|
-
|
|
838
|
-
return IdentifierNode(ident, line, col)
|
|
839
|
-
|
|
840
|
-
def _parse_comparison(self) -> ASTNode:
|
|
841
|
-
"""解析比较表达式"""
|
|
842
|
-
left = self._parse_expression()
|
|
843
|
-
|
|
844
|
-
if self.current_token and self.current_token.type in (
|
|
845
|
-
TokenType.DENGYU, TokenType.DAYU, TokenType.XIAOYU,
|
|
846
|
-
TokenType.WEI, TokenType.BUWEI,
|
|
847
|
-
):
|
|
848
|
-
op_token = self.current_token
|
|
849
|
-
op_map = {
|
|
850
|
-
TokenType.DENGYU: "==",
|
|
851
|
-
TokenType.DAYU: ">",
|
|
852
|
-
TokenType.XIAOYU: "<",
|
|
853
|
-
TokenType.WEI: "==",
|
|
854
|
-
TokenType.BUWEI: "!=",
|
|
855
|
-
}
|
|
856
|
-
op = op_map.get(op_token.type, "==")
|
|
857
|
-
self._advance()
|
|
858
|
-
|
|
859
|
-
right = self._parse_numeric_value()
|
|
860
|
-
if right is None:
|
|
861
|
-
right = self._parse_expression()
|
|
862
|
-
return ComparisonNode(left, op, right, line=op_token.line, column=op_token.column)
|
|
863
|
-
|
|
864
|
-
return left
|
|
865
|
-
|
|
866
|
-
def _parse_expression(self) -> ASTNode:
|
|
867
|
-
"""解析表达式(支持二元算术 + 括号优先:标识符/数值/(表达式) + 运算符 + 右)"""
|
|
868
|
-
if self.current_token is None:
|
|
869
|
-
return LiteralNode("", "string")
|
|
870
|
-
|
|
871
|
-
if self.current_token.type == TokenType.LPAREN:
|
|
872
|
-
# 括号优先: ( 表达式 )
|
|
873
|
-
line = self.current_token.line
|
|
874
|
-
col = self.current_token.column
|
|
875
|
-
self._advance()
|
|
876
|
-
inner = self._parse_expression()
|
|
877
|
-
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
878
|
-
self._advance()
|
|
879
|
-
return self._parse_binary_tail(inner)
|
|
880
|
-
|
|
881
|
-
if self.current_token.type == TokenType.IDENTIFIER:
|
|
882
|
-
# 若后跟 ( → 函数调用;否则合并多词短语
|
|
883
|
-
if (self._peek_next() and
|
|
884
|
-
self._peek_next().type == TokenType.LPAREN):
|
|
885
|
-
name = self.current_token.value
|
|
886
|
-
line = self.current_token.line
|
|
887
|
-
col = self.current_token.column
|
|
888
|
-
self._advance() # 消费函数名
|
|
889
|
-
self._advance() # 消费 (
|
|
890
|
-
args = []
|
|
891
|
-
while (self.current_token and
|
|
892
|
-
self.current_token.type != TokenType.RPAREN and
|
|
893
|
-
not self._is_at_end()):
|
|
894
|
-
if self.current_token.type in (TokenType.IDENTIFIER,
|
|
895
|
-
TokenType.NUMBER):
|
|
896
|
-
args.append(self._parse_expression())
|
|
897
|
-
else:
|
|
898
|
-
self._advance()
|
|
899
|
-
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
900
|
-
self._advance()
|
|
901
|
-
return self._parse_binary_tail(
|
|
902
|
-
CallExprNode(name, args, line, col))
|
|
903
|
-
left = self._merge_identifiers()
|
|
904
|
-
return self._parse_binary_tail(left)
|
|
905
|
-
elif self.current_token.type == TokenType.NUMBER:
|
|
906
|
-
node = LiteralNode(float(self.current_token.value), "number",
|
|
907
|
-
self.current_token.line,
|
|
908
|
-
self.current_token.column)
|
|
909
|
-
self._advance()
|
|
910
|
-
return self._parse_binary_tail(node)
|
|
911
|
-
elif self.current_token.type == TokenType.STRING:
|
|
912
|
-
node = LiteralNode(self.current_token.value, "string",
|
|
913
|
-
self.current_token.line,
|
|
914
|
-
self.current_token.column)
|
|
915
|
-
self._advance()
|
|
916
|
-
return self._parse_binary_tail(node)
|
|
917
|
-
else:
|
|
918
|
-
parts = []
|
|
919
|
-
# 停止条件:标点符号 + 语句开头关键字
|
|
920
|
-
_EXPR_STOP = (
|
|
921
|
-
TokenType.ZE, TokenType.FOUZE,
|
|
922
|
-
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
923
|
-
TokenType.WENYUE, TokenType.DAYUE, TokenType.SHUYUE,
|
|
924
|
-
TokenType.RUO, TokenType.DAO, TokenType.DE,
|
|
925
|
-
TokenType.ZIRAN, TokenType.WUWEI, TokenType.GU,
|
|
926
|
-
TokenType.PIN, TokenType.ROU, TokenType.PU,
|
|
927
|
-
TokenType.ZHI, TokenType.ZHIZU,
|
|
928
|
-
)
|
|
929
|
-
while (self.current_token and
|
|
930
|
-
self.current_token.type not in _EXPR_STOP and
|
|
931
|
-
not self._is_at_end()):
|
|
932
|
-
parts.append(self.current_token.value)
|
|
933
|
-
self._advance()
|
|
934
|
-
return LiteralNode("".join(parts).strip(), "string")
|
|
935
|
-
|
|
936
|
-
def _parse_binary_tail(self, left: ASTNode) -> ASTNode:
|
|
937
|
-
"""解析二元算术尾部:left [+|-|*|/] right(右结合单级,满足循环体自增语义)"""
|
|
938
|
-
if self.current_token and self.current_token.type in (
|
|
939
|
-
TokenType.OP_ADD, TokenType.OP_SUB,
|
|
940
|
-
TokenType.OP_MUL, TokenType.OP_DIV,
|
|
941
|
-
):
|
|
942
|
-
op_token = self.current_token
|
|
943
|
-
op_map = {
|
|
944
|
-
TokenType.OP_ADD: "+", TokenType.OP_SUB: "-",
|
|
945
|
-
TokenType.OP_MUL: "*", TokenType.OP_DIV: "/",
|
|
946
|
-
}
|
|
947
|
-
op = op_map[op_token.type]
|
|
948
|
-
self._advance()
|
|
949
|
-
right = self._parse_expression()
|
|
950
|
-
return BinaryExprNode(left, op, right,
|
|
951
|
-
line=op_token.line, column=op_token.column)
|
|
952
|
-
return left
|
|
953
|
-
|
|
954
|
-
# ---- 辅助方法 ----
|
|
955
|
-
|
|
956
|
-
def _is_at_end(self) -> bool:
|
|
957
|
-
"""指针越界探测:True=已消费全部 token。"""
|
|
958
|
-
return self.pos >= len(self.tokens) or (
|
|
959
|
-
self.current_token is not None and self.current_token.type == TokenType.EOF
|
|
960
|
-
)
|
|
961
|
-
|
|
962
|
-
def _advance(self) -> Optional[Token]:
|
|
963
|
-
"""消费当前 token 并前进一格。"""
|
|
964
|
-
if self.pos < len(self.tokens) - 1:
|
|
965
|
-
self.pos += 1
|
|
966
|
-
self.current_token = self.tokens[self.pos]
|
|
967
|
-
elif self.pos == len(self.tokens) - 1:
|
|
968
|
-
self.pos += 1
|
|
969
|
-
self.current_token = None
|
|
970
|
-
return self.current_token
|
|
971
|
-
|
|
972
|
-
def _peek_next(self) -> Optional[Token]:
|
|
973
|
-
"""查看下一个 token(不消费)"""
|
|
974
|
-
if self.pos < len(self.tokens) - 1:
|
|
975
|
-
return self.tokens[self.pos + 1]
|
|
976
|
-
return None
|
|
977
|
-
|
|
978
|
-
def _match(self, *types: TokenType) -> bool:
|
|
979
|
-
"""类型相符即消费返回 True,否则 False。"""
|
|
980
|
-
if self.current_token and self.current_token.type in types:
|
|
981
|
-
self._advance()
|
|
982
|
-
return True
|
|
983
|
-
return False
|
|
984
|
-
|
|
985
|
-
def _consume(self, token_type: TokenType, message: str) -> Optional[Token]:
|
|
986
|
-
"""消费当前 token:类型相符即前进返回,否则带行列定位报错。"""
|
|
987
|
-
if self.current_token and self.current_token.type == token_type:
|
|
988
|
-
token = self.current_token
|
|
989
|
-
self._advance()
|
|
990
|
-
return token
|
|
991
|
-
else:
|
|
992
|
-
loc = f"L{self.current_token.line}:C{self.current_token.column}" if self.current_token else "EOF"
|
|
993
|
-
self.errors.append(f"{loc} {message},实际得到: '{self.current_token.value if self.current_token else 'EOF'}'")
|
|
994
|
-
return None
|
|
995
|
-
|
|
996
|
-
def _collect_until(self, *stop_types: TokenType) -> List[Token]:
|
|
997
|
-
"""收集 Token 直到遇到停止类型"""
|
|
998
|
-
parts = []
|
|
999
|
-
while (self.current_token and
|
|
1000
|
-
self.current_token.type not in stop_types and
|
|
1001
|
-
self.current_token.type != TokenType.EOF):
|
|
1002
|
-
if self.current_token.type == TokenType.COLON:
|
|
1003
|
-
self._advance()
|
|
1004
|
-
continue
|
|
1005
|
-
parts.append(self.current_token)
|
|
1006
|
-
self._advance()
|
|
1007
|
-
return parts
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
# =============================================================================
|
|
1011
|
-
# 便捷函数
|
|
1012
|
-
# =============================================================================
|
|
1013
|
-
|
|
1014
|
-
def parse_tokens(tokens: List[Token], errors: List[str] = None) -> ProgramNode:
|
|
1015
|
-
"""便捷函数:将 Token 列表解析为 AST"""
|
|
1016
|
-
parser = Parser(tokens, errors or [])
|
|
1017
|
-
return parser.parse()
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
def parse_source(source: str) -> tuple:
|
|
1021
|
-
"""便捷函数:从源代码直接解析为 AST"""
|
|
1022
|
-
from .lexer import tokenize
|
|
1023
|
-
tokens, lex_errors = tokenize(source)
|
|
1024
|
-
ast = parse_tokens(tokens, [])
|
|
1025
|
-
return ast, lex_errors, ast.errors if hasattr(ast, 'errors') else []
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
# =============================================================================
|
|
1029
|
-
# 测试
|
|
1030
|
-
# =============================================================================
|
|
1031
|
-
|
|
1032
|
-
if __name__ == "__main__":
|
|
1033
|
-
test_code = """若条件空间为伴侣,则止情感权重于0.15。
|
|
1034
|
-
道 新信任路径
|
|
1035
|
-
德 累积信任值
|
|
1036
|
-
问曰:如何验证信任?
|
|
1037
|
-
答曰:信任值大于0.7。
|
|
1038
|
-
术曰:1。柔 响应强度;2。自然 恢复默认;3。知足 验证单元。"""
|
|
1039
|
-
|
|
1040
|
-
print("=" * 60)
|
|
1041
|
-
print("语法分析器 v2.0 测试")
|
|
1042
|
-
print("=" * 60)
|
|
1043
|
-
print(f"源代码:\n{test_code}\n")
|
|
1044
|
-
|
|
1045
|
-
from .lexer import tokenize
|
|
1046
|
-
tokens, lex_errors = tokenize(test_code)
|
|
1047
|
-
|
|
1048
|
-
if lex_errors:
|
|
1049
|
-
print("词法错误:")
|
|
1050
|
-
for e in lex_errors:
|
|
1051
|
-
print(f" ❌ {e}")
|
|
1052
|
-
|
|
1053
|
-
parser = Parser(tokens, [])
|
|
1054
|
-
ast = parser.parse()
|
|
1055
|
-
|
|
1056
|
-
print(f"\nAST 根节点:{ast.type.name}")
|
|
1057
|
-
print(f"语句数量:{len(ast.statements)}")
|
|
1058
|
-
print()
|
|
1059
|
-
|
|
1060
|
-
for i, stmt in enumerate(ast.statements):
|
|
1061
|
-
print(f" 语句{i+1}: {stmt.type.name}")
|
|
1062
|
-
if stmt.type == NodeType.SHUYUE:
|
|
1063
|
-
print(f" 问题: {stmt.attributes.get('question', '')}")
|
|
1064
|
-
print(f" 答案: {stmt.attributes.get('answer', '')}")
|
|
1065
|
-
for step in stmt.steps:
|
|
1066
|
-
op_count = len(step.statement.children) if step.statement else 0
|
|
1067
|
-
print(f" 步骤{step.step_num}: {step.statement.type.name if step.statement else 'None'}")
|
|
1068
|
-
elif stmt.type == NodeType.INSTRUCTION_STMT:
|
|
1069
|
-
op_names = []
|
|
1070
|
-
for op in stmt.operands:
|
|
1071
|
-
if hasattr(op, 'name'):
|
|
1072
|
-
op_names.append(f"ID({op.name})")
|
|
1073
|
-
elif hasattr(op, 'literal_value'):
|
|
1074
|
-
op_names.append(f"Lit({op.literal_value})")
|
|
1075
|
-
print(f" 操作数: [{', '.join(op_names)}]")
|
|
1076
|
-
|
|
1077
|
-
if parser.errors:
|
|
1078
|
-
print(f"\n语法错误:")
|
|
1079
|
-
for e in parser.errors:
|
|
1080
|
-
print(f" ❌ {e}")
|
|
1081
|
-
|
|
1082
|
-
print(f"\n总计:{len(parser.errors)} 个语法错误")
|
|
1
|
+
"""
|
|
2
|
+
parser.py · 语法分析器 v2.0
|
|
3
|
+
将 Token 序列构建为抽象语法树(AST)
|
|
4
|
+
支持:条件语句、指令语句、九章算术结构
|
|
5
|
+
|
|
6
|
+
v2.0 变更:
|
|
7
|
+
- 新增 _merge_identifiers:合并紧密相连的 IDENTIFIER/NUMBER 序列
|
|
8
|
+
- 修复"道 新信任路径"被切成多个操作数的问题
|
|
9
|
+
- 支持"止 X 于 Y"结构中的多词 X
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from enum import Enum, auto
|
|
13
|
+
from dataclasses import dataclass, field
|
|
14
|
+
from typing import List, Optional, Union, Any, Tuple
|
|
15
|
+
from .lexer import Token, TokenType
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# =============================================================================
|
|
19
|
+
# AST 节点类型
|
|
20
|
+
# =============================================================================
|
|
21
|
+
|
|
22
|
+
class NodeType(Enum):
|
|
23
|
+
"""AST 节点类型"""
|
|
24
|
+
|
|
25
|
+
# === 顶层 ===
|
|
26
|
+
PROGRAM = auto() # 整个程序
|
|
27
|
+
|
|
28
|
+
# === 九章算术结构 ===
|
|
29
|
+
WENYUE = auto() # 问曰
|
|
30
|
+
DAYUE = auto() # 答曰
|
|
31
|
+
SHUYUE = auto() # 术曰
|
|
32
|
+
STEP = auto() # 步骤(序号.操作)
|
|
33
|
+
|
|
34
|
+
# === 语句 ===
|
|
35
|
+
CONDITION_STMT = auto() # 条件语句(若...则...)
|
|
36
|
+
LOOP_STMT = auto() # 循环语句(当...执行...)
|
|
37
|
+
BLOCK = auto() # 语句块(多条顺序语句)
|
|
38
|
+
FUNC_DEF = auto() # 函数定义(定义 名(参数):语句)
|
|
39
|
+
RETURN_STMT = auto() # 返回语句(返回 表达式)
|
|
40
|
+
ASSIGN_STMT = auto() # 赋值语句
|
|
41
|
+
INSTRUCTION_STMT = auto() # 指令语句(道德经助记符)
|
|
42
|
+
OPERATION_STMT = auto() # 操作语句
|
|
43
|
+
|
|
44
|
+
# === 表达式 ===
|
|
45
|
+
BINARY_EXPR = auto() # 二元表达式
|
|
46
|
+
UNARY_EXPR = auto() # 一元表达式
|
|
47
|
+
IDENTIFIER = auto() # 标识符
|
|
48
|
+
LITERAL = auto() # 字面量
|
|
49
|
+
CALL_EXPR = auto() # 调用表达式
|
|
50
|
+
|
|
51
|
+
# === 条件 ===
|
|
52
|
+
COMPARISON = auto() # 比较表达式
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# =============================================================================
|
|
56
|
+
# AST 节点定义
|
|
57
|
+
# =============================================================================
|
|
58
|
+
|
|
59
|
+
@dataclass
|
|
60
|
+
class ASTNode:
|
|
61
|
+
"""AST 节点基类"""
|
|
62
|
+
type: NodeType
|
|
63
|
+
line: int = 1
|
|
64
|
+
column: int = 1
|
|
65
|
+
children: List['ASTNode'] = field(default_factory=list)
|
|
66
|
+
value: Any = None
|
|
67
|
+
attributes: dict = field(default_factory=dict)
|
|
68
|
+
|
|
69
|
+
def add_child(self, child: 'ASTNode'):
|
|
70
|
+
"""追加子节点(None 直接忽略)。"""
|
|
71
|
+
if child is not None:
|
|
72
|
+
self.children.append(child)
|
|
73
|
+
|
|
74
|
+
def __repr__(self) -> str:
|
|
75
|
+
"""AST 节点调试表示:类型与子节点概览。"""
|
|
76
|
+
return f"ASTNode({self.type.name}, value={self.value!r}, children={len(self.children)})"
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass
|
|
80
|
+
class ProgramNode(ASTNode):
|
|
81
|
+
"""程序根节点"""
|
|
82
|
+
def __init__(self, line: int = 1, column: int = 1):
|
|
83
|
+
"""构造 ProgramNode:绑定本节点的声明字段与子结构。"""
|
|
84
|
+
super().__init__(NodeType.PROGRAM, line, column)
|
|
85
|
+
self.statements: List[ASTNode] = []
|
|
86
|
+
|
|
87
|
+
def add_statement(self, stmt: ASTNode):
|
|
88
|
+
"""追加语句并同时挂入子树。"""
|
|
89
|
+
if stmt is not None:
|
|
90
|
+
self.statements.append(stmt)
|
|
91
|
+
self.add_child(stmt)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass
|
|
95
|
+
class WenyueNode(ASTNode):
|
|
96
|
+
"""问曰节点"""
|
|
97
|
+
def __init__(self, question: str, line: int = 1, column: int = 1):
|
|
98
|
+
"""构造 WenyueNode:绑定本节点的声明字段与子结构。"""
|
|
99
|
+
super().__init__(NodeType.WENYUE, line, column)
|
|
100
|
+
self.question = question
|
|
101
|
+
self.value = question
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass
|
|
105
|
+
class DayueNode(ASTNode):
|
|
106
|
+
"""答曰节点"""
|
|
107
|
+
def __init__(self, answer: str, line: int = 1, column: int = 1):
|
|
108
|
+
"""构造 DayueNode:绑定本节点的声明字段与子结构。"""
|
|
109
|
+
super().__init__(NodeType.DAYUE, line, column)
|
|
110
|
+
self.answer = answer
|
|
111
|
+
self.value = answer
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass
|
|
115
|
+
class ShuyueNode(ASTNode):
|
|
116
|
+
"""术曰节点"""
|
|
117
|
+
def __init__(self, line: int = 1, column: int = 1):
|
|
118
|
+
"""构造 ShuyueNode:绑定本节点的声明字段与子结构。"""
|
|
119
|
+
super().__init__(NodeType.SHUYUE, line, column)
|
|
120
|
+
self.steps: List['StepNode'] = []
|
|
121
|
+
|
|
122
|
+
def add_step(self, step: 'StepNode'):
|
|
123
|
+
"""追加步骤节点并挂入子树。"""
|
|
124
|
+
self.steps.append(step)
|
|
125
|
+
self.add_child(step)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@dataclass
|
|
129
|
+
class StepNode(ASTNode):
|
|
130
|
+
"""步骤节点"""
|
|
131
|
+
def __init__(self, step_num: int, statement: ASTNode, line: int = 1, column: int = 1):
|
|
132
|
+
"""构造 StepNode:绑定本节点的声明字段与子结构。"""
|
|
133
|
+
super().__init__(NodeType.STEP, line, column)
|
|
134
|
+
self.step_num = step_num
|
|
135
|
+
self.statement = statement
|
|
136
|
+
self.add_child(statement)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@dataclass
|
|
140
|
+
class ConditionStmtNode(ASTNode):
|
|
141
|
+
"""条件语句:若 [条件] 则 [操作] [否则 [操作]](body 可为语句列表)"""
|
|
142
|
+
def __init__(self, condition: ASTNode, then_body, else_body=None,
|
|
143
|
+
line: int = 1, column: int = 1):
|
|
144
|
+
"""构造 ConditionStmtNode:绑定本节点的声明字段与子结构。"""
|
|
145
|
+
super().__init__(NodeType.CONDITION_STMT, line, column)
|
|
146
|
+
self.condition = condition
|
|
147
|
+
self.then_body = then_body
|
|
148
|
+
self.else_body = else_body
|
|
149
|
+
self.add_child(condition)
|
|
150
|
+
self.add_child(then_body)
|
|
151
|
+
if else_body is not None:
|
|
152
|
+
self.add_child(else_body)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
@dataclass
|
|
156
|
+
class LoopStmtNode(ASTNode):
|
|
157
|
+
"""循环语句:当 [条件] 执行 [操作](while 语义,body 可为语句列表)"""
|
|
158
|
+
def __init__(self, condition: ASTNode, body, line: int = 1, column: int = 1):
|
|
159
|
+
"""构造 LoopStmtNode:绑定本节点的声明字段与子结构。"""
|
|
160
|
+
super().__init__(NodeType.LOOP_STMT, line, column)
|
|
161
|
+
self.condition = condition
|
|
162
|
+
self.body = body
|
|
163
|
+
self.add_child(condition)
|
|
164
|
+
self.add_child(body)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@dataclass
|
|
168
|
+
class BlockNode(ASTNode):
|
|
169
|
+
"""语句块:多条顺序执行的语句(循环体/条件体多语句支持)"""
|
|
170
|
+
def __init__(self, statements, line: int = 1, column: int = 1):
|
|
171
|
+
"""构造 BlockNode:绑定本节点的声明字段与子结构。"""
|
|
172
|
+
super().__init__(NodeType.BLOCK, line, column)
|
|
173
|
+
self.statements = statements
|
|
174
|
+
for s in statements:
|
|
175
|
+
self.add_child(s)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
@dataclass
|
|
179
|
+
class FuncDefNode(ASTNode):
|
|
180
|
+
"""函数定义:定义 名(参数):语句(body 可为语句列表)"""
|
|
181
|
+
def __init__(self, name: str, params: List[str], body, line: int = 1, column: int = 1):
|
|
182
|
+
"""构造 FuncDefNode:绑定本节点的声明字段与子结构。"""
|
|
183
|
+
super().__init__(NodeType.FUNC_DEF, line, column)
|
|
184
|
+
self.name = name
|
|
185
|
+
self.params = params
|
|
186
|
+
self.body = body
|
|
187
|
+
self.add_child(body)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@dataclass
|
|
191
|
+
class ReturnStmtNode(ASTNode):
|
|
192
|
+
"""返回语句:返回 [表达式]"""
|
|
193
|
+
def __init__(self, value: Optional[ASTNode], line: int = 1, column: int = 1):
|
|
194
|
+
"""构造 ReturnStmtNode:绑定本节点的声明字段与子结构。"""
|
|
195
|
+
super().__init__(NodeType.RETURN_STMT, line, column)
|
|
196
|
+
self.value = value
|
|
197
|
+
if value:
|
|
198
|
+
self.add_child(value)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@dataclass
|
|
202
|
+
class CallExprNode(ASTNode):
|
|
203
|
+
"""函数调用:名(参数1,参数2)"""
|
|
204
|
+
def __init__(self, name: str, args: List[ASTNode], line: int = 1, column: int = 1):
|
|
205
|
+
"""构造 CallExprNode:绑定本节点的声明字段与子结构。"""
|
|
206
|
+
super().__init__(NodeType.CALL_EXPR, line, column)
|
|
207
|
+
self.name = name
|
|
208
|
+
self.args = args
|
|
209
|
+
for a in args:
|
|
210
|
+
self.add_child(a)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
@dataclass
|
|
214
|
+
class InstructionStmtNode(ASTNode):
|
|
215
|
+
"""指令语句:道德经助记符 + 操作数"""
|
|
216
|
+
def __init__(self, instruction: TokenType, operands: List[ASTNode] = None,
|
|
217
|
+
line: int = 1, column: int = 1):
|
|
218
|
+
"""构造 InstructionStmtNode:绑定本节点的声明字段与子结构。"""
|
|
219
|
+
super().__init__(NodeType.INSTRUCTION_STMT, line, column)
|
|
220
|
+
self.instruction = instruction
|
|
221
|
+
self.operands = operands or []
|
|
222
|
+
for op in self.operands:
|
|
223
|
+
self.add_child(op)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@dataclass
|
|
227
|
+
class AssignStmtNode(ASTNode):
|
|
228
|
+
"""赋值语句:标识符 = 值"""
|
|
229
|
+
def __init__(self, target: str, value: ASTNode, line: int = 1, column: int = 1):
|
|
230
|
+
"""构造 AssignStmtNode:绑定本节点的声明字段与子结构。"""
|
|
231
|
+
super().__init__(NodeType.ASSIGN_STMT, line, column)
|
|
232
|
+
self.target = target
|
|
233
|
+
self.value_node = value
|
|
234
|
+
self.add_child(value)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
@dataclass
|
|
238
|
+
class BinaryExprNode(ASTNode):
|
|
239
|
+
"""二元表达式:左 操作符 右"""
|
|
240
|
+
def __init__(self, left: ASTNode, operator: str, right: ASTNode,
|
|
241
|
+
line: int = 1, column: int = 1):
|
|
242
|
+
"""构造 BinaryExprNode:绑定本节点的声明字段与子结构。"""
|
|
243
|
+
super().__init__(NodeType.BINARY_EXPR, line, column)
|
|
244
|
+
self.left = left
|
|
245
|
+
self.operator = operator
|
|
246
|
+
self.right = right
|
|
247
|
+
self.add_child(left)
|
|
248
|
+
self.add_child(right)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
@dataclass
|
|
252
|
+
class ComparisonNode(ASTNode):
|
|
253
|
+
"""比较表达式"""
|
|
254
|
+
def __init__(self, left: ASTNode, op: str, right: ASTNode,
|
|
255
|
+
line: int = 1, column: int = 1):
|
|
256
|
+
"""构造 ComparisonNode:绑定本节点的声明字段与子结构。"""
|
|
257
|
+
super().__init__(NodeType.COMPARISON, line, column)
|
|
258
|
+
self.left = left
|
|
259
|
+
self.op = op
|
|
260
|
+
self.right = right
|
|
261
|
+
self.add_child(left)
|
|
262
|
+
self.add_child(right)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@dataclass
|
|
266
|
+
class IdentifierNode(ASTNode):
|
|
267
|
+
"""标识符"""
|
|
268
|
+
def __init__(self, name: str, line: int = 1, column: int = 1):
|
|
269
|
+
"""构造 IdentifierNode:绑定本节点的声明字段与子结构。"""
|
|
270
|
+
super().__init__(NodeType.IDENTIFIER, line, column)
|
|
271
|
+
self.name = name
|
|
272
|
+
self.value = name
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@dataclass
|
|
276
|
+
class LiteralNode(ASTNode):
|
|
277
|
+
"""字面量"""
|
|
278
|
+
def __init__(self, value: Union[str, float, int], literal_type: str,
|
|
279
|
+
line: int = 1, column: int = 1):
|
|
280
|
+
"""构造 LiteralNode:绑定本节点的声明字段与子结构。"""
|
|
281
|
+
super().__init__(NodeType.LITERAL, line, column)
|
|
282
|
+
self.literal_value = value
|
|
283
|
+
self.literal_type = literal_type # "number", "string"
|
|
284
|
+
self.value = value
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
# =============================================================================
|
|
288
|
+
# 语法分析器
|
|
289
|
+
# =============================================================================
|
|
290
|
+
|
|
291
|
+
class Parser:
|
|
292
|
+
"""
|
|
293
|
+
语法分析器 v2.0
|
|
294
|
+
将 Token 序列构建为 AST
|
|
295
|
+
"""
|
|
296
|
+
|
|
297
|
+
# 指令助记符集合
|
|
298
|
+
INSTRUCTION_TOKENS = {
|
|
299
|
+
TokenType.DAO, TokenType.DE, TokenType.ZIRAN,
|
|
300
|
+
TokenType.WUWEI, TokenType.GU, TokenType.PIN,
|
|
301
|
+
TokenType.ROU, TokenType.PU, TokenType.ZHI, TokenType.ZHIZU,
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
def __init__(self, tokens: List[Token], errors: List[str] = None):
|
|
305
|
+
"""构造 Parser:绑定本节点的声明字段与子结构。"""
|
|
306
|
+
self.tokens = tokens
|
|
307
|
+
self.pos = 0
|
|
308
|
+
self.errors: List[str] = errors or []
|
|
309
|
+
self.current_token = self.tokens[0] if tokens else None
|
|
310
|
+
|
|
311
|
+
def parse(self) -> ProgramNode:
|
|
312
|
+
"""解析整个程序"""
|
|
313
|
+
program = ProgramNode()
|
|
314
|
+
|
|
315
|
+
while not self._is_at_end():
|
|
316
|
+
if self._match(TokenType.NEWLINE, TokenType.SEMICOLON, TokenType.COMMA):
|
|
317
|
+
continue
|
|
318
|
+
|
|
319
|
+
stmt = self._parse_statement()
|
|
320
|
+
if stmt:
|
|
321
|
+
program.add_statement(stmt)
|
|
322
|
+
# DEBUG
|
|
323
|
+
# print(f" [parse] Added: type={stmt.type.name} value={stmt.value!r}")
|
|
324
|
+
# if hasattr(stmt, 'operands'):
|
|
325
|
+
# for j, op in enumerate(stmt.operands):
|
|
326
|
+
# print(f" op{j}: {op.type.name} value={op.value!r}")
|
|
327
|
+
else:
|
|
328
|
+
# print(f" [parse] None at {self.current_token}")
|
|
329
|
+
self._advance()
|
|
330
|
+
|
|
331
|
+
return program
|
|
332
|
+
|
|
333
|
+
# ---- 语句解析 ----
|
|
334
|
+
|
|
335
|
+
def _parse_statement(self) -> Optional[ASTNode]:
|
|
336
|
+
"""解析单个语句"""
|
|
337
|
+
token = self.current_token
|
|
338
|
+
|
|
339
|
+
if token is None or token.type == TokenType.EOF:
|
|
340
|
+
return None
|
|
341
|
+
|
|
342
|
+
# 问曰 → 答曰 → 术曰(九章算术结构)
|
|
343
|
+
if token.type == TokenType.WENYUE:
|
|
344
|
+
return self._parse_wenyue_block()
|
|
345
|
+
|
|
346
|
+
# 术曰 → 术曰块(可以独立出现)
|
|
347
|
+
if token.type == TokenType.SHUYUE:
|
|
348
|
+
return self._parse_shuyue_block()
|
|
349
|
+
|
|
350
|
+
# 若 → 条件语句
|
|
351
|
+
if token.type == TokenType.RUO:
|
|
352
|
+
return self._parse_condition()
|
|
353
|
+
|
|
354
|
+
# 当 → 循环语句(白箱循环语法:当 条件 执行 操作)
|
|
355
|
+
if token.type == TokenType.DANG:
|
|
356
|
+
return self._parse_loop()
|
|
357
|
+
|
|
358
|
+
# 定义 → 函数定义(定义 名(参数):语句)
|
|
359
|
+
if token.type == TokenType.DINGYI:
|
|
360
|
+
return self._parse_func_def()
|
|
361
|
+
|
|
362
|
+
# 返回 → 返回语句(返回 表达式)
|
|
363
|
+
if token.type == TokenType.FANHUI:
|
|
364
|
+
self._advance()
|
|
365
|
+
val = self._parse_expression() if self.current_token else None
|
|
366
|
+
return ReturnStmtNode(val,
|
|
367
|
+
line=token.line, column=token.column)
|
|
368
|
+
|
|
369
|
+
# 道德经助记符 → 指令语句
|
|
370
|
+
if token.type in self.INSTRUCTION_TOKENS:
|
|
371
|
+
return self._parse_instruction()
|
|
372
|
+
|
|
373
|
+
# 标识符 → 可能是赋值
|
|
374
|
+
if token.type == TokenType.IDENTIFIER:
|
|
375
|
+
return self._parse_assign_or_call()
|
|
376
|
+
|
|
377
|
+
# 句号结束
|
|
378
|
+
if token.type == TokenType.PERIOD:
|
|
379
|
+
self._advance()
|
|
380
|
+
return None
|
|
381
|
+
|
|
382
|
+
# 无法识别
|
|
383
|
+
self.errors.append(
|
|
384
|
+
f"L{token.line}:C{token.column} 无法解析的语句开头: '{token.value}' ({token.type.name})"
|
|
385
|
+
)
|
|
386
|
+
self._advance()
|
|
387
|
+
return None
|
|
388
|
+
|
|
389
|
+
def _parse_shuyue_block(self) -> Optional[ASTNode]:
|
|
390
|
+
"""
|
|
391
|
+
解析术曰块(独立形式):术曰:1。... 2。...
|
|
392
|
+
不依赖前面的问曰/答曰
|
|
393
|
+
"""
|
|
394
|
+
self._consume(TokenType.SHUYUE, "期望 '术曰'")
|
|
395
|
+
|
|
396
|
+
shuyue = ShuyueNode(line=self.current_token.line if self.current_token else 1,
|
|
397
|
+
column=self.current_token.column if self.current_token else 1)
|
|
398
|
+
|
|
399
|
+
# 解析步骤序列
|
|
400
|
+
while not self._is_at_end():
|
|
401
|
+
# 跳过分隔符
|
|
402
|
+
while (self.current_token and
|
|
403
|
+
self.current_token.type in (TokenType.SEMICOLON,
|
|
404
|
+
TokenType.COMMA,
|
|
405
|
+
TokenType.COLON)):
|
|
406
|
+
self._advance()
|
|
407
|
+
|
|
408
|
+
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
409
|
+
step_num = int(float(self.current_token.value))
|
|
410
|
+
self._advance()
|
|
411
|
+
|
|
412
|
+
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
413
|
+
self._advance()
|
|
414
|
+
|
|
415
|
+
step_content = self._parse_step_content()
|
|
416
|
+
if step_content:
|
|
417
|
+
shuyue.add_step(StepNode(step_num, step_content,
|
|
418
|
+
line=step_content.line,
|
|
419
|
+
column=step_content.column))
|
|
420
|
+
else:
|
|
421
|
+
break
|
|
422
|
+
|
|
423
|
+
return shuyue
|
|
424
|
+
|
|
425
|
+
def _parse_wenyue_block(self) -> Optional[ASTNode]:
|
|
426
|
+
"""解析 问曰:... 答曰:... 术曰:... 结构"""
|
|
427
|
+
# 问曰
|
|
428
|
+
self._consume(TokenType.WENYUE, "期望 '问曰'")
|
|
429
|
+
question_parts = self._collect_until(TokenType.DAYUE)
|
|
430
|
+
question = "".join(p.value for p in question_parts).strip()
|
|
431
|
+
|
|
432
|
+
# 答曰
|
|
433
|
+
self._consume(TokenType.DAYUE, "期望 '答曰'")
|
|
434
|
+
answer_parts = self._collect_until(TokenType.SHUYUE)
|
|
435
|
+
answer = "".join(p.value for p in answer_parts).strip()
|
|
436
|
+
|
|
437
|
+
# 术曰
|
|
438
|
+
self._consume(TokenType.SHUYUE, "期望 '术曰'")
|
|
439
|
+
|
|
440
|
+
# 用独立的术曰解析逻辑
|
|
441
|
+
shuyue = ShuyueNode(line=self.current_token.line if self.current_token else 1,
|
|
442
|
+
column=self.current_token.column if self.current_token else 1)
|
|
443
|
+
|
|
444
|
+
while not self._is_at_end():
|
|
445
|
+
# 跳过分隔符
|
|
446
|
+
while (self.current_token and
|
|
447
|
+
self.current_token.type in (TokenType.SEMICOLON,
|
|
448
|
+
TokenType.COMMA,
|
|
449
|
+
TokenType.COLON)):
|
|
450
|
+
self._advance()
|
|
451
|
+
|
|
452
|
+
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
453
|
+
step_num = int(float(self.current_token.value))
|
|
454
|
+
self._advance()
|
|
455
|
+
|
|
456
|
+
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
457
|
+
self._advance()
|
|
458
|
+
|
|
459
|
+
step_content = self._parse_step_content()
|
|
460
|
+
if step_content:
|
|
461
|
+
shuyue.add_step(StepNode(step_num, step_content,
|
|
462
|
+
line=step_content.line,
|
|
463
|
+
column=step_content.column))
|
|
464
|
+
else:
|
|
465
|
+
break
|
|
466
|
+
|
|
467
|
+
shuyue.attributes["question"] = question
|
|
468
|
+
shuyue.attributes["answer"] = answer
|
|
469
|
+
|
|
470
|
+
return shuyue
|
|
471
|
+
|
|
472
|
+
def _parse_step_content(self) -> Optional[ASTNode]:
|
|
473
|
+
"""解析步骤内容"""
|
|
474
|
+
if self.current_token and self.current_token.type == TokenType.RUO:
|
|
475
|
+
return self._parse_condition()
|
|
476
|
+
elif self.current_token and self.current_token.type == TokenType.DANG:
|
|
477
|
+
return self._parse_loop()
|
|
478
|
+
elif self.current_token and self.current_token.type == TokenType.DINGYI:
|
|
479
|
+
return self._parse_func_def()
|
|
480
|
+
elif self.current_token and self.current_token.type == TokenType.FANHUI:
|
|
481
|
+
self._advance()
|
|
482
|
+
val = self._parse_expression() if self.current_token else None
|
|
483
|
+
return ReturnStmtNode(val, line=1, column=1)
|
|
484
|
+
elif self.current_token and self.current_token.type in self.INSTRUCTION_TOKENS:
|
|
485
|
+
return self._parse_instruction()
|
|
486
|
+
elif self.current_token and self.current_token.type == TokenType.IDENTIFIER:
|
|
487
|
+
# 可能是单个标识符或合并后的多词短语
|
|
488
|
+
#
|
|
489
|
+
# issue #326-②(2026-10-14):本函数是 compiler/parser.py 的**孪生副本**,
|
|
490
|
+
# 而该处修复(2026-09-14)长期**未回移**——两条链对同一中文源码产出
|
|
491
|
+
# **不同字节码**:本副本原先无条件走 _merge_identifiers()(多词短语
|
|
492
|
+
# 合并),从不探视其后是否紧跟 = 或 (,于是「1。计数 = 0;」只留下裸
|
|
493
|
+
# 标识符 计数、`= 0` 被丢掉,且**不报错**——只降级为一条 warning
|
|
494
|
+
# 「L2 未编译语句类型: IDENTIFIER」,即静默错值(比崩溃更危险)。
|
|
495
|
+
# 实测复现:compiler/ 14 条指令(含 PUSH_CONST 0.0; STORE_NAME 计数),
|
|
496
|
+
# 本副本 12 条且初始化缺失。
|
|
497
|
+
# 回移 lookahead(与 compiler/parser.py 同款):下一个 token 是 = 或 (
|
|
498
|
+
# 则走 _parse_assign_or_call,否则保持原多词合并行为(「道 新信任路径」
|
|
499
|
+
# 等短语不受影响)。双链字节码等价性由守卫
|
|
500
|
+
# md_cg/test_issue326_twin_parser_parity.py 回归守护。
|
|
501
|
+
_nxt = self._peek_next()
|
|
502
|
+
if _nxt is not None and _nxt.type in (TokenType.EQUALS,
|
|
503
|
+
TokenType.LPAREN):
|
|
504
|
+
return self._parse_assign_or_call()
|
|
505
|
+
merged = self._merge_identifiers()
|
|
506
|
+
return merged
|
|
507
|
+
else:
|
|
508
|
+
parts = []
|
|
509
|
+
while (self.current_token and
|
|
510
|
+
self.current_token.type not in (TokenType.NUMBER, TokenType.PERIOD) and
|
|
511
|
+
not self._is_at_end()):
|
|
512
|
+
parts.append(self.current_token.value)
|
|
513
|
+
self._advance()
|
|
514
|
+
if parts:
|
|
515
|
+
return LiteralNode("".join(parts).strip(), "string",
|
|
516
|
+
line=self.current_token.line if self.current_token else 1,
|
|
517
|
+
column=self.current_token.column if self.current_token else 1)
|
|
518
|
+
return None
|
|
519
|
+
|
|
520
|
+
def _parse_condition(self) -> Optional[ASTNode]:
|
|
521
|
+
"""解析条件语句:若 [条件] 则 [操作] [否则 [操作]]"""
|
|
522
|
+
start_line = self.current_token.line if self.current_token else 1
|
|
523
|
+
start_col = self.current_token.column if self.current_token else 1
|
|
524
|
+
|
|
525
|
+
self._consume(TokenType.RUO, "期望 '若'")
|
|
526
|
+
|
|
527
|
+
condition = self._parse_comparison()
|
|
528
|
+
|
|
529
|
+
self._skip_punctuation_before(TokenType.ZE)
|
|
530
|
+
|
|
531
|
+
self._consume(TokenType.ZE, "期望 '则'")
|
|
532
|
+
|
|
533
|
+
then_body = self._parse_single_statement()
|
|
534
|
+
|
|
535
|
+
else_body = None
|
|
536
|
+
# 跳过 then 与 否则 之间的分隔符(逗号/分号等)
|
|
537
|
+
self._skip_punctuation_before(TokenType.FOUZE)
|
|
538
|
+
if self.current_token and self.current_token.type == TokenType.FOUZE:
|
|
539
|
+
self._advance()
|
|
540
|
+
else_body = self._parse_single_statement()
|
|
541
|
+
|
|
542
|
+
return ConditionStmtNode(condition, then_body, else_body, start_line, start_col)
|
|
543
|
+
|
|
544
|
+
def _parse_loop(self) -> Optional[ASTNode]:
|
|
545
|
+
"""解析循环语句:当 [条件] 执行 [操作](while 语义)"""
|
|
546
|
+
start_line = self.current_token.line if self.current_token else 1
|
|
547
|
+
start_col = self.current_token.column if self.current_token else 1
|
|
548
|
+
|
|
549
|
+
self._consume(TokenType.DANG, "期望 '当'")
|
|
550
|
+
|
|
551
|
+
condition = self._parse_comparison()
|
|
552
|
+
|
|
553
|
+
self._skip_punctuation_before(TokenType.ZHIXING)
|
|
554
|
+
|
|
555
|
+
self._consume(TokenType.ZHIXING, "期望 '执行'")
|
|
556
|
+
|
|
557
|
+
body = self._parse_statement_or_block()
|
|
558
|
+
|
|
559
|
+
return LoopStmtNode(condition, body, start_line, start_col)
|
|
560
|
+
|
|
561
|
+
def _parse_func_def(self) -> Optional[ASTNode]:
|
|
562
|
+
"""解析函数定义:定义 名(参数1,参数2):语句
|
|
563
|
+
参数在( )内,逗号分隔;返回 FuncDefNode(body 可为块)"""
|
|
564
|
+
start_line = self.current_token.line if self.current_token else 1
|
|
565
|
+
start_col = self.current_token.column if self.current_token else 1
|
|
566
|
+
|
|
567
|
+
self._consume(TokenType.DINGYI, "期望 '定义'")
|
|
568
|
+
|
|
569
|
+
# 函数名(标识符)
|
|
570
|
+
if not (self.current_token and self.current_token.type == TokenType.IDENTIFIER):
|
|
571
|
+
self.errors.append("定义后期望函数名")
|
|
572
|
+
return None
|
|
573
|
+
name = self.current_token.value
|
|
574
|
+
self._advance()
|
|
575
|
+
|
|
576
|
+
# 参数列表( )
|
|
577
|
+
params = []
|
|
578
|
+
if self.current_token and self.current_token.type == TokenType.LPAREN:
|
|
579
|
+
self._advance()
|
|
580
|
+
while (self.current_token and
|
|
581
|
+
self.current_token.type != TokenType.RPAREN and
|
|
582
|
+
not self._is_at_end()):
|
|
583
|
+
if self.current_token.type == TokenType.IDENTIFIER:
|
|
584
|
+
params.append(self.current_token.value)
|
|
585
|
+
self._advance() # 跳过标识符/逗号/空格
|
|
586
|
+
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
587
|
+
self._advance()
|
|
588
|
+
|
|
589
|
+
# 冒号(可选分隔)
|
|
590
|
+
if self.current_token and self.current_token.type == TokenType.COLON:
|
|
591
|
+
self._advance()
|
|
592
|
+
|
|
593
|
+
body = self._parse_single_statement()
|
|
594
|
+
|
|
595
|
+
return FuncDefNode(name, params, body, start_line, start_col)
|
|
596
|
+
|
|
597
|
+
def _skip_punctuation_before(self, *target_types: TokenType):
|
|
598
|
+
"""跳过标点符号"""
|
|
599
|
+
while self.current_token and self.current_token.type in (
|
|
600
|
+
TokenType.COMMA, TokenType.PERIOD, TokenType.SEMICOLON,
|
|
601
|
+
TokenType.COLON, TokenType.QUESTION, TokenType.EXCLAM,
|
|
602
|
+
):
|
|
603
|
+
self._advance()
|
|
604
|
+
|
|
605
|
+
def _parse_statement_or_block(self) -> Any:
|
|
606
|
+
"""解析语句或块:返回语句列表(单语句=[stmt];分号/句号分隔多条)
|
|
607
|
+
块内多条语句:`则 语句1;语句2;...`(支持循环体/条件体多语句)"""
|
|
608
|
+
stmts = []
|
|
609
|
+
while True:
|
|
610
|
+
stmt = self._parse_single_statement()
|
|
611
|
+
if stmt is not None:
|
|
612
|
+
stmts.append(stmt)
|
|
613
|
+
# 分隔符:分号/句号 → 继续收下一条;否则块结束
|
|
614
|
+
if self.current_token and self.current_token.type in (
|
|
615
|
+
TokenType.SEMICOLON, TokenType.PERIOD, TokenType.COMMA):
|
|
616
|
+
self._advance()
|
|
617
|
+
# 分隔符后若是步骤号/块边界 → 块结束(九章算术步骤边界 1。…2。…)
|
|
618
|
+
if (self.current_token and
|
|
619
|
+
self.current_token.type in (TokenType.NUMBER, TokenType.SHUYUE)):
|
|
620
|
+
break
|
|
621
|
+
continue
|
|
622
|
+
break
|
|
623
|
+
if not stmts:
|
|
624
|
+
return None
|
|
625
|
+
if len(stmts) == 1:
|
|
626
|
+
return stmts[0]
|
|
627
|
+
return BlockNode(stmts, line=stmts[0].line, column=stmts[0].column)
|
|
628
|
+
|
|
629
|
+
def _parse_single_statement(self) -> Optional[ASTNode]:
|
|
630
|
+
"""解析单条语句"""
|
|
631
|
+
if self.current_token and self.current_token.type == TokenType.RUO:
|
|
632
|
+
return self._parse_condition()
|
|
633
|
+
elif self.current_token and self.current_token.type == TokenType.DANG:
|
|
634
|
+
return self._parse_loop()
|
|
635
|
+
elif self.current_token and self.current_token.type == TokenType.DINGYI:
|
|
636
|
+
return self._parse_func_def()
|
|
637
|
+
elif self.current_token and self.current_token.type == TokenType.FANHUI:
|
|
638
|
+
self._advance()
|
|
639
|
+
val = self._parse_expression() if self.current_token else None
|
|
640
|
+
return ReturnStmtNode(val, line=1, column=1)
|
|
641
|
+
elif self.current_token and self.current_token.type in self.INSTRUCTION_TOKENS:
|
|
642
|
+
return self._parse_instruction()
|
|
643
|
+
elif self.current_token and self.current_token.type == TokenType.IDENTIFIER:
|
|
644
|
+
return self._parse_assign_or_call()
|
|
645
|
+
else:
|
|
646
|
+
parts = []
|
|
647
|
+
# 停止条件:标点符号 + 语句开头关键字
|
|
648
|
+
_STOP = (
|
|
649
|
+
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
650
|
+
TokenType.WENYUE, TokenType.DAYUE, TokenType.SHUYUE,
|
|
651
|
+
TokenType.RUO, TokenType.FOUZE, TokenType.DANG,
|
|
652
|
+
TokenType.ZHIXING,
|
|
653
|
+
)
|
|
654
|
+
while (self.current_token and
|
|
655
|
+
self.current_token.type not in _STOP and
|
|
656
|
+
not self._is_at_end()):
|
|
657
|
+
parts.append(self.current_token.value)
|
|
658
|
+
self._advance()
|
|
659
|
+
text = "".join(parts).strip()
|
|
660
|
+
if text:
|
|
661
|
+
return LiteralNode(text, "string")
|
|
662
|
+
return None
|
|
663
|
+
|
|
664
|
+
def _parse_instruction(self) -> Optional[ASTNode]:
|
|
665
|
+
"""
|
|
666
|
+
解析指令语句 v2.0
|
|
667
|
+
|
|
668
|
+
关键改进:使用 _merge_identifiers 合并紧密相连的
|
|
669
|
+
IDENTIFIER/NUMBER 序列为一个逻辑操作数。
|
|
670
|
+
|
|
671
|
+
例:道 新信任路径 → 道 + [ID(新信任路径)]
|
|
672
|
+
例:柔 响应强度 → 柔 + [ID(响应强度)]
|
|
673
|
+
例:止情感权重于0.15 → 止 + [ID(情感权重)] + [Lit(0.15)]
|
|
674
|
+
"""
|
|
675
|
+
instr_token = self.current_token
|
|
676
|
+
instr_type = instr_token.type
|
|
677
|
+
self._advance()
|
|
678
|
+
|
|
679
|
+
operands = []
|
|
680
|
+
|
|
681
|
+
# 停止条件:标点符号 + 语句开头关键字(防止跨语句吞噬)
|
|
682
|
+
_INST_STOP = (
|
|
683
|
+
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
684
|
+
TokenType.EOF,
|
|
685
|
+
# 语句开头关键字 —— 遇到这些说明指令操作数已结束
|
|
686
|
+
TokenType.WENYUE, # 问曰 → 新语句
|
|
687
|
+
TokenType.DAYUE, # 答曰 → 新语句
|
|
688
|
+
TokenType.SHUYUE, # 术曰 → 新语句
|
|
689
|
+
TokenType.RUO, # 若 → 条件语句
|
|
690
|
+
TokenType.FOUZE, # 否则 → 条件语句
|
|
691
|
+
TokenType.DAO, # 道 → 指令(但不在操作数位置)
|
|
692
|
+
TokenType.DE, # 德 → 指令
|
|
693
|
+
TokenType.ZIRAN, # 自然 → 指令
|
|
694
|
+
TokenType.WUWEI, # 无为 → 指令
|
|
695
|
+
TokenType.GU, # 谷 → 指令
|
|
696
|
+
TokenType.PIN, # 牝 → 指令
|
|
697
|
+
TokenType.ROU, # 柔 → 指令
|
|
698
|
+
TokenType.PU, # 朴 → 指令
|
|
699
|
+
TokenType.ZHI, # 止 → 指令
|
|
700
|
+
TokenType.ZHIZU, # 知足 → 指令
|
|
701
|
+
)
|
|
702
|
+
|
|
703
|
+
while (self.current_token and
|
|
704
|
+
self.current_token.type not in _INST_STOP and
|
|
705
|
+
not self._is_at_end()):
|
|
706
|
+
|
|
707
|
+
tok = self.current_token
|
|
708
|
+
|
|
709
|
+
if tok.type == TokenType.YU:
|
|
710
|
+
# "于" → 后面跟数值
|
|
711
|
+
self._advance()
|
|
712
|
+
value = self._parse_numeric_value()
|
|
713
|
+
if value is not None:
|
|
714
|
+
operands.append(value)
|
|
715
|
+
continue
|
|
716
|
+
|
|
717
|
+
elif tok.type == TokenType.IDENTIFIER:
|
|
718
|
+
# 合并后续紧密相连的 IDENTIFIER/NUMBER
|
|
719
|
+
merged = self._merge_identifiers()
|
|
720
|
+
operands.append(merged)
|
|
721
|
+
|
|
722
|
+
elif tok.type == TokenType.NUMBER:
|
|
723
|
+
operands.append(LiteralNode(float(tok.value), "number", tok.line, tok.column))
|
|
724
|
+
self._advance()
|
|
725
|
+
|
|
726
|
+
elif tok.type == TokenType.STRING:
|
|
727
|
+
operands.append(LiteralNode(tok.value, "string", tok.line, tok.column))
|
|
728
|
+
self._advance()
|
|
729
|
+
|
|
730
|
+
else:
|
|
731
|
+
self._advance()
|
|
732
|
+
|
|
733
|
+
return InstructionStmtNode(instr_type, operands,
|
|
734
|
+
line=instr_token.line,
|
|
735
|
+
column=instr_token.column)
|
|
736
|
+
|
|
737
|
+
def _merge_identifiers(self) -> ASTNode:
|
|
738
|
+
"""
|
|
739
|
+
合并从当前位置开始的紧密相连的 IDENTIFIER/NUMBER 序列
|
|
740
|
+
|
|
741
|
+
例:ID(新) ID(信任) ID(路径) → IdentifierNode("新信任路径")
|
|
742
|
+
例:ID(响应) ID(强度) → IdentifierNode("响应强度")
|
|
743
|
+
例:ID(累积) ID(信任值) → IdentifierNode("累积信任值")
|
|
744
|
+
|
|
745
|
+
判断标准:同一行、列号连续
|
|
746
|
+
"""
|
|
747
|
+
start_tok = self.current_token
|
|
748
|
+
parts = [start_tok.value]
|
|
749
|
+
line = start_tok.line
|
|
750
|
+
col = start_tok.column
|
|
751
|
+
self._advance()
|
|
752
|
+
|
|
753
|
+
while (self.current_token and
|
|
754
|
+
self.current_token.line == line and
|
|
755
|
+
self.current_token.type in (TokenType.IDENTIFIER, TokenType.NUMBER)):
|
|
756
|
+
parts.append(self.current_token.value)
|
|
757
|
+
self._advance()
|
|
758
|
+
|
|
759
|
+
merged_name = "".join(parts)
|
|
760
|
+
return IdentifierNode(merged_name, line, col)
|
|
761
|
+
|
|
762
|
+
def _parse_numeric_value(self) -> Optional[ASTNode]:
|
|
763
|
+
"""解析数值(可能跨多个 token)"""
|
|
764
|
+
if self.current_token is None:
|
|
765
|
+
return None
|
|
766
|
+
|
|
767
|
+
if self.current_token.type == TokenType.NUMBER:
|
|
768
|
+
val = float(self.current_token.value)
|
|
769
|
+
line, col = self.current_token.line, self.current_token.column
|
|
770
|
+
self._advance()
|
|
771
|
+
return LiteralNode(val, "number", line, col)
|
|
772
|
+
|
|
773
|
+
if self.current_token.type == TokenType.IDENTIFIER:
|
|
774
|
+
prefix = self.current_token.value
|
|
775
|
+
line, col = self.current_token.line, self.current_token.column
|
|
776
|
+
self._advance()
|
|
777
|
+
|
|
778
|
+
if self.current_token and self.current_token.type == TokenType.PERIOD:
|
|
779
|
+
self._advance()
|
|
780
|
+
if self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
781
|
+
val = float(prefix + "." + self.current_token.value)
|
|
782
|
+
self._advance()
|
|
783
|
+
return LiteralNode(val, "number", line, col)
|
|
784
|
+
else:
|
|
785
|
+
try:
|
|
786
|
+
return LiteralNode(float(prefix), "number", line, col)
|
|
787
|
+
except ValueError:
|
|
788
|
+
return IdentifierNode(prefix, line, col)
|
|
789
|
+
elif self.current_token and self.current_token.type == TokenType.NUMBER:
|
|
790
|
+
val = float(prefix + self.current_token.value)
|
|
791
|
+
self._advance()
|
|
792
|
+
return LiteralNode(val, "number", line, col)
|
|
793
|
+
else:
|
|
794
|
+
return IdentifierNode(prefix, line, col)
|
|
795
|
+
|
|
796
|
+
return None
|
|
797
|
+
|
|
798
|
+
def _parse_assign_or_call(self) -> Optional[ASTNode]:
|
|
799
|
+
"""解析赋值或调用
|
|
800
|
+
|
|
801
|
+
若后面紧跟 = 或 = → 赋值语句
|
|
802
|
+
若后面紧跟 ( → 函数调用表达式(名(参数))
|
|
803
|
+
否则 → 标识符表达式
|
|
804
|
+
"""
|
|
805
|
+
ident = self.current_token.value
|
|
806
|
+
line = self.current_token.line
|
|
807
|
+
col = self.current_token.column
|
|
808
|
+
self._advance()
|
|
809
|
+
|
|
810
|
+
# 检查是否是赋值
|
|
811
|
+
if self.current_token and self.current_token.type == TokenType.EQUALS:
|
|
812
|
+
self._advance() # 跳过 =
|
|
813
|
+
value_node = self._parse_expression()
|
|
814
|
+
return AssignStmtNode(
|
|
815
|
+
target=ident,
|
|
816
|
+
value=value_node,
|
|
817
|
+
line=line,
|
|
818
|
+
column=col
|
|
819
|
+
)
|
|
820
|
+
|
|
821
|
+
# 函数调用:名(参数1,参数2)
|
|
822
|
+
if self.current_token and self.current_token.type == TokenType.LPAREN:
|
|
823
|
+
self._advance()
|
|
824
|
+
args = []
|
|
825
|
+
while (self.current_token and
|
|
826
|
+
self.current_token.type != TokenType.RPAREN and
|
|
827
|
+
not self._is_at_end()):
|
|
828
|
+
# 参数可为表达式(n 减 1)/标识符/数值
|
|
829
|
+
if self.current_token.type in (TokenType.IDENTIFIER,
|
|
830
|
+
TokenType.NUMBER):
|
|
831
|
+
args.append(self._parse_expression())
|
|
832
|
+
else:
|
|
833
|
+
self._advance()
|
|
834
|
+
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
835
|
+
self._advance()
|
|
836
|
+
return CallExprNode(ident, args, line, col)
|
|
837
|
+
|
|
838
|
+
return IdentifierNode(ident, line, col)
|
|
839
|
+
|
|
840
|
+
def _parse_comparison(self) -> ASTNode:
|
|
841
|
+
"""解析比较表达式"""
|
|
842
|
+
left = self._parse_expression()
|
|
843
|
+
|
|
844
|
+
if self.current_token and self.current_token.type in (
|
|
845
|
+
TokenType.DENGYU, TokenType.DAYU, TokenType.XIAOYU,
|
|
846
|
+
TokenType.WEI, TokenType.BUWEI,
|
|
847
|
+
):
|
|
848
|
+
op_token = self.current_token
|
|
849
|
+
op_map = {
|
|
850
|
+
TokenType.DENGYU: "==",
|
|
851
|
+
TokenType.DAYU: ">",
|
|
852
|
+
TokenType.XIAOYU: "<",
|
|
853
|
+
TokenType.WEI: "==",
|
|
854
|
+
TokenType.BUWEI: "!=",
|
|
855
|
+
}
|
|
856
|
+
op = op_map.get(op_token.type, "==")
|
|
857
|
+
self._advance()
|
|
858
|
+
|
|
859
|
+
right = self._parse_numeric_value()
|
|
860
|
+
if right is None:
|
|
861
|
+
right = self._parse_expression()
|
|
862
|
+
return ComparisonNode(left, op, right, line=op_token.line, column=op_token.column)
|
|
863
|
+
|
|
864
|
+
return left
|
|
865
|
+
|
|
866
|
+
def _parse_expression(self) -> ASTNode:
|
|
867
|
+
"""解析表达式(支持二元算术 + 括号优先:标识符/数值/(表达式) + 运算符 + 右)"""
|
|
868
|
+
if self.current_token is None:
|
|
869
|
+
return LiteralNode("", "string")
|
|
870
|
+
|
|
871
|
+
if self.current_token.type == TokenType.LPAREN:
|
|
872
|
+
# 括号优先: ( 表达式 )
|
|
873
|
+
line = self.current_token.line
|
|
874
|
+
col = self.current_token.column
|
|
875
|
+
self._advance()
|
|
876
|
+
inner = self._parse_expression()
|
|
877
|
+
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
878
|
+
self._advance()
|
|
879
|
+
return self._parse_binary_tail(inner)
|
|
880
|
+
|
|
881
|
+
if self.current_token.type == TokenType.IDENTIFIER:
|
|
882
|
+
# 若后跟 ( → 函数调用;否则合并多词短语
|
|
883
|
+
if (self._peek_next() and
|
|
884
|
+
self._peek_next().type == TokenType.LPAREN):
|
|
885
|
+
name = self.current_token.value
|
|
886
|
+
line = self.current_token.line
|
|
887
|
+
col = self.current_token.column
|
|
888
|
+
self._advance() # 消费函数名
|
|
889
|
+
self._advance() # 消费 (
|
|
890
|
+
args = []
|
|
891
|
+
while (self.current_token and
|
|
892
|
+
self.current_token.type != TokenType.RPAREN and
|
|
893
|
+
not self._is_at_end()):
|
|
894
|
+
if self.current_token.type in (TokenType.IDENTIFIER,
|
|
895
|
+
TokenType.NUMBER):
|
|
896
|
+
args.append(self._parse_expression())
|
|
897
|
+
else:
|
|
898
|
+
self._advance()
|
|
899
|
+
if self.current_token and self.current_token.type == TokenType.RPAREN:
|
|
900
|
+
self._advance()
|
|
901
|
+
return self._parse_binary_tail(
|
|
902
|
+
CallExprNode(name, args, line, col))
|
|
903
|
+
left = self._merge_identifiers()
|
|
904
|
+
return self._parse_binary_tail(left)
|
|
905
|
+
elif self.current_token.type == TokenType.NUMBER:
|
|
906
|
+
node = LiteralNode(float(self.current_token.value), "number",
|
|
907
|
+
self.current_token.line,
|
|
908
|
+
self.current_token.column)
|
|
909
|
+
self._advance()
|
|
910
|
+
return self._parse_binary_tail(node)
|
|
911
|
+
elif self.current_token.type == TokenType.STRING:
|
|
912
|
+
node = LiteralNode(self.current_token.value, "string",
|
|
913
|
+
self.current_token.line,
|
|
914
|
+
self.current_token.column)
|
|
915
|
+
self._advance()
|
|
916
|
+
return self._parse_binary_tail(node)
|
|
917
|
+
else:
|
|
918
|
+
parts = []
|
|
919
|
+
# 停止条件:标点符号 + 语句开头关键字
|
|
920
|
+
_EXPR_STOP = (
|
|
921
|
+
TokenType.ZE, TokenType.FOUZE,
|
|
922
|
+
TokenType.PERIOD, TokenType.COMMA, TokenType.SEMICOLON,
|
|
923
|
+
TokenType.WENYUE, TokenType.DAYUE, TokenType.SHUYUE,
|
|
924
|
+
TokenType.RUO, TokenType.DAO, TokenType.DE,
|
|
925
|
+
TokenType.ZIRAN, TokenType.WUWEI, TokenType.GU,
|
|
926
|
+
TokenType.PIN, TokenType.ROU, TokenType.PU,
|
|
927
|
+
TokenType.ZHI, TokenType.ZHIZU,
|
|
928
|
+
)
|
|
929
|
+
while (self.current_token and
|
|
930
|
+
self.current_token.type not in _EXPR_STOP and
|
|
931
|
+
not self._is_at_end()):
|
|
932
|
+
parts.append(self.current_token.value)
|
|
933
|
+
self._advance()
|
|
934
|
+
return LiteralNode("".join(parts).strip(), "string")
|
|
935
|
+
|
|
936
|
+
def _parse_binary_tail(self, left: ASTNode) -> ASTNode:
|
|
937
|
+
"""解析二元算术尾部:left [+|-|*|/] right(右结合单级,满足循环体自增语义)"""
|
|
938
|
+
if self.current_token and self.current_token.type in (
|
|
939
|
+
TokenType.OP_ADD, TokenType.OP_SUB,
|
|
940
|
+
TokenType.OP_MUL, TokenType.OP_DIV,
|
|
941
|
+
):
|
|
942
|
+
op_token = self.current_token
|
|
943
|
+
op_map = {
|
|
944
|
+
TokenType.OP_ADD: "+", TokenType.OP_SUB: "-",
|
|
945
|
+
TokenType.OP_MUL: "*", TokenType.OP_DIV: "/",
|
|
946
|
+
}
|
|
947
|
+
op = op_map[op_token.type]
|
|
948
|
+
self._advance()
|
|
949
|
+
right = self._parse_expression()
|
|
950
|
+
return BinaryExprNode(left, op, right,
|
|
951
|
+
line=op_token.line, column=op_token.column)
|
|
952
|
+
return left
|
|
953
|
+
|
|
954
|
+
# ---- 辅助方法 ----
|
|
955
|
+
|
|
956
|
+
def _is_at_end(self) -> bool:
|
|
957
|
+
"""指针越界探测:True=已消费全部 token。"""
|
|
958
|
+
return self.pos >= len(self.tokens) or (
|
|
959
|
+
self.current_token is not None and self.current_token.type == TokenType.EOF
|
|
960
|
+
)
|
|
961
|
+
|
|
962
|
+
def _advance(self) -> Optional[Token]:
|
|
963
|
+
"""消费当前 token 并前进一格。"""
|
|
964
|
+
if self.pos < len(self.tokens) - 1:
|
|
965
|
+
self.pos += 1
|
|
966
|
+
self.current_token = self.tokens[self.pos]
|
|
967
|
+
elif self.pos == len(self.tokens) - 1:
|
|
968
|
+
self.pos += 1
|
|
969
|
+
self.current_token = None
|
|
970
|
+
return self.current_token
|
|
971
|
+
|
|
972
|
+
def _peek_next(self) -> Optional[Token]:
|
|
973
|
+
"""查看下一个 token(不消费)"""
|
|
974
|
+
if self.pos < len(self.tokens) - 1:
|
|
975
|
+
return self.tokens[self.pos + 1]
|
|
976
|
+
return None
|
|
977
|
+
|
|
978
|
+
def _match(self, *types: TokenType) -> bool:
|
|
979
|
+
"""类型相符即消费返回 True,否则 False。"""
|
|
980
|
+
if self.current_token and self.current_token.type in types:
|
|
981
|
+
self._advance()
|
|
982
|
+
return True
|
|
983
|
+
return False
|
|
984
|
+
|
|
985
|
+
def _consume(self, token_type: TokenType, message: str) -> Optional[Token]:
|
|
986
|
+
"""消费当前 token:类型相符即前进返回,否则带行列定位报错。"""
|
|
987
|
+
if self.current_token and self.current_token.type == token_type:
|
|
988
|
+
token = self.current_token
|
|
989
|
+
self._advance()
|
|
990
|
+
return token
|
|
991
|
+
else:
|
|
992
|
+
loc = f"L{self.current_token.line}:C{self.current_token.column}" if self.current_token else "EOF"
|
|
993
|
+
self.errors.append(f"{loc} {message},实际得到: '{self.current_token.value if self.current_token else 'EOF'}'")
|
|
994
|
+
return None
|
|
995
|
+
|
|
996
|
+
def _collect_until(self, *stop_types: TokenType) -> List[Token]:
|
|
997
|
+
"""收集 Token 直到遇到停止类型"""
|
|
998
|
+
parts = []
|
|
999
|
+
while (self.current_token and
|
|
1000
|
+
self.current_token.type not in stop_types and
|
|
1001
|
+
self.current_token.type != TokenType.EOF):
|
|
1002
|
+
if self.current_token.type == TokenType.COLON:
|
|
1003
|
+
self._advance()
|
|
1004
|
+
continue
|
|
1005
|
+
parts.append(self.current_token)
|
|
1006
|
+
self._advance()
|
|
1007
|
+
return parts
|
|
1008
|
+
|
|
1009
|
+
|
|
1010
|
+
# =============================================================================
|
|
1011
|
+
# 便捷函数
|
|
1012
|
+
# =============================================================================
|
|
1013
|
+
|
|
1014
|
+
def parse_tokens(tokens: List[Token], errors: List[str] = None) -> ProgramNode:
|
|
1015
|
+
"""便捷函数:将 Token 列表解析为 AST"""
|
|
1016
|
+
parser = Parser(tokens, errors or [])
|
|
1017
|
+
return parser.parse()
|
|
1018
|
+
|
|
1019
|
+
|
|
1020
|
+
def parse_source(source: str) -> tuple:
|
|
1021
|
+
"""便捷函数:从源代码直接解析为 AST"""
|
|
1022
|
+
from .lexer import tokenize
|
|
1023
|
+
tokens, lex_errors = tokenize(source)
|
|
1024
|
+
ast = parse_tokens(tokens, [])
|
|
1025
|
+
return ast, lex_errors, ast.errors if hasattr(ast, 'errors') else []
|
|
1026
|
+
|
|
1027
|
+
|
|
1028
|
+
# =============================================================================
|
|
1029
|
+
# 测试
|
|
1030
|
+
# =============================================================================
|
|
1031
|
+
|
|
1032
|
+
if __name__ == "__main__":
|
|
1033
|
+
test_code = """若条件空间为伴侣,则止情感权重于0.15。
|
|
1034
|
+
道 新信任路径
|
|
1035
|
+
德 累积信任值
|
|
1036
|
+
问曰:如何验证信任?
|
|
1037
|
+
答曰:信任值大于0.7。
|
|
1038
|
+
术曰:1。柔 响应强度;2。自然 恢复默认;3。知足 验证单元。"""
|
|
1039
|
+
|
|
1040
|
+
print("=" * 60)
|
|
1041
|
+
print("语法分析器 v2.0 测试")
|
|
1042
|
+
print("=" * 60)
|
|
1043
|
+
print(f"源代码:\n{test_code}\n")
|
|
1044
|
+
|
|
1045
|
+
from .lexer import tokenize
|
|
1046
|
+
tokens, lex_errors = tokenize(test_code)
|
|
1047
|
+
|
|
1048
|
+
if lex_errors:
|
|
1049
|
+
print("词法错误:")
|
|
1050
|
+
for e in lex_errors:
|
|
1051
|
+
print(f" ❌ {e}")
|
|
1052
|
+
|
|
1053
|
+
parser = Parser(tokens, [])
|
|
1054
|
+
ast = parser.parse()
|
|
1055
|
+
|
|
1056
|
+
print(f"\nAST 根节点:{ast.type.name}")
|
|
1057
|
+
print(f"语句数量:{len(ast.statements)}")
|
|
1058
|
+
print()
|
|
1059
|
+
|
|
1060
|
+
for i, stmt in enumerate(ast.statements):
|
|
1061
|
+
print(f" 语句{i+1}: {stmt.type.name}")
|
|
1062
|
+
if stmt.type == NodeType.SHUYUE:
|
|
1063
|
+
print(f" 问题: {stmt.attributes.get('question', '')}")
|
|
1064
|
+
print(f" 答案: {stmt.attributes.get('answer', '')}")
|
|
1065
|
+
for step in stmt.steps:
|
|
1066
|
+
op_count = len(step.statement.children) if step.statement else 0
|
|
1067
|
+
print(f" 步骤{step.step_num}: {step.statement.type.name if step.statement else 'None'}")
|
|
1068
|
+
elif stmt.type == NodeType.INSTRUCTION_STMT:
|
|
1069
|
+
op_names = []
|
|
1070
|
+
for op in stmt.operands:
|
|
1071
|
+
if hasattr(op, 'name'):
|
|
1072
|
+
op_names.append(f"ID({op.name})")
|
|
1073
|
+
elif hasattr(op, 'literal_value'):
|
|
1074
|
+
op_names.append(f"Lit({op.literal_value})")
|
|
1075
|
+
print(f" 操作数: [{', '.join(op_names)}]")
|
|
1076
|
+
|
|
1077
|
+
if parser.errors:
|
|
1078
|
+
print(f"\n语法错误:")
|
|
1079
|
+
for e in parser.errors:
|
|
1080
|
+
print(f" ❌ {e}")
|
|
1081
|
+
|
|
1082
|
+
print(f"\n总计:{len(parser.errors)} 个语法错误")
|