{"id":3720,"date":"2026-01-12T12:02:46","date_gmt":"2026-01-12T04:02:46","guid":{"rendered":"https:\/\/ufqi.com\/blog\/?p=3720"},"modified":"2026-01-12T12:06:50","modified_gmt":"2026-01-12T04:06:50","slug":"hanjst-ast-engine-refactor","status":"publish","type":"post","link":"https:\/\/ufqi.com\/blog\/hanjst-ast-engine-refactor\/","title":{"rendered":"Hanjst \u6c49\u5409\u65af\u7279: \u6b63\u5219\u9a71\u52a8\u5bb9\u9519\u578b\u62fc\u63a5\u5f0f\u6a21\u677f\u5f15\u64ce"},"content":{"rendered":"\n<p>2026\u65b0\u5e74\u4f0a\u59cb\uff0c\u7531\u4e8e <a rel=\"noreferrer noopener\" href=\"https:\/\/ufqi.com\/?mod=finance\" target=\"_blank\">UfqiFina \u6709\u798f\u91d1\u878d <\/a> ( https:\/\/ufqi.com\/finance ) \u6709\u4e9b\u529f\u80fd\u4f9d\u8d56  Hanjst \u6c49\u5409\u65af\u7279 \u6a21\u677f\u8bed\u8a00\u5448\u73b0\uff0c\u6211\u4eec\u57282026\u5e74\u5143\u65e6\u521a\u8fc7\u53bb\u7684\u8fd9\u4e2a\u5468\u672b\u5bf9 Hanjst \u5c1d\u8bd5\u8fdb\u884c\u91cd\u5199\u3002<br \/>\u5728\u8fd9\u4e2a\u5c1d\u8bd5\u91cd\u5199\/\u91cd\u6784\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 OpenAI \u7684 ChatGPT \u8fdb\u884c\u534f\u52a9\u3002<\/p>\n\n\n\n<p>\u5148\u8bf4\u7ed3\u679c\uff0c\u5728\u4f7f\u7528 ChatGPT \u5c1d\u8bd5\u8fdb\u884c\u91cd\u5199\/\u91cd\u6784 Hanjst \u65f6\uff0c\u4efb\u52a1\u5931\u8d25\u4e86\uff0c\u8d77\u521d\u5b83\u96c4\u5fc3\u58ee\u5fd7\u5730\u5c06 Hanjst \u62c6\u89e3\u5730\u975e\u5e38\u8f7b\u91cf\u5316\uff0c\u5141\u8bfa\u4e86\u6027\u80fd\u5927\u6982\u6709 3x \u7684\u63d0\u5347\u3002<br \/>\u53ef\u4ee5\u968f\u7740\u65b0\u4ee3\u7801\u7684\u5b9e\u73b0\u53ca\u843d\u5730\u6d4b\u8bd5\uff0cChatGPT \u5728\u591a\u8f6e\u6d4b\u8bd5\u4ecd\u65e0\u6cd5\u901a\u8fc7\u7684\u60c5\u51b5\u4e0b\uff0c\u4e5f\u662f\u5728\u6df1\u5165\u7406\u89e3\u4e86 Hanjst \u7684\u6df1\u5c42\u903b\u8f91\u4e4b\u540e\uff0c\u5b83\u5bf9\u6211\u63d0\u51fa\u7684\u662f\u5426\u653e\u5f03\u91cd\u5199\/\u91cd\u6784\u5efa\u8bae\uff0c\u6df1\u8868\u8d5e\u540c\uff0c\u5e76\u5bf9 Hanjst \u8fdb\u884c\u4e86\u786e\u5207\u5730\u5b9a\u4e49:<\/p>\n\n\n\n<p><strong>Hanjst \u662f\u201c\u6b63\u5219\u9a71\u52a8 + \u5bb9\u9519\u578b\u62fc\u63a5\u5f15\u64ce\u201d<\/strong>, \u800c\u4e0d\u662f <strong>AST ( Abstract Syntax Tree, \u62bd\u8c61\u8bed\u6cd5\u6811 ) \/ \u7f16\u8bd1\u5668\u3002<\/strong><\/p>\n\n\n\n<p>\u4e8e\u662f\uff0c\u6211\u8bf7 ChatGPT \u6309\u7167\u80fd\u8dd1\u80fd\u7528\/\u5de5\u7a0b\u7ea7\u7684\u8bbe\u8ba1\u601d\u8def\uff0c\u4ece\u96f6\u91cd\u65b0\u89c4\u5212\u8bbe\u8ba1 Hanjst \u4f1a\u662f\u600e\u6837\u7684? \u540c\u65f6\uff0c\u4e5f\u501f\u8fd9\u4e2a\u673a\u4f1a\u5c55\u73b0\u5b9e\u9645\u80fd\u7528\/\u8fd0\u884c\u4e2d\u7684 Hanjst \u8ddd\u79bb\u6bbf\u5802\u7ea7\u5b66\u9662\u6d3e\u7684\u7406\u60f3\u7684\u5b8c\u7f8e\u7684 Hanjst \u6709\u591a\u5c11\u5dee\u8ddd\uff0c\u8fd9\u4e9b\u5dee\u5f02\u4f53\u73b0\u5728\u54ea\u4e9b\u5730\u65b9\u3002\u540c\u6837\u5730\uff0c\u6211\u4eec\u4e5f\u662f\u5148\u8bf4\u7ed3\u8bba:<\/p>\n\n\n\n<p><strong>\u5982\u679c\u6309 AST \/ \u7f16\u8bd1\u5668\u601d\u8def\u8bbe\u8ba1\uff0cHanjst \u4f1a\u53d8\u6210\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u4e00\u4e2a <strong>\u201c\u6a21\u677f\u8bed\u8a00 \u2192 AST \u2192 JS \u51fd\u6570\u201d<\/strong> \u7684\u7f16\u8bd1\u5668\uff0c<\/li><li><code>{}<\/code> \u4e0d\u518d\u9760\u6b63\u5219\uff0c\u800c\u662f\u9760<strong>\u8bcd\u6cd5 + \u8bed\u6cd5\u89c4\u5219<\/strong>\uff0c<\/li><\/ul>\n\n\n\n<p>\u4f46\u4ee3\u4ef7\u662f\uff1a<strong>\u5b9e\u73b0\u590d\u6742\u5ea6 \u00d75\uff0c\u4ee3\u7801\u91cf \u00d73\uff0c\u8c03\u8bd5\u6210\u672c \u00d710<\/strong> \u3002<\/p>\n\n\n\n<h2>A1. \u539f\u7248 Hanjst \u7684\u672c\u8d28\u662f\uff1a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>HTML\n \u2514\u2500 \u6b63\u5219\u626b\u63cf\n     \u2514\u2500 \u5b57\u7b26\u4e32\u62fc\u63a5 JS\n         \u2514\u2500 new Function()\n<\/code><\/pre>\n\n\n\n<p>\u5b83\u7684\u8bbe\u8ba1\u54f2\u5b66\u662f\uff1a<strong>\u201c\u80fd\u8dd1\u5c31\u884c\uff0cJS \u81ea\u5df1\u515c\u5e95\u201d<\/strong>, \u8fd9\u5728 2015 \u5e74\u5de6\u53f3\u662f<strong>\u5b8c\u5168\u6b63\u786e\u7684\u8bbe\u8ba1<\/strong>\u3002<\/p>\n\n\n\n<p>\u539f\u7248 Hanjst \u5728\u91cd\u6784\/\u91cd\u5199\u8fc7\u7a0b\u4e2d\u5fc5\u7136\u9047\u5230\u7684\u95ee\u9898\uff0c\u4e0d\u53ef\u56de\u907f\u4e14\u65e0\u6cd5\u59a5\u5584\u89e3\u51b3\uff1a<\/p>\n\n\n\n<h3>1\ufe0f\u20e3 <code>{}<\/code> \u5355\u5206\u9694\u7b26\u662f\u201c\u8bed\u6cd5\u5c42\u707e\u96be\u201d<\/h3>\n\n\n\n<ul><li>HTML \/ CSS \/ JS \u90fd\u5929\u7136\u7528 <code>{<\/code> \uff0c\u539f\u7248\u9760 <strong>\u201c\u5173\u952e\u8bcd\u524d\u7f00 + \u5bbd\u677e\u5339\u914d\u201d<\/strong> \u82df\u6d3b\u3002<\/li><\/ul>\n\n\n\n<h3>2\ufe0f\u20e3 tpl2js \u2260 AST<\/h3>\n\n\n\n<ul><li>\u5b83\u662f <strong>\u5b57\u7b26\u4e32\u542f\u53d1\u5f0f\u7ffb\u8bd1<\/strong> \uff0c\u4efb\u4f55\u201c\u8bed\u4e49\u5316\u91cd\u6784\u201d\u90fd\u4f1a\u7834\u574f\u9690\u542b\u5047\u8bbe\u3002<\/li><\/ul>\n\n\n\n<h3>3\ufe0f\u20e3 Hanjst \u7684\u7a33\u5b9a\u6027\u6765\u81ea\uff1a<strong>Bug \u00d7 \u65f6\u95f4 \u00d7 \u517c\u5bb9\u6027\uff0c<\/strong><\/h3>\n\n\n\n<p>Hanjst \u7684\u80fd\u7528\u80fd\u8dd1\uff0c\u4f9d\u9760\u7684\u5185\u6838\u4e0d\u662f\u6765\u81ea\u8bbe\u8ba1\u3002<br \/>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u5bf9 Hanjst \u6309 \u62bd\u8c61\u8bed\u6cd5\u6811 \u7f16\u8bd1\u5668 \u7684\u6807\u51c6\u4ece\u96f6\u5f00\u59cb\u8fdb\u884c\u91cd\u6784\u3002\u5c3d\u7ba1\u4ed8\u51fa\u7684\u4ee3\u4ef7\u662f\u5b83\u5c06\u53d8\u5f97\u4f53\u79ef\u66f4\u5927\u66f4\u81c3\u80bf\u7ed3\u6784\u66f4\u590d\u6742\u8fd0\u884c\u66f4\u6162\uff0c\u4f46\u662f\uff0c\u65b0\u7248\u7684 Hanjst \u5c06\u662f \u6bbf\u5802\u7ea7\u5b66\u9662\u6d3e\u7684\u6c34\u5e73!<\/p>\n\n\n\n<h2>B2. \u62bd\u8c61\u8bed\u6cd5\u6811 AST \u7248\u672c\u7684 Hanjst\uff1a\u6574\u4f53\u67b6\u6784\u56fe<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Template String\n   \u2502\n   \u25bc\n&#091; Lexer ]\n   \u2502  Token\n   \u25bc\n&#091; Parser ]\n   \u2502  AST\n   \u25bc\n&#091; Code Generator ]\n   \u2502  JS Source\n   \u25bc\nnew Function(data) \u2192 HTML<\/code><\/pre>\n\n\n\n<h3>1\ufe0f\u20e3 Lexer\uff08\u8bcd\u6cd5\u5206\u6790\u5668\uff09<\/h3>\n\n\n\n<p><strong>\u76ee\u6807\uff1a<\/strong>\u628a\u6a21\u677f\u62c6\u6210<strong>\u6700\u5c0f\u8bed\u4e49\u5355\u5143<\/strong>\uff0cToken \u7c7b\u578b\uff08\u793a\u4f8b\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TEXT            \"&lt;div&gt;\"\nOPEN_TAG        \"{\"\nCLOSE_TAG       \"}\"\nIDENTIFIER      \"if\"\nVARIABLE        \"$user\"\nSTRING          \"'abc'\"\nNUMBER          123\nOPERATOR        \"&gt;\", \"==\"\nKEYWORD         if \/ else \/ for \/ foreach \/ while<\/code><\/pre>\n\n\n\n<h3>2\ufe0f\u20e3 Parser\uff08\u8bed\u6cd5\u5206\u6790\u5668\uff09<\/h3>\n\n\n\n<p>Parser \u4e0d\u518d\u5173\u5fc3\u5b57\u7b26\u4e32\uff0c\u800c\u662f<strong>\u8bed\u6cd5\u7ed3\u6784<\/strong>\uff0cAST \u8282\u70b9\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IfNode {\n  test: BinaryExpression,\n  consequent: BlockNode,\n  alternate: BlockNode\n}\n<\/code><\/pre>\n\n\n\n<h4><code>{if}<\/code> \u7684 AST \u8868\u8fbe<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>{if $user.age &gt; 18}\n  adult\n{else}\n  child\n{\/if}\n<\/code><\/pre>\n\n\n\n<p>\u27a1 AST\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"type\": \"If\",\n  \"test\": {\n    \"type\": \"Binary\",\n    \"left\": \"$user.age\",\n    \"op\": \"&gt;\",\n    \"right\": 18\n  },\n  \"then\": &#091;...],\n  \"else\": &#091;...]\n}<\/code><\/pre>\n\n\n\n<h3>3\ufe0f\u20e3 HTML \u662f AST \u7684\u4e00\u7b49\u516c\u6c11\uff08\u5173\u952e\u70b9\uff09<\/h3>\n\n\n\n<p>\u5728 AST \u6a21\u5f0f\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;span class=\"{if $x&gt;1}big{else}small{\/if}\"&gt;\n<\/code><\/pre>\n\n\n\n<p>\u4f1a\u53d8\u6210\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AttributeNode {\n  name: \"class\",\n  value: &#091;\n    Text(\"\"),\n    IfNode(...),\n    Text(\"\")\n  ]\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <strong>\u539f\u7248 Hanjst \u6700\u9ad8\u7ea7\u4e5f\u662f\u6700\u75db\u82e6\u7684 embedded attribute\uff0c\u5728 AST \u4e16\u754c\u662f\u201c\u5929\u7136\u652f\u6301\u201d\u7684<\/strong>\u3002<\/p>\n\n\n\n<h3>4\ufe0f\u20e3 Code Generator\uff08\u751f\u6210 JS\uff09<\/h3>\n\n\n\n<p>AST \u2192 JS \u7684\u6838\u5fc3\u89c4\u5219\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>AST \u8282\u70b9<\/th><th>JS \u8f93\u51fa<\/th><\/tr><\/thead><tbody><tr><td>TextNode<\/td><td><code>out.push(\"xxx\")<\/code><\/td><\/tr><tr><td>ExprNode<\/td><td><code>out.push(expr)<\/code><\/td><\/tr><tr><td>IfNode<\/td><td><code>if(...) { ... }<\/code><\/td><\/tr><tr><td>ForEachNode<\/td><td><code>for(...) { ... }<\/code><\/td><\/tr><\/tbody><\/table><figcaption>Hanjst \u62bd\u8c61\u8bed\u6cd5\u6811 \u8868\u8fbe\u5f0f \u4e0e JS\u5bf9\u5e94\u8f93\u51fa<\/figcaption><\/figure>\n\n\n\n<h4>\u793a\u4f8b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function render(ctx){\n  let out = &#091;];\n  if(ctx.user.age &gt; 18){\n    out.push(\"adult\");\n  } else {\n    out.push(\"child\");\n  }\n  return out.join(\"\");\n}<\/code><\/pre>\n\n\n\n<h2>C3. \u4e00\u53e5\u975e\u5e38\u91cd\u8981\u7684\u5de5\u7a0b\u5224\u65ad:<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>Hanjst \u80fd\u8dd1\u5230\u4eca\u5929\uff0c\u672c\u8eab\u5c31\u8bf4\u660e\u5b83\u7684\u8bbe\u8ba1\u5728\u5f53\u5e74\u662f\u6210\u529f\u7684\u3002<\/strong><\/p><\/blockquote>\n\n\n\n<p>\u4f60\uff08 \u91cd\u6784\/\u91cd\u5199\u8005 \uff09\u73b0\u5728\u9047\u5230\u7684\u201c\u75db\u82e6\u201d\uff0c\u6070\u6070\u8bf4\u660e\uff1a<br \/>\u4f60\u5728\u628a\u4e00\u4e2a <strong>\u201c\u811a\u672c\u62fc\u63a5\u5668\u201d<\/strong>\uff0c\u5347\u7ea7\u6210\u4e00\u4e2a <strong>\u201c\u8bed\u8a00\u7f16\u8bd1\u5668\u201d<\/strong>\uff0c<\/p>\n\n\n\n<p>\ud83d\udc49 \u8fd9\u4e0d\u662f\u91cd\u6784\uff0c\u8fd9\u662f<strong>\u6362\u7269\u79cd<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p><a href=\"https:\/\/ufqi.com\/dev\/hanjst\/Hanjst.demo.html#\">Hanjst, Han JavaScript Template Engine \u6c49\u5409\u65af\u7279 \u6a21\u677f\u8bed\u8a00\u53ca\u5f15\u64ce<\/a><br \/><a href=\"https:\/\/ufqi.com\/dev\/hanjst\/Hanjst.demo.html\">https:\/\/ufqi.com\/dev\/hanjst\/Hanjst.demo.html<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/ufqi.com\/blog\/wp-content\/uploads\/2019\/06\/hanjst-logo.201901.jpg\"><img loading=\"lazy\" width=\"227\" height=\"100\" src=\"https:\/\/ufqi.com\/blog\/wp-content\/uploads\/2019\/06\/hanjst-logo.201901.jpg\" alt=\"\" class=\"wp-image-2058\" \/><\/a><figcaption>Hanjst \u6c49\u5409\u65af\u7279<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2026\u65b0\u5e74\u4f0a\u59cb\uff0c\u7531\u4e8e UfqiFina \u6709\u798f\u91d1\u878d ( https:\/\/ufqi &hellip; <a href=\"https:\/\/ufqi.com\/blog\/hanjst-ast-engine-refactor\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2058,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[301,6,2],"tags":[293,835,297,298,296,295,294,834],"_links":{"self":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3720"}],"collection":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/comments?post=3720"}],"version-history":[{"count":4,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3720\/revisions"}],"predecessor-version":[{"id":3724,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/posts\/3720\/revisions\/3724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media\/2058"}],"wp:attachment":[{"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/media?parent=3720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/categories?post=3720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ufqi.com\/blog\/wp-json\/wp\/v2\/tags?post=3720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}