Skip to content

Commit

Permalink
REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
lollipopkit committed Sep 29, 2022
1 parent 08c1034 commit d496824
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ builds:
goarm:
- 6
- 7
ignore:
- goos: windows
goarch: arm
goarm: 6
- goos: windows
goarch: arm
goarm: 7

checksum:
name_template: 'checksums.txt'
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"mode": "debug",
"program": "main.go",
"args": ["-f", "test/os.lk"]
"args": ["-f", "run", "test/re.lk"]
}
]
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ if http.listen(':8080', handle) != nil {
- [x] 自动添加`range` ( `paris` )
- CLI
- [x] 利用HASH,文件无变化不编译
- [x] 支持传入参数
- [x] 支持传入参数
- [x] REPL
5 changes: 4 additions & 1 deletion compiler/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (

. "git.lolli.tech/lollipopkit/go-lang-lk/compiler/ast"
. "git.lolli.tech/lollipopkit/go-lang-lk/compiler/lexer"
"git.lolli.tech/lollipopkit/go-lang-lk/consts"
)



var (
replaceRules = map[string]*regexp.Regexp{
// for in:自动添加range
"for $1 in range($3) {": regexp.MustCompile(`for +(\S+(, {0,1}\S+)) +in +(\S*) *\{`),
"for $1 in range($3) {": consts.ForInRe,
}
)

Expand Down
25 changes: 25 additions & 0 deletions consts/re.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package consts

import "regexp"

const (
ForInReStr = `for +(\S+(, *\S+)*) +in +(\S+) *\{`
FnReStr = `fn +(\S*)\((\S+(, *\S+)*)\) *\{`
WhileReStr = `while +(\S+) *\{`
IfReStr = `if +(\S+) *\{`
ElseIfReStr = `elif +(\S+) *\{`
ElseReStr = `else *\{`
)

var (
ForInRe = _re(ForInReStr)
FnRe = _re(FnReStr)
WhileRe = _re(WhileReStr)
IfRe = _re(IfReStr)
ElseIfRe = _re(ElseIfReStr)
ElseRe = _re(ElseReStr)
)

func _re(s string) *regexp.Regexp {
return regexp.MustCompile(s)
}
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,21 @@ import (
func main() {
force := flag.Bool("f", false, "force to re-compile")
flag.Parse()
args := flag.Args()
if len(args) == 0 {
println("Usage: \n lang-lk repl\n lang-lk [options] run <file>")
return
}

switch args[0] {
case "repl":
repl()
return
case "run":
break
}

file := flag.Arg(0)
file := args[1]
if file == "" {
panic("no input file")
}
Expand Down
35 changes: 35 additions & 0 deletions repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"bufio"
"os"

"git.lolli.tech/lollipopkit/go-lang-lk/consts"
"git.lolli.tech/lollipopkit/go-lang-lk/state"
)

func repl() {
ls := state.New()
ls.OpenLibs()
println(`
_ _ ____ _
| | | | __ | _ \ ___ _ __ | |
| | | |/ / | |_) / _ \ '_ \| |
| |___| < | _ < __/ |_) | |
|_____|_|\_\ |_| \_\___| .__/|_|
|_| `)
println(" v" + consts.VERSION)

for {
os.Stdout.WriteString("> ")
ls.LoadString(readline());

ls.PCall(0, -1, 0);
}
}

func readline() string {
inputReader := bufio.NewReader(os.Stdin)
input, _ := inputReader.ReadString('\n')
return input
}
3 changes: 0 additions & 3 deletions test/basic.lk
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ long comment
shy long = '''
abc
'''
goto SKIP
error('never reached')
::SKIP::

if #long >= 0 and '' {
print("hello", #long)
Expand Down

0 comments on commit d496824

Please sign in to comment.