Skip to content

Commit

Permalink
默认输出编译文件至同目录
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 24, 2022
1 parent 91bb6c8 commit c31ef73
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 37 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ fn Header:__str() {
处理监听事件
`req`包含属性`method`, `url`, `body`, `headers`
*/
shy fn handle(req) {
shy h = Header:fromTable(req.headers)
rt 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, h, req.body)
}
handle := fn(req) => 200, fmt('%s %s\n\n%s\n%s', req.method, req.url, Header:fromTable(req.headers), req.body)

// 监听
if http.listen(':8080', handle) != nil {
Expand Down Expand Up @@ -100,7 +97,6 @@ if http.listen(':8080', handle) != nil {
- [x] 自动添加 `range` ( `paris` )
- [x] 支持 `a++` `a+=b`
- [x] CLI
- [x] 利用HASH,文件无变化不编译
- [x] 支持传入参数 ( `lk args.lk arg1` -> `.lk`内调用`os.args` )
- [x] REPL,直接运行 `./lk` 即可进入
- [x] 支持方向键
Expand Down
5 changes: 3 additions & 2 deletions binchunk/binary_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ type LocVar struct {
EndPC uint32 `json:"epc"`
}

func IsJsonChunk(data []byte) (bool, *Prototype) {
func Verify(data []byte) (bool, *Prototype) {
var bin binaryChunk
err := json.Unmarshal(data, &bin)
if err != nil {
return false, nil
}
if bin.Sign != consts.SIGNATURE || bin.Version != consts.VERSION {
panic("mismatch version or signature")
println("mismatch version or signature\n")
return false, nil
}
return err == nil, bin.Proto
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/parser/parse_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ func parseAssignStat(lexer *Lexer, var0 Exp) Stat {
if lexer.LookAhead() == TOKEN_OP_ASSIGNSHY {
lexer.NextToken() // :=
expList := parseExpList(lexer) // explist
strExps := make([]string, len(expList))
for i, exp := range varList {
name, ok := exp.(*NameExp)
strExps := make([]string, len(varList))
for i := range varList {
name, ok := varList[i].(*NameExp)
if !ok {
panic("invalid assignment")
}
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

var (
force = flag.Bool("f", false, "force to re-compile")
debug = flag.Bool("d", false, "debug mode")
args = []string{}
)
Expand Down
32 changes: 7 additions & 25 deletions run.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package main

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

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

func compile(source string) {
func compile(source string) []byte {
if !exist(source) {
panic("file not found")
}
Expand All @@ -32,6 +29,7 @@ func compile(source string) {
panic(err)
}
f.Write(compiledData)
return compiledData
}

func run(file string) {
Expand All @@ -44,24 +42,14 @@ func run(file string) {
panic(err)
}

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

isJChunk, _ := binchunk.IsJsonChunk(data)
if isJChunk {
valid, _ := binchunk.Verify(data)
if valid {
compiledData = data
} else if !exist(compiledFilePath) || *force {
bin := compiler.Compile(string(data), file)
f, err := os.Create(compiledFilePath)
if err != nil {
panic(err)
}
compiledData, err = bin.Dump()
if err != nil {
panic(err)
}
f.Write(compiledData)
} else {
compile(file)
}

ls := state.New()
Expand All @@ -74,9 +62,3 @@ 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 state/api_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// http://www.lua.org/manual/5.3/manual.html#lua_load
func (self *luaState) Load(chunk []byte, chunkName, mode string) int {
var proto *binchunk.Prototype
isJson, prot := binchunk.IsJsonChunk(chunk)
isJson, prot := binchunk.Verify(chunk)
logger.I("[state.Load] Using compiled chunk: %v", isJson)
if isJson {
proto = prot
Expand Down

0 comments on commit c31ef73

Please sign in to comment.