金鱼Scheme / Goldfish Scheme
让Scheme和Python一样易用且实用!
金鱼Scheme 是一个 Scheme 解释器,具有以下特性:
- 兼容 R7RS-small 标准
- 提供类似 Scala 的函数式集合库
- 提供类似 Python 的功能丰富的标准库
- 小巧且快速
(define* (person (name "Bob") (age 21))
(string-append name ": " (number->string age)))
(person :name "Alice" :age 3)
((box "你好,世界") 0) ; => 你
((box "你好,世界") 4) ; => 界
((box "你好,世界") :length) ; => 5
((box (list 1 2 3 4 5))
:map (lambda (x) (* x x))
:filter even?
:collect) ; => (list 4 16)
((box (vector 1 2 3 4 5))
:map (lambda (x) (* x x))
:filter even?
:collect) ; => (vector 4 16)
(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!"
金鱼Scheme仍旧遵循和 S7 Scheme 一样的简约的原则。目前,它仅依赖于 S7 Scheme 、tbox 和 C++98 范围内的标准库。
与 S7 Scheme 类似,src/goldfish.hpp 和 src/goldfish.cpp 是构建金鱼Scheme解释器二进制文件所需的唯一关键源代码。
形如(liii xyz)
的是金鱼标准库,模仿Python标准库和Scala集合库的函数接口和实现方式,降低用户的学习成本。
库 | 描述 | 示例函数 |
---|---|---|
(liii lang) | 类似Scala的集合库 | box 支持一致的函数式集合库, case-char 和case-string 支持Unicode |
(liii base) | 基础库 | == , != , display* |
(liii error) | 提供类似Python的错误函数 | os-error 函数抛出'os-error ,类似Python的OSError |
(liii check) | 基于SRFI 78的轻量级测试库加强版 | check , check-catch |
(liii case) | 模式匹配 | case* |
(liii list) | 列表函数库 | list-view , fold |
(liii bitwise) | 位运算函数库 | bitwise-and , bitwise-or |
(liii string) | 字符串函数库 | string-join |
(liii vector) | 向量函数库 | vector-index |
(liii stack) | 栈 | stack-pop! , stack-push! |
(liii hash-table) | 哈希表 | hash-table-empty? , hash-table-contains? |
(liii sys) | 库类似于 Python 的 sys 模块 |
argv |
(liii os) | 库类似于 Python 的 os 模块 |
getenv , mkdir |
(liii path) | 路径函数库 | path-dir? , path-file? |
(liii uuid) | UUID 生成 | uuid4 |
库 | 状态 | 描述 |
---|---|---|
(srfi srfi-1) |
部分 | 列表库 |
(srfi srfi-8) |
完整 | 提供 receive |
(srfi srfi-9) |
完整 | 提供 define-record-type |
(srfi srfi-13) |
完整 | 字符串库 |
(srfi srfi-16) |
完整 | 提供 case-lambda |
(srfi srfi-39) |
完整 | 参数对象 |
(srfi srfi-78) |
部分 | 轻量级测试框架 |
(srfi srfi-125) |
部分 | 哈希表 |
(srfi srfi-133) |
部分 | 向量 |
(srfi srfi-151) |
部分 | 位运算 |
(srfi srfi-216) |
部分 | SICP |
库 | 描述 |
---|---|
(scheme base) |
基础库 |
(scheme case-lambda) |
提供 case-lambda |
(scheme char) |
字符函数库 |
(scheme file) |
文件操作 |
(scheme time) |
时间库 |
金鱼Scheme自 v1.2.8 起已集成在墨干理工套件中,只需安装墨干即可安装 金鱼Scheme。
除了金鱼Scheme解释器外,墨干还提供了一个结构化的金鱼Scheme REPL。
以下是在 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/goldfish --version
您也可以将其安装到 /opt
:
sudo xmake i -o /opt/goldfish --root
/opt/goldfish/bin/goldfish
卸载时只需:
sudo rm -rf /opt/goldfish
本节假设您已成功执行 xmake b goldfish
并且 bin/goldfish
可用。
不使用任何选项时,它将打印帮助信息:
> bin/goldfish
Goldfish Scheme 17.10.0 by LiiiLabs
--version display version
-e -e '(+ 1 2)'
-l FILE Load the scheme code on path
FILE Load the scheme code on path and print the evaluated result
--version
将打印 金鱼Scheme 的版本和 S7 Scheme 的版本:
> bin/goldfish --version
Goldfish Scheme 17.10.0 by LiiiLabs
based on S7 Scheme 10.11 (2-July-2024)
-m
帮助您指定预加载的标准库。
default
:-m default
等价于-m liii
liii
: 预加载(liii lang)
,(liii base)
和(liii error)
的Goldfish Schemescheme
: 预加载(liii base)
和(liii error)
的Goldfish Schemesicp
: 预加载(srfi sicp)
和(scheme base)
的S7 Schemer7rs
: 预加载(scheme base)
的S7 Schemes7
: 无任何无加载库的S7 Scheme
-e
帮助您即时求值 Scheme 代码:
> bin/goldfish -e "(+ 1 2)"
3
> bin/goldfish -e "(begin (import (srfi srfi-1)) (first (list 1 2 3)))"
1
> bin/goldfish -e "(begin (import (liii sys)) (display (argv)) (newline))" 1 2 3
("bin/goldfish" "-e" "(begin (import (liii sys)) (display (argv)) (newline))" "1" "2" 3)
#\newline
-l
帮助您加载文件:
> bin/goldfish -l tests/demo_error.scm
;car argument, (), is nil but should be a pair
; (list)
; tests/demo_error.scm, line 1, position: 10
; (list)
> bin/goldfish -l tests/demo_no_error.scm
> bin/goldfish -l tests/demo_argv.scm 1 2 3
("bin/goldfish" "tests/demo_argv.scm" "1" "2" "3")
如果没有提供选项,它将加载文件并打印求值结果:
> bin/goldfish tests/demo_no_error.scm
tests/demo_no_error.scm => 3
> bin/goldfish tests/demo_error.scm
;car argument, (), is nil but should be a pair
; (list)
; tests/demo_error.scm, line 1, position: 10
; (list)
tests/demo_error.scm => wrong-type-arg
> bin/goldfish tests/demo_argv.scm 1 2 3
("bin/goldfish" "tests/demo_argv.scm" "1" "2" "3")
tests/demo_argv.scm => #\newline
注意,文件和求值结果之间用 =>
分隔。
金鱼Scheme x.y.z
表示它使用的是 tbox 版本 x
,基于 S7 Scheme y
,而 z
是补丁版本。例如,金鱼Scheme 的第二个版本是 17.10.1
,表示它使用tbox 1.7.x
,基于 S7 Scheme 10.x
,补丁版本是 1
。
金鱼Scheme 是为了克服 S7 Scheme 的缺陷而实现的:
- 在 Linux/macOS/Windows 上分发无须编译即可安装的金鱼Scheme解释器和结构化的REPL
- 尝试实现 R7RS-small 标准
- 尝试以 R7RS 库格式提供有用的 SRFI
金鱼Scheme 根据 Apache 2.0 许可证授权,一些源自 S7 Scheme 和 SRFI 的代码片段已在相关源文件中明确声明。
读者可以用以下的BibTeX代码来引用我们的工作.
@book{goldfish,
author = {Da Shen and Nian Liu and Yansong Li and Duolei Wang and Leiyu He and Yingyao Zhou and Shuting Zhao and Shen Wei and Noctis Zhang and Andy Yu and Siyu Xing and Jiayi Dong and Yancheng Li and Xinyi Yu},
title = {Goldfish Scheme: A Scheme Interpreter with Python-Like Standard Library},
publisher = {LIII NETWORK},
year = {2024},
url = {https://gitee.com/LiiiLabs/goldfish/releases/download/v17.10.9/Goldfish.pdf}
}