嘿,脚本开发者们!
还记得上周我介绍的那个能编译成 VBScript 的现代编程语言 Hulo 吗?这周它又有了重大更新!
是的,你没看错! Hulo 现在不仅支持 VBScript ,还支持 Bash 了!
这意味着你可以用同一套现代语法,同时生成 Windows 和 Linux/macOS 的脚本:
// main.hl
class User {
pub name: str
pub age: num
pub fn to_str() -> str {
return "User(name: $name, age: $age)"
}
pub fn greet(other: str) {
MsgBox "Hello, $other! I'm $name."
}
}
let u = User("John", 20)
MsgBox $u.to_str();
$u.greet("Jane");
在文件所在的工作目录执行 hulo main.hl
命令即可得到两份转译后的文件(当然 Hulo 命令还支持其他功能,以及从 huloc.yaml 读取配置,在此就不一一列举了,可以通过 hulo -h
查看,或者查询官方文档)。
生成的 Bash 代码:
#!/bin/bash
function create_user() {
local name=$1
local age=$2
declare -A user
user["name"]=$name
user["age"]=$age
echo "$(declare -p user)"
}
function user_to_str() {
eval "declare -A user=${1}"
echo "User(name: $name, age: $age)"
}
function user_greet() {
eval "declare -A user=${1}"
local other=$2
MsgBox "Hello, $other! I'm $name."
}
u=$(create_user "John" 20)
MsgBox $(user_to_str $u)
user_greet $u "Jane"
生成的 VBScript 代码:
Class User
Public name
Public age
Public Function to_str()
to_str = "User(name: " & name & ", age: " & age & ")"
End Function
Public Function greet(other)
MsgBox("Hello, " & other & "! I'm " & name & ".")
End Function
End Class
Set u = New User
u.name = "John"
u.age = 20
MsgBox(u.to_str())
u.greet("Jane")
至此,看起来一切正常对吗?不幸的是,在 Bash 平台上的代码是无法正常运行的,他会因为缺少 MsgBox 而报错,这是因为我们在 Hulo 代码中使用的是 MsgBox 而非 echo 。因此,如果你想让他正常运行就需要将 MsgBox 更改为 echo 在进行转译。但是,这不就和 Hulo 的跨平台宣传产生冲突了? hhh ,原因是类似 use MsgBox = If<$platform == "vbs", MsgBox, If<$platform == "powershell", Write-Host, echo>>
的语法糖还没有完工,造成了命令没法转译的割裂感。Hulo 也不想采用硬编码的方式强行把命令在转译器中做转换,由此带来了比较差的开发体验。这个特性将在未来的版本实现,请给 Hulo 以时间。
Ps. Hulo 将这个特性称之为命令体操,为了实现这个特性,Hulo 吸取了 TypeScript 类型体操的所有优点,这意味着这套系统将连带着 Omit 、Pick 、Exclude 等类型工具一同构成强大的命令系统。
hlpm
的核心功能就是分发第三方库,由于 import 暂时不支持模块解析,尽管 hlpm 核心功能已经开发完成,但是调用模块的运行还是不支持的。但是,你可以用其先初始化项目,并编写 hulo.pkg.yaml
和 huloc.yaml
文件控制项目的编译过程。这有点类似于 package.json
和 tsconfig.json
的作用。
# 初始化新项目
hlpm init my-script
# 运行脚本
hlpm run test
# 运行文件,等价于 hulo main.hl
hlpm run main.hl
新增了 hulo-repl
命令:
PS C:\hulo> hulo-repl
Hulo-REPL dev
➜ Type help for commands, exit to quit
>>> e
else Else statement
enum Enum declaration
extend Extend declaration
exit Exit the REPL
echo "Hello World"
字符串转译问题本次更新是一次破坏性更新,v0.1.0 版本所实现的功能可能部分无法在 v0.2.0 运行。尤其是涉及到 import 的地方,模块的设计会在接下来更近。
hulo
命令现在支持从工作目录的 huloc.yaml
读取配置项目地址: https://github.com/hulo-lang/hulo
如果你觉得这个项目有意思,欢迎在 GitHub 提 issue 或参与讨论!给个 Star 支持一下,让更多人看到这个项目。
你觉得这种"一次编写,多平台运行"的脚本开发方式怎么样?有什么建议或想法吗?
![]() |
1
xiaohanyu 13 天前
好奇,这门语言设想的应用场景是?
|
2
w568w 13 天前
看着挺好的。建议增加和同类项目的比较:
- https://github.com/amber-lang/amber (这个和 Hulo 一样是类 Rust 语法,本身也是用 Rust 写的) - https://github.com/batsh-dev-team/Batsh - https://github.com/monstermichl/TypeShell 另外你这里 bash 的转译可能有个问题。Bash 的 local scope 是 dynamic scope 而不是 lexical scope ,如果不为每个函数重命名变量,会导致意外的变量覆盖。此外还有不能递归的问题。 |
![]() |
3
ansurfen OP ![]() @w568w okok 我有参考过他们,这个比较会在 Hulo 开发完成度比较高的时候在贴出。这几个项目基本上是把命令当函数使唤,本质上和通用编程语言无异,amber 的 unsafe 没什么用,那样设计不能对命令进行代码提示和类型检测,因此 Hulo 的解决方案是直接将命令当成基础数据类型,对 cmd 进行操作,cmd 就是 class 就是 interface ,这点以后会实现。现在的翻译不是最终定稿,我知道需要对变量名进行混淆,混淆器在实现,但是他涉及到 import "time" as t, import { date as d } from "time",这样情况,我始终没法设计出对 import 的最佳实践,只能边发语言边优化,所以现在基本上只能看个大概。
|