Skip to content

Commit

Permalink
新增 sync 测试
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 6, 2022
1 parent b956ec6 commit f7c4732
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 63 deletions.
6 changes: 3 additions & 3 deletions LANG.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ fn Vector.new(x, y) {

// 为 `Vector` 设置 `__add` 元方法,实现 `+` 运算符
fn Vector.__add(v1, v2) {
v = new(Vector)
shy v = new(Vector)
v.x = v1.x + v2.x
v.y = v1.y + v2.y
rt v
Expand All @@ -323,9 +323,9 @@ fn Vector:__str() {
// 此时 x = 0, y = 0
shy v1 = new(Vector)
// 带值的初始化对象
// 此时x = 3, y = 4
// 此时 x = 3, y = 4
shy v2 = Vector.new(3, 4)
// 调用Vector:set(x, y)方法,修改v1的值
// 调用 `Vector:set(x, y)` 方法,修改v1的值
v1:set(1, 2)
shy v3 = v1 + v2
print(v3.x, v3.y) // 4 6
Expand Down
2 changes: 0 additions & 2 deletions api/lua_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type BasicAPI interface {
ToGoFunction(idx int) GoFunction
ToThread(idx int) LkState
ToPointer(idx int) interface{}
RawLen(idx int) uint
/* push functions (Go -> stack) */
PushNil()
PushBoolean(b bool)
Expand All @@ -68,7 +67,6 @@ type BasicAPI interface {
/* Comparison and arithmetic functions */
Arith(op ArithOp)
Compare(idx1, idx2 int, op CompareOp) bool
RawEqual(idx1, idx2 int) bool
/* get functions (Lua -> stack) */
NewTable()
CreateTable(nArr, nRec int)
Expand Down
12 changes: 9 additions & 3 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
consts.ElseReStr,
consts.ClassDefReStr,
}, "|"))
blockEndReg = regexp.MustCompile("} *$")
promptLen = len([]rune(prompt))
printReg = regexp.MustCompile(`print\(.*\)`)
)
Expand All @@ -50,23 +51,28 @@ func repl() {
if blockStartReg.MatchString(line) {
blockStartCount++
}
if strings.HasSuffix(line, "}") {
blockStr += line + "\n"
if blockEndReg.MatchString(line) {
blockEndCount++
}

blockStr += line
if blockStartCount != blockEndCount {
blockStr += "\n"
}

cmd := ""
if blockStartCount > 0 && blockStartCount == blockEndCount {
blockStartCount = 0
blockEndCount = 0
cmd = blockStr
blockStr = ""
} else if blockStartCount > 0 {
blockStr += line + "\n"
continue
} else {
blockStr = ""
cmd = line
}
// println("==", cmd, "==")

// 更新历史记录
updateHistory(cmd)
Expand Down
14 changes: 0 additions & 14 deletions state/api_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ import (
. "git.lolli.tech/lollipopkit/lk/api"
)

// [-0, +0, –]
// http://www.lua.org/manual/5.3/manual.html#lua_rawlen
func (self *luaState) RawLen(idx int) uint {
val := self.stack.get(idx)
switch x := val.(type) {
case string:
return uint(len(x))
case *luaTable:
return uint(x.len())
default:
return 0
}
}

// [-0, +0, –]
// http://www.lua.org/manual/5.3/manual.html#lua_typename
func (self *luaState) TypeName(tp LuaType) string {
Expand Down
12 changes: 0 additions & 12 deletions state/api_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,6 @@ package state

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

// [-0, +0, –]
// http://www.lua.org/manual/5.3/manual.html#lua_rawequal
func (self *luaState) RawEqual(idx1, idx2 int) bool {
if !self.stack.isValid(idx1) || !self.stack.isValid(idx2) {
return false
}

a := self.stack.get(idx1)
b := self.stack.get(idx2)
return _eq(a, b, nil)
}

// [-0, +0, e]
// http://www.lua.org/manual/5.3/manual.html#lua_compare
func (self *luaState) Compare(idx1, idx2 int, op CompareOp) bool {
Expand Down
30 changes: 2 additions & 28 deletions stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ var baseFuncs = map[string]GoFunction{
"loadfile": baseLoadFile,
"dofile": baseDoFile,
"pcall": basePCall,
"rawequal": baseRawEqual,
"rawlen": baseRawLen,
"rawget": baseRawGet,
"rawset": baseRawSet,
// "rawget": baseRawGet,
// "rawset": baseRawSet,
"type": baseType,
"str": baseToString,
"num": baseToNumber,
"int": mathToInt,
"kv": baseKV,
/* placeholders */
"_G": nil,
"_VERSION": nil,
// string
"fmt": strFormat,
}
Expand Down Expand Up @@ -306,27 +301,6 @@ func basePCall(ls LkState) int {
return ls.GetTop()
}

// rawequal (v1, v2)
// http://www.lua.org/manual/5.3/manual.html#pdf-rawequal
// lua-5.3.4/src/lbaselib.c#luaB_rawequal()
func baseRawEqual(ls LkState) int {
ls.CheckAny(1)
ls.CheckAny(2)
ls.PushBoolean(ls.RawEqual(1, 2))
return 1
}

// rawlen (v)
// http://www.lua.org/manual/5.3/manual.html#pdf-rawlen
// lua-5.3.4/src/lbaselib.c#luaB_rawlen()
func baseRawLen(ls LkState) int {
t := ls.Type(1)
ls.ArgCheck(t == LUA_TTABLE || t == LUA_TSTRING, 1,
"table or string expected")
ls.PushInteger(int64(ls.RawLen(1)))
return 1
}

// rawget (table, index)
// http://www.lua.org/manual/5.3/manual.html#pdf-rawget
// lua-5.3.4/src/lbaselib.c#luaB_rawget()
Expand Down
2 changes: 1 addition & 1 deletion test/metatable.lk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn Vector.new(x, y) {
}

fn Vector.__add(v1, v2) {
v = new(Vector)
shy v = new(Vector)
v.x = v1.x + v2.x
v.y = v1.y + v2.y
rt v
Expand Down
24 changes: 24 additions & 0 deletions test/sync.lk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fn foo(a) {
print("foo 函数输出", a)
rt sync.yield(2 * a) // 返回 2*a 的值
}

co = sync.create(fn (a , b) {
print("第一次协同程序执行输出", a, b) // co-body 1 10
shy r = foo(a + 1)

print("第二次协同程序执行输出", r)
shy r, s = sync.yield(a + b, a - b) // a,b的值为第一次调用协同程序时传入

print("第三次协同程序执行输出", r, s)
rt b, "结束协同程序" // b的值为第二次调用协同程序时传入
})

print("main", sync.resume(co, 1, 10)) // true, 4
print()
print("main", sync.resume(co, "r")) // true 11 -9
print()
print("main", sync.resume(co, "x", "y")) // true 10 end
print()
print("main", sync.resume(co, "x", "y")) // cannot resume dead sync
print()

0 comments on commit f7c4732

Please sign in to comment.