diff --git a/README.md b/README.md index f440acd..fe578cc 100644 --- a/README.md +++ b/README.md @@ -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) } // 监听 @@ -71,4 +71,4 @@ if http.listen(':8080', handle) != nil { - [x] key为StringExp,而不是NameExp - [x] `=` -> `:`, eg: `{a = 'a'}` -> `{a: 'a'}` - CLI - - [ ] 利用HASH,如果文件内容没变化,就不需要重新编译 \ No newline at end of file + - [x] 利用HASH,如果文件内容没变化,就不需要重新编译 \ No newline at end of file diff --git a/consts/lang.go b/consts/lang.go index 684608a..376b77f 100644 --- a/consts/lang.go +++ b/consts/lang.go @@ -1,6 +1,6 @@ package consts var ( - VERSION = `0.0.1` + VERSION = `0.1.1` SIGNATURE = `LANG_LK` ) \ No newline at end of file diff --git a/main.go b/main.go index 8037b53..f7e11fb 100644 --- a/main.go +++ b/main.go @@ -1,17 +1,18 @@ 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) @@ -19,7 +20,8 @@ func main() { 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) { @@ -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 { @@ -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 +} diff --git a/stdlib/lib_basic.go b/stdlib/lib_basic.go index b5c194d..13c00ce 100644 --- a/stdlib/lib_basic.go +++ b/stdlib/lib_basic.go @@ -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{ @@ -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() @@ -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 } @@ -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() diff --git a/stdlib/lib_string.go b/stdlib/lib_string.go index 94a6fab..741fced 100644 --- a/stdlib/lib_string.go +++ b/stdlib/lib_string.go @@ -17,7 +17,6 @@ var strLib = map[string]GoFunction{ "byte": strByte, "char": strChar, "dump": strDump, - "format": strFormat, "packsize": strPackSize, "pack": strPack, "unpack": strUnpack, @@ -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 diff --git a/test/basic.lk b/test/basic.lk index db5843f..dba81af 100644 --- a/test/basic.lk +++ b/test/basic.lk @@ -19,4 +19,4 @@ if #long >= 0 and '' { } print(6 ~/ 2, 6 & 2, 6 / 2) -print({'a'} .. '', 1 .. 2) \ No newline at end of file +print({'a'} .. '', 1 .. 2, _VERSION) \ No newline at end of file diff --git a/test/http/listen.lk b/test/http/listen.lk index 7643307..376f946 100644 --- a/test/http/listen.lk +++ b/test/http/listen.lk @@ -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 {