Skip to content

Commit

Permalink
添加测试 error
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 1, 2022
1 parent 625a28f commit 7c6fdd4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
5 changes: 2 additions & 3 deletions LANG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
print("Hello World!") // 我是注释
```
以上内容在屏幕上打印出 `Hello World!`
`String` 除了可以用 `'` `"` 包裹,还可以用 `` ` `` 包裹(表示这是个 `Raw String` ),这样可以避免转义字符的问题。


## 基本类型
Expand All @@ -29,8 +28,8 @@ shy a = `
😊`
print(a) // 😊
```
使用 `str` 时可能会遇到需要转义的情况,这时使用 ``` ` ``` 包裹字符,构造一个 `Raw String` 就可以避免内部字符被转义。
⚠️ 如果使用 `Raw String` 构造字符,且第一个字符为换行 ( `\n` ),**一个**换行会被忽略。
`String` 除了可以用 `'` `"` 包裹,还可以用 `` ` `` 包裹( 表示这是个 `Raw String` ),这样可以避免被转义。
⚠️ 如果使用 `Raw String` 构造字符,且第一个字符为换行 ( `\n` ),**这第一个**换行会被忽略。


## 变量
Expand Down
45 changes: 44 additions & 1 deletion state/api_arith.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state

import (
"fmt"
"math"

. "git.lolli.tech/lollipopkit/lk/api"
Expand Down Expand Up @@ -53,6 +54,41 @@ var operators = []operator{
{"__bnot", bnot, nil},
}

func ArithName(a ArithOp) string {
switch a {
case LUA_OPADD:
return "add"
case LUA_OPSUB:
return "sub"
case LUA_OPMUL:
return "mul"
case LUA_OPMOD:
return "mod"
case LUA_OPPOW:
return "pow"
case LUA_OPDIV:
return "div"
case LUA_OPIDIV:
return "idiv"
case LUA_OPBAND:
return "band"
case LUA_OPBOR:
return "bor"
case LUA_OPBXOR:
return "bxor"
case LUA_OPSHL:
return "shl"
case LUA_OPSHR:
return "shr"
case LUA_OPUNM:
return "unm"
case LUA_OPBNOT:
return "bnot"
default:
panic("invalid arith op")
}
}

// [-(2|1), +1, e]
// http://www.lua.org/manual/5.3/manual.html#lua_arith
func (self *luaState) Arith(op ArithOp) {
Expand All @@ -76,7 +112,14 @@ func (self *luaState) Arith(op ArithOp) {
return
}

self.PushNil()
if a == nil && b == nil {
self.PushNil()
return
}

self.stack.push(a)
self.stack.push(b)
panic(fmt.Sprintf("invalid arith: %v <%v> %v", self.TypeName(-3), ArithName(op), self.TypeName(-2)))
}

func _arith(a, b any, op operator) any {
Expand Down
14 changes: 14 additions & 0 deletions test/error.lk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
shy fn div0(a, b) {
if b == 0 {
error("DIV BY ZERO !")
} else {
rt a / b
}
}

shy fn div1(a, b) {rt div0(a, b)}
shy fn div2(a, b) {rt div1(a, b)}

shy ok, result = pcall(div2, 4, 2); print(ok, result)
shy ok, err = pcall(div2, 5, 0); print(ok, err)
shy ok, err = pcall(div2, {}, {}); print(ok, err)

0 comments on commit 7c6fdd4

Please sign in to comment.