Skip to content

Commit

Permalink
去除 type luaValue
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Sep 29, 2022
1 parent 90a9419 commit 38a4dae
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 171 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Lang LK
改编自Lua5.3,[luago](https://github.com/zxh0/luago-book)

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

## 速览
**详细语法**,可以查看[test](test)文件夹的内容

Expand Down Expand Up @@ -57,7 +60,6 @@ if http.listen(':8080', handle) != nil {
- 编译器
- [x] 自动添加`range` ( `paris` )
- Table
- [ ] 索引从0开始
- [x] key为StringExp,而不是NameExp
- [x] `=` -> `:`, eg: `{a = 'a'}` -> `{a: 'a'}`
- CLI
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module git.lolli.tech/lollipopkit/go-lang-lk

go 1.18
go 1.19

require (
git.lolli.tech/lollipopkit/go_lru_cacher v0.0.4
Expand Down
4 changes: 2 additions & 2 deletions state/api_arith.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var operators = []operator{
// [-(2|1), +1, e]
// http://www.lua.org/manual/5.3/manual.html#lua_arith
func (self *luaState) Arith(op ArithOp) {
var a, b luaValue // operands
var a, b any // operands
b = self.stack.pop()
if op != LUA_OPUNM && op != LUA_OPBNOT {
a = self.stack.pop()
Expand All @@ -79,7 +79,7 @@ func (self *luaState) Arith(op ArithOp) {
panic("arithmetic error!")
}

func _arith(a, b luaValue, op operator) luaValue {
func _arith(a, b any, op operator) any {
if op.floatFunc == nil { // bitwise
if x, ok := convertToInteger(a); ok {
if y, ok := convertToInteger(b); ok {
Expand Down
6 changes: 3 additions & 3 deletions state/api_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (self *luaState) Compare(idx1, idx2 int, op CompareOp) bool {
}
}

func _eq(a, b luaValue, ls *luaState) bool {
func _eq(a, b any, ls *luaState) bool {
switch x := a.(type) {
case nil:
return b == nil
Expand Down Expand Up @@ -75,7 +75,7 @@ func _eq(a, b luaValue, ls *luaState) bool {
}
}

func _lt(a, b luaValue, ls *luaState) bool {
func _lt(a, b any, ls *luaState) bool {
switch x := a.(type) {
case string:
if y, ok := b.(string); ok {
Expand Down Expand Up @@ -104,7 +104,7 @@ func _lt(a, b luaValue, ls *luaState) bool {
}
}

func _le(a, b luaValue, ls *luaState) bool {
func _le(a, b any, ls *luaState) bool {
switch x := a.(type) {
case string:
if y, ok := b.(string); ok {
Expand Down
2 changes: 1 addition & 1 deletion state/api_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (self *luaState) GetMetatable(idx int) bool {
}

// push(t[k])
func (self *luaState) getTable(t, k luaValue, raw bool) LuaType {
func (self *luaState) getTable(t, k any, raw bool) LuaType {
if tbl, ok := t.(*luaTable); ok {
v := tbl.get(k)
if raw || v != nil || !tbl.hasMetafield("__index") {
Expand Down
2 changes: 1 addition & 1 deletion state/api_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (self *luaState) SetMetatable(idx int) {
}

// t[k]=v
func (self *luaState) setTable(t, k, v luaValue, raw bool) {
func (self *luaState) setTable(t, k, v any, raw bool) {
if tbl, ok := t.(*luaTable); ok {
if raw || tbl.get(k) != nil || !tbl.hasMetafield("__newindex") {
tbl.put(k, v)
Expand Down
2 changes: 1 addition & 1 deletion state/closure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type upvalue struct {
val *luaValue
val *any
}

type closure struct {
Expand Down
20 changes: 10 additions & 10 deletions state/lua_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import . "git.lolli.tech/lollipopkit/go-lang-lk/api"

type luaStack struct {
/* virtual stack */
slots []luaValue
slots []any
top int
/* call info */
state *luaState
closure *closure
varargs []luaValue
varargs []any
openuvs map[int]*upvalue
pc int
/* linked list */
Expand All @@ -18,7 +18,7 @@ type luaStack struct {

func newLuaStack(size int, state *luaState) *luaStack {
return &luaStack{
slots: make([]luaValue, size),
slots: make([]any, size),
top: 0,
state: state,
}
Expand All @@ -31,15 +31,15 @@ func (self *luaStack) check(n int) {
}
}

func (self *luaStack) push(val luaValue) {
func (self *luaStack) push(val any) {
if self.top == len(self.slots) {
panic("stack overflow!")
}
self.slots[self.top] = val
self.top++
}

func (self *luaStack) pop() luaValue {
func (self *luaStack) pop() any {
if self.top < 1 {
panic("stack underflow!")
}
Expand All @@ -49,7 +49,7 @@ func (self *luaStack) pop() luaValue {
return val
}

func (self *luaStack) pushN(vals []luaValue, n int) {
func (self *luaStack) pushN(vals []any, n int) {
nVals := len(vals)
if n < 0 {
n = nVals
Expand All @@ -64,8 +64,8 @@ func (self *luaStack) pushN(vals []luaValue, n int) {
}
}

func (self *luaStack) popN(n int) []luaValue {
vals := make([]luaValue, n)
func (self *luaStack) popN(n int) []any {
vals := make([]any, n)
for i := n - 1; i >= 0; i-- {
vals[i] = self.pop()
}
Expand All @@ -92,7 +92,7 @@ func (self *luaStack) isValid(idx int) bool {
return absIdx > 0 && absIdx <= self.top
}

func (self *luaStack) get(idx int) luaValue {
func (self *luaStack) get(idx int) any {
if idx < LUA_REGISTRYINDEX { /* upvalues */
uvIdx := LUA_REGISTRYINDEX - idx - 1
c := self.closure
Expand All @@ -113,7 +113,7 @@ func (self *luaStack) get(idx int) luaValue {
return nil
}

func (self *luaStack) set(idx int, val luaValue) {
func (self *luaStack) set(idx int, val any) {
if idx < LUA_REGISTRYINDEX { /* upvalues */
uvIdx := LUA_REGISTRYINDEX - idx - 1
c := self.closure
Expand Down
26 changes: 13 additions & 13 deletions state/lua_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (

type luaTable struct {
metatable *luaTable
arr []luaValue
_map map[luaValue]luaValue
keys map[luaValue]luaValue // used by next()
lastKey luaValue // 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 newLuaTable(nArr, nRec int) *luaTable {
t := &luaTable{}
if nArr > 0 {
t.arr = make([]luaValue, 0, nArr)
t.arr = make([]any, 0, nArr)
}
if nRec > 0 {
t._map = make(map[luaValue]luaValue, nRec)
t._map = make(map[any]any, nRec)
}
return t
}
Expand All @@ -36,7 +36,7 @@ func (self *luaTable) len() int {
return len(self.arr)
}

func (self *luaTable) get(key luaValue) luaValue {
func (self *luaTable) get(key any) any {
key = _floatToInteger(key)
if idx, ok := key.(int64); ok {
if idx >= 1 && idx <= int64(len(self.arr)) {
Expand All @@ -46,7 +46,7 @@ func (self *luaTable) get(key luaValue) luaValue {
return self._map[key]
}

func _floatToInteger(key luaValue) luaValue {
func _floatToInteger(key any) any {
if f, ok := key.(float64); ok {
if i, ok := number.FloatToInteger(f); ok {
return i
Expand All @@ -55,7 +55,7 @@ func _floatToInteger(key luaValue) luaValue {
return key
}

func (self *luaTable) put(key, val luaValue) {
func (self *luaTable) put(key, val any) {
if key == nil {
panic("table index is nil!")
}
Expand Down Expand Up @@ -85,7 +85,7 @@ func (self *luaTable) put(key, val luaValue) {
}
if val != nil {
if self._map == nil {
self._map = make(map[luaValue]luaValue, 8)
self._map = make(map[any]any, 8)
}
self._map[key] = val
} else {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (self *luaTable) _expandArray() {
}
}

func (self *luaTable) nextKey(key luaValue) luaValue {
func (self *luaTable) nextKey(key any) any {
if self.keys == nil || (key == nil && self.changed) {
self.initKeys()
self.changed = false
Expand All @@ -133,8 +133,8 @@ func (self *luaTable) nextKey(key luaValue) luaValue {
}

func (self *luaTable) initKeys() {
self.keys = make(map[luaValue]luaValue)
var key luaValue = nil
self.keys = make(map[any]any)
var key any = nil
for i := range self.arr {
if self.arr[i] != nil {
self.keys[key] = int64(i + 1)
Expand Down
112 changes: 0 additions & 112 deletions state/lua_table.go.new

This file was deleted.

Loading

0 comments on commit 38a4dae

Please sign in to comment.