Janetsh Janet 实现的系统 shell 项目简介
Janetsh 是一个使用 Janet 语言实现的系统 shell。Janet 是一个命令式与函数式编程语言,同时也是一个字节码解释器,它是一个现代的 Lisp,但是列表替换为其它数据结构,包括数组、表、结构与元组,具有更好的实用性和性能。该语言还支持桥接到用 C 编写的原生代码、宏元编程和字节码汇编。此外 Janet 还提供了 REPL 环境,以及运行脚本文件的能力。Janet 客户端程序与核心运行时分开,因此可以嵌入到其它程序中。Janetsh 特性:功能强大的标准库 函数式与命令式编程 强大的 Lisp 宏 运行时可加载用 C/C++、Rust 与 zig 等编写的扩展模块 协程与异常处理基本使用:$ ls -la | head -n 3
total 100
drwxr-xr-x 1 ac users 220 May 13 20:16 .
drwxr-xr-x 1 ac users 760 May 12 21:08 ..
0
$ echo foo > /dev/null
0
$ sleep 5 &
@{:pgid 82190 :procs @[@{:args @[@["sleep"] "5"]
:pid 82190
:stopped false
:redirs @[]}]}
$ rm ./demos/*.gif
0函数式编程:$ (map string/ascii-upper ["functional" "programming"])
@["FUNCTIONAL" "PROGRAMMING"]
$ (defn lines [s] (string/split "\n" s))
<function lines>
$ (lines ($$ ls | head -n 3))
@["build.sh" "demos" "janetsh" ""]
$ echo (reduce + 0 [1 2 3])
6
0命令式编程:$ (string/ascii-upper ($$ echo command string capture))
"COMMAND STRING CAPTURE\n"
$ (if (= 0 ($? touch /tmp/test.txt)) (print "success"))
success
nilSubshells:$ ls | head -n 3 | (out-lines string/ascii-upper)
BUILD.SH
DEMOS
JANETSH
0Exceptions/Errors:$ (try
(do
($ rm foo.txt)
($ rm bar.txt)
($ rm baz.txt))
([err] (print "got an error:" err)))
total 100
drwxr-xr-x 1 ac users 220 May 13 20:16 .
drwxr-xr-x 1 ac users 760 May 12 21:08 ..
0
$ echo foo > /dev/null
0
$ sleep 5 &
@{:pgid 82190 :procs @[@{:args @[@["sleep"] "5"]
:pid 82190
:stopped false
:redirs @[]}]}
$ rm ./demos/*.gif
0函数式编程:$ (map string/ascii-upper ["functional" "programming"])
@["FUNCTIONAL" "PROGRAMMING"]
$ (defn lines [s] (string/split "\n" s))
<function lines>
$ (lines ($$ ls | head -n 3))
@["build.sh" "demos" "janetsh" ""]
$ echo (reduce + 0 [1 2 3])
6
0命令式编程:$ (string/ascii-upper ($$ echo command string capture))
"COMMAND STRING CAPTURE\n"
$ (if (= 0 ($? touch /tmp/test.txt)) (print "success"))
success
nilSubshells:$ ls | head -n 3 | (out-lines string/ascii-upper)
BUILD.SH
DEMOS
JANETSH
0Exceptions/Errors:$ (try
(do
($ rm foo.txt)
($ rm bar.txt)
($ rm baz.txt))
([err] (print "got an error:" err)))