{"id":10006536,"date":"2022-09-19T15:15:06","date_gmt":"2022-09-19T07:15:06","guid":{"rendered":"https:\/\/ityet.com\/?p=10006536"},"modified":"2022-09-19T15:15:06","modified_gmt":"2022-09-19T07:15:06","slug":"%e6%89%8b%e5%86%99%e7%bc%96%e7%a8%8b%e8%af%ad%e8%a8%80-%e5%ae%9e%e7%8e%b0%e8%bf%90%e7%ae%97%e7%ac%a6%e9%87%8d%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006536","title":{"rendered":"\u624b\u5199\u7f16\u7a0b\u8bed\u8a00-\u5b9e\u73b0\u8fd0\u7b97\u7b26\u91cd\u8f7d"},"content":{"rendered":"<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042507438\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u524d\u8a00<\/h2>\n<p>\u5148\u5e26\u6765\u65e5\u5e38\u7684 <a href=\"https:\/\/link.segmentfault.com\/?enc=d4jVR9l5NueL8f6Iz1S8sA%3D%3D.w9RpX4GbRLuG66RdwZWkU4JGj%2FgHIISgb9j5g3iUn367DtoZCtPj3qSueO6WzHWV\" rel=\"nofollow\">GScript<\/a> \u66f4\u65b0\uff1a\u65b0\u589e\u4e86\u53ef\u53d8\u53c2\u6570\u7684\u7279\u6027\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">int add(string s, int ...num){\n    println(s);\n    int sum = 0;\n    for(int i=0;i&lt;len(num);i++){\n        int v = num[i];\n        sum = sum+v;\n    }\n    return sum;\n}\nint x = add(&quot;abc&quot;, 1,2,3,4);\nprintln(x);\nassertEqual(x, 10);<\/code><\/pre>\n<p>\u5f97\u76ca\u4e8e\u53ef\u53d8\u53c2\u6570\uff0c\u6240\u4ee5\u65b0\u589e\u4e86\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u5185\u7f6e\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"js\">\/\/formats according to a format specifier and writes to standard output.\nprintf(string format, any ...a){}\n\n\/\/formats according to a format specifier and returns the resulting string.\nstring sprintf(string format, any ...a){}<\/code><\/pre>\n<p>\u4e0b\u9762\u91cd\u70b9\u770b\u770b <a href=\"https:\/\/link.segmentfault.com\/?enc=wk8tQ6LSU1BQQR8exqukjg%3D%3D.eMfr7eKzACQFbV7dFt6QylnqiYme4V1hjioI5GLNhmXmqXJIlSVSqiMCe4MO7leTRfnNylGWuOOMudlDoQy1YA%3D%3D\" rel=\"nofollow\">GScript<\/a> \u6240\u652f\u6301\u7684\u8fd0\u7b97\u7b26\u91cd\u8f7d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002<\/p>\n<h2>\u4f7f\u7528<\/h2>\n<p>\u8fd0\u7b97\u7b26\u91cd\u8f7d\u5176\u5b9e\u4e5f\u662f\u591a\u6001\u7684\u4e00\u79cd\u8868\u73b0\u5f62\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u5199\u8fd0\u7b97\u7b26\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u4ece\u800c\u6539\u53d8\u4ed6\u4eec\u7684\u8ba1\u7b97\u89c4\u5219\u3002<\/p>\n<pre><code class=\"go\">println(100+2*2);<\/code><\/pre>\n<p>\u4ee5\u8fd9\u6bb5\u4ee3\u7801\u7684\u8fd0\u7b97\u7b26\u4e3a\u4f8b\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u81ea\u7136\u662f\uff1a104.<\/p>\n<p>\u4f46\u5982\u679c\u6211\u4eec\u662f\u5bf9\u4e24\u4e2a\u5bf9\u8c61\u8fdb\u884c\u8ba1\u7b97\u5462\uff0c\u4e3e\u4e2a\u4f8b\u5b50:<\/p>\n<pre><code class=\"java\">class Person{\n    int age;\n    Person(int a){\n        age = a;\n    }\n}\nPerson p1 = Person(10);\nPerson p2 = Person(20);\nPerson p3 = p1+p2;<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u5199\u6cd5\u5728 <code>Java\/Go<\/code> \u4e2d\u90fd\u4f1a\u62a5\u7f16\u8bd1\u9519\u8bef\uff0c\u8fd9\u662f\u56e0\u4e3a\u4ed6\u4eec\u4e24\u8005\u90fd\u4e0d\u652f\u6301\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff1b<\/p>\n<p>\u4f46 <code>Python\/C#<\/code> \u662f\u652f\u6301\u7684\uff0c\u76f8\u6bd4\u4e4b\u4e0b\u6211\u89c9\u5f97 <code>C#<\/code> \u7684\u5b9e\u73b0\u65b9\u5f0f\u66f4\u7b26\u5408 <code>GScript<\/code> \u8bed\u6cd5\uff0c\u6240\u4ee5\u53c2\u8003 C# \u5b9e\u73b0\u4e86\u4ee5\u4e0b\u7684\u8bed\u6cd5\u89c4\u5219\u3002<\/p>\n<pre><code class=\"js\">Person operator + (Person p1, Person p2){\n    Person pp = Person(p1.age+p2.age);\n    return pp;\n}\nPerson p3 = p1+p2;\nprintln(&quot;p3.age=&quot;+p3.age);\nassertEqual(p3.age, 30);<\/code><\/pre>\n<p>\u6709\u51e0\u4e2a\u786c\u6027\u6761\u4ef6\uff1a<\/p>\n<ul>\n<li>\u51fd\u6570\u540d\u5fc5\u987b\u662f <code>operator<\/code><\/li>\n<li>\u540d\u79f0\u540e\u8ddf\u4e0a\u8fd0\u7b97\u7b26\u5373\u53ef\u3002<\/li>\n<\/ul>\n<blockquote><p>\u76ee\u524d\u652f\u6301\u7684\u8fd0\u7b97\u7b26\u6709\uff1a+-*\/   == != &lt; &lt;= &gt; &gt;=<\/p><\/blockquote>\n<h2>\u5b9e\u73b0<\/h2>\n<p>\u4ee5\u524d\u5728\u4f7f\u7528 <code>Python<\/code> \u8fd0\u7b97\u7b26\u91cd\u8f7d\u65f6\u5c31\u6709\u60f3\u8fc7\u5b83\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff1f\u4f46\u6ca1\u6709\u6df1\u7a76\uff0c\u8fd9\u6b21\u501f\u7740\u81ea\u5df1\u5b9e\u73b0\u76f8\u5173\u529f\u80fd\u4ece\u800c\u9700\u8981\u6df1\u5165\u7406\u89e3\u3002<\/p>\n<p>\u5176\u4e2d\u91cd\u70b9\u5c31\u4e3a\u4e24\u6b65\uff1a<\/p>\n<ol>\n<li>\u7f16\u8bd1\u671f\u95f4\uff1a\u8bb0\u5f55\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\u548c\u8fd0\u7b97\u7b26\u7684\u5173\u7cfb\u3002<\/li>\n<li>\u8fd0\u884c\u671f\uff1a\u6839\u636e\u5f53\u524d\u7684\u8fd0\u7b97\u627e\u5230\u58f0\u660e\u7684\u51fd\u6570\uff0c\u76f4\u63a5\u8fd0\u884c\u5373\u53ef\u3002<\/li>\n<\/ol>\n<p>\u7b2c\u4e00\u6b65\u7684\u91cd\u70b9\u662f\u626b\u63cf\u6240\u6709\u7684\u91cd\u8f7d\u51fd\u6570\uff0c\u5c06\u91cd\u8f7d\u51fd\u6570\u4e0e\u8fd0\u7b97\u7b26\u5b58\u653e\u8d77\u6765\uff0c\u9700\u8981\u5173\u6ce8\u7684\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e0e\u8fd0\u7b97\u7b26\u7c7b\u578b\u3002<\/p>\n<pre><code class=\"go\">\/\/ OpOverload \u91cd\u8f7d\u7b26\ntype OpOverload struct {\n    function  *Func\n    tokenType int\n}\n\n\/\/ \u8fd0\u7b97\u7b26\u91cd\u8f7d\u81ea\u5b9a\u4e49\u51fd\u6570\nopOverloads []*symbol.OpOverload<\/code><\/pre>\n<p>\u5728\u7f16\u8bd1\u5668\u4e2d\u4f7f\u7528\u4e00\u4e2a\u5207\u7247\u5b58\u653e\u3002<\/p>\n<p>\u800c\u5728\u8fd0\u884c\u671f\u4e2d\u5f53\u4e24\u4e2a\u5165\u53c2\u7c7b\u578b\u76f8\u540c\u65f6\uff0c\u5219\u9700\u8981\u67e5\u627e\u91cd\u8f7d\u51fd\u6570\u3002<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042507439\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code class=\"go\">\/\/ GetOpFunction \u83b7\u53d6\u8fd0\u7b97\u7b26\u91cd\u8f7d\u51fd\u6570\n\/\/ \u901a\u8fc7\u8fd4\u56de\u503c\u4ee5\u53ca\u8fd0\u7b97\u7b26\u53f7(+-*\/) \u5339\u914d\u91cd\u8f7d\u51fd\u6570\nfunc (a *AnnotatedTree) GetOpFunction(returnType symbol.Type, tokenType int) *symbol.Func {\n    for _, overload := range a.opOverloads {\n        isType := overload.GetFunc().GetReturnType().IsType(returnType)\n        if isType &amp;&amp; overload.GetTokenType() == tokenType {\n            return overload.GetFunc()\n        }\n    }\n    return nil\n}<\/code><\/pre>\n<p>\u67e5\u627e\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7\u7f16\u8bd1\u671f\u5b58\u653e\u7684\u6570\u636e\u8fdb\u884c\u5339\u914d\uff0c\u62ff\u5230\u91cd\u8f7d\u51fd\u6570\u540e\u81ea\u52a8\u8c03\u7528\u4fbf\u5b9e\u73b0\u4e86\u91cd\u8f7d\u3002<\/p>\n<p>\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u67e5\u770b\u76f8\u5173\u4ee3\u7801\uff1a<\/p>\n<ul>\n<li>\u7f16\u8bd1\u671f\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Bv%2Bz3wGfcVF5R7vHCb8wGw%3D%3D.6ZKpqpAbR5ZLv4ZAzzBKKHRNo1NezwC0KFVmkrzyfu6hZVf5LlIefy44tyfJN4YeykKvypbnPaQZfN5cumtO85Zt%2FSBI52MEZgc5WQsN7fMSBkHGUeHNx5IKUBv0Z6l%2BIqi%2BVO92PwHhz3UexRHzq3nXWDtu7NOj%2FbwegvPPC4k%3D\" rel=\"nofollow\">https:\/\/github.com\/crossoverJie\/gscript\/blob\/ae729ce7d4cf39fe115121993fcd2222716755e5\/resolver\/type_scope_resolver.go#L127<\/a><\/li>\n<li>\u8fd0\u884c\u671f\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=QOQqijiDlcxPFBb06MUHRw%3D%3D.EaiVA7a3S2QgttC1a%2BYA6ayqC3qay7qQaU1%2BQhXDFWClInAg9m4owbn9G6DJTEUWiYrsZkaoahSIfWilhM4ZbyeffST2AsGaUWtFGFwdq2iPnuzv32EMttN9HgnLXPqOnA1rbu4%2F9vKDxFleGF%2FGrA%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/crossoverJie\/gscript\/blob\/499236af549be47ff827c6d55de1fc8e5600b9b3\/visitor.go#L387<\/a><\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd0\u7b97\u7b26\u91cd\u8f7d\u5176\u5b9e\u5e76\u4e0d\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u529f\u80fd\uff1b\u56e0\u4e3a\u4f1a\u6539\u53d8\u8fd0\u7b97\u7b26\u7684\u8bed\u4e49\uff0c\u6bd4\u5982\u660e\u660e\u662f\u52a0\u6cd5\u5374\u5728\u91cd\u8f7d\u51fd\u6570\u4e2d\u5199\u4e3a\u51cf\u6cd5\u3002<\/p>\n<p>\u8fd9\u4f1a\u4f7f\u5f97\u4ee3\u7801\u9605\u8bfb\u8d77\u6765\u56f0\u96be\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u53c8\u975e\u5e38\u5e0c\u671b\u8bed\u8a00\u672c\u8eab\u80fd\u652f\u6301\u8fd0\u7b97\u7b26\u91cd\u8f7d\u3002<\/p>\n<p>\u6bd4\u5982\u5728 Go \u4e2d\u5e38\u7528\u7684\u4e00\u4e2a\u7b2c\u4e09\u65b9\u7cbe\u5ea6\u5e93<code>decimal.Decimal<\/code>\uff0c\u8fdb\u884c\u8fd0\u7b97\u65f6\u53ea\u80fd\u4f7f\u7528 <code>d1.Add(d2)<\/code> \u8fd9\u6837\u7684\u51fd\u6570\uff0c\u5f53\u8fd0\u7b97\u590d\u6742\u65f6\uff1a<\/p>\n<pre><code class=\"go\">a5 = (a1.Add(a2).Add(a3)).Mul(a4);<\/code><\/pre>\n<pre><code class=\"go\">a5 = (a1+a2+a3)*a4;<\/code><\/pre>\n<p>\u5c31\u4e0d\u5982\u4e0b\u9762\u8fd9\u79cd\u76f4\u89c2\uff0c\u6240\u4ee5\u6709\u5229\u6709\u5f0a\u5427\uff0c\u591a\u4e00\u4e2a\u9009\u9879\u603b\u4e0d\u662f\u574f\u4e8b\u3002<\/p>\n<p>GScript \u6e90\u7801\uff1a<br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=FReGYHzdciWG3vghdRvw3w%3D%3D.TJo9%2B%2F9bsAHhgk7cwjKXQtgdgCRGl7boux5cgQS%2BtUvF%2Bmk370J90pb%2Bj0xa3KEA\" rel=\"nofollow\">https:\/\/github.com\/crossoverJie\/gscript<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042507433\">https:\/\/segmentfault.com\/a\/1190000042507433<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5148\u5e26\u6765\u65e5\u5e38\u7684 GScript \u66f4\u65b0\uff1a\u65b0\u589e\u4e86\u53ef\u53d8\u53c2\u6570\u7684\u7279\u6027\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a int add(string s, [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006536","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006536"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006536\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}