feat: 新增内置函数input
This commit is contained in:
2
examples/input.ast
Normal file
2
examples/input.ast
Normal file
@@ -0,0 +1,2 @@
|
||||
let name = input("Your name: ");
|
||||
print("Hello, " + name)
|
||||
40
examples/script.ast
Normal file
40
examples/script.ast
Normal file
@@ -0,0 +1,40 @@
|
||||
// 压测:循环、递归、闭包
|
||||
|
||||
fn fib(n) {
|
||||
if (n <= 1) {
|
||||
return n
|
||||
}
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
}
|
||||
|
||||
fn make_adder(x) {
|
||||
return fn(y) { return x + y }
|
||||
}
|
||||
|
||||
let t0 = clock()
|
||||
|
||||
// 1. 循环压测:100万次算术
|
||||
let sum = 0
|
||||
let i = 0
|
||||
while (i < 1000000) {
|
||||
sum = sum + i
|
||||
i = i + 1
|
||||
}
|
||||
print("loop 1M:", sum)
|
||||
|
||||
// 2. 递归压测:fib(25)
|
||||
let f = fib(25)
|
||||
print("fib(25):", f)
|
||||
|
||||
// 3. 闭包压测:100万次调用
|
||||
let add10 = make_adder(10)
|
||||
let j = 0
|
||||
let total = 0
|
||||
while (j < 1000000) {
|
||||
total = add10(j)
|
||||
j = j + 1
|
||||
}
|
||||
print("closure 1M calls:", total)
|
||||
|
||||
let t1 = clock()
|
||||
print("total time (s):", t1 - t0)
|
||||
Reference in New Issue
Block a user