2 个版本
0.0.4 | 2020 年 3 月 27 日 |
---|---|
0.0.3 | 2020 年 3 月 27 日 |
#34 在 #声明
用于 rual-bin
56KB
1.5K SLoC
rual
一个受 Lua 启发的,小巧且以函数优先的 Rust 可嵌入语言。
语言设计
- 简单的语法
- 所有值都应该是不可变的
- 值可以被隐藏
简单且不显眼的函数定义
-- this is a single-line comment..
-{
this is
a multiline comment
}-
-- a flat lambda
f: x, y -> x + y
-- or with a block
g: x, y -> {
let z = 5
x + y + z
}
简单的内置类型
let x = 3
let b = true
let s = 'string'
开箱即用的元组、列表和映射
-- a tuple
let (p, q) = (1, 7)
-- a list of `i32`
let xs = [ 1..10 ]
-- this `xs` shadows the previous definition
let [x, ...xs] = map(xs, \x -> 11 - x) -- x = 10, xs = [ 9..1 ]
-- a map with `Num` keys and `String` values
let m = { 1: '0x01', 0: '0x00' }
语法
并非所有语法都目前支持
声明
let num = 73
let [t, f] = [true, false]
let (x, y) = (21, 'fhtagn')
决策
-- the good ol' `if-else`
if condition {
-- ..
}
else {
-- ..
}
-- ..or a slightly different version
-- this is equivalent to a chain of `if/else if/else` statements
if {
| condition1 -> routine1(),
| condition2 && condition3 -> routine2(),
| _ -> fallback(), -- a fallthrough is necessary
}
-- ..or standard pattern matching
let lvl = match tup {
| (1, 8) -> 1,
| (x, y) when x % y == 0 -> 2,
| _ -> 3
}
函数
-- parameters are listed after `:`, separated by `,`
factorial: x -> {
if {
| x < 2 -> 1,
| _ -> factorial(x - 1)
}
}
sum: x, y -> {
let z = x + y
-- functions return the last expression in their block
z
}
-- functions without parameters are written as
two {
let x = 2
}
-- ..and are ivoked as
two()
-- ..thus being used to represent a lazily initialized value