Skip to content

Commit b2fb56e

Browse files
committed
string.format -> fmt
1 parent 1af6dc7 commit b2fb56e

File tree

7 files changed

+51
-44
lines changed

7 files changed

+51
-44
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ headers.__str = fn(a) {
3939
*/
4040
shy fn handle(req) {
4141
setmetatable(req.headers, headers)
42-
rt 200, string.format('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
42+
rt 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
4343
}
4444

4545
// 监听
@@ -71,4 +71,4 @@ if http.listen(':8080', handle) != nil {
7171
- [x] key为StringExp,而不是NameExp
7272
- [x] `=` -> `:`, eg: `{a = 'a'}` -> `{a: 'a'}`
7373
- CLI
74-
- [ ] 利用HASH,如果文件内容没变化,就不需要重新编译
74+
- [x] 利用HASH,如果文件内容没变化,就不需要重新编译

consts/lang.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package consts
22

33
var (
4-
VERSION = `0.0.1`
4+
VERSION = `0.1.1`
55
SIGNATURE = `LANG_LK`
66
)

main.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package main
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
46
"flag"
57
"io/ioutil"
68
"os"
7-
"strings"
9+
"path"
810

911
"git.lolli.tech/lollipopkit/go-lang-lk/compiler"
1012
"git.lolli.tech/lollipopkit/go-lang-lk/state"
1113
)
1214

1315
func main() {
14-
compile := flag.Bool("c", false, "compile, not run")
1516
flag.Parse()
1617

1718
file := flag.Arg(0)
1819
if file == "" {
1920
panic("no input file")
2021
}
2122

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

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

42-
if !*compile {
43-
ls := state.New()
44-
ls.OpenLibs()
45-
ls.Load(compiledData, file, "bt")
46-
ls.Call(0, -1)
47-
}
44+
ls := state.New()
45+
ls.OpenLibs()
46+
ls.Load(compiledData, file, "bt")
47+
ls.Call(0, -1)
4848
}
4949

5050
func exist(path string) bool {
@@ -63,3 +63,9 @@ func sourceChanged(source, compiled string) bool {
6363
}
6464
return s.ModTime().After(c.ModTime())
6565
}
66+
67+
func getSHA256HashCode(message []byte) string {
68+
bytes := sha256.Sum256(message)
69+
hashCode := hex.EncodeToString(bytes[:])
70+
return hashCode
71+
}

stdlib/lib_basic.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
. "git.lolli.tech/lollipopkit/go-lang-lk/api"
10+
"git.lolli.tech/lollipopkit/go-lang-lk/consts"
1011
)
1112

1213
var baseFuncs = map[string]GoFunction{
@@ -39,6 +40,8 @@ var baseFuncs = map[string]GoFunction{
3940
"insert": tabInsert,
4041
"delete": tabRemove,
4142
"sort": tabSort,
43+
// string
44+
"fmt": strFormat,
4245
}
4346

4447
// lua-5.3.4/src/lbaselib.c#luaopen_base()
@@ -50,7 +53,7 @@ func OpenBaseLib(ls LkState) int {
5053
ls.PushValue(-1)
5154
ls.SetField(-2, "_G")
5255
/* set global _VERSION */
53-
ls.PushString("LK 5.3") // todo
56+
ls.PushString(consts.VERSION) // todo
5457
ls.SetField(-2, "_VERSION")
5558
return 1
5659
}
@@ -70,6 +73,33 @@ func baseKV(ls LkState) int {
7073
return 2
7174
}
7275

76+
77+
// format (formatstring, ···)
78+
// http://www.lua.org/manual/5.3/manual.html#pdf-string.format
79+
func strFormat(ls LkState) int {
80+
fmtStr := ls.CheckString(1)
81+
if len(fmtStr) <= 1 || strings.IndexByte(fmtStr, '%') < 0 {
82+
ls.PushString(fmtStr)
83+
return 1
84+
}
85+
86+
argIdx := 1
87+
arr := parseFmtStr(fmtStr)
88+
for i := range arr {
89+
if arr[i][0] == '%' {
90+
if arr[i] == "%%" {
91+
arr[i] = "%"
92+
} else {
93+
argIdx += 1
94+
arr[i] = _fmtArg(arr[i], ls, argIdx)
95+
}
96+
}
97+
}
98+
99+
ls.PushString(strings.Join(arr, ""))
100+
return 1
101+
}
102+
73103
// print (···)
74104
// http://www.lua.org/manual/5.3/manual.html#pdf-print
75105
// lua-5.3.4/src/lbaselib.c#luaB_print()

stdlib/lib_string.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var strLib = map[string]GoFunction{
1717
"byte": strByte,
1818
"char": strChar,
1919
"dump": strDump,
20-
"format": strFormat,
2120
"packsize": strPackSize,
2221
"pack": strPack,
2322
"unpack": strUnpack,
@@ -225,34 +224,6 @@ func strUnpack(ls LkState) int {
225224
panic("todo: strUnpack!")
226225
}
227226

228-
/* STRING FORMAT */
229-
230-
// string.format (formatstring, ···)
231-
// http://www.lua.org/manual/5.3/manual.html#pdf-string.format
232-
func strFormat(ls LkState) int {
233-
fmtStr := ls.CheckString(1)
234-
if len(fmtStr) <= 1 || strings.IndexByte(fmtStr, '%') < 0 {
235-
ls.PushString(fmtStr)
236-
return 1
237-
}
238-
239-
argIdx := 1
240-
arr := parseFmtStr(fmtStr)
241-
for i := range arr {
242-
if arr[i][0] == '%' {
243-
if arr[i] == "%%" {
244-
arr[i] = "%"
245-
} else {
246-
argIdx += 1
247-
arr[i] = _fmtArg(arr[i], ls, argIdx)
248-
}
249-
}
250-
}
251-
252-
ls.PushString(strings.Join(arr, ""))
253-
return 1
254-
}
255-
256227
func _fmtArg(tag string, ls LkState, argIdx int) string {
257228
switch tag[len(tag)-1] { // specifier
258229
case 'c': // character

test/basic.lk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ if #long >= 0 and '' {
1919
}
2020

2121
print(6 ~/ 2, 6 & 2, 6 / 2)
22-
print({'a'} .. '', 1 .. 2)
22+
print({'a'} .. '', 1 .. 2, _VERSION)

test/http/listen.lk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ headers.__str = fn(a) {
1313

1414
shy fn handle(req) {
1515
setmetatable(req.headers, headers)
16-
rt 200, string.format('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
16+
rt 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, req.headers, req.body)
1717
}
1818

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

0 commit comments

Comments
 (0)