{"id":13680494,"url":"https://github.com/bd4sur/Animac","last_synced_at":"2025-04-29T23:31:56.285Z","repository":{"id":78094493,"uuid":"168516869","full_name":"bd4sur/Animac","owner":"bd4sur","description":"Scheme语言实现和运行时环境 / Scheme runtime \u0026 implementation","archived":false,"fork":false,"pushed_at":"2025-04-13T14:24:23.000Z","size":3810,"stargazers_count":66,"open_issues_count":9,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T15:31:20.952Z","etag":null,"topics":["compiler","interpreter","javascript","scheme","scheme-compiler","scheme-interpreter","virtual-machine","vm"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/bd4sur.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-01-31T11:49:47.000Z","updated_at":"2025-04-13T14:24:26.000Z","dependencies_parsed_at":"2023-04-08T15:30:42.489Z","dependency_job_id":"2d12436f-bb87-45e1-8cba-3bab5a2ea262","html_url":"https://github.com/bd4sur/Animac","commit_stats":null,"previous_names":["bd4sur/animach"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd4sur%2FAnimac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd4sur%2FAnimac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd4sur%2FAnimac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bd4sur%2FAnimac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bd4sur","download_url":"https://codeload.github.com/bd4sur/Animac/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251600385,"owners_count":21615700,"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":["compiler","interpreter","javascript","scheme","scheme-compiler","scheme-interpreter","virtual-machine","vm"],"created_at":"2024-08-02T13:01:17.688Z","updated_at":"2025-04-29T23:31:51.275Z","avatar_url":"https://github.com/bd4sur.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\u003cimg src=\"./doc/logo.png\" width=\"400\"\u003e\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003eAnimac · 灵机\u003c/h1\u003e\n\n**灵机 · Animac**是一款[Scheme](https://zh.wikipedia.org/wiki/Scheme)解释器，是Scheme语言的一个实现。Animac能够将Scheme代码编译为中间语言代码，并且在虚拟机上执行中间语言代码。目前，Animac使用TypeScript开发，基于Node.js实现。\n\n## 演示\n\n**案例1**：在Animac的REPL中，通过调用事先封装好的Scheme接口模块`chatbot.scm`，构造提示语（prompt），通过本地宿主环境接口，将其传递给部署于本地的[ChatGLM2-6B](https://github.com/THUDM/ChatGLM2-6B)语言模型，并且将ChatGLM2推理输出的文本回传Animac，实现问答式对话。\n\nhttps://github.com/bd4sur/Animac/assets/20069428/6fb423b5-7798-41e8-917c-ed828b54ac3e\n\n## 开始使用\n\n请先安装Node.js，建议使用 V18.17.1 LTS。然后执行以下命令，以启动Animac：\n\n```\ngit clone https://github.com/bd4sur/Animac.git\ncd Animac\nnode build/animac.js -h\n```\n\n构建：`npx tsc`\n\n## 系统框图\n\n![System Architecture](./doc/sysarch.png)\n\n## 特性\n\n### Scheme语言特性\n\n- 支持Scheme核心子集，包括第一等（first-class）的函数、词法作用域和列表操作。\n- 支持第一等续体（continuation）和`call/cc`。\n- 自动尾调用优化。\n- 具备模块机制，可检测并管理模块间依赖关系。\n- **不遵守R\u003csup\u003e5\u003c/sup\u003eRS标准**。\n\n### 运行时系统\n\n- 基于栈的虚拟机，执行Scheme代码编译成的中间语言代码。\n- 支持虚拟机层次上的多线程。支持“端口”机制以实现线程间通信。\n- 暂不具备垃圾回收机制。\n\n### 宿主接口和可扩展性\n\n- 提供宿主接口机制，称为“Native接口”，类似于JNI，实现Animac与宿主环境（Node.js）的互操作，例如文件读写、网络收发等。通过Native接口的二次开发，可以灵活扩展Animac的功能，无需修改Animac核心。\n\n## 用例\n\n全部测试用例位于 [`test`](https://github.com/bd4sur/Animac/tree/master/test) 目录，主要包括3个用例集合和两个较大规模的单独的测试用例，详情如下。\n\n用例集1 `test/test_1.scm` 包括：\n\n- 格式化输出日历\n- 简单递归函数\n- 列表操作\n- Man or Boy test ([Wikipedia](https://en.wikipedia.org/wiki/Man_or_boy_test))\n- 使用CPS风格实现的复杂的阶乘\n- 快速排序\n- Quine（自己输出自己的程序）\n- 准引用列表\n- *The Little Schemer* 书中给出的简单解释器\n- 中缀表达式解析器\n- 生成器\n- 快速傅里叶变换（FFT）\n- 基于FFT的大整数乘法\n\n用例集2 `test/test_2.scm` 包括：\n\n- Church encoding ([Wikipedia](https://en.wikipedia.org/wiki/Church_encoding))\n- Brainfuck解释器\n\n用例集3 `test/test_3.scm` 包括：\n\n- 线程和本地库（文件、HTTPS）测试\n- Yin-yang puzzle ([Wikipedia](https://en.wikipedia.org/wiki/Call-with-current-continuation#Examples))\n\n用例 `test/test_deadlock.scm` 基于虚拟机提供的多线程机制，实现了一个死锁现象的案例，旨在测试线程调度器和端口操作。\n\n用例 `test/test_cr.scm` 基于`call/cc`实现了一个简单的协程机制，借助典型的生产者消费者问题来演示单个虚拟机线程内实现关键资源的无锁同步操作能力。\n\n**词法作用域**\n\n```scheme\n(define free 100)\n(define foo (lambda () `(,free))) ; 准引用列表也是词法作用域的\n(define bar (lambda (free) (foo)))\n(bar 200) ; 输出(100)，而不是(200)\n```\n\n**函数作为一等公民**\n\n```scheme\n(import List \"test/std.list.scm\")         ; 引入列表操作高阶函数\n(native Math)                             ; 声明使用数学本地库\n(List.reduce '(1 2 3 4 5 6 7 8 9 10) + 0) ; 55\n(List.map '(-2 -1 0 1 2) Math.abs)        ; (2 1 0 1 2)\n(List.filter '(0 1 2 3)\n             (lambda (x) (= 0 (% x 2))))  ; (0 2)\n```\n\n**Quine（自己输出自己的程序）**\n\n```scheme\n((lambda (x) (cons x (cons (cons quote (cons x '())) '()))) (quote (lambda (x) (cons x (cons (cons quote (cons x '())) '())))))\n```\n\n**续体和`call/cc`**\n\n```scheme\n;; Yin-yang puzzle\n;; see https://en.wikipedia.org/wiki/Call-with-current-continuation\n(((lambda (x) (begin (display \"@\") x)) (call/cc (lambda (k) k)))\n ((lambda (x) (begin (display \"*\") x)) (call/cc (lambda (k) k))))\n; Output @*@**@***@**** ...\n```\n\n## 研发方针\n\n- **Animac是一个实验性的系统，并非健壮可靠的软件产品**。开发过程的首要考虑是写出给人看的代码，以代码为文档，将关键设计思想固化在可执行的代码中，而不刻意采用软件工程的种种设计模式和“最佳实践”，以免关键思想被掩盖在软件工程的迷雾之中。\n- **Animac从构建最小可用系统(MVP)开始，持续扩充功能特性“做加法”**。目前，Animac重视解决“从0到1”的问题，暂时不关注性能优化、异常和边界情况处理、安全加固等打造一款成熟软件时所必须考虑的问题。\n- **Animac贯彻极简主义，期望打造成一个自持的、具体而微的系统**。Animac尽可能减少外部依赖，其核心功能仅需Node.js和一份代码即可运行，无需额外安装其他依赖。\n\n## 特性规划\n\n|Features|Priority|Status|\n|----|-----|----|\n|Web IDE|★★★|正在开发|\n|垃圾回收|★★★|待研究|\n|卫生宏和模式匹配|★★★|待研究|\n|自动CPST \u0026 自动柯里化|★★☆|待研究|\n|模板字符串和正则表达式|★★☆|待研究|\n|数值类型塔（数学库）|★★☆|待研究|\n|图形库|★★☆|待研究|\n|尽量兼容R\u003csup\u003en\u003c/sup\u003eRS|★☆☆|待研究|\n|高级编译优化|★☆☆|待研究|\n|C语言重构|★☆☆|待研究|\n|类型系统|★☆☆|待研究|\n\n## 形式语法（BNF表示）\n\n```\n    \u003cSourceCode\u003e ::= (lambda () \u003cTERM\u003e*) CRLF\n          \u003cTerm\u003e ::= \u003cSList\u003e | \u003cLambda\u003e | \u003cQuote\u003e | \u003cUnquote\u003e | \u003cQuasiquote\u003e | \u003cSymbol\u003e\n         \u003cSList\u003e ::= ( \u003cSListSeq\u003e )\n      \u003cSListSeq\u003e ::= \u003cTerm\u003e \u003cSListSeq\u003e | ε\n        \u003cLambda\u003e ::= ( lambda \u003cArgList\u003e \u003cBody\u003e )\n       \u003cArgList\u003e ::= ( \u003cArgListSeq\u003e )\n    \u003cArgListSeq\u003e ::= \u003cArgSymbol\u003e \u003cArgListSeq\u003e | ε\n     \u003cArgSymbol\u003e ::= \u003cSymbol\u003e\n          \u003cBody\u003e ::= \u003cBodyTerm\u003e \u003cBody_\u003e\n         \u003cBody_\u003e ::= \u003cBodyTerm\u003e \u003cBody_\u003e | ε\n      \u003cBodyTerm\u003e ::= \u003cTerm\u003e\n         \u003cQuote\u003e ::= ' \u003cQuoteTerm\u003e | ( quote \u003cQuoteTerm\u003e )\n       \u003cUnquote\u003e ::= , \u003cUnquoteTerm\u003e | ( unquote \u003cQuoteTerm\u003e )\n    \u003cQuasiquote\u003e ::= ` \u003cQuasiquoteTerm\u003e | ( quasiquote \u003cQuoteTerm\u003e )\n     \u003cQuoteTerm\u003e ::= \u003cTerm\u003e\n   \u003cUnquoteTerm\u003e ::= \u003cTerm\u003e\n\u003cQuasiquoteTerm\u003e ::= \u003cTerm\u003e\n        \u003cSymbol\u003e ::= SYMBOL\n```\n\n## 参考文献\n\n- R Kelsey, et al. **Revised^5 Report on the Algorithmic Language Scheme**. 1998.\n- D P Friedman, M Wand. **Essentials of Programming Panguages**. 3rd Edition. 2001.\n- G Springer, D P Friedman. **Scheme and the Art of Programming**. 1989.\n- H Abelson, G J Sussman. **Structure and Interpretation of Computer Programs**. 2nd Edition. 1996.\n- A V Aho, M S Lam, et al. **编译原理**. 第2版. 赵建华等译. 2009.\n- D P Friedman, M Felleisen. **The Little Schemer**. 1995.\n- D P Friedman, M Felleisen. **The Seasoned Schemer**. 1995.\n- B C Pierce. **Types and Programming Languages**. 2002.\n- G L Steele. **Rabbit: A Compiler for Scheme**. 1978.\n- R K Dybvig. **Three Implementation Models for Scheme**. 1987.\n- O Danvy, A Filinski. **Representing Control: A Study of the CPS Transformation**. 1992.\n- C Flanagan, A Sabry, B F Duba, M Felleisen. **The Essence of Compiling with Continuations**. 1993.\n- R A Kelsey. **A Correspondence between Continuation Passing Style and Static Single Assignment Form**. 1995.\n- O Danvy. **Three Steps for the CPS Transformation**. 1992.\n\n## 名称和图标\n\n**Animac**，是自创的合成词，由拉丁语词汇Anima“灵魂”和Machina“机器”缩合而成，寓意“有灵魂的机器”。汉语名称为“**灵机**”，从“灵机一动”而来，也暗示本系统与图**灵机**的计算能力等价。\n\n图标是六元环状图形，表示Eval-Apply循环。相邻的两边，形如“λ”，表示λ-calculus。六边形表示本系统基于Node.js实现。图形整体与艾舍尔名作《[上升与下降](https://en.wikipedia.org/wiki/Ascending_and_Descending)》相似，表达“无限循环”的意思。\n\n## 权利声明\n\n版权所有 \u0026copy; 2019~2023 BD4SUR，保留所有权利。\n\n本系统“按原样”提供，采用MIT协议授权。本系统为作者个人以学习和自用目的所创作的作品。作者不对本系统的质量作任何承诺。作者不保证提供有关本系统的任何形式的解释、维护或支持。作者不为任何人使用此系统所造成的任何正面的或负面的后果负责。\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbd4sur%2FAnimac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbd4sur%2FAnimac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbd4sur%2FAnimac/lists"}