Plato 是一个用 Swift 编写并受 R 语言启发的解释器。它是一种高级的、命令式、结构化编程和渐进类型语言。其目标是使数学运算更简单,并能够开发可以集成编程语言而无需编译的 iOS 应用程序。
let code = """
a = 25 + (2 ** 3)
b = a - 5
b
"""
let plato = Plato()
try await plato.run(code)
您也可以配置 Plato
// Limit the run loop until the specified value (inclusive).
plato.config.loop = .max(100)
或者使用打印处理器进行自定义打印
let results: [String] = []
plato.config.setPrintHandler { printValue in
results.append(printValue.formattedValue)
}
或者在使用 readLine() 函数时读取用户输入
let code = """
a = readLine()
a // Prints "Plato is awesome!"
"""
let plato = Plato()
plato.config.readLine = .continuation
try await plato.run(code)
plato.readLineContinuation.resume(returning: Value(string: "Plato is awesome!"))
要进行简单的计算,只需将方程式相加,它将打印结果,而无需使用 print
函数。
1 + 3 * 2 # prints 7
"Hello, World!" # prints Hello, World!
或者您可以直接使用传统的 print
函数。
print(10 % 2)
x = 10
y = 1
print("x = ", x, " y = ", y)
print
函数也可以与 separator 和 terminator 参数一起使用,以提供更自定义的表示。
print(1, 2, 3, 4, separator: " ... ", terminator: "\n\n")
# prints 1 ... 2 ... 3 ... 4
print("Hello, ", terminator: "")
print("World")
#prints Hello, World
print(10, 15, 20, 25, separator: ", ")
#prints 10, 15, 20, 25
您可以通过添加新行或使用 **;**
分隔符来编写多个表达式。
1 + 40.5
-23 / 3
print(20.02); 60 == 2
Plato 中的单行注释以 #
开头,或者您也可以通过将多行注释包裹在 ##
中来编写多行注释
# This is a comment
60.2 + 10
70 * 2 # This is also a comment
## This is
A multiline
comment
##
print(61 < 3)
# boolean
true
false
# int
42
1_000_00 # is equivalent to 10000
# float
15.123456
1_00.00_2 # is equivalent to 100.002
# double
0.236819230486571
# string
"Hello, World!"
"Plato" + " is" + " awesome!"
# Array
x = [1, 2, 3, 4]
x[0] = 5
# Append another array
x = x + [5, 6]
# Arrays can hold any type
y = [1, 1.5, true, "hello"]
变量可以使用 =
符号声明,并且隐式地为 any
类型
a = 20
b = 25.5
c = a + b
变量可以被赋予特定的类型。一旦赋值,变量必须始终保持该类型,并且分配不兼容的值将导致错误。
类型使用 :
符号指定。
a: any = 25
b: bool = true
c: int = 1
d: float = 1.5
e: double = 1.1235632
f: number = 25 # accepts float, int and bool
g: string = "This is a string"
h: array = [1, 2, 3]
运算符 | 名称 |
---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
** | 指数 |
% | 取模 |
运算符 | 名称 |
---|---|
== | 等于 |
!= | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于或等于 |
<= | 小于或等于 |
运算符 | 描述 |
---|---|
和 | 逻辑 AND 运算符 |
或 | 逻辑 OR 运算符 |
非 | 逻辑非 |
运算符 | 描述 |
---|---|
= | 为变量赋值 |
*= | 乘法赋值 |
/= | 除法赋值 |
%= | 取模赋值 |
+= | 加法赋值 |
-= | 减法赋值 |
如果条件为 TRUE,则 if 语句执行一组语句。否则,执行 else 语句。
if 2==3 {
print("2==3 is true")
} else if 2<3 {
print("2<3 is true")
} else {
print("no condition is true")
}
i = 0
while i < 10 {
print(i)
i += 1
}
numbers = [1, 2, 3, 4, 5, 6, 7]
for number in numbers {
print(number)
}
for index from 0 to 10 by 1 {
print(index)
}
类型函数是特殊的方法,它方便值的类型转换或执行一些特殊操作,例如 array
。
bool("true") # returns true
int("hello") # returns 0
float(true) # returns 1.0
string(42) # returns "42"
array(1, 2, 3, 4) # returns [1, 2, 3, 4]
array(repeating: 2.5, count: 4) # returns [2.5, 2.5, 2.5, 2.5]
func helloWorld() {
"Hello, World!"
}
func sum(a, b) {
return a + b
}
Plato 中的函数支持多态性,其中参数的类型决定了每个函数的标识。
func mul(a: int, b: int) {
return a * b
}
func mul(a: float, b: float) {
return a * b
}
在 Plato 中,声明函数的顺序很重要。
func sum(a, b) {
return a + b
}
func sum(a: int, b: int) {
return a + b
}
# The first function will always be called because the parameters are
# inplicitly of any type
正确的方法将需要重新排序函数,如下所示
func sum(a: int, b: int) {
return a + b
}
func sum(a, b) {
return a + b
}
您还可以在参数标识符之前使用 at
关键字,以强制在调用函数时使用它。
func sum(at a, at b) {
return a + b
}
result = sum(a: 10, b: 10)
以下是所有内置函数。
print("Hello, World!", 1, 2, 3)
print(1, 2, 3, 4, separator: " ... ", terminator: "\n\n")
random(1, 10)
random(0.1, 0.9)
指数函数: exp
(x 的以 e 为底的指数)
对数函数: log
、log2
、log10
三角函数: cos
、sin
、tan
反三角函数: acos
、asin
、atan
双曲函数: cosh
、sinh
、tanh
反双曲函数: acosh
、asinh
、atanh
幂函数和根函数: pow
、sqrt
伽玛函数: gamma
、logGamma
示例
pow(2, 4) # 16
sqrt(4) # 2
cos(90) # -0.44807363