Skip to content

Commit

Permalink
优化 REPL & math.random
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Oct 25, 2022
1 parent 2d4df33 commit 6d9e7cc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# CHANGELOG
**仅包含语言LK的变化**

## 0.1.6
- 支持 `shy a = b` -> `a := b` 语法糖
- 支持 `a := fn(b) => c` 语法
- 优化 `stdlib_os`

## 0.1.5
- `CLI` 增加 `-d` 开启调试模式
- `os.time()` 返回毫秒时间戳
- 去除 `select` ,使用 `#{...}` 替代
- 支持三元操作符 `a ? b : c`
- 去除 `concat`
- 修复 `os.args` 索引错误
- 支持 `shy a = b` -> `a := b` 语法糖
- 支持 `a := fn(b) => c` 语法
- 优化 `stdlib_os`

## 0.1.4
- `+` 支持 `String`
Expand Down
2 changes: 1 addition & 1 deletion consts/re.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "regexp"

const (
ForInReStr = `for +(\S+(, *\S+)*) +in +(\S+) *\{`
FnReStr = `fn +(\S*)\((\S*(, *\S+)*)\) *\{`
FnReStr = `fn *(\S*)\((\S*(, *\S+)*)\) *\{`
WhileReStr = `while +(\S+ ) *\{`
IfReStr = `if +(\S+ )+ *\{`
ElseIfReStr = `elif +(\S+ ) *\{`
Expand Down
10 changes: 9 additions & 1 deletion repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func protectedLoadString(ls *api.LkState, cmd string) {
(*ls).LoadString(addedPrintCmd, "stdin")
}

func updateHistory(str string) {
func _updateHistory(str string) {
idx := -1
for i := range linesHistory {
if linesHistory[i] == str {
Expand All @@ -120,6 +120,14 @@ func updateHistory(str string) {
linesHistory = append(linesHistory, str)
}

func updateHistory(str string) {
str = strings.Trim(str, "\n")
strs := strings.Split(str, "\n")
for idx := range strs {
_updateHistory(strs[idx])
}
}

func readline() string {
str := ""
linesIdx := len(linesHistory)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/lib_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stdlib
import (
"math"
"math/rand"
"time"

. "git.lolli.tech/lollipopkit/lk/api"
"git.lolli.tech/lollipopkit/lk/number"
Expand Down Expand Up @@ -34,6 +35,7 @@ var mathLib = map[string]GoFunction{
}

func OpenMathLib(ls LkState) int {
rand.Seed(time.Now().UnixMilli())
ls.NewLib(mathLib)
ls.PushNumber(math.Pi)
ls.SetField(-2, "pi")
Expand Down

0 comments on commit 6d9e7cc

Please sign in to comment.