Skip to content

Commit

Permalink
REPL 自动添加 print, PCall会输出错误
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 1, 2022
1 parent 6df2e9d commit 625a28f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@
<img alt="badge-lang" src="https://badgen.net/badge/Go/1.19/purple">
</p>

## 🌳 生态
- Vscode插件:[高亮](https://git.lolli.tech/lollipopkit/vscode-lang-lk-highlight)


## ⌨️ 体验
可以前往 [Release](https://github.com/LollipopKit/lang-lk/releases) 下载 `LK CLI`,或使用`go build .`生成。
#### REPL交互解释器
`./lk`
`LK CLI`,可前往 [Release](https://github.com/LollipopKit/lang-lk/releases) 下载,或使用 `go build .` 生成。

#### 运行`.lk`
```bash
# 进入REPL交互式解释器
./lk
# 执行.lk文件
./lk <file>
# 如果修改了.lk文件导致无法运行,可以尝试添加-f参数
./lk -f <file>
```

## 📄 语法
### 详细语法
#### 详细语法
- [LANG.md](LANG.md)
- [测试集](test)

### 速览
#### 速览
```js
// 发送请求
shy resp, err = http.post(
Expand Down Expand Up @@ -94,7 +93,7 @@ if http.listen(':8080', handle) != nil {
- [x] REPL
- [x] 直接运行 `./lk` 即可进入
- [x] 支持方向键
- [ ] 识别代码块,并自动缩进
- [x] 识别代码块
- [x] 资源
- [x] 文档
- [x] `CHANGELOG.md`
Expand All @@ -103,6 +102,9 @@ if http.listen(':8080', handle) != nil {
- [x] IDE
- [x] VSCode高亮

## 🌳 生态
- Vscode插件:[高亮](https://git.lolli.tech/lollipopkit/vscode-lang-lk-highlight)

## 💌 致谢
- Lua
- [luago](https://github.com/zxh0/luago-book)
Expand Down
6 changes: 3 additions & 3 deletions consts/re.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "regexp"
const (
ForInReStr = `for +(\S+(, *\S+)*) +in +(\S+) *\{`
FnReStr = `fn +(\S*)\((\S*(, *\S+)*)\) *\{`
WhileReStr = `while +(\S+) *\{`
IfReStr = `if +(\S+) *\{`
ElseIfReStr = `elif +(\S+) *\{`
WhileReStr = `while +(\S+ ) *\{`
IfReStr = `if +(\S+ )+ *\{`
ElseIfReStr = `elif +(\S+ ) *\{`
ElseReStr = `else *\{`

// a++
Expand Down
19 changes: 18 additions & 1 deletion repl.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"fmt"
"os"
"regexp"
"strings"

"atomicgo.dev/cursor"
"atomicgo.dev/keyboard"
"atomicgo.dev/keyboard/keys"
"git.lolli.tech/lollipopkit/lk/api"
"git.lolli.tech/lollipopkit/lk/consts"
"git.lolli.tech/lollipopkit/lk/state"
)
Expand Down Expand Up @@ -69,11 +71,26 @@ func repl() {
updateHistory(cmd)

// 加载line,调用
ls.LoadString(cmd, "stdin")
protectedLoadString(&ls, cmd)
ls.PCall(0, -1, 0)
}
}

func catchErr(ls *api.LkState, first *bool, cmd string) {
if err := recover(); err != nil && *first {
cmd = fmt.Sprintf("print(%s)", cmd)
*first = false
(*ls).LoadString(cmd, "stdin")
}
}

func protectedLoadString(ls *api.LkState, cmd string) {
first := true
// 捕获错误
defer catchErr(ls, &first, cmd)
(*ls).LoadString(cmd, "stdin")
}

func updateHistory(str string) {
idx := -1
for i := range linesHistory {
Expand Down
2 changes: 1 addition & 1 deletion state/api_arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (self *luaState) Arith(op ArithOp) {
return
}

panic("arithmetic error!")
self.PushNil()
}

func _arith(a, b any, op operator) any {
Expand Down
1 change: 1 addition & 0 deletions state/api_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func (self *luaState) PCall(nArgs, nResults, msgh int) (status int) {
self.popLuaStack()
}
self.stack.push(err)
fmt.Printf("%v\n", err)
}
}()

Expand Down
8 changes: 6 additions & 2 deletions state/api_get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package state

import . "git.lolli.tech/lollipopkit/lk/api"
import (
"fmt"

. "git.lolli.tech/lollipopkit/lk/api"
)

// [-0, +1, m]
// http://www.lua.org/manual/5.3/manual.html#lua_newtable
Expand Down Expand Up @@ -98,5 +102,5 @@ func (self *luaState) getTable(t, k any, raw bool) LuaType {
}
}

panic("index error!")
panic(fmt.Sprintf("'%v' is not a table and has no '__index' metafield, cannot get '%v'", t, k))
}
6 changes: 0 additions & 6 deletions state/auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ func (self *luaState) LoadFileX(filename, mode string) int {
// [-0, +1, –]
// http://www.lua.org/manual/5.3/manual.html#luaL_loadstring
func (self *luaState) LoadString(s, source string) int {
// 捕获错误
defer func() {
if err := recover(); err != nil {
println(fmt.Sprintf("%v", err))
}
}()
return self.Load([]byte(s), source, "bt")
}

Expand Down

0 comments on commit 625a28f

Please sign in to comment.