Skip to content

Commit

Permalink
去除 concat
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 24, 2022
1 parent 9f998a9 commit 7937bb9
Show file tree
Hide file tree
Showing 17 changed files with 18 additions and 112 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- `CLI` 增加 `-d` 开启调试模式
- `os.time()` 返回毫秒时间戳
- 去除 `select` ,使用 `#{...}` 替代
- 支持三元操作符 `a ? b : c`
- 去除 `concat`
- 修复 `os.args` 索引错误

## 0.1.4
- `+` 支持 `String`
Expand Down
9 changes: 6 additions & 3 deletions LANG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ addN(1, 2, 3, 4, 5) // 15
```js
while condition {
// ...
break
}
```
```js
Expand Down Expand Up @@ -237,6 +238,9 @@ fn varagsLen(...) {
}

varagsLen(1, 2, 3, 4, 5) // 5

// 三元操作符
print(true ? 'support ternary exp' : 'unreachable')
```
### 运算符优先级
Expand Down Expand Up @@ -369,7 +373,6 @@ print(fmt('%s + %s = %s', v1, v2, v3)) // Vector(1, 2) + Vector(3, 4) = Vector(
|`%`|`__mod`|
|`^`|`__pow`|
|`-`|`__unm`|
|`..`|`__concat`|
|`~/`|`__idiv`|
|`#`|`__len`|
|`==`|`__eq`|
Expand Down Expand Up @@ -518,7 +521,7 @@ print()
- `json.get(source, path)`
获取 JSON 数据,`source` 为 JSON 数据,`path` 为路径。
`path`遵循`gjson`规则。详情请查看[gjson](https://github.com/tidwall/gjson)。
### 其他
- `string` https://www.runoob.com/lua/lua-strings.html
- `utf8` https://www.jianshu.com/p/dcbb6b47bb32
- `sync` https://www.runoob.com/lua/lua-coroutine.html
- `utf8` https://www.jianshu.com/p/dcbb6b47bb32
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ if http.listen(':8080', handle) != nil {
## 🔖 TODO
- 语法
- [x] 注释:`//` `/* */`
- [x] 去除 `repeat` `until` `goto`
- [x] 去除 `repeat`, `until`, `goto`, `..` (`concat`)
- [x] Raw String, 使用 ``` ` ``` 包裹字符
- [x] 支持任意对象拼接( `concat` ),使用语法 `..`
- [x] 面向对象
- [ ] 语法糖
- [x] 三元操作符 `a ? b : c`
Expand Down
1 change: 0 additions & 1 deletion api/lua_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ type BasicAPI interface {
PCall(nArgs, nResults, msgh int, print bool) int
/* miscellaneous functions */
Len(idx int)
Concat(n int)
Next(idx int) bool
Error() int
StringToNumber(s string) bool
Expand Down
5 changes: 0 additions & 5 deletions compiler/ast/exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ type TernaryExp struct {
Exp3 Exp
}

type ConcatExp struct {
Line int // line of last ..
Exps []Exp
}

// tableconstructor ::= ‘{’ [fieldlist] ‘}’
// fieldlist ::= field {fieldsep field} [fieldsep]
// field ::= ‘[’ exp ‘]’ ‘=’ exp | Name ‘=’ exp | exp
Expand Down
17 changes: 0 additions & 17 deletions compiler/codegen/cg_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package codegen
import (
. "git.lolli.tech/lollipopkit/lk/compiler/ast"
. "git.lolli.tech/lollipopkit/lk/compiler/lexer"

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

// kind of operands
Expand Down Expand Up @@ -46,8 +44,6 @@ func cgExp(fi *funcInfo, node Exp, a, n int) {
cgBinopExp(fi, exp, a)
case *TernaryExp:
cgTernaryExp(fi, exp, a)
case *ConcatExp:
cgConcatExp(fi, exp, a)
case *NameExp:
cgNameExp(fi, exp, a)
case *TableAccessExp:
Expand Down Expand Up @@ -194,19 +190,6 @@ func cgTernaryExp(fi *funcInfo, node *TernaryExp, a int) {
fi.fixSbx(pcOfJmp2, fi.pc()-pcOfJmp2)
}

// r[a] := exp1 .. exp2
func cgConcatExp(fi *funcInfo, node *ConcatExp, a int) {
for i := range node.Exps {
a := fi.allocReg()
cgExp(fi, node.Exps[i], a, 1)
}

c := fi.usedRegs - 1
b := c - len(node.Exps) + 1
fi.freeRegs(c - b + 1)
fi.emitABC(node.Line, OP_CONCAT, a, b, c)
}

// r[a] := name
func cgNameExp(fi *funcInfo, node *NameExp, a int) {
if r := fi.slotOfLocVar(node.Name); r >= 0 {
Expand Down
4 changes: 0 additions & 4 deletions compiler/codegen/exp_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ func lineOf(exp Exp) int {
return x.Line
case *TableAccessExp:
return lineOf(x.PrefixExp)
case *ConcatExp:
return lineOf(x.Exps[0])
case *BinopExp:
return lineOf(x.Exp1)
case *TernaryExp:
Expand Down Expand Up @@ -84,8 +82,6 @@ func lastLineOf(exp Exp) int {
return x.LastLine
case *TableAccessExp:
return x.LastLine
case *ConcatExp:
return lastLineOf(x.Exps[len(x.Exps)-1])
case *BinopExp:
return lastLineOf(x.Exp2)
case *UnopExp:
Expand Down
3 changes: 0 additions & 3 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ func (self *Lexer) NextToken() (line, kind int, token string) {
if self.test("...") {
self.next(3)
return self.line, TOKEN_VARARG, "..."
} else if self.test("..") {
self.next(2)
return self.line, TOKEN_OP_CONCAT, ".."
} else if len(self.chunk) == 1 || !isDigit(self.chunk[1]) {
self.next(1)
return self.line, TOKEN_SEP_DOT, "."
Expand Down
2 changes: 0 additions & 2 deletions compiler/lexer/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const (
TOKEN_OP_BOR
TOKEN_OP_SHR
TOKEN_OP_SHL
TOKEN_OP_CONCAT
TOKEN_OP_LT
TOKEN_OP_LE
TOKEN_OP_GT
Expand Down Expand Up @@ -88,7 +87,6 @@ var tokenNames = map[int]string{
TOKEN_OP_BOR: "|",
TOKEN_OP_SHR: ">>",
TOKEN_OP_SHL: "<<",
TOKEN_OP_CONCAT: "..",
TOKEN_OP_LT: "<",
TOKEN_OP_LE: "<=",
TOKEN_OP_GT: ">",
Expand Down
20 changes: 2 additions & 18 deletions compiler/parser/parse_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,19 @@ func parseExp7(lexer *Lexer) Exp {

// shift
func parseExp6(lexer *Lexer) Exp {
exp := parseExp5(lexer)
exp := parseExp4(lexer)
for {
switch lexer.LookAhead() {
case TOKEN_OP_SHL, TOKEN_OP_SHR:
line, op, _ := lexer.NextToken()
shx := &BinopExp{line, op, exp, parseExp5(lexer)}
shx := &BinopExp{line, op, exp, parseExp4(lexer)}
exp = optimizeBitwiseBinaryOp(shx)
default:
return exp
}
}
}

// a .. b
func parseExp5(lexer *Lexer) Exp {
exp := parseExp4(lexer)
if lexer.LookAhead() != TOKEN_OP_CONCAT {
return exp
}

line := 0
exps := []Exp{exp}
for lexer.LookAhead() == TOKEN_OP_CONCAT {
line, _, _ = lexer.NextToken()
exps = append(exps, parseExp4(lexer))
}
return &ConcatExp{line, exps}
}

// x +/- y
func parseExp4(lexer *Lexer) Exp {
exp := parseExp3(lexer)
Expand Down
2 changes: 1 addition & 1 deletion consts/lang.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package consts

var (
VERSION = `0.1.4`
VERSION = `0.1.5`
SIGNATURE = `LANG_LK`
)
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
)

var (
force *bool
debug *bool
args []string
force = flag.Bool("f", false, "force to re-compile")
debug = flag.Bool("d", false, "debug mode")
args = []string{}
)

func main() {
force = flag.Bool("f", false, "force to re-compile")
debug = flag.Bool("d", false, "debug mode")
flag.Parse()

consts.Debug = *debug
Expand Down
31 changes: 0 additions & 31 deletions state/api_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"fmt"

"git.lolli.tech/lollipopkit/lk/logger"
"git.lolli.tech/lollipopkit/lk/number"
jsoniter "github.com/json-iterator/go"
)
Expand All @@ -28,36 +27,6 @@ func (self *luaState) Len(idx int) {
}
}

// [-n, +1, e]
// http://www.lua.org/manual/5.3/manual.html#lua_concat
func (self *luaState) Concat(n int) {
if n == 0 {
self.stack.push("")
} else if n >= 2 {
for i := 1; i < n; i++ {
if self.IsString(-1) && self.IsString(-2) {
s2 := self.ToString(-1)
s1 := self.ToString(-2)
self.stack.pop()
self.stack.pop()
self.stack.push(s1 + s2)
continue
}

b := self.stack.pop()
a := self.stack.pop()
if result, ok := callMetamethod(a, b, "__concat", self); ok {
self.stack.push(result)
continue
}

logger.W("[state.Concat] between %v and %v", a, b)
self.stack.push("")
}
}
// n == 1, do nothing
}

// [-1, +(2|0), e]
// http://www.lua.org/manual/5.3/manual.html#lua_next
func (self *luaState) Next(idx int) bool {
Expand Down
2 changes: 1 addition & 1 deletion test/module.lkc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"v":"0.1.4","si":"LANG_LK","h":"","p":{"s":"test/module.lk","ld":0,"lld":0,"np":0,"iv":1,"ms":5,"c":[1,16459,32897,49345,16826442,2155888648,4210758,65665,65729,16826442,108,4210822,82113,16684,25231498,8388646],"cs":[1,"test2","b",2,"const","add"],"us":[{"is":1,"idx":0}],"ps":[{"s":"test/module.lk","ld":8,"lld":10,"np":1,"iv":0,"ms":3,"c":[133,16777293,73,8388646],"cs":[],"us":[{"is":1,"idx":0}],"ps":[],"li":[9,9,9,10],"lvs":[{"vn":"n","spc":0,"epc":4}],"uns":["a"]},{"s":"test/module.lk","ld":12,"lld":15,"np":1,"iv":0,"ms":6,"c":[69,4194439,16793700,64,129,4194567,8388933,33636557,16826442,8388646],"cs":["b"],"us":[{"is":1,"idx":1},{"is":1,"idx":0}],"ps":[],"li":[13,13,13,14,14,14,14,14,14,15],"lvs":[{"vn":"self","spc":0,"epc":10}],"uns":["add","a"]}],"li":[1,2,3,3,3,2,6,6,6,6,10,12,12,15,12,15],"lvs":[{"vn":"a","spc":1,"epc":16},{"vn":"add","spc":11,"epc":16}],"uns":["_ENV"]}}
{"v":"0.1.4","si":"LANG_LK","h":"","p":{"s":"test/module.lk","ld":0,"lld":0,"np":0,"iv":1,"ms":5,"c":[1,16459,32897,49345,16826442,2155888648,4210758,65665,65729,16826442,107,4210822,82113,16683,25231498,8388645],"cs":[1,"test2","b",2,"const","add"],"us":[{"is":1,"idx":0}],"ps":[{"s":"test/module.lk","ld":8,"lld":10,"np":1,"iv":0,"ms":3,"c":[133,16777293,73,8388645],"cs":[],"us":[{"is":1,"idx":0}],"ps":[],"li":[9,9,9,10],"lvs":[{"vn":"n","spc":0,"epc":4}],"uns":["a"]},{"s":"test/module.lk","ld":12,"lld":15,"np":1,"iv":0,"ms":6,"c":[69,4194439,16793699,64,129,4194567,8388933,33636557,16826442,8388645],"cs":["b"],"us":[{"is":1,"idx":1},{"is":1,"idx":0}],"ps":[],"li":[13,13,13,14,14,14,14,14,14,15],"lvs":[{"vn":"self","spc":0,"epc":10}],"uns":["add","a"]}],"li":[1,2,3,3,3,2,6,6,6,6,10,12,12,15,12,15],"lvs":[{"vn":"a","spc":1,"epc":16},{"vn":"add","spc":11,"epc":16}],"uns":["_ENV"]}}
2 changes: 1 addition & 1 deletion test/os.lk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ shy fn pri(section, ...) {
shy tmpDir = os.tmp()
pri('temp_dir: ', tmpDir)

shy path = tmpDir .. '/test'
shy path = tmpDir + '/test'
shy err = os.write(path, "test")
pri('write err: ', err)
shy data, err = os.read(path)
Expand Down
16 changes: 0 additions & 16 deletions vm/inst_operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,3 @@ func length(i Instruction, vm LuaVM) {
vm.Len(b)
vm.Replace(a)
}

// R(A) := R(B).. ... ..R(C)
func concat(i Instruction, vm LuaVM) {
a, b, c := i.ABC()
a += 1
b += 1
c += 1

n := c - b + 1
vm.CheckStack(n)
for i := b; i <= c; i++ {
vm.PushValue(i)
}
vm.Concat(n)
vm.Replace(a)
}
2 changes: 0 additions & 2 deletions vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const (
OP_BNOT
OP_NOT
OP_LEN
OP_CONCAT
OP_JMP
OP_EQ
OP_LT
Expand Down Expand Up @@ -111,7 +110,6 @@ var opcodes = []opcode{
{0, 1, OpArgR, OpArgN, IABC /* */, "BNOT ", bnot}, // R(A) := ~R(B)
{0, 1, OpArgR, OpArgN, IABC /* */, "NOT ", not}, // R(A) := not R(B)
{0, 1, OpArgR, OpArgN, IABC /* */, "LEN ", length}, // R(A) := length of R(B)
{0, 1, OpArgR, OpArgR, IABC /* */, "CONCAT ", concat}, // R(A) := R(B).. ... ..R(C)
{0, 0, OpArgR, OpArgN, IAsBx /**/, "JMP ", jmp}, // pc+=sBx; if (A) close all upvalues >= R(A - 1)
{1, 0, OpArgK, OpArgK, IABC /* */, "EQ ", eq}, // if ((RK(B) == RK(C)) ~= A) then pc++
{1, 0, OpArgK, OpArgK, IABC /* */, "LT ", lt}, // if ((RK(B) < RK(C)) ~= A) then pc++
Expand Down

0 comments on commit 7937bb9

Please sign in to comment.