{"id":21182280,"url":"https://github.com/tangxiangmin/tiny-compiler","last_synced_at":"2025-07-28T17:07:28.629Z","repository":{"id":79098645,"uuid":"188688085","full_name":"tangxiangmin/tiny-compiler","owner":"tangxiangmin","description":"A simple compiler that converts Dart function named parameter calls to JavaScript function calls","archived":false,"fork":false,"pushed_at":"2019-05-26T13:45:24.000Z","size":2,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T19:50:39.016Z","etag":null,"topics":["compiler","javascript-compiler"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tangxiangmin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-26T13:43:40.000Z","updated_at":"2022-06-02T18:41:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"e73c6e33-cf60-40bb-baac-d4408f33777b","html_url":"https://github.com/tangxiangmin/tiny-compiler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tangxiangmin/tiny-compiler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Ftiny-compiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Ftiny-compiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Ftiny-compiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Ftiny-compiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tangxiangmin","download_url":"https://codeload.github.com/tangxiangmin/tiny-compiler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tangxiangmin%2Ftiny-compiler/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267552097,"owners_count":24106000,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-28T02:00:09.689Z","response_time":68,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","javascript-compiler"],"created_at":"2024-11-20T17:56:21.537Z","updated_at":"2025-07-28T17:07:28.486Z","avatar_url":"https://github.com/tangxiangmin.png","language":"JavaScript","readme":"tiny-compiler\n===\n\n尝试编写一个简单的编译器，感谢[the-super-tiny-compiler](https://github.com/jamiebuilds/the-super-tiny-compiler)。\n\n这个编译器的功能是：将dart函数命名参数调用形式\n```dart\nsayHello(name: \"shymean\", msg: 'hello');\n```\n转换为javascript函数参数调用\n```js\nsayHello({name: 'shymean', msg: 'hello'})\n```\n\n看起来只要加一对大括号就行了吗？不，我们将学习编译原理，并了解编译器的基本工作流程。\n\n## 词法分析\n首先分析输入源代码\n```dart\nlet input = `sayHello(userName:getUsername(\"shymean\"), msg:\"hello\");`;\n```\n将其拆分成一系列token\n```js\n[ { type: 'name', value: 'sayHello' },\n  { type: 'paren', value: '(' },\n  { type: 'name', value: 'userName' },\n  { type: 'colon', value: ':' },\n  { type: 'name', value: 'getUsername' },\n  { type: 'paren', value: '(' },\n  { type: 'string', value: 'shymean' },\n  { type: 'paren', value: ')' },\n  { type: 'comma', value: ',' },\n  { type: 'name', value: 'msg' },\n  { type: 'colon', value: ':' },\n  { type: 'string', value: 'hello' },\n  { type: 'paren', value: ')' } \n```\n\n## 语法分析\n根据词法分析获取的token，然后将其转换成AST\n```js\n{\n    type: \"Program\",\n    body: [\n        {\n            type: \"CallExpression\",\n            name: \"sayHello\",\n            params: [\n                {\n                    type: \"NameParam\",\n                    name: \"userName\",\n                    value: {\n                        type: \"CallExpression\",\n                        name: \"getUsername\",\n                        params: [{ type: \"StringLiteral\", value: \"shymean\" }]\n                    }\n                },\n                {\n                    type: \"NameParam\",\n                    name: \"msg\",\n                    value: { type: \"StringLiteral\", value: \"hello\" }\n                }\n            ]\n        }\n    ]\n}\n```\n\n## 代码生成\n最后根据AST，输出JavaScript代码\n```js\nsayHello({userName:getUsername(\"shymean\"), msg:\"hello\"})\n```\n\n如果从源代码获取的AST直接转换成JS代码比较麻烦，还可以进行中间步骤，即遍历旧的AST，然后输出更符合JavaScript语义的新AST，然后根据新的AST，输出JS代码","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftangxiangmin%2Ftiny-compiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftangxiangmin%2Ftiny-compiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftangxiangmin%2Ftiny-compiler/lists"}