Skip to content

Commit

Permalink
去除 select
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 14, 2022
1 parent abae3ed commit 0d8a75e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 57 deletions.
10 changes: 5 additions & 5 deletions LANG.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ print(fmt('%s + %s = %s', v1, v2, v3)) // Vector(1, 2) + Vector(3, 4) = Vector(

##
```js
// 文件名为 module.lua
// 文件名为 module.lk
// 定义一个名为 module 的模块
class module {}

Expand All @@ -384,17 +384,17 @@ fn module.func3() {
```
如上定义了一个包,然后在另一个文件中导入:
```js
import "test"
import "module"
```
可以通过 `import` 关键字导入包,导入的包会在当前文件作用域中有效。
导入路径 `test` 为当前文件的相对路径。
导入路径 `module` 为当前文件的相对路径。
例如`import "a/b/c"`,会尝试导入:`./a/b/c.lk` `./a/b/c/init.lk`

导入后如下使用:
```js
test.func1()
module.func1()
// test.func2() 不可直接使用,因为是局部函数,但可以通过 module.func3() 调用
test.func3()
module.func3()
```

## 多线程
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ func W(fm string, a ...any) {
s := fmt.Sprintf("[WARN] %s\n", fm)
fmt.Printf(s, a...)
}
}
}
4 changes: 2 additions & 2 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
consts.ClassDefReStr,
}, "|"))
blockEndReg = regexp.MustCompile("} *$")
promptLen = len([]rune(prompt))
printReg = regexp.MustCompile(`print\(.*\)`)
promptLen = len([]rune(prompt))
printReg = regexp.MustCompile(`print\(.*\)`)
)

const (
Expand Down
2 changes: 1 addition & 1 deletion state/api_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ func (self *luaState) setTable(t, k, v any, raw bool) {
}
}

panic("expect table, got "+fmt.Sprintf("%v", t))
panic("expect table, got " + fmt.Sprintf("%v", t))
}
10 changes: 5 additions & 5 deletions state/lua_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

type luaTable struct {
arr []any
_map map[any]any
keys map[any]any // used by next()
lastKey any // used by next()
changed bool // used by next()
arr []any
_map map[any]any
keys map[any]any // used by next()
lastKey any // used by next()
changed bool // used by next()
}

func (self *luaTable) String() (string, error) {
Expand Down
53 changes: 16 additions & 37 deletions stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@ import (
)

var baseFuncs = map[string]GoFunction{
"new": baseNew,
"print": basePrint,
"assert": baseAssert,
"error": baseError,
"select": baseSelect,
"irange": baseIPairs,
"range": basePairs,
"next": baseNext,
"load": baseLoad,
"loadfile": baseLoadFile,
"dofile": baseDoFile,
"pcall": basePCall,
"new": baseNew,
"print": basePrint,
"assert": baseAssert,
"error": baseError,
"irange": baseIPairs,
"range": basePairs,
"next": baseNext,
"load": baseLoad,
"loadfile": baseLoadFile,
"dofile": baseDoFile,
"pcall": basePCall,
// "rawget": baseRawGet,
// "rawset": baseRawSet,
"type": baseType,
"str": baseToString,
"num": baseToNumber,
"int": mathToInt,
"kv": baseKV,
"type": baseType,
"str": baseToString,
"num": baseToNumber,
"int": mathToInt,
"kv": baseKV,
// string
"fmt": strFormat,
}
Expand Down Expand Up @@ -159,26 +158,6 @@ func baseError(ls LkState) int {
return ls.Error()
}

// select (index, ···)
// http://www.lua.org/manual/5.3/manual.html#pdf-select
// lua-5.3.4/src/lbaselib.c#luaB_select()
func baseSelect(ls LkState) int {
n := int64(ls.GetTop())
if ls.Type(1) == LUA_TSTRING && ls.CheckString(1) == "#" {
ls.PushInteger(n - 1)
return 1
} else {
i := ls.CheckInteger(1)
if i < 0 {
i = n + i
} else if i > n {
i = n
}
ls.ArgCheck(1 <= i, 1, "index out of range")
return int(n - i)
}
}

// ipairs (t)
// http://www.lua.org/manual/5.3/manual.html#pdf-ipairs
// lua-5.3.4/src/lbaselib.c#luaB_ipairs()
Expand Down
14 changes: 8 additions & 6 deletions test/basic.lk
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import "test/module"
import 'test/gf'

shy func = fn (e) {print(e)}
shy func = fn (...) {
print(fmt('[args: %d]\t', #{...}), ...)
}
func(1!=2 and 3!=3)
test2:add()
func(test2.b)
test2:add()
func(test2.b)
func(test2['b'])
func(test2.const)

// comment
Expand All @@ -18,12 +20,12 @@ shy long = `
abc`

if #long >= 0 and '' {
print(long, #long)
func(long, #long)
}

print(6 ~/ 2, 6 & 2, 6 / 2)
print(_VERSION, math.pi)
print(str({5, 'd': false}))
func(6 ~/ 2, 6 & 2, 6 / 2)
func(_VERSION, math.pi)
func(str({5, 'd': false, 1}))

shy gf = new(Girlfriend)
gf:herName('lxy')
Expand Down

0 comments on commit 0d8a75e

Please sign in to comment.