简介
当脚本给出错误结果时,echo通常会告诉你原因。如果它没有给出信息,调试器可以让你观察脚本的运行过程。你可以在某一行暂停脚本,逐条语句单步执行,并查看每个变量在变化时的值。无需在脚本中添加任何内容,也无需安装任何东西:VS Code 的 KScript 扩展以及 kscript 本身已经包含所需的一切。
这是一个计算购物篮总价的脚本:
<!-- basket.kscript -->
<fn name="line_total" params="price: number, quantity: int">
<let name="total" value="$price * $quantity" />
<return value="$total" />
</fn>
<let name="prices" value="[2.5, 4, 1.25]" />
<let name="quantities" value="[2, 1, 4]" />
<let name="basket_total" value="0" />
<for each="price" at="index" in="$prices">
<set name="basket_total" op="+=" value="@line_total($price, quantity: $quantities[$index])" />
</for>
<echo>Basket: {$basket_total}</echo>// basket.kscript
fn line_total(price: number, quantity: int) {
$total = $price * $quantity
return $total
}
$prices = [2.5, 4, 1.25]
$quantities = [2, 1, 4]
$basket_total = 0
for price at index in $prices {
$basket_total += @line_total($price, quantity: $quantities[$index])
}
echo "Basket: {$basket_total}"在第 11 行左侧的边距中单击,设置断点(红点),然后按 F5。脚本会一直运行到第 11 行并在此暂停。变量视图中现在显示 $price = 2.5、$index = 0 和 $basket_total = 0。按 F11 进入 line_total():调用堆栈中 basket.kscript 之上显示 line_total(),局部变量中显示 $price = 2.5 和 $quantity = 2。按 F10,$total = 5 出现,因为设置它的那一行已经运行完毕。