Skip to content

Commit

Permalink
string.format -> fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Sep 29, 2022
1 parent 1af6dc7 commit b2fb56e
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 44 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ headers.__str = fn(a) {
*/
shy fn handle(req) {
setmetatable(req.headers, headers)
rt 200, string.format('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
rt 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
}

// 监听
Expand Down Expand Up @@ -71,4 +71,4 @@ if http.listen(':8080', handle) != nil {
- [x] key为StringExp,而不是NameExp
- [x] `=` -> `:`, eg: `{a = 'a'}` -> `{a: 'a'}`
- CLI
- [ ] 利用HASH,如果文件内容没变化,就不需要重新编译
- [x] 利用HASH,如果文件内容没变化,就不需要重新编译
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.0.1`
VERSION = `0.1.1`
SIGNATURE = `LANG_LK`
)
24 changes: 15 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
package main

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

"git.lolli.tech/lollipopkit/go-lang-lk/compiler"
"git.lolli.tech/lollipopkit/go-lang-lk/state"
)

func main() {
compile := flag.Bool("c", false, "compile, not run")
flag.Parse()

file := flag.Arg(0)
if file == "" {
panic("no input file")
}

compiledFile := strings.Replace(file, ".lk", ".lkc", 1)
compiledFileName := getSHA256HashCode([]byte(file)) + ".lkc"
compiledFile := path.Join(os.TempDir(), compiledFileName)
compiledData, _ := ioutil.ReadFile(compiledFile)

if !exist(compiledFile) || sourceChanged(file, compiledFile) {
Expand All @@ -39,12 +41,10 @@ func main() {
f.Write(compiledData)
}

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

func exist(path string) bool {
Expand All @@ -63,3 +63,9 @@ func sourceChanged(source, compiled string) bool {
}
return s.ModTime().After(c.ModTime())
}

func getSHA256HashCode(message []byte) string {
bytes := sha256.Sum256(message)
hashCode := hex.EncodeToString(bytes[:])
return hashCode
}
32 changes: 31 additions & 1 deletion stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

. "git.lolli.tech/lollipopkit/go-lang-lk/api"
"git.lolli.tech/lollipopkit/go-lang-lk/consts"
)

var baseFuncs = map[string]GoFunction{
Expand Down Expand Up @@ -39,6 +40,8 @@ var baseFuncs = map[string]GoFunction{
"insert": tabInsert,
"delete": tabRemove,
"sort": tabSort,
// string
"fmt": strFormat,
}

// lua-5.3.4/src/lbaselib.c#luaopen_base()
Expand All @@ -50,7 +53,7 @@ func OpenBaseLib(ls LkState) int {
ls.PushValue(-1)
ls.SetField(-2, "_G")
/* set global _VERSION */
ls.PushString("LK 5.3") // todo
ls.PushString(consts.VERSION) // todo
ls.SetField(-2, "_VERSION")
return 1
}
Expand All @@ -70,6 +73,33 @@ func baseKV(ls LkState) int {
return 2
}


// format (formatstring, ···)
// http://www.lua.org/manual/5.3/manual.html#pdf-string.format
func strFormat(ls LkState) int {
fmtStr := ls.CheckString(1)
if len(fmtStr) <= 1 || strings.IndexByte(fmtStr, '%') < 0 {
ls.PushString(fmtStr)
return 1
}

argIdx := 1
arr := parseFmtStr(fmtStr)
for i := range arr {
if arr[i][0] == '%' {
if arr[i] == "%%" {
arr[i] = "%"
} else {
argIdx += 1
arr[i] = _fmtArg(arr[i], ls, argIdx)
}
}
}

ls.PushString(strings.Join(arr, ""))
return 1
}

// print (···)
// http://www.lua.org/manual/5.3/manual.html#pdf-print
// lua-5.3.4/src/lbaselib.c#luaB_print()
Expand Down
29 changes: 0 additions & 29 deletions stdlib/lib_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var strLib = map[string]GoFunction{
"byte": strByte,
"char": strChar,
"dump": strDump,
"format": strFormat,
"packsize": strPackSize,
"pack": strPack,
"unpack": strUnpack,
Expand Down Expand Up @@ -225,34 +224,6 @@ func strUnpack(ls LkState) int {
panic("todo: strUnpack!")
}

/* STRING FORMAT */

// string.format (formatstring, ···)
// http://www.lua.org/manual/5.3/manual.html#pdf-string.format
func strFormat(ls LkState) int {
fmtStr := ls.CheckString(1)
if len(fmtStr) <= 1 || strings.IndexByte(fmtStr, '%') < 0 {
ls.PushString(fmtStr)
return 1
}

argIdx := 1
arr := parseFmtStr(fmtStr)
for i := range arr {
if arr[i][0] == '%' {
if arr[i] == "%%" {
arr[i] = "%"
} else {
argIdx += 1
arr[i] = _fmtArg(arr[i], ls, argIdx)
}
}
}

ls.PushString(strings.Join(arr, ""))
return 1
}

func _fmtArg(tag string, ls LkState, argIdx int) string {
switch tag[len(tag)-1] { // specifier
case 'c': // character
Expand Down
2 changes: 1 addition & 1 deletion test/basic.lk
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ if #long >= 0 and '' {
}

print(6 ~/ 2, 6 & 2, 6 / 2)
print({'a'} .. '', 1 .. 2)
print({'a'} .. '', 1 .. 2, _VERSION)
2 changes: 1 addition & 1 deletion test/http/listen.lk
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ headers.__str = fn(a) {

shy fn handle(req) {
setmetatable(req.headers, headers)
rt 200, string.format('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
rt 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
}

if http.listen(':8080', handle) != nil {
Expand Down

0 comments on commit b2fb56e

Please sign in to comment.