diff --git a/README.md b/README.md index 6ce1ea1d..eef23950 100644 --- a/README.md +++ b/README.md @@ -3,62 +3,12 @@ Goldfish Scheme is a Scheme interpreter with the following features: + R7RS-small compatible -+ Scala-like functional collection + Python-like versatile standard library ++ AI Coding friendly + Small and fast 示例图片 -## Demo Code -### Named parameter -``` scheme -(define* (person (name "Bob") (age 21)) - (string-append name ": " (number->string age))) - -(person :name "Alice" :age 3) -``` -### Unicode Support -``` scheme -(import (liii lang)) - -($ "你好,世界" 0) ; => 你 -($ "你好,世界" 4) ; => 界 -($ "你好,世界" :length) ; => 5 -``` - -### Functional Data Pipeline -![](r7rs_vs_goldfish.png) - -With `prime?` provided, filter twin prime numbers in this way: -``` scheme -(import (liii lang)) - -(($ 1 :to 100) - :filter prime? - :filter (lambda (x) (prime? (+ x 2))) - :map (lambda (x) (cons x (+ x 2))) - :collect) -``` - -### Scala like case class -``` scheme -(define-case-class person - ((name string?) - (age integer?)) - - (define (%to-string) - (string-append "I am " name " " (number->string age) " years old!")) - (define (%greet x) - (string-append "Hi " x ", " (%to-string)))) - -(define bob (person "Bob" 21)) - -(bob :to-string) ; => "I am Bob 21 years old!" -(bob :greet "Alice") ; => "Hi Alice, I am Bob 21 years old!" -``` - -> **Performance Warning**: `define-case-class` is implemented via macros and has significant performance overhead. It is suitable for hand-written code and prototyping, but **not recommended for AI-generated code or production deployments**. - ## Simplicity is Beauty Goldfish Scheme still follows the same principle of simplicity as S7 Scheme. Currently, Goldfish Scheme only depends on [S7 Scheme](https://ccrma.stanford.edu/software/s7/), [tbox](https://gitee.com/tboox/tbox) and C++ standard library defined in C++ 98. @@ -66,16 +16,6 @@ Just like S7 Scheme, [src/goldfish.hpp](src/goldfish.hpp) and [src/goldfish.cpp] ## Standard Library -### Scala-like collections -| Library | Description | -|---------|-------------| -| [(liii rich-char)](tests/goldfish/liii/rich-char-test.scm) | boxed char with rich char and instance methods | -| [(liii rich-string)](tests/goldfish/liii/rich-string-test.scm) | boxed string with rich char and instance methods | -| [(liii rich-list)](tests/goldfish/liii/rich-list-test.scm) | boxed list with rich static and instance methods | -| [(liii rich-vector)](tests/goldfish/liii/rich-vector-test.scm) | boxed vector with rich static and instance methods | -| [(liii rich-hash-table)](tests/goldfish/liii/rich-hash-table-test.scm) | boxed hash-table with rich static and instance methods | -| [(liii rich-path)](tests/goldfish/liii/rich-path-test.scm) | boxed path with rich static and instance methods | - ### Python-like standard library | Library | Description | Example functions | @@ -94,7 +34,10 @@ Just like S7 Scheme, [src/goldfish.hpp](src/goldfish.hpp) and [src/goldfish.cpp] | [(liii path)](goldfish/liii/path.scm) | Path Library | `path-dir?`, `path-file?` | | [(liii range)](goldfish/liii/range.scm) | Range Library | `numeric-range`, `iota` | | [(liii option)](goldfish/liii/option.scm) | Option Type Library | `option?`, `option-map`, `option-flatten` | +| [(liii either)](goldfish/liii/either.scm) | Either Type Library | `left?`, `right?`, `either-map` | | [(liii uuid)](goldfish/liii/uuid.scm) | UUID generation | `uuid4` | +| [(liii http)](goldfish/liii/http.scm) | HTTP client library | `http-get`, `http-post`, `http-head` | +| [(liii json)](goldfish/liii/json.scm) | JSON parsing and manipulation | `string->json`, `json->string` | ### SRFI @@ -131,26 +74,6 @@ Besides the Goldfish Scheme interpreter, a nice structured [Goldfish Scheme REPL The following guide will help you build and install Goldfish step by step. -### GNU/Linux -Here are commandlines to build it on Debian bookworm: -``` -sudo apt install xmake git unzip curl g++ -git clone https://gitee.com/LiiiLabs/goldfish.git -# git clone https://github.com/LiiiLabs/goldfish.git -cd goldfish -xmake b goldfish -bin/gf --version -``` -You can also install it to `/opt`: -``` -sudo xmake i -o /opt/goldfish --root -/opt/goldfish/bin/gf -``` -For uninstallation, just: -``` -sudo rm -rf /opt/goldfish -``` - ### macOS Here are commandlines to build it on macOS: ``` @@ -163,7 +86,7 @@ brew uninstall goldfish ``` ## Commandlinefu -This section assumes you have executed `xmake b goldfish` sucessfully and `bin/gf` is available. +If you build from source manually, you can find the executable at `bin/gf`. ### Subcommands @@ -184,7 +107,7 @@ Goldfish Scheme uses subcommands for different operations: ### Display Help Without any command, it will print the help message: ``` -> bin/gf +> gf Goldfish Scheme 17.11.37 by LiiiLabs Commands: @@ -198,7 +121,7 @@ Commands: ### Display Version `version` subcommand will print the Goldfish Scheme version and the underlying S7 Scheme version: ``` -> bin/gf version +> gf version Goldfish Scheme 17.11.37 by LiiiLabs based on S7 Scheme 11.5 (22-Sep-2025) ``` @@ -206,33 +129,33 @@ based on S7 Scheme 11.5 (22-Sep-2025) ### Evaluate Code `eval` subcommand helps you evaluate Scheme code on the fly: ``` -> bin/gf eval "(+ 1 2)" +> gf eval "(+ 1 2)" 3 -> bin/gf eval "(begin (import (srfi srfi-1)) (first (list 1 2 3)))" +> gf eval "(begin (import (srfi srfi-1)) (first (list 1 2 3)))" 1 -> bin/gf eval "(begin (import (liii sys)) (display (argv)) (newline))" 1 2 3 +> gf eval "(begin (import (liii sys)) (display (argv)) (newline))" 1 2 3 ("bin/gf" "eval" "(begin (import (liii sys)) (display (argv)) (newline))" "1" "2" "3") ``` ### Load File `load` subcommand helps you load a Scheme file and enter REPL: ``` -> bin/gf load tests/goldfish/liii/base-test.scm +> gf load tests/goldfish/liii/base-test.scm ; load the file and enter REPL ``` ### Run File Directly You can also load and evaluate a Scheme file directly: ``` -> bin/gf tests/goldfish/liii/base-test.scm +> gf tests/goldfish/liii/base-test.scm ; *** checks *** : 1973 correct, 0 failed. ``` ### Mode Option `-m` or `--mode` helps you specify the standard library mode: -+ `default`: `-m default` is the equiv of `-m liii` -+ `liii`: Goldfish Scheme with `(liii oop)`, `(liii base)` and `(liii error)` ++ `default`: `-m default` is the equiv of `-m r7rs` ++ `liii`: Goldfish Scheme with `(liii base)`, `(liii error)` and `(liii string)` + `scheme`: Goldfish Scheme with `(liii base)` and `(liii error)` + `sicp`: S7 Scheme with `(scheme base)` and `(srfi sicp)` + `r7rs`: S7 Scheme with `(scheme base)` @@ -246,7 +169,7 @@ Goldfish also supports extra library search directories during startup: For example: ```bash -bin/gf -I ~/.local/goldfish/liii-goldfix eval "(begin (import (liii goldfix)) 'ok)" +gf -I ~/.local/goldfish/liii-goldfix eval "(begin (import (liii goldfix)) 'ok)" ``` On startup, Goldfish also automatically prepends each directory under `~/.local/goldfish/` whose name matches `xxx-yyy` and which contains at least one `.scm` file. diff --git a/README_ZH.md b/README_ZH.md index 9923226b..b6fe3821 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -3,84 +3,23 @@ 金鱼Scheme 是一个 Scheme 解释器,具有以下特性: + 兼容 R7RS-small 标准 -+ 提供类似 Scala 的函数式集合库 + 提供类似 Python 的功能丰富的标准库 ++ AI 编程友好 + 小巧且快速 示例图片 -## 示例代码 -### 具名参数 -``` scheme -(define* (person (name "Bob") (age 21)) - (string-append name ": " (number->string age))) - -(person :name "Alice" :age 3) -``` -### Unicode支持 -``` scheme -(import (liii lang)) - -($ "你好,世界" 0) ; => 你 -($ "你好,世界" 4) ; => 界 -($ "你好,世界" :length) ; => 5 -``` - -### 函数式数据管道 -![](r7rs_vs_goldfish.png) - -在`prime?`已提供的情况下,用如下方法过滤出1到100的孪生质数(致敬张益唐): -``` scheme -(import (liii lang)) - -(($ 1 :to 100) - :filter prime? - :filter (lambda (x) (prime? (+ x 2))) - :map (lambda (x) (cons x (+ x 2))) - :collect) -``` - -### 类似Scala的case class -``` scheme -(define-case-class person - ((name string?) - (age integer?)) - - (define (%to-string) - (string-append "I am " name " " (number->string age) " years old!")) - (define (%greet x) - (string-append "Hi " x ", " (%to-string)))) - -(define bob (person "Bob" 21)) - -(bob :to-string) ; => "I am Bob 21 years old!" -(bob :greet "Alice") ; => "Hi Alice, I am Bob 21 years old!" -``` - -> **性能警告**:`define-case-class` 通过宏实现,有显著的性能开销。它适合手写代码和原型开发,但**不推荐用于 AI 生成的代码或生产环境部署**。 - ## 以简为美 金鱼Scheme仍旧遵循和 S7 Scheme 一样的简约的原则。目前,它仅依赖于 [S7 Scheme](https://ccrma.stanford.edu/software/s7/) 、[tbox](https://gitee.com/tboox/tbox) 和 C++98 范围内的标准库。 与 S7 Scheme 类似,[src/goldfish.hpp](src/goldfish.hpp) 和 [src/goldfish.cpp](src/goldfish.cpp) 是构建金鱼Scheme解释器二进制文件所需的唯一关键源代码。 ## 标准库 -### 类似Scala的集合 -| 库 | 描述 | -|------------------------------------------------------------|------------------------------------| -| [(liii rich-char)](tests/goldfish/liii/rich-char-test.scm) | 面向`char`的静态方法和实例方法 | -| [(liii rich-string)](tests/goldfish/liii/rich-string-test.scm) | 面向`string`的静态方法和实例方法 | -| [(liii rich-list)](tests/goldfish/liii/rich-list-test.scm) | 面向`list`的静态方法和实例方法 | -| [(liii rich-vector)](tests/goldfish/liii/rich-vector-test.scm) | 面向`vector`的静态方法和实例方法 | -| [(liii rich-hash-table)](tests/goldfish/liii/rich-hash-table-test.scm) | 面向`hash-table`的静态方法和实例方法 | -| [(liii rich-path)](tests/goldfish/liii/rich-path-test.scm) | 面向`path`的静态方法和实例方法 | - ### 类似Python的标准库 -形如`(liii xyz)`的是金鱼标准库,模仿Python标准库和Scala集合库的函数接口和实现方式,降低用户的学习成本。 +形如`(liii xyz)`的是金鱼标准库,模仿Python标准库的函数接口和实现方式,降低用户的学习成本。 | 库 | 描述 | 示例函数 | | ------------------------------------------------- | ------------------------------- | ------------------------------------------------------------------ | -| [(liii lang)](goldfish/liii/lang.scm) | 类似Scala的集合库 | `box`支持一致的函数式集合库, `rich-char`和`rich-string`支持Unicode | | [(liii base)](goldfish/liii/base.scm) | 基础库 | `==`, `!=`, `display*` | | [(liii error)](goldfish/liii/error.scm) | 提供类似Python的错误函数 | `os-error`函数抛出`'os-error`,类似Python的OSError | | [(liii check)](goldfish/liii/check.scm) | 基于SRFI 78的轻量级测试库加强版 | `check`, `check-catch` | @@ -95,7 +34,10 @@ | [(liii path)](goldfish/liii/path.scm) | 路径函数库 | `path-dir?`, `path-file?` | | [(liii range)](goldfish/liii/range.scm) | 范围库 | `numeric-range`, `iota` | | [(liii option)](goldfish/liii/option.scm) | Option 类型库 | `option?`, `option-map`, `option-flatten` | +| [(liii either)](goldfish/liii/either.scm) | Either 类型库(左值/右值) | `left?`, `right?`, `either-map` | | [(liii uuid)](goldfish/liii/uuid.scm) | UUID 生成 | `uuid4` | +| [(liii http)](goldfish/liii/http.scm) | HTTP 客户端库 | `http-get`, `http-post`, `http-head` | +| [(liii json)](goldfish/liii/json.scm) | JSON 解析和操作 | `string->json`, `json->string` | ### SRFI @@ -132,26 +74,6 @@ 以下是分步构建和安装指南。 -### GNU/Linux -以下是在 Debian bookworm 上构建的命令: -``` -sudo apt install xmake git unzip curl g++ -git clone https://gitee.com/LiiiLabs/goldfish.git -# git clone https://github.com/LiiiLabs/goldfish.git -cd goldfish -xmake b goldfish -bin/gf --version -``` -您也可以将其安装到 `/opt`: -``` -sudo xmake i -o /opt/goldfish --root -/opt/goldfish/bin/gf -``` -卸载时只需: -``` -sudo rm -rf /opt/goldfish -``` - ### macOS 安装 在 macOS 上,推荐使用 Homebrew 进行安装: ``` @@ -169,7 +91,7 @@ brew uninstall goldfish ``` ## 命令行技巧 -本节假设您已成功执行 `xmake b goldfish` 并且 `bin/gf` 可用。 +如果您手动从源码编译,可以在 `bin/gf` 找到可执行文件。 ### 子命令 @@ -190,7 +112,7 @@ brew uninstall goldfish ### 显示帮助 不带任何命令时,将打印帮助信息: ``` -> bin/gf +> gf Goldfish Scheme 17.11.37 by LiiiLabs Commands: @@ -204,7 +126,7 @@ Commands: ### 显示版本 `version` 子命令将打印 金鱼Scheme 版本和底层 S7 Scheme 版本: ``` -> bin/gf version +> gf version Goldfish Scheme 17.11.37 by LiiiLabs based on S7 Scheme 11.5 (22-Sep-2025) ``` @@ -212,33 +134,33 @@ based on S7 Scheme 11.5 (22-Sep-2025) ### 求值代码 `eval` 子命令帮助您即时求值 Scheme 代码: ``` -> bin/gf eval "(+ 1 2)" +> gf eval "(+ 1 2)" 3 -> bin/gf eval "(begin (import (srfi srfi-1)) (first (list 1 2 3)))" +> gf eval "(begin (import (srfi srfi-1)) (first (list 1 2 3)))" 1 -> bin/gf eval "(begin (import (liii sys)) (display (argv)) (newline))" 1 2 3 +> gf eval "(begin (import (liii sys)) (display (argv)) (newline))" 1 2 3 ("bin/gf" "eval" "(begin (import (liii sys)) (display (argv)) (newline))" "1" "2" "3") ``` ### 加载文件 `load` 子命令帮助您加载 Scheme 文件并进入 REPL: ``` -> bin/gf load tests/goldfish/liii/base-test.scm +> gf load tests/goldfish/liii/base-test.scm ; 加载文件并进入 REPL ``` ### 直接运行文件 您也可以直接加载并求值 Scheme 文件: ``` -> bin/gf tests/goldfish/liii/base-test.scm +> gf tests/goldfish/liii/base-test.scm ; *** checks *** : 1973 correct, 0 failed. ``` ### 模式选项 `-m` 或 `--mode` 帮助您指定标准库模式: -+ `default`: `-m default` 等价于 `-m liii` -+ `liii`: 预加载 `(liii oop)`、`(liii base)` 和 `(liii error)` 的 Goldfish Scheme ++ `default`: `-m default` 等价于 `-m r7rs` ++ `liii`: 预加载 `(liii base)`、`(liii error)` 和 `(liii string)` 的 Goldfish Scheme + `scheme`: 预加载 `(liii base)` 和 `(liii error)` 的 Goldfish Scheme + `sicp`: 预加载 `(scheme base)` 和 `(srfi sicp)` 的 S7 Scheme + `r7rs`: 预加载 `(scheme base)` 的 S7 Scheme @@ -252,7 +174,7 @@ Goldfish 启动时也支持额外的库搜索目录: 例如: ```bash -bin/gf -I ~/.local/goldfish/liii-goldfix eval "(begin (import (liii goldfix)) 'ok)" +gf -I ~/.local/goldfish/liii-goldfix eval "(begin (import (liii goldfix)) 'ok)" ``` 启动时,Goldfish 还会自动把 `~/.local/goldfish/` 下所有名称匹配 `xxx-yyy` 且至少包含一个 `.scm` 文件的目录前置到库搜索路径中。 diff --git a/devel/200_61.md b/devel/200_61.md new file mode 100644 index 00000000..8cc7ba4c --- /dev/null +++ b/devel/200_61.md @@ -0,0 +1,36 @@ +# [200_61] 更新 README 文档 + +## 任务相关的代码文件 +- README.md +- README_ZH.md + +## 2025/04/03 + +### What + +1. 移除基于 Scala 的 data pipeline 相关内容 + - 移除 "Scala-like functional collection" 特性描述 + - 移除 Functional Data Pipeline 示例代码和图片引用 + - 移除 Scala like case class 示例和性能警告 + +2. 移除 (liii lang) 相关内容 + - 移除 Unicode Support 示例(使用 `(liii lang)`) + - 从标准库表格中移除 `(liii lang)` 行 + - 移除 "Scala-like collections" 表格 + +3. 简化安装说明 + - 移除 GNU/Linux 安装说明,保留 macOS 安装说明 + +4. 更新模式选项 + - 移除 `(liii oop)` 预加载项 + - `liii` 模式现在只预加载 `(liii base)` 和 `(liii error)` + +5. 新增标准库 + - 新增 `(liii http)`:HTTP 客户端库 + - 新增 `(liii json)`:JSON 解析和操作库 + +### Why + +由于基于 Scala 的 data pipeline(包括 `(liii lang)`、case class、rich collections 等)已经从代码库中移除,README 文档需要同步更新,以反映当前 Goldfish Scheme 的实际状态,避免误导用户。 + +同时,新增 `(liii http)` 和 `(liii json)` 两个实用库的介绍,帮助用户了解 Goldfish Scheme 的网络和 JSON 处理能力。 diff --git a/src/goldfish.hpp b/src/goldfish.hpp index dd1bf862..795403c1 100644 --- a/src/goldfish.hpp +++ b/src/goldfish.hpp @@ -4547,9 +4547,16 @@ goldfish_repl (s7_scheme* sc, const string& mode) { ic_style_def ("symbol", "cyan"); ic_printf ("[b gold]Goldfish Scheme[/] [b plum]%s[/] by LiiiLabs\n" - "[i]Based on S7 Scheme %s [dim](%s)[/][/]\n" - "[b]Mode:[/] [b]%s[/]\n\n", - GOLDFISH_VERSION, S7_VERSION, S7_DATE, mode.c_str ()); + "[i]Based on S7 Scheme %s [dim](%s)[/][/]\n", + GOLDFISH_VERSION, S7_VERSION, S7_DATE); + // Display mode info; liii mode shows extra imported libraries + if (mode == "liii" || mode == "default") { + ic_printf ("[b]Mode:[/] [b]%s[/] (additionally imports: (liii base) (liii error) (liii string) compared to r7rs)\n\n", + mode.c_str ()); + } + else { + ic_printf ("[b]Mode:[/] [b]%s[/]\n\n", mode.c_str ()); + } ic_printf ("- Type ',quit' or ',q' to quit. (or use [kbd]ctrl-d[/]).\n" "- Type ',help' for REPL commands help.\n" "- Press [kbd]F1[/] for help on editing commands.\n" @@ -4599,8 +4606,8 @@ struct StartupCliOptions { }; static std::string -parse_mode_option (int argc, char** argv) { - std::string mode= "default"; +parse_mode_option (int argc, char** argv, const std::string& default_mode= "default") { + std::string mode= default_mode; for (int i= 1; i < argc; ++i) { string arg= argv[i]; if ((arg == "--mode" || arg == "-m") && (i + 1) < argc) { @@ -5107,7 +5114,6 @@ repl_for_community_edition (s7_scheme* sc, int argc, char** argv) { string command = startup_opts.command; int command_index= startup_opts.command_index; - string mode = parse_mode_option (argc, argv); // 如果没有找到命令或没有参数,显示帮助 if (argc <= 1 || command.empty ()) { @@ -5115,6 +5121,15 @@ repl_for_community_edition (s7_scheme* sc, int argc, char** argv) { return 0; } + // 根据命令类型确定默认模式: + // - repl/load 命令默认使用 liii 模式 + // - 其他命令(eval, run, 直接执行脚本)默认使用 r7rs 模式 + string default_mode= "r7rs"; + if (command == "repl" || command == "load") { + default_mode= "liii"; + } + string mode= parse_mode_option (argc, argv, default_mode); + // 处理旧版的 --help, -h, --version, -v(为了向后兼容) if (command == "--help" || command == "-h") { display_help ();