{"id":33136,"date":"2023-04-25T21:44:17","date_gmt":"2023-04-25T13:44:17","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=33136"},"modified":"2023-04-25T21:44:17","modified_gmt":"2023-04-25T13:44:17","slug":"ohm-javascript","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/ohm-javascript.html","title":{"rendered":"Ohm\uff1a\u7528 JavaScript \u521b\u9020\u4f60\u7684\u7f16\u7a0b\u8bed\u8a00"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u89e3\u6790\u5668\u662f\u4e00\u79cd\u8d85\u7ea7\u6709\u7528\u7684\u8f6f\u4ef6\u5e93\u3002\u4ece\u6982\u5ff5\u4e0a\u7b80\u5355\u7684\u8bf4\uff0c\u5b83\u4eec\u7684\u5b9e\u73b0\u5f88\u6709\u6311\u6218\u6027\uff0c\u5e76\u4e14\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u7ecf\u5e38\u88ab\u8ba4\u4e3a\u662f\u9ed1\u9b54\u6cd5\u3002\u5728\u8fd9\u4e2a\u7cfb\u5217\u7684\u535a\u6587\u4e2d\uff0c\u6211\u4f1a\u5411\u4f60\u4eec\u5c55\u793a\u4e3a\u4ec0\u4e48\u4f60\u4e0d\u9700\u8981\u6210\u4e3a\u54c8\u5229\u6ce2\u7279\u5c31\u80fd\u591f\u7cbe\u901a\u89e3\u6790\u5668\u8fd9\u79cd\u9b54\u6cd5\u3002\u4f46\u662f\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\u5e26\u4e0a\u4f60\u7684\u9b54\u6756\u5427\uff01<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6211\u4eec\u5c06\u63a2\u7d22\u4e00\u79cd\u53eb\u505a Ohm \u7684\u65b0\u7684\u5f00\u6e90\u5e93\uff0c\u5b83\u4f7f\u5f97\u642d\u5efa\u89e3\u6790\u5668\u5f88\u7b80\u5355\u5e76\u4e14\u6613\u4e8e\u91cd\u7528\u3002\u5728\u8fd9\u4e2a\u7cfb\u5217\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 Ohm \u53bb\u8bc6\u522b\u6570\u5b57\uff0c\u6784\u5efa\u4e00\u4e2a\u8ba1\u7b97\u5668\u7b49\u7b49\u3002\u5728\u8fd9\u4e2a\u7cfb\u5217\u7684\u6700\u540e\u4f60\u5c06\u5df2\u7ecf\u7528\u4e0d\u5230 200 \u884c\u7684\u4ee3\u7801\u53d1\u660e\u4e86\u4e00\u79cd\u5b8c\u6574\u7684\u7f16\u7a0b\u8bed\u8a00\u3002\u8fd9\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u5c06\u8ba9\u4f60\u80fd\u591f\u505a\u5230\u4e00\u4e9b\u4f60\u53ef\u80fd\u8fc7\u53bb\u8ba4\u4e3a\u4e0d\u53ef\u80fd\u7684\u4e8b\u60c5\u3002<\/p>\n

\u4e3a\u4ec0\u4e48\u89e3\u6790\u5668\u5f88\u56f0\u96be\uff1f<\/strong><\/span><\/div>\n

\u89e3\u6790\u5668\u975e\u5e38\u6709\u7528\u3002\u5728\u5f88\u591a\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u4e00\u4e2a\u89e3\u6790\u5668\u3002\u6216\u8bb8\u6709\u4e00\u79cd\u4f60\u9700\u8981\u5904\u7406\u7684\u65b0\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u4f46\u8fd8\u6ca1\u6709\u4eba\u4e3a\u5b83\u5199\u4e86\u4e00\u4e2a\u5e93\uff1b\u53c8\u6216\u8bb8\u4f60\u53d1\u73b0\u4e86\u4e00\u79cd\u53e4\u8001\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u4f46\u662f\u5df2\u6709\u7684\u89e3\u6790\u5668\u4e0d\u80fd\u5728\u4f60\u7684\u5e73\u53f0\u4e0a\u6784\u5efa\u3002\u6211\u5df2\u7ecf\u770b\u5230\u8fd9\u6837\u7684\u4e8b\u53d1\u751f\u65e0\u6570\u6b21\u3002 Code \u5728\u6216\u8005\u4e0d\u5728\uff0c Data \u5c31\u5728\u90a3\u91cc\uff0c\u4e0d\u589e\u4e0d\u51cf\u3002
\n\u4ece\u6839\u672c\u4e0a\u6765\u8bf4\uff0c\u89e3\u6790\u5668\u5f88\u7b80\u5355\uff1a\u53ea\u662f\u628a\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u8f6c\u5316\u6210\u53e6\u4e00\u4e2a\u3002\u6240\u4ee5\u4f60\u4f1a\u4e0d\u4f1a\u89c9\u5f97\u4f60\u8981\u662f\u9093\u5e03\u5229\u591a\u6821\u957f\u5c31\u597d\u4e86\uff1f
\n\u89e3\u6790\u5668\u5386\u6765\u662f\u51fa\u5947\u5730\u96be\u5199\uff0c\u6240\u9762\u4e34\u7684\u6311\u6218\u662f\u7edd\u5927\u591a\u6570\u73b0\u6709\u7684\u5de5\u5177\u90fd\u5f88\u8001\uff0c\u5e76\u4e14\u9700\u8981\u4e00\u5b9a\u7684\u6666\u6da9\u96be\u61c2\u7684\u8ba1\u7b97\u673a\u79d1\u5b66\u77e5\u8bc6\u3002\u5982\u679c\u4f60\u5728\u5927\u5b66\u91cc\u4e0a\u8fc7\u7f16\u8bd1\u5668\u8bfe\u7a0b\uff0c\u90a3\u4e48\u8bfe\u672c\u91cc\u4e5f\u8bb8\u8fd8\u6709\u4ece\u4e0a\u4e16\u7eaa\u4e03\u5341\u5e74\u4f20\u4e0b\u6765\u7684\u6280\u672f\u3002\u5e78\u8fd0\u7684\u662f\uff0c\u89e3\u6790\u5668\u6280\u672f\u4ece\u90a3\u65f6\u5019\u8d77\u5df2\u7ecf\u63d0\u9ad8\u4e86\u5f88\u591a\u3002
\n\u5178\u578b\u7684\uff0c\u89e3\u6790\u5668\u662f\u901a\u8fc7\u4f7f\u7528\u4e00\u79cd\u53eb\u4f5c\u5f62\u5f0f\u8bed\u6cd5\uff08formal grammar\uff09\u7684\u7279\u6b8a\u8bed\u6cd5\u6765\u5b9a\u4e49\u4f60\u60f3\u8981\u89e3\u6790\u7684\u4e1c\u897f\u6765\u521b\u9020\u7684\uff0c\u7136\u540e\u4f60\u9700\u8981\u628a\u5b83\u653e\u5165\u50cf Bison \u548c Yacc \u7684\u5de5\u5177\u4e2d\uff0c\u8fd9\u4e9b\u5de5\u5177\u80fd\u591f\u4ea7\u751f\u4e00\u5806 C \u4ee3\u7801\uff0c\u8fd9\u4e9b\u4ee3\u7801\u4f60\u9700\u8981\u4fee\u6539\u6216\u8005\u94fe\u63a5\u5230\u4f60\u5b9e\u9645\u5199\u5165\u7684\u7f16\u7a0b\u8bed\u8a00\u4e2d\u3002\u53e6\u5916\u7684\u9009\u62e9\u662f\u7528\u4f60\u66f4\u559c\u6b22\u7684\u8bed\u8a00\u4eb2\u81ea\u52a8\u624b\u5199\u4e00\u4e2a\u89e3\u6790\u5668\uff0c\u8fd9\u5f88\u6162\u4e14\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u5728\u4f60\u80fd\u591f\u771f\u6b63\u4f7f\u7528\u5b83\u4e4b\u524d\u8fd8\u6709\u8bb8\u591a\u989d\u5916\u7684\u5de5\u4f5c\u3002
\n\u60f3\u50cf\u4e00\u4e0b\uff0c\u662f\u5426\u4f60\u5173\u4e8e\u4f60\u60f3\u8981\u89e3\u6790\u7684\u4e1c\u897f\u7684\u8bed\u6cd5\u63cf\u8ff0\u4e5f\u662f\u89e3\u6790\u5668\uff1f\u5982\u679c\u4f60\u80fd\u591f\u53ea\u662f\u76f4\u63a5\u8fd0\u884c\u8fd9\u4e9b\u8bed\u6cd5\uff0c\u7136\u540e\u4ec5\u5728\u4f60\u9700\u8981\u7684\u5730\u65b9\u589e\u52a0\u4e00\u4e9b\u6302\u94a9\uff08hook\uff09\u5462\uff1f\u90a3\u5c31\u662f Ohm \u6240\u53ef\u4ee5\u505a\u5230\u7684\u4e8b\u3002<\/p>\n

Ohm\u7b80\u4ecb<\/strong><\/div>\n

Ohm\u00a0\u662f\u4e00\u79cd\u65b0\u7684\u89e3\u6790\u7cfb\u7edf\u3002\u5b83\u7c7b\u4f3c\u4e8e\u4f60\u53ef\u80fd\u5df2\u7ecf\u5728\u8bfe\u672c\u91cc\u9762\u770b\u5230\u8fc7\u7684\u8bed\u6cd5\uff0c\u4f46\u662f\u5b83\u66f4\u5f3a\u5927\uff0c\u4f7f\u7528\u8d77\u6765\u66f4\u7b80\u5355\u3002\u901a\u8fc7 Ohm, \u4f60\u80fd\u591f\u4f7f\u7528\u4e00\u79cd\u7075\u6d3b\u7684\u8bed\u6cd5\u5728\u4e00\u4e2a .ohm \u6587\u4ef6\u4e2d\u6765\u5199\u4f60\u81ea\u5df1\u7684\u683c\u5f0f\u5b9a\u4e49\uff0c\u7136\u540e\u4f7f\u7528\u4f60\u7684\u5bbf\u4e3b\u8bed\u8a00\u628a\u8bed\u4e49\u52a0\u5165\u5230\u91cc\u9762\u3002\u5728\u8fd9\u7bc7\u535a\u6587\u91cc\uff0c\u6211\u4eec\u5c06\u7528 JavaScript \u4f5c\u4e3a\u5bbf\u4e3b\u8bed\u8a00\u3002
\nOhm \u5efa\u7acb\u4e8e\u4e00\u4e2a\u4e3a\u521b\u9020\u66f4\u7b80\u5355\u3001\u66f4\u7075\u6d3b\u7684\u89e3\u6790\u5668\u7684\u591a\u5e74\u7814\u7a76\u57fa\u7840\u4e4b\u4e0a\u3002VPRI \u7684 STEPS program (pdf) \u4f7f\u7528 Ohm \u7684\u524d\u8eab Ometa \u4e3a\u8bb8\u591a\u7279\u6b8a\u7684\u4efb\u52a1\u521b\u9020\u4e86\u4e13\u95e8\u7684\u8bed\u8a00\uff08\u6bd4\u5982\u4e00\u4e2a\u6709 400 \u884c\u4ee3\u7801\u7684\u5e73\u884c\u5236\u56fe\u63cf\u7ed8\u5668\uff09\u3002
\nOhm \u6709\u8bb8\u591a\u6709\u8da3\u7684\u7279\u70b9\u548c\u7b26\u53f7\uff0c\u4f46\u662f\u76f8\u6bd4\u4e8e\u5168\u90e8\u89e3\u91ca\u5b83\u4eec\uff0c\u6211\u8ba4\u4e3a\u6211\u4eec\u53ea\u9700\u8981\u6df1\u5165\u5176\u4e2d\u5e76\u6784\u5efa\u4e00\u4e9b\u4e1c\u897f\u5c31\u884c\u4e86\u3002<\/p>\n

\u89e3\u6790\u6574\u6570<\/strong><\/span><\/div>\n

\u8ba9\u6211\u4eec\u6765\u89e3\u6790\u4e00\u4e9b\u6570\u5b57\u3002\u8fd9\u770b\u8d77\u6765\u4f1a\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u5728\u4e00\u4e2a\u6587\u672c\u4e32\u4e2d\u5bfb\u627e\u6bd7\u90bb\u7684\u6570\u5b57\uff0c\u4f46\u662f\u8ba9\u6211\u4eec\u5c1d\u8bd5\u53bb\u5904\u7406\u6240\u6709\u5f62\u5f0f\u7684\u6570\u5b57\uff1a\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3001\u5341\u516d\u8fdb\u5236\u6570\u548c\u516b\u8fdb\u5236\u6570\u3001\u79d1\u5b66\u8ba1\u6570\u3001\u8d1f\u6570\u3002\u89e3\u6790\u6570\u5b57\u5f88\u7b80\u5355\uff0c\u6b63\u786e\u89e3\u6790\u5374\u5f88\u96be\u3002
\n\u4eb2\u81ea\u6784\u5efa\u8fd9\u4e2a\u4ee3\u7801\u5c06\u4f1a\u5f88\u56f0\u96be\uff0c\u4f1a\u6709\u5f88\u591a\u95ee\u9898\uff0c\u4f1a\u4f34\u968f\u6709\u8bb8\u591a\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u6bd4\u5982\u6709\u65f6\u4f1a\u76f8\u4e92\u77db\u76fe\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u8bb8\u53ef\u4ee5\u505a\u7684\u8fd9\u4e00\u70b9\uff0c\u4f46\u662f\u5b83\u4f1a\u975e\u5e38\u4e11\u964b\u800c\u96be\u4ee5\u7ef4\u62a4\u3002\u8ba9\u6211\u4eec\u7528 Ohm \u6765\u8bd5\u8bd5\u3002
\n\u7528 Ohm \u6784\u5efa\u7684\u89e3\u6790\u5668\u6d89\u53ca\u4e09\u4e2a\u90e8\u5206\uff1a\u8bed\u6cd5\uff08grammar\uff09\u3001\u8bed\u4e49\uff08semantics\uff09\u548c\u6d4b\u8bd5\uff08tests\uff09\u3002\u6211\u901a\u5e38\u6311\u9009\u95ee\u9898\u7684\u4e00\u90e8\u5206\u4e3a\u5b83\u5199\u6d4b\u8bd5\uff0c\u7136\u540e\u6784\u5efa\u8db3\u591f\u7684\u8bed\u6cd5\u548c\u8bed\u4e49\u6765\u4f7f\u6d4b\u8bd5\u901a\u8fc7\u3002\u7136\u540e\u6211\u518d\u6311\u9009\u95ee\u9898\u7684\u53e6\u4e00\u90e8\u5206\uff0c\u589e\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u3001\u66f4\u65b0\u8bed\u6cd5\u548c\u8bed\u4e49\uff0c\u4ece\u800c\u786e\u4fdd\u6240\u6709\u7684\u6d4b\u8bd5\u80fd\u591f\u7ee7\u7eed\u901a\u8fc7\u3002\u5373\u4f7f\u6211\u4eec\u6709\u4e86\u65b0\u7684\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5199\u89e3\u6790\u5668\u4ece\u6982\u5ff5\u4e0a\u6765\u8bf4\u4f9d\u65e7\u5f88\u590d\u6742\u3002\u6d4b\u8bd5\u662f\u7528\u4e00\u79cd\u5408\u7406\u7684\u65b9\u5f0f\u6765\u6784\u5efa\u89e3\u6790\u5668\u7684\u552f\u4e00\u65b9\u6cd5\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u5f00\u59cb\u5de5\u4f5c\u3002
\n\u6211\u4eec\u5c06\u4ece\u6574\u6570\u5f00\u59cb\u3002\u4e00\u4e2a\u6574\u6570\u7531\u4e00\u7cfb\u5217\u76f8\u4e92\u6bd7\u90bb\u7684\u6570\u5b57\u7ec4\u6210\u3002\u8ba9\u6211\u4eec\u628a\u4e0b\u9762\u7684\u5185\u5bb9\u653e\u5165\u4e00\u4e2a\u53eb\u505a grammar.ohm \u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n

CoolNums {\r\n   \/\/ just a basic integer\r\n   Number = digit+\r\n}\r\n<\/pre>\n

\u8fd9\u521b\u9020\u4e86\u4e00\u6761\u5339\u914d\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57digit<\/span>\u53eb\u4f5c Number<\/span> \u7684\u5355\u4e00\u89c4\u5219\u3002+<\/span>\u610f\u5473\u7740\u4e00\u4e2a\u6216\u66f4\u591a\uff0c\u5c31\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u4e00\u6837\u3002\u5f53\u6709\u4e00\u4e2a\u6216\u66f4\u591a\u7684\u6570\u5b57\u65f6\uff0c\u8fd9\u4e2a\u89c4\u5219\u5c06\u4f1a\u5339\u914d\u5b83\u4eec\uff0c\u5982\u679c\u6ca1\u6709\u6570\u5b57\u6216\u8005\u6709\u4e00\u4e9b\u4e0d\u662f\u6570\u5b57\u7684\u4e1c\u897f\u5c06\u4e0d\u4f1a\u5339\u914d\u3002\u201c\u6570\u5b57\uff08digit<\/span>\uff09\u201d\u7684\u5b9a\u4e49\u662f\u4ece 0 \u5230 9 \u5176\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u3002digit<\/span> \u4e5f\u662f\u50cf Number<\/span>\u4e00\u6837\u7684\u89c4\u5219\uff0c\u4f46\u662f\u5b83\u662f Ohm \u7684\u5176\u4e2d\u4e00\u6761\u6784\u5efa\u89c4\u5219\u56e0\u6b64\u6211\u4eec\u4e0d\u9700\u8981\u53bb\u5b9a\u4e49\u5b83\u3002\u5982\u679c\u6211\u4eec\u60f3\u7684\u8bdd\u53ef\u4ee5\u63a8\u7ffb\u5b83\uff0c\u4f46\u5728\u8fd9\u65f6\u5019\u8fd9\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u6bd5\u7adf\u6211\u4eec\u4e0d\u6253\u7b97\u53bb\u53d1\u660e\u4e00\u79cd\u65b0\u7684\u6570\u3002
\n\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u5165\u8fd9\u4e2a\u8bed\u6cd5\u5e76\u7528 Ohm \u5e93\u6765\u8fd0\u884c\u5b83\u3002
\n\u628a\u5b83\u653e\u5165 test1.js\uff1a<\/p>\n

var ohm = require('ohm-js');\r\nvar fs = require('fs');\r\nvar assert = require('assert');\r\nvar grammar = ohm.grammar(fs.readFileSync('src\/blog_numbers\/syntax1.ohm').toString());\r\n<\/pre>\n

Ohm.grammar<\/span>\u8c03\u7528\u5c06\u8bfb\u5165\u8be5\u6587\u4ef6\u5e76\u89e3\u6790\u6210\u4e00\u4e2a\u8bed\u6cd5\u5bf9\u8c61\u3002\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u4e00\u4e9b\u8bed\u4e49\u3002\u628a\u4e0b\u9762\u5185\u5bb9\u589e\u52a0\u5230\u4f60\u7684 JavaScript \u6587\u4ef6\u4e2d\uff1a<\/p>\n

var sem = grammar.createSemantics().addOperation('toJS', {\r\n    Number: function(a) {\r\n        return parseInt(this.sourceString,10);\r\n    }\r\n});\r\n<\/pre>\n

\u8fd9\u901a\u8fc7 toJS<\/span>\u64cd\u4f5c\u521b\u9020\u4e86\u4e00\u4e2a\u53eb\u4f5c sem<\/span>\u7684\u8bed\u6cd5\u96c6\u3002\u8fd9\u4e9b\u8bed\u4e49\u672c\u8d28\u4e0a\u662f\u4e00\u4e9b\u5bf9\u5e94\u5230\u8bed\u6cd5\u4e2d\u6bcf\u4e2a\u89c4\u5219\u7684\u51fd\u6570\u3002\u6bcf\u4e2a\u51fd\u6570\u5f53\u4e0e\u4e4b\u76f8\u5339\u914d\u7684\u8bed\u6cd5\u89c4\u5219\u88ab\u89e3\u6790\u65f6\u5c06\u4f1a\u88ab\u8c03\u7528\u3002\u4e0a\u9762\u7684 Number<\/span> \u51fd\u6570\u5c06\u4f1a\u5728\u8bed\u6cd5\u4e2d\u7684 Number<\/span>\u89c4\u5219\u88ab\u89e3\u6790\u65f6\u88ab\u8c03\u7528\u3002\u8bed\u6cd5\uff08grammar\uff09\u5b9a\u4e49\u4e86\u5728\u8bed\u8a00\u4e2d\u8fd9\u4e9b\u4ee3\u7801\u662f\u4ec0\u4e48\uff0c\u8bed\u4e49\uff08semantics\uff09\u5b9a\u4e49\u4e86\u5f53\u8fd9\u4e9b\u4ee3\u7801\u88ab\u89e3\u6790\u65f6\u5e94\u8be5\u505a\u4ec0\u4e48\u3002
\n\u8bed\u4e49\u51fd\u6570\u80fd\u591f\u505a\u6211\u4eec\u60f3\u505a\u7684\u4efb\u4f55\u4e8b\uff0c\u6bd4\u5982\u6253\u5370\u51fa\u6545\u969c\u4fe1\u606f\u3001\u521b\u5efa\u5bf9\u8c61\uff0c\u6216\u8005\u5728\u4efb\u4f55\u5b50\u8282\u70b9\u4e0a\u9012\u5f52\u8c03\u7528 toJS<\/span>\u3002\u6b64\u65f6\u6211\u4eec\u4ec5\u4ec5\u60f3\u628a\u5339\u914d\u7684\u6587\u672c\u8f6c\u6362\u6210\u771f\u6b63\u7684 JavaScript \u6574\u6570\u3002
\n\u6240\u6709\u7684\u8bed\u4e49\u51fd\u6570\u6709\u4e00\u4e2a\u5185\u542b\u7684 this<\/span>\u5bf9\u8c61\uff0c\u5e26\u6709\u4e00\u4e9b\u6709\u7528\u7684\u5c5e\u6027\u3002\u5176 source<\/span> \u5c5e\u6027\u4ee3\u8868\u4e86\u8f93\u5165\u6587\u672c\u4e2d\u548c\u8fd9\u4e2a\u8282\u70b9\u76f8\u5339\u914d\u7684\u90e8\u5206\u3002this.sourceString<\/span>\u662f\u4e00\u4e2a\u5339\u914d\u8f93\u5165\u7684\u4e32\uff0c\u8c03\u7528\u5185\u7f6e\u5728 JavaScript \u4e2d\u7684parseInt<\/span>\u51fd\u6570\u4f1a\u628a\u8fd9\u4e2a\u4e32\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\u3002\u4f20\u7ed9 parseInt<\/span>\u7684 10<\/span>\u8fd9\u4e2a\u53c2\u6570\u544a\u8bc9 JavaScript \u6211\u4eec\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u4ee5 10<\/span>\u4e3a\u57fa\u5e95\uff0810 \u8fdb\u5236\uff09\u7684\u6570\u3002\u5982\u679c\u5c11\u4e86\u8fd9\u4e2a\u53c2\u6570\uff0c JavaScript \u4e5f\u4f1a\u5047\u5b9a\u4ee5 10 \u4e3a\u57fa\u5e95\uff0c\u4f46\u662f\u6211\u4eec\u628a\u5b83\u5305\u542b\u5728\u91cc\u9762\u56e0\u4e3a\u540e\u9762\u6211\u4eec\u5c06\u652f\u6301\u4ee5 16 \u4e3a\u57fa\u5e95\u7684\u6570\uff0c\u6240\u4ee5\u4f7f\u4e4b\u660e\u786e\u6bd4\u8f83\u597d\u3002
\n\u65e2\u7136\u6211\u4eec\u6709\u4e00\u4e9b\u8bed\u6cd5\uff0c\u8ba9\u6211\u4eec\u6765\u5b9e\u9645\u89e3\u6790\u4e00\u4e9b\u4e1c\u897f\u770b\u4e00\u770b\u6211\u4eec\u7684\u89e3\u6790\u5668\u662f\u5426\u80fd\u591f\u5de5\u4f5c\u3002\u5982\u4f55\u77e5\u9053\u6211\u4eec\u7684\u89e3\u6790\u5668\u53ef\u4ee5\u5de5\u4f5c\uff1f\u901a\u8fc7\u6d4b\u8bd5\uff0c\u8bb8\u591a\u8bb8\u591a\u7684\u6d4b\u8bd5\uff0c\u6bcf\u4e00\u4e2a\u53ef\u80fd\u7684\u8fb9\u7f18\u60c5\u51b5\u90fd\u9700\u8981\u4e00\u4e2a\u6d4b\u8bd5\u3002
\n\u4f7f\u7528\u6807\u51c6\u7684\u65ad\u8a00 assert<\/span>API\uff0c\u4ee5\u4e0b\u8fd9\u4e2a\u6d4b\u8bd5\u51fd\u6570\u80fd\u591f\u5339\u914d\u4e00\u4e9b\u8f93\u5165\u5e76\u8fd0\u7528\u6211\u4eec\u7684\u8bed\u4e49\u628a\u5b83\u8f6c\u6362\u6210\u4e00\u4e2a\u6570\uff0c\u7136\u540e\u628a\u8fd9\u4e2a\u6570\u548c\u6211\u4eec\u671f\u671b\u7684\u8f93\u5165\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n

  function test(input, answer) {\r\n     var match = grammar.match(input);\r\n     if(match.failed()) return console.log(\"input failed to match \" + input + match.message);     \r\n     var result = sem(match).toJS();\r\n     assert.deepEqual(result,answer);\r\n     console.log('success = ', result, answer);\r\n    }\r\n<\/pre>\n

\u5c31\u662f\u5982\u6b64\u3002\u73b0\u5728\u6211\u4eec\u80fd\u591f\u4e3a\u5404\u79cd\u4e0d\u540c\u7684\u6570\u5199\u4e00\u5806\u6d4b\u8bd5\u3002\u5982\u679c\u5339\u914d\u5931\u8d25\u6211\u4eec\u7684\u811a\u672c\u5c06\u4f1a\u629b\u51fa\u4e00\u4e2a\u4f8b\u5916\u3002\u5426\u5219\u5c31\u6253\u5370\u6210\u529f\u4fe1\u606f\u3002\u8ba9\u6211\u4eec\u5c1d\u8bd5\u4e00\u4e0b\uff0c\u628a\u4e0b\u9762\u8fd9\u4e9b\u5185\u5bb9\u52a0\u5165\u5230\u811a\u672c\u4e2d\uff1a<\/p>\n

   test(\"123\",123);\r\n    test(\"999\",999);\r\n    test(\"abc\",999);\r\n<\/pre>\n

\u7136\u540e\u7528node test1.js<\/span>\u8fd0\u884c\u811a\u672c\u3002
\n\u4f60\u7684\u8f93\u51fa\u5e94\u8be5\u662f\u8fd9\u6837\uff1a<\/p>\n

success =  123 123\r\nsuccess =  999 999\r\ninput failed to match abcLine 1, col 1:\r\n> 1 | abc\r\n      ^\r\nExpected a digit\r\n<\/pre>\n

\u771f\u9177\u3002\u6b63\u5982\u9884\u671f\u7684\u90a3\u6837\uff0c\u524d\u4e24\u4e2a\u6210\u529f\u4e86\uff0c\u7b2c\u4e09\u4e2a\u5931\u8d25\u4e86\u3002\u66f4\u597d\u7684\u662f\uff0cOhm \u81ea\u52a8\u7ed9\u4e86\u6211\u4eec\u4e00\u4e2a\u5f88\u68d2\u7684\u9519\u8bef\u4fe1\u606f\u6307\u51fa\u5339\u914d\u5931\u8d25\u3002<\/p>\n

\u6d6e\u70b9\u6570<\/strong><\/span><\/div>\n

\u6211\u4eec\u7684\u89e3\u6790\u5668\u5de5\u4f5c\u4e86\uff0c\u4f46\u662f\u5b83\u505a\u7684\u5de5\u4f5c\u4e0d\u662f\u5f88\u6709\u8da3\u3002\u8ba9\u6211\u4eec\u628a\u5b83\u6269\u5c55\u6210\u65e2\u80fd\u89e3\u6790\u6574\u6570\u53c8\u80fd\u89e3\u6790\u6d6e\u70b9\u6570\u3002\u6539\u53d8 grammar.ohm \u6587\u4ef6\u4f7f\u5b83\u770b\u8d77\u6765\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n

CoolNums {\r\n  \/\/ just a basic integer\r\n  Number = float | int\r\n  int    = digit+\r\n  float  = digit+ \".\" digit+\r\n}\r\n<\/pre>\n

\u8fd9\u628aNumber<\/span>\u89c4\u5219\u6539\u53d8\u6210\u6307\u5411\u4e00\u4e2a\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u6216\u8005\u4e00\u4e2a\u6574\u6570\uff08int<\/span>\uff09\u3002\u8fd9\u4e2a |<\/span> \u4ee3\u8868\u7740\u201c\u6216\u201d\u3002\u6211\u4eec\u628a\u8fd9\u4e2a\u8bfb\u6210\u201c\u4e00\u4e2a Number<\/span>\u7531\u4e00\u4e2a\u6d6e\u70b9\u6570\u6216\u8005\u4e00\u4e2a\u6574\u6570\u6784\u6210\u3002\u201d\u7136\u540e\u6574\u6570\uff08int<\/span>\uff09\u5b9a\u4e49\u6210 digit+<\/span>\uff0c\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u5b9a\u4e49\u6210digit+<\/span>\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u53e5\u53f7\u7136\u540e\u518d\u8ddf\u7740\u53e6\u4e00\u4e2adigit+<\/span>\u3002\u8fd9\u610f\u5473\u7740\u5728\u53e5\u53f7\u524d\u548c\u53e5\u53f7\u540e\u90fd\u81f3\u5c11\u8981\u6709\u4e00\u4e2a\u6570\u5b57\u3002\u5982\u679c\u4e00\u4e2a\u6570\u4e2d\u6ca1\u6709\u4e00\u4e2a\u53e5\u53f7\u90a3\u4e48\u5b83\u5c31\u4e0d\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u56e0\u6b64\u5c31\u662f\u4e00\u4e2a\u6574\u6570\u3002
\n\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u518d\u6b21\u770b\u4e00\u4e0b\u6211\u4eec\u7684\u8bed\u4e49\u529f\u80fd\u3002\u7531\u4e8e\u6211\u4eec\u73b0\u5728\u6709\u4e86\u65b0\u7684\u89c4\u5219\u6240\u4ee5\u6211\u4eec\u9700\u8981\u65b0\u7684\u529f\u80fd\u51fd\u6570\uff1a\u4e00\u4e2a\u4f5c\u4e3a\u6574\u6570\u7684\uff0c\u4e00\u4e2a\u4f5c\u4e3a\u6d6e\u70b9\u6570\u7684\u3002<\/p>\n

var sem = grammar.createSemantics().addOperation('toJS', {\r\n    Number: function(a) {\r\n        return a.toJS();\r\n    },\r\n    int: function(a) {\r\n        console.log(\"doing int\", this.sourceString);\r\n        return parseInt(this.sourceString,10);\r\n    },\r\n    float: function(a,b,c) {\r\n        console.log(\"doing float\", this.sourceString);\r\n        return parseFloat(this.sourceString);\r\n    }\r\n});\r\n<\/pre>\n

\u8fd9\u91cc\u6709\u4e24\u4ef6\u4e8b\u60c5\u9700\u8981\u6ce8\u610f\u3002\u9996\u5148\uff0c\u6574\u6570\uff08int<\/span>\uff09\u3001\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u548c\u6570\uff08Number<\/span>\uff09\u90fd\u6709\u76f8\u5339\u914d\u7684\u8bed\u6cd5\u89c4\u5219\u548c\u51fd\u6570\u3002\u7136\u800c\uff0c\u9488\u5bf9Number<\/span>\u7684\u529f\u80fd\u4e0d\u518d\u6709\u4efb\u4f55\u610f\u4e49\u3002\u5b83\u63a5\u6536\u5b50\u8282\u70b9 a<\/span> \u7136\u540e\u8fd4\u56de\u8be5\u5b50\u8282\u70b9\u7684 toJS<\/span> \u7ed3\u679c\u3002\u6362\u53e5\u8bdd\u8bf4\uff0cNumber<\/span> \u89c4\u5219\u7b80\u5355\u7684\u8fd4\u56de\u76f8\u5339\u914d\u7684\u5b50\u89c4\u5219\u3002\u7531\u4e8e\u8fd9\u662f\u5728 Ohm \u4e2d\u4efb\u4f55\u89c4\u5219\u7684\u9ed8\u8ba4\u884c\u4e3a\uff0c\u56e0\u6b64\u5b9e\u9645\u4e0a\u6211\u4eec\u4e0d\u7528\u53bb\u8003\u8651 Number<\/span> \u7684\u4f5c\u7528\uff0cOhm \u4f1a\u66ff\u6211\u4eec\u505a\u597d\u8fd9\u4ef6\u4e8b\u3002
\n\u5176\u6b21\uff0c\u6574\u6570\uff08int<\/span>\uff09\u6709\u4e00\u4e2a\u53c2\u6570 a<\/span>\uff0c\u7136\u800c\u6d6e\u70b9\u6570\u6709\u4e09\u4e2a\uff1aa\u3001b\u548cc<\/span>\u3002\u8fd9\u662f\u7531\u4e8e\u89c4\u5219\u7684\u5b9e\u53c2\u6570\u91cf\uff08arity\uff09\u51b3\u5b9a\u7684\u3002\u5b9e\u53c2\u6570\u91cf\uff08arity\uff09\u610f\u5473\u7740\u4e00\u4e2a\u89c4\u5219\u91cc\u9762\u6709\u591a\u5c11\u53c2\u6570\u3002\u5982\u679c\u6211\u4eec\u56de\u8fc7\u5934\u53bb\u770b\u8bed\u6cd5\uff0c\u6d6e\u70b9\u6570\uff08float<\/span>\uff09\u7684\u89c4\u5219\u662f\uff1a<\/p>\n

  float  = digit+ \".\" digit+\r\n<\/pre>\n

\u6d6e\u70b9\u6570\u89c4\u5219\u901a\u8fc7\u4e09\u4e2a\u90e8\u5206\u6765\u5b9a\u4e49\uff1a\u7b2c\u4e00\u4e2a digit+\u3001.\u3001\u4ee5\u53ca\u7b2c\u4e8c\u4e2a digit+<\/span>\u3002\u8fd9\u4e09\u4e2a\u90e8\u5206\u90fd\u4f1a\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u6d6e\u70b9\u6570\u7684\u529f\u80fd\u51fd\u6570\u3002\u56e0\u6b64\u6d6e\u70b9\u6570\u5fc5\u987b\u6709\u4e09\u4e2a\u53c2\u6570\uff0c\u5426\u5219 Ohm \u5e93\u4f1a\u7ed9\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u7528\u5728\u610f\u53c2\u6570\uff0c\u56e0\u4e3a\u6211\u4eec\u4ec5\u4ec5\u76f4\u63a5\u652b\u53d6\u4e86\u8f93\u5165\u4e32\uff0c\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u9700\u8981\u53c2\u6570\u5217\u5728\u90a3\u91cc\u6765\u907f\u514d\u7f16\u8bd1\u5668\u9519\u8bef\u3002\u540e\u9762\u6211\u4eec\u5c06\u5b9e\u9645\u4f7f\u7528\u5176\u4e2d\u4e00\u4e9b\u53c2\u6570\u3002
\n\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u4e3a\u65b0\u7684\u6d6e\u70b9\u6570\u652f\u6301\u6dfb\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u3002<\/p>\n

test(\"123\",123);\r\ntest(\"999\",999);\r\n\/\/test(\"abc\",999);\r\ntest('123.456',123.456);\r\ntest('0.123',0.123);\r\ntest('.123',0.123);\r\n<\/pre>\n

\u6ce8\u610f\u6700\u540e\u4e00\u4e2a\u6d4b\u8bd5\u5c06\u4f1a\u5931\u8d25\u3002\u4e00\u4e2a\u6d6e\u70b9\u6570\u5fc5\u987b\u4ee5\u4e00\u4e2a\u6570\u5f00\u59cb\uff0c\u5373\u4f7f\u5b83\u5c31\u662f\u4e2a 0\uff0c.123<\/span> \u4e0d\u662f\u6709\u6548\u7684\uff0c\u5b9e\u9645\u4e0a\u771f\u6b63\u7684 JavaScript \u8bed\u8a00\u4e5f\u6709\u76f8\u540c\u7684\u89c4\u5219\u3002<\/p>\n

\u5341\u516d\u8fdb\u5236\u6570<\/strong><\/span><\/div>\n

\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e9b\u5176\u5b83\u7684\u6570\u7684\u8bed\u6cd5\u6700\u597d\u53ef\u4ee5\u652f\u6301\uff1a\u5341\u516d\u8fdb\u5236\u6570\u548c\u79d1\u5b66\u8ba1\u6570\u3002\u5341\u516d\u8fdb\u5236\u6570\u662f\u4ee5 16 \u4e3a\u57fa\u5e95\u7684\u6574\u6570\u3002\u5341\u516d\u8fdb\u5236\u6570\u7684\u6570\u5b57\u80fd\u4ece 0 \u5230 9 \u548c\u4ece A \u5230 F\u3002\u5341\u516d\u8fdb\u5236\u6570\u7ecf\u5e38\u7528\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u5f53\u7528\u4e8c\u8fdb\u5236\u6570\u636e\u5de5\u4f5c\u65f6\uff0c\u4f60\u53ef\u4ee5\u4ec5\u4ec5\u4f7f\u7528\u4e24\u4e2a\u6570\u5b57\u8868\u793a 0 \u5230 255 \u7684\u6570\u3002
\n\u5728\u7edd\u5927\u591a\u6570\u6e90\u81ea C \u7684\u7f16\u7a0b\u8bed\u8a00\uff08\u5305\u62ec JavaScript\uff09\uff0c\u5341\u516d\u8fdb\u5236\u6570\u901a\u8fc7\u5728\u524d\u9762\u52a0\u4e0a 0x<\/span> \u6765\u5411\u7f16\u8bd1\u5668\u8868\u660e\u540e\u9762\u8ddf\u7684\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002\u4e3a\u4e86\u8ba9\u6211\u4eec\u7684\u89e3\u6790\u5668\u652f\u6301\u5341\u516d\u8fdb\u5236\u6570\uff0c\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u53e6\u4e00\u6761\u89c4\u5219\u3002<\/p>\n

  Number = hex | float | int\r\n  int    = digit+\r\n  float  = digit+ \".\" digit+\r\n  hex    = \"0x\" hexDigit+\r\n  hexDigit := \"0\"..\"9\" | \"a\"..\"f\" | \"A\"..\"F\"\r\n<\/pre>\n

\u6211\u5b9e\u9645\u4e0a\u5df2\u7ecf\u589e\u52a0\u4e86\u4e24\u6761\u89c4\u5219\u3002\u5341\u516d\u8fdb\u5236\u6570\uff08hex<\/span>\uff09\u8868\u660e\u5b83\u662f\u4e00\u4e2a 0x<\/span> \u540e\u9762\u4e00\u4e2a\u6216\u591a\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff08hexDigits<\/span>\uff09\u7684\u4e32\u3002\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u5b57\uff08hexDigit<\/span>\uff09\u662f\u4ece 0 \u5230 9\uff0c\u6216\u4ece a \u5230 f\uff0c\u6216 A \u5230 F\uff08\u5305\u62ec\u5927\u5199\u548c\u5c0f\u5199\u7684\u60c5\u51b5\uff09\u7684\u4e00\u4e2a\u5b57\u7b26\u3002\u6211\u4e5f\u4fee\u6539\u4e86 Number<\/span> \u89c4\u5219\u6765\u8bc6\u522b\u5341\u516d\u8fdb\u5236\u6570\u4f5c\u4e3a\u53e6\u5916\u4e00\u79cd\u53ef\u80fd\u7684\u60c5\u51b5\u3002\u73b0\u5728\u6211\u4eec\u53ea\u9700\u8981\u53e6\u4e00\u6761\u9488\u5bf9\u5341\u516d\u8fdb\u5236\u6570\u7684\u529f\u80fd\u89c4\u5219\u3002<\/p>\n

    hex: function(a,b) {\r\n        return parseInt(this.sourceString,16);\r\n    }\r\n<\/pre>\n

\u6ce8\u610f\u5230\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u628a16<\/span> \u4f5c\u4e3a\u57fa\u5e95\u4f20\u9012\u7ed9parseInt<\/span>\uff0c\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b JavaScript \u77e5\u9053\u8fd9\u662f\u4e00\u4e2a\u5341\u516d\u8fdb\u5236\u6570\u3002
\n\u6211\u7565\u8fc7\u4e86\u4e00\u4e9b\u5f88\u91cd\u8981\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u3002hexDigit<\/span> \u7684\u89c4\u5219\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n

  hexDigit := \"0\"..\"9\" | \"a\"..\"f\" | \"A\"..\"F\"\r\n<\/pre>\n

\u6ce8\u610f\u6211\u4f7f\u7528\u7684\u662f:=<\/span>\u800c\u4e0d\u662f =<\/span>\u3002\u5728 Ohm \u4e2d\uff0c:=<\/span>\u662f\u5f53\u4f60\u9700\u8981\u63a8\u7ffb\u4e00\u6761\u89c4\u5219\u7684\u65f6\u5019\u4f7f\u7528\u3002\u8fd9\u8868\u660e Ohm \u5df2\u7ecf\u6709\u4e86\u4e00\u6761\u9488\u5bf9 hexDigit<\/span> \u7684\u9ed8\u8ba4\u89c4\u5219\uff0c\u5c31\u50cf digit<\/span>\u3001space<\/span> \u7b49\u4e00\u5806\u5176\u4ed6\u7684\u4e1c\u897f\u3002\u5982\u679c\u6211\u4f7f\u7528\u4e86 =<\/span>, Ohm \u5c06\u4f1a\u62a5\u544a\u4e00\u4e2a\u9519\u8bef\u3002\u8fd9\u662f\u4e00\u4e2a\u68c0\u67e5\uff0c\u4ece\u800c\u907f\u514d\u6211\u65e0\u610f\u8bc6\u7684\u63a8\u7ffb\u4e00\u4e2a\u89c4\u5219\u3002\u7531\u4e8e\u65b0\u7684hexDigit<\/span> \u89c4\u5219\u548c Ohm \u7684\u6784\u5efa\u89c4\u5219\u4e00\u6837\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u6ce8\u91ca\u6389\uff0c\u7136\u540e\u8ba9 Ohm \u81ea\u5df1\u6765\u5b9e\u73b0\u5b83\u3002\u6211\u7559\u4e0b\u8fd9\u4e2a\u89c4\u5219\u53ea\u662f\u56e0\u4e3a\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5b83\u5b9e\u9645\u4e0a\u662f\u5982\u4f55\u8fdb\u884c\u7684\u3002
\n\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u6d4b\u8bd5\u7136\u540e\u770b\u5230\u5341\u516d\u8fdb\u5236\u6570\u771f\u7684\u80fd\u5de5\u4f5c\uff1a<\/p>\n

test('0x456',0x456);\r\ntest('0xFF',255);\r\n<\/pre>\n
\u79d1\u5b66\u8ba1\u6570<\/strong><\/span><\/div>\n

\u6700\u540e\uff0c\u8ba9\u6211\u4eec\u6765\u652f\u6301\u79d1\u5b66\u8ba1\u6570\u3002\u79d1\u5b66\u8ba1\u6570\u662f\u9488\u5bf9\u975e\u5e38\u5927\u6216\u975e\u5e38\u5c0f\u7684\u6570\u7684\uff0c\u6bd4\u5982 1.8\u00d710^3<\/span>\u3002\u5728\u5927\u591a\u6570\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u79d1\u5b66\u8ba1\u6570\u6cd5\u8868\u793a\u7684\u6570\u4f1a\u5199\u6210\u8fd9\u6837\uff1a1.8e3 \u8868\u793a 18000\uff0c\u6216\u8005 1.8e-3 \u8868\u793a .018\u3002\u8ba9\u6211\u4eec\u589e\u52a0\u53e6\u5916\u4e00\u5bf9\u89c4\u5219\u6765\u652f\u6301\u8fd9\u4e2a\u6307\u6570\u8868\u793a\uff1a<\/p>\n

    float  = digit+ \".\" digit+ exp?\r\n    exp    = \"e\" \"-\"? digit+\r\n<\/pre>\n

\u4e0a\u9762\u5728\u6d6e\u70b9\u6570\u89c4\u5219\u672b\u5c3e\u589e\u52a0\u4e86\u4e00\u4e2a\u6307\u6570\uff08exp<\/span>\uff09\u89c4\u5219\u548c\u4e00\u4e2a?<\/span>\u3002?<\/span> \u8868\u793a\u6ca1\u6709\u6216\u6709\u4e00\u4e2a\uff0c\u6240\u4ee5\u6307\u6570\uff08exp<\/span>\uff09\u662f\u53ef\u9009\u7684\uff0c\u4f46\u662f\u4e0d\u80fd\u8d85\u8fc7\u4e00\u4e2a\u3002\u589e\u52a0\u6307\u6570\uff08exp<\/span>\uff09\u89c4\u5219\u4e5f\u6539\u53d8\u4e86\u6d6e\u70b9\u6570\u89c4\u5219\u7684\u5b9e\u53c2\u6570\u91cf\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e3a\u6d6e\u70b9\u6570\u529f\u80fd\u589e\u52a0\u53e6\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u4f7f\u6211\u4eec\u4e0d\u4f7f\u7528\u5b83\u3002<\/p>\n

    float: function(a,b,c,d) {\r\n        console.log(\"doing float\", this.sourceString);\r\n        return parseFloat(this.sourceString);\r\n    },\r\n<\/pre>\n

\u73b0\u5728\u6211\u4eec\u7684\u6d4b\u8bd5\u53ef\u4ee5\u901a\u8fc7\u4e86\uff1a<\/p>\n

test('4.8e10',4.8e10);\r\ntest('4.8e-10',4.8e-10);\r\n<\/pre>\n
\u7ed3\u8bba<\/strong><\/div>\n

Ohm \u662f\u6784\u5efa\u89e3\u6790\u5668\u7684\u4e00\u4e2a\u5f88\u68d2\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u6613\u4e8e\u4e0a\u624b\uff0c\u5e76\u4e14\u4f60\u53ef\u4ee5\u9012\u589e\u7684\u589e\u52a0\u89c4\u5219\u3002Ohm \u4e5f\u8fd8\u6709\u5176\u4ed6\u6211\u4eca\u5929\u6ca1\u6709\u5199\u5230\u7684\u5f88\u68d2\u7684\u7279\u70b9\uff0c\u6bd4\u5982\u8c03\u8bd5\u89c2\u5bdf\u4eea\u548c\u5b50\u7c7b\u5316\u3002
\n\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528 Ohm \u6765\u628a\u5b57\u7b26\u4e32\u7ffb\u8bd1\u6210 JavaScript \u6570\uff0c\u5e76\u4e14 Ohm \u7ecf\u5e38\u7528\u4e8e\u628a\u4e00\u79cd\u8868\u793a\u65b9\u5f0f\u8f6c\u5316\u6210\u53e6\u5916\u4e00\u79cd\u3002\u7136\u800c\uff0cOhm \u8fd8\u6709\u66f4\u591a\u7684\u7528\u9014\u3002\u901a\u8fc7\u653e\u5165\u4e0d\u540c\u7684\u8bed\u4e49\u529f\u80fd\u96c6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 Ohm \u6765\u771f\u6b63\u5904\u7406\u548c\u8ba1\u7b97\u4e1c\u897f\u3002\u4e00\u4e2a\u5355\u72ec\u7684\u8bed\u6cd5\u53ef\u4ee5\u88ab\u8bb8\u591a\u4e0d\u540c\u7684\u8bed\u4e49\u4f7f\u7528\uff0c\u8fd9\u662f Ohm \u7684\u9b54\u6cd5\u4e4b\u4e00\u3002
\n\u5728\u8fd9\u4e2a\u7cfb\u5217\u7684\u4e0b\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u5411\u4f60\u4eec\u5c55\u793a\u5982\u4f55\u50cf\u771f\u6b63\u7684\u8ba1\u7b97\u673a\u4e00\u6837\u8ba1\u7b97\u50cf (4.85 + 5 * (238 - 68)\/2)<\/span> \u8fd9\u6837\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\uff0c\u4e0d\u4ec5\u4ec5\u662f\u89e3\u6790\u6570\u3002
\n\u989d\u5916\u7684\u6311\u6218\uff1a\u4f60\u80fd\u591f\u6269\u5c55\u8bed\u6cd5\u6765\u652f\u6301\u516b\u8fdb\u5236\u6570\u5417\uff1f\u8fd9\u4e9b\u4ee5 8 \u4e3a\u57fa\u5e95\u7684\u6570\u80fd\u591f\u53ea\u7528 0 \u5230 7 \u8fd9\u51e0\u4e2a\u6570\u5b57\u6765\u8868\u793a\uff0c\u524d\u9762\u52a0\u4e0a\u4e00\u4e2a\u6570\u5b57 0 \u6216\u8005\u5b57\u6bcd o<\/span>\u3002\u770b\u770b\u9488\u5bf9\u4e0b\u9762\u8fd9\u4e9b\u6d4b\u8bd5\u60c5\u51b5\u662f\u591f\u6b63\u786e\u3002\u4e0b\u6b21\u6211\u5c06\u7ed9\u51fa\u7b54\u6848\u3002<\/p>\n

test('0o77',7*8+7);\r\ntest('0o23',0o23);\r\n<\/pre>\n
\n

via: https:\/\/www.pubnub.com\/blog\/2016-08-30-javascript-parser-ohm-makes-creating-a-programming-language-easy\/<\/a><\/p>\n

\u4f5c\u8005\uff1aJosh Marinacci<\/a> \u8bd1\u8005\uff1aucasFL<\/a> \u6821\u5bf9\uff1awxy<\/a><\/p>\n

\u672c\u6587\u7531 LCTT<\/a> \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd<\/a> \u8363\u8a89\u63a8\u51fa<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-7805-1.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/ohm-javascript.html<\/a>\u7f16\u8f91\u5458\uff1a\u5218\u5cf0\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u89e3\u6790\u5668\u662f\u4e00\u79cd\u8d85\u7ea7\u6709\u7528\u7684\u8f6f\u4ef6\u5e93\u3002\u4ece\u6982\u5ff5\u4e0a\u7b80\u5355\u7684\u8bf4\uff0c\u5b83\u4eec\u7684\u5b9e\u73b0\u5f88\u6709\u6311\u6218\u6027\uff0c\u5e76\u4e14\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u7ecf\u5e38\u88ab\u8ba4\u4e3a\u662f\u9ed1\u9b54\u6cd5\u3002\u5728\u8fd9\u4e2a\u7cfb\u5217\u7684\u535a\u6587\u4e2d\uff0c\u6211\u4f1a\u5411\u4f60\u4eec\u5c55\u793a\u4e3a\u4ec0\u4e48\u4f60\u4e0d\u9700\u8981\u6210\u4e3a\u54c8\u5229\u6ce2\u7279\u5c31\u80fd\u591f\u7cbe\u901a\u89e3\u6790\u5668\u8fd9\u79cd\u9b54\u6cd5\u3002\u4f46\u662f\u4e3a\u4e86\u4ee5\u9632\u4e07\u4e00\u5e26\u4e0a\u4f60\u7684\u9b54\u6756\u5427\uff01<\/p>\n","protected":false},"author":116,"featured_media":30488,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-33136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/33136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/116"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=33136"}],"version-history":[{"count":10,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/33136\/revisions"}],"predecessor-version":[{"id":270657,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/33136\/revisions\/270657"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/30488"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=33136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=33136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=33136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}