Skip to content

Commit

Permalink
修复 REPL,优化 CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 20, 2022
1 parent 6140dce commit b9010f6
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 69 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# CHANGELOG
**仅包含语言LK的变化**

## 0.1.5
- `CLI` 增加 `-d` 开启调试模式
- `os.time()` 返回毫秒时间戳
- 去除 `select` ,使用 `#{...}` 替代

## 0.1.4
- `+` 支持 `String`
- 去除 `metatable` 属性,直接将其置于 `luaTable` 对象中
- `PCall` 仅在 `REPL` 输出错误
- 优化 `luaTable` 默认 `print` 格式

## 0.1.3
- 添加`prefixexp : Name args`的语法
- 支持面向对象
- `REPL`支持自动添加`print`
- 不再支持任意对象 `concat`,如不能拼接则会返回 `""`
- 去除 `metatable` 属性,直接将其置于 `luaTable` 对象中
- 优化 `luaTable` 默认 `print` 格式
- `PCall` 仅在 `REPL` 输出错误

## 0.1.2
- `Table`索引从`0`开始
Expand Down
15 changes: 3 additions & 12 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"os"
"strings"
"testing"

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

const (
Expand All @@ -18,13 +16,6 @@ var (
}
)

func run(f string) {
ls := state.New()
ls.OpenLibs()
ls.LoadFile(f)
ls.Call(0, -1)
}

func contains[T string](list []T, item T) bool {
for idx := range list {
if list[idx] == item {
Expand All @@ -45,21 +36,21 @@ func TestMain(m *testing.M) {
continue
}
println("=== " + name + " ===")
run("test/" + name)
run("test/" + name, false)
println()
}
}

func BenchmarkRun(b *testing.B) {
f := file + ".lk"
for i := 0; i < b.N; i++ {
run(f)
run(f, false)
}
}

func BenchmarkRunCompiled(b *testing.B) {
f := file + ".lkc"
for i := 0; i < b.N; i++ {
run(f)
run(f, true)
}
}
56 changes: 4 additions & 52 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"flag"
"io/ioutil"
"os"
"path"

"git.lolli.tech/lollipopkit/lk/compiler"
"git.lolli.tech/lollipopkit/lk/consts"
"git.lolli.tech/lollipopkit/lk/state"
)

func main() {
Expand All @@ -21,51 +14,10 @@ func main() {
consts.Debug = *debug

args := flag.Args()
if len(args) == 0 {
switch len(args) {
case 0:
repl()
return
default:
run(args[0], *force)
}

file := args[0]
if file == "" {
panic("no input file")
}

data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}

compiledFileName := getSHA256HashCode(data) + ".lkc"
compiledFile := path.Join(os.TempDir(), compiledFileName)
compiledData, _ := ioutil.ReadFile(compiledFile)

if !exist(compiledFile) || *force {
bin := compiler.Compile(string(data), file)
f, err := os.Create(compiledFile)
if err != nil {
panic(err)
}
compiledData, err = bin.Dump()
if err != nil {
panic(err)
}
f.Write(compiledData)
}

ls := state.New()
ls.OpenLibs()
ls.Load(compiledData, file, "bt")
ls.Call(0, -1)
}

func exist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

func getSHA256HashCode(message []byte) string {
bytes := sha256.Sum256(message)
hashCode := hex.EncodeToString(bytes[:])
return hashCode
}
6 changes: 5 additions & 1 deletion repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func readline() string {
println()
return true, nil
case keys.Backspace:
if len(str) > 0 {
if len(str) > 0 && cursorIdx > 0 {
str = str[:cursorIdx-1] + str[cursorIdx:]
resetLine(str)
cursorIdx--
Expand All @@ -162,6 +162,10 @@ func readline() string {
str = linesHistory[linesIdx]
resetLine(str)
cursorIdx = len(str)
} else if linesIdx == len(linesHistory)-1 {
str = ""
resetLine("")
cursorIdx = 0
}
case keys.Space:
str += " "
Expand Down
53 changes: 53 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"crypto/sha256"
"encoding/hex"
"io/ioutil"
"os"
"path"

"git.lolli.tech/lollipopkit/lk/compiler"
"git.lolli.tech/lollipopkit/lk/state"
)


func run(file string, force bool) {
data, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}

compiledFileName := getSHA256HashCode(data) + ".lkc"
compiledFile := path.Join(os.TempDir(), compiledFileName)
compiledData, _ := ioutil.ReadFile(compiledFile)

if !exist(compiledFile) || force {
bin := compiler.Compile(string(data), file)
f, err := os.Create(compiledFile)
if err != nil {
panic(err)
}
compiledData, err = bin.Dump()
if err != nil {
panic(err)
}
f.Write(compiledData)
}

ls := state.New()
ls.OpenLibs()
ls.Load(compiledData, file, "bt")
ls.Call(0, -1)
}

func exist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}

func getSHA256HashCode(message []byte) string {
bytes := sha256.Sum256(message)
hashCode := hex.EncodeToString(bytes[:])
return hashCode
}
2 changes: 1 addition & 1 deletion test/os.lk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
shy fn pri(section, ...) {
print('=== ' + section + ' ===')
print('### ' + section + ' ###')
print(...)
}
shy tmpDir = os.tmp()
Expand Down

0 comments on commit b9010f6

Please sign in to comment.