{"id":13779368,"url":"https://github.com/xupingmao/minipy","last_synced_at":"2025-05-11T12:33:21.478Z","repository":{"id":75705268,"uuid":"40075610","full_name":"xupingmao/minipy","owner":"xupingmao","description":"迷你Python解释器(A python interpreter)","archived":false,"fork":false,"pushed_at":"2025-01-12T08:52:45.000Z","size":3977,"stargazers_count":79,"open_issues_count":2,"forks_count":15,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-01-12T09:21:05.874Z","etag":null,"topics":["python-interpreter"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xupingmao.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2015-08-02T10:05:28.000Z","updated_at":"2024-12-28T05:19:46.000Z","dependencies_parsed_at":"2024-01-16T14:04:10.388Z","dependency_job_id":"4efaa352-5093-4032-8c7f-8ad787a24930","html_url":"https://github.com/xupingmao/minipy","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xupingmao%2Fminipy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xupingmao%2Fminipy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xupingmao%2Fminipy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xupingmao%2Fminipy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xupingmao","download_url":"https://codeload.github.com/xupingmao/minipy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253567588,"owners_count":21928857,"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","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":["python-interpreter"],"created_at":"2024-08-03T18:01:04.444Z","updated_at":"2025-05-11T12:33:20.932Z","avatar_url":"https://github.com/xupingmao.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"\u003cpre\u003e\n\n __    __     __     __   __     __     ______   __  __    \n/\\ \"-./  \\   /\\ \\   /\\ \"-.\\ \\   /\\ \\   /\\  == \\ /\\ \\_\\ \\   \n\\ \\ \\-./\\ \\  \\ \\ \\  \\ \\ \\-.  \\  \\ \\ \\  \\ \\  _-/ \\ \\____ \\  \n \\ \\_\\ \\ \\_\\  \\ \\_\\  \\ \\_\\\\\"\\_\\  \\ \\_\\  \\ \\_\\    \\/\\_____\\ \n  \\/_/  \\/_/   \\/_/   \\/_/ \\/_/   \\/_/   \\/_/     \\/_____/ \n                                                           \n\u003c/pre\u003e\n\n# minipy\n\n[![Build Status](https://travis-ci.org/xupingmao/minipy.svg?branch=master)](https://travis-ci.org/xupingmao/minipy)\n\n迷你Python解释器，Python实现的编译器+C语言实现的VM.\n\n注意：本项目主要是用于学习编译器相关原理，如果需要用于生产环境，可以参考以下项目\n- [micropython](https://github.com/micropython/micropython)\n\n## 如何开始\n\n### 编译解释器\n\n```\ncd minipy\nmake \u0026\u0026 make test\n\n# and enjoy yourself ^_^\n```\n\n### 打包成可执行文件\n\n```\n# 编辑 pack/main.py\n\n# 打包\npython3 build.py pack\n\n# 执行打包后的文件\n./pack_main\n```\n\n## 特性\n\n### 编译器\n\n位于 `/src/python`\n\n- `mp_opcode.py` 字节码定义\n- `mp_tokenize.py` 词法分析器，将代码转换成单词(`tokens`)\n    - 运行 `python mp_tokenize.py {script.py}` 可以打印出单词\n- `mp_parse.py` 语法分析器，将单词(`tokens`)转换成语法树(`Syntax Tree`)\n    - 运行 `python mp_parse.py {script.py}` 可以打印出语法树\n- `mp_encode.py` 代码生成器，将语法树(`Syntax Tree`)转换成字节码(`opcodes`)\n    - 运行 `python mp_encode.py {script.py}` 可以打印出字节码(未处理过的)\n\n### 特性\n- [x] 基于栈的计算机模型，字节码定义在 `src/python/mp_opcode.py`\n- [x] 支持异常处理，基于`setjmp/longjmp`实现\n- [x] 支持Native方法扩展\n- [x] 支持常用的Python类型\n- [x] 支持函数定义、简单类定义\n- [x] Mark-Sweep垃圾回收\n- [x] 字符串常量池\n- [x] 尾调用优化\n- [] DEBUG功能\n- [] 用户级线程\n- [] 类的继承\n\n### 工具\n1. minipy -dis {test.py} 打印字节码(常量处理过)\n\n\n### 代码结构\n01. `main.c` 程序入口\n02. `vm.c` 虚拟机入口\n03. `execute.c` 解释器\n04. `builtins.c` 一些常用的内置方法\n05. `obj_ops.c` 对象的操作符实现\n06. `argument.c` 函数调用参数API\n07. `exception.c` 异常处理\n08. `gc.c` 垃圾回收器\n09. `string.c` 字符串处理\n10. `number.c` 数字处理\n11. `list.c`   列表处理\n12. `dict.c`   字典处理\n13. `function.c` 函数/方法处理\n\n### 类型系统\n1. `string`, 是不可变对象\n2. `number`, 全部使用double类型\n3. `list`, 列表（动态数组）\n4. `dict`, 哈希表\n5. `function`, 包括native的C函数和自定义的Python函数\n6. `class`, 自定义Python类型\n7. `None`, None类型\n8. `data`, 自定义的C语言类型\n\n### 相关项目\n\n其他Python的实现\n- [CPython](https://github.com/python/cpython) Python的官方实现版本\n- [micropython](https://github.com/micropython/micropython) 嵌入式版本\n- [tinypy](https://github.com/philhassey/tinypy) 64K的迷你版本，支持Python的部分子集\n- [cython](https://github.com/cython/cython) Python的超集，可以把Python转换成C语言编译以提升运行速度，同时也可以简化Python的C语言扩展的开发\n\n其他嵌入式脚本语言实现\n- Lua\n    - [lua](https://github.com/lua/lua) 非常流行的嵌入式脚本引擎\n    - [luajit](https://github.com/LuaJIT/LuaJIT) lua的JIT性能优化版本\n- JavaScript\n    - [quickjs](https://github.com/bellard/quickjs)\n    - [duktape](https://github.com/svaarala/duktape)\n\n更多有意思的编译器项目\n- [ShivyC](https://github.com/ShivamSarodia/ShivyC) 一个Python编写的C语言编译器\n- [NASM](https://github.com/netwide-assembler/nasm) 跨平台的x86汇编和反汇编器\n\n### 协议\n\n- MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxupingmao%2Fminipy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxupingmao%2Fminipy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxupingmao%2Fminipy/lists"}