Skip to content

Commit

Permalink
新增 strs.replace
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 28, 2022
1 parent b05ff36 commit dd8f087
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
3 changes: 0 additions & 3 deletions compiler/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"regexp"
"strconv"
"strings"

"git.lolli.tech/lollipopkit/lk/term"
)

// var reSpaces = regexp.MustCompile(`^\s+`)
Expand Down Expand Up @@ -315,7 +313,6 @@ func (self *Lexer) scanRawString() string {
str = str[1:]
}
self.next(openIdx + 1)
term.Info(str)
return str
}

Expand Down
50 changes: 46 additions & 4 deletions scripts/build.lk
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
args := os.args
if #args < 2 {
print("Usage: lk build.lk <version>")
if #args < 3 {
print("Usage: lk build.lk <version> [-compile]")
os.exit(1)
}

version := args[2]
version = args[2]
if not re.have(`^\d+\.\d+\.\d+$`, version) {
print("Invalid version: %s", version)
os.exit(1)
}
compile := args[3] == "-compile"

mods_index_path := "mods/index.json"
consts_ver_path := 'consts/lang.go'
mods_re_exp := `"vm": "(.*)"`
consts_re_exp := `VERSION *= *"(.*)"`

replace_lang_ver := fn(path, re_exp) {
content, err := os.read(path)
if err != nil {
print("Error reading "+path)
os.exit(1)
}

old_ver := re.find(re_exp, content)
if old_ver == nil {
print("Error finding version in "+path)
os.exit(1)
}

old_ver = old_ver[1]

if old_ver != version {
content = strs.replace(content, old_ver, version)
err := os.write(path, content)
if err != nil {
print("Error writing "+path)
os.exit(1)
}
}
}

replace_lang_ver(mods_index_path, mods_re_exp)
replace_lang_ver(consts_ver_path, consts_re_exp)

if not compile {
os.exit()
}

archs = {'arm64', 'amd64'}
platforms = {'darwin', 'linux', 'windows'}
Expand All @@ -17,7 +59,7 @@ for _, arch in archs {
cmd = fmt('%s go build -o %s', prefix, output)
_, err := os.exec(cmd)
if err != nil {
print(fmt('Failed to build %s-%s', platform, arch))
print(fmt('Failed to build %s-%s: ', platform, arch, err))
os.exit()
}
print(fmt('Successfully built %s-%s', platform, arch))
Expand Down
10 changes: 9 additions & 1 deletion stdlib/lib_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ var strLib = map[string]GoFunction{
"split": strSplit,
"join": strJoin,
"contains": strContains,
"replace": strReplace,
}

func OpenStringLib(ls LkState) int {
ls.NewLib(strLib)
return 1
}

/* Basic String Functions */
func strReplace(ls LkState) int {
s := ls.CheckString(1)
old := ls.CheckString(2)
new := ls.CheckString(3)
times := ls.OptInteger(4, -1)
ls.PushString(strings.Replace(s, old, new, int(times)))
return 1
}

func strContains(ls LkState) int {
s := ls.CheckString(1)
Expand Down

0 comments on commit dd8f087

Please sign in to comment.