Skip to content

Commit

Permalink
新增 os.cp input,库string -> strs
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 27, 2022
1 parent d7839e9 commit 1a7b9dd
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 41 deletions.
4 changes: 2 additions & 2 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func WriteAst(path string) {
if err != nil {
term.Error(err.Error())
}

err = ioutil.WriteFile(path+".ast.json", j, 0644)
if err != nil {
term.Error(err.Error())
}
}
}
2 changes: 0 additions & 2 deletions compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func Parse(chunk, chunkName string) *Block {
lexer := NewLexer(chunk, chunkName)
block := parseBlock(lexer)



lexer.NextTokenOfKind(TOKEN_EOF)
return block
}
Expand Down
3 changes: 1 addition & 2 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var (
printReg = regexp.MustCompile(`print\(.*\)`)
)


func repl(wg *sync.WaitGroup) {
ls := state.New()
ls.OpenLibs()
Expand All @@ -36,7 +35,7 @@ func repl(wg *sync.WaitGroup) {
blockStartCount := 0
blockEndCount := 0
wg.Wait()

for {
line := term.ReadLine(linesHistory)
if line == "" {
Expand Down
22 changes: 11 additions & 11 deletions state/auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ func (self *luaState) CallMeta(obj int, event string) bool {
// http://www.lua.org/manual/5.3/manual.html#luaL_openlibs
func (self *luaState) OpenLibs() {
libs := map[string]GoFunction{
"_G": stdlib.OpenBaseLib,
"math": stdlib.OpenMathLib,
"string": stdlib.OpenStringLib,
"utf8": stdlib.OpenUTF8Lib,
"os": stdlib.OpenOSLib,
"pkg": stdlib.OpenPackageLib,
"sync": stdlib.OpenCoroutineLib,
"http": stdlib.OpenHttpLib,
"json": stdlib.OpenJsonLib,
"re": stdlib.OpenReLib,
"rand": stdlib.OpenRandLib,
"_G": stdlib.OpenBaseLib,
"math": stdlib.OpenMathLib,
"strs": stdlib.OpenStringLib,
"utf8": stdlib.OpenUTF8Lib,
"os": stdlib.OpenOSLib,
"pkg": stdlib.OpenPackageLib,
"sync": stdlib.OpenCoroutineLib,
"http": stdlib.OpenHttpLib,
"json": stdlib.OpenJsonLib,
"re": stdlib.OpenReLib,
"rand": stdlib.OpenRandLib,
}

for name := range libs {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/lib_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var baseFuncs = map[string]GoFunction{
"new": baseNew,
"print": basePrint,
"input": baseInput,
"input": baseInput,
"assert": baseAssert,
"error": baseError,
"irange": baseIPairs,
Expand Down
12 changes: 12 additions & 0 deletions stdlib/lib_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var sysLib = map[string]GoFunction{
"date": osDate,
"rm": osRemove,
"mv": osRename,
"cp": osCp,
"link": osLink,
"tmp": osTmpName,
"get_env": osGetEnv,
Expand All @@ -42,6 +43,17 @@ func pushArgs(ls LkState) {
ls.SetField(-2, "args")
}

func osCp(ls LkState) int {
src := ls.CheckString(1)
dst := ls.CheckString(2)
if err := utils.Copy(src, dst); err != nil {
ls.PushString(err.Error())
return 1
}
ls.PushNil()
return 1
}

func osStat(ls LkState) int {
path := ls.CheckString(1)
info, err := os.Stat(path)
Expand Down
11 changes: 2 additions & 9 deletions stdlib/lib_re.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ func getExp(pattern string) *regexp.Regexp {
if ok {
exp, ok = cache.(*regexp.Regexp)
if ok {
goto END
return exp
}

}
exp = regexp.MustCompile(pattern)
reCacher.Set(pattern, exp)
END:
return exp
}

Expand All @@ -51,11 +49,6 @@ func reFind(ls LkState) int {
ls.PushNil()
return 1
}
tb := make(map[string]string, len(matches))
names := getExp(pattern).SubexpNames()
for idx := range names {
tb[names[idx]] = matches[idx]
}
pushTable(&ls, tb)
pushList(&ls, matches)
return 1
}
28 changes: 18 additions & 10 deletions stdlib/lib_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
)

var strLib = map[string]GoFunction{
"len": strLen,
"repeat": strRep,
"reverse": strReverse,
"lower": strLower,
"upper": strUpper,
"sub": strSub,
"byte": strByte,
"char": strChar,
"split": strSplit,
"join": strJoin,
"len": strLen,
"repeat": strRep,
"reverse": strReverse,
"lower": strLower,
"upper": strUpper,
"sub": strSub,
"byte": strByte,
"char": strChar,
"split": strSplit,
"join": strJoin,
"contains": strContains,
}

func OpenStringLib(ls LkState) int {
Expand All @@ -27,6 +28,13 @@ func OpenStringLib(ls LkState) int {

/* Basic String Functions */

func strContains(ls LkState) int {
s := ls.CheckString(1)
sub := ls.CheckString(2)
ls.PushBoolean(strings.Contains(s, sub))
return 1
}

func strJoin(ls LkState) int {
list := CheckList(&ls, 1)
sep := ls.CheckString(2)
Expand Down
4 changes: 2 additions & 2 deletions term/interact.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
)

var (
promptLen = len([]rune(prompt))
promptLen = len([]rune(prompt))
)

func ReadLine(linesHistory []string) string {
Expand Down Expand Up @@ -92,4 +92,4 @@ func resetLine(str string) {
cursor.ClearLine()
cursor.StartOfLine()
print(prompt + str)
}
}
2 changes: 1 addition & 1 deletion test.lk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ contains := fn(name) {
}

for _, file in files {
if string.sub(file, #file - 3) != '.lkc' {
if strs.sub(file, #file - 3) != '.lkc' {
if not contains(file) {
print(fmt('====== %s ======', file))
do_file(dir + file)
Expand Down
80 changes: 80 additions & 0 deletions utils/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package utils

import (
"io"
"io/ioutil"
"os"
"path"
)

func Copy(src, dst string) error {
var err error
var srcinfo os.FileInfo

if srcinfo, err = os.Stat(src); err != nil {
return err
}

if !srcinfo.IsDir() {
return copyFile(src, dst)
}
return copyDir(src, dst)
}

func copyFile(src, dst string) error {
var err error
var srcfd *os.File
var dstfd *os.File
var srcinfo os.FileInfo

if srcfd, err = os.Open(src); err != nil {
return err
}
defer srcfd.Close()

if dstfd, err = os.Create(dst); err != nil {
return err
}
defer dstfd.Close()

if _, err = io.Copy(dstfd, srcfd); err != nil {
return err
}
if srcinfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcinfo.Mode())
}

func copyDir(src string, dst string) error {
var err error
var fds []os.FileInfo
var srcinfo os.FileInfo

if srcinfo, err = os.Stat(src); err != nil {
return err
}

if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
return err
}

if fds, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range fds {
srcfp := path.Join(src, fd.Name())
dstfp := path.Join(dst, fd.Name())

if fd.IsDir() {
if err = copyDir(srcfp, dstfp); err != nil {
return err
}
} else {
if err = copyFile(srcfp, dstfp); err != nil {
return err
}
}
}
return nil
}
1 change: 0 additions & 1 deletion utils/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func CheckUpgrade(wg *sync.WaitGroup) {
return
}


resp, err := client.Get(consts.ReleaseApiUrl)
if err != nil {
return
Expand Down

0 comments on commit 1a7b9dd

Please sign in to comment.