Skip to content

Commit b022027

Browse files
authored
Improved CLI (#26)
* feat: better cli * fix: imports and declarations in repl were broken
1 parent 180add6 commit b022027

File tree

12 files changed

+960
-79
lines changed

12 files changed

+960
-79
lines changed

.goreleaser.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ before:
88
- go generate ./...
99
builds:
1010
- id: lithia-macos
11-
main: ./cmd/lithia
11+
main: ./app/lithia
1212
env:
1313
- CGO_ENABLED=1
1414
goos:
@@ -19,7 +19,7 @@ builds:
1919
# brew install FiloSottile/musl-cross/musl-cross --with-aarch64
2020
# brew install mingw-w64
2121
- id: lithia-x-linux-amd64
22-
main: ./cmd/lithia
22+
main: ./app/lithia
2323
env:
2424
- CGO_ENABLED=1
2525
- CC=x86_64-linux-musl-gcc
@@ -28,7 +28,7 @@ builds:
2828
goarch:
2929
- amd64
3030
- id: lithia-x-linux-arm64
31-
main: ./cmd/lithia
31+
main: ./app/lithia
3232
env:
3333
- CGO_ENABLED=1
3434
- CC=aarch64-linux-musl-gcc
@@ -37,7 +37,7 @@ builds:
3737
goarch:
3838
- arm64
3939
- id: lithia-x-windows-amd64
40-
main: ./cmd/lithia
40+
main: ./app/lithia
4141
env:
4242
- CGO_ENABLED=1
4343
- CC=x86_64-w64-mingw32-gcc
@@ -88,7 +88,7 @@ dockers:
8888
# -> only the second stage
8989
extra_files:
9090
- ast
91-
- cmd
91+
- app
9292
- parser
9393
- reporting
9494
- runtime

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.0.12-next
4+
5+
- cli: new CLI interface, including, help and version
6+
- fix: imports and declarations in repl were broken
7+
38
## v0.0.11
49

510
- compiler: binaries for macOS, linux, windows

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN go mod download
1313

1414
COPY ./ ./
1515

16-
RUN go build ./cmd/lithia
16+
RUN go build ./app/lithia
1717

1818
##
1919
## Deploy

app/lithia/cmd/lsp.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func init() {
10+
// rootCmd.AddCommand(lspCmd)
11+
lspCmd.AddCommand(lspStdioCmd)
12+
lspCmd.AddCommand(lspSocketCmd)
13+
14+
lspSocketCmd.Flags().StringVarP(
15+
&lspSocketAddress,
16+
"listen",
17+
"l",
18+
"127.0.0.1:7998",
19+
"Address and port on which to listen for LSP connections",
20+
)
21+
}
22+
23+
var lspCmd = &cobra.Command{
24+
Use: "lsp",
25+
Short: "Language Server",
26+
Long: `Runs the language server for the use inside an editor.`,
27+
Args: cobra.NoArgs,
28+
Run: func(cmd *cobra.Command, args []string) {
29+
lspStdioCmd.Run(lspStdioCmd, args)
30+
},
31+
}
32+
33+
var lspStdioCmd = &cobra.Command{
34+
Use: "stdio",
35+
Aliases: []string{"stdin", "-"},
36+
Short: "stdio mode. Supported by most editors.",
37+
Run: func(cmd *cobra.Command, args []string) {
38+
fmt.Println("stdio")
39+
},
40+
}
41+
42+
var lspSocketAddress string = "127.0.0.1:7998"
43+
var lspSocketCmd = &cobra.Command{
44+
Use: "socket",
45+
Short: `opens a socket on the specified address. Make sure the port is free.`,
46+
Args: cobra.RangeArgs(0, 1),
47+
Run: func(cmd *cobra.Command, args []string) {
48+
fmt.Println("socket", lspSocketAddress)
49+
},
50+
}

app/lithia/cmd/repl.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmd
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/spf13/cobra"
10+
"github.com/vknabel/go-lithia/reporting"
11+
"github.com/vknabel/go-lithia/runtime"
12+
)
13+
14+
func init() {
15+
rootCmd.AddCommand(replCmd)
16+
}
17+
18+
var replCmd = &cobra.Command{
19+
Use: "repl",
20+
Short: "Runs interactive Lithia REPL.",
21+
Long: ``,
22+
Args: cobra.NoArgs,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
runPrompt()
25+
},
26+
}
27+
28+
func runPrompt() {
29+
importRoot, err := os.Getwd()
30+
if err != nil {
31+
fmt.Fprint(os.Stderr, err)
32+
os.Exit(1)
33+
}
34+
reader := bufio.NewReader(os.Stdin)
35+
inter := runtime.NewInterpreter(importRoot)
36+
for {
37+
fmt.Print("> ")
38+
line, err := reader.ReadString('\n')
39+
if err == io.EOF {
40+
return
41+
}
42+
if err != nil {
43+
reporting.ReportErrorOrPanic(err)
44+
continue
45+
}
46+
value, err := inter.InterpretEmbed("prompt", line)
47+
if err != nil {
48+
reporting.ReportErrorOrPanic(err)
49+
continue
50+
}
51+
if value != nil {
52+
fmt.Println("- ", value)
53+
}
54+
}
55+
}

app/lithia/cmd/root.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
func Execute() error {
8+
return rootCmd.Execute()
9+
}
10+
11+
var rootCmd = &cobra.Command{
12+
Use: "lithia",
13+
Short: "Lithia programming language",
14+
Long: "Lithia is an experimental functional programming language " +
15+
"with an implicit but strong and dynamic type system.\n" +
16+
"It is designed around a few core concepts in mind " +
17+
"all language features contribute to.\n" +
18+
"\n" +
19+
"Lean more at https://github.com/vknabel/lithia",
20+
Version: "0.0.12-next",
21+
Args: cobra.RangeArgs(0, 1),
22+
Run: func(cmd *cobra.Command, args []string) {
23+
if len(args) == 1 {
24+
runFile(args[0])
25+
} else {
26+
runPrompt()
27+
}
28+
},
29+
}

app/lithia/cmd/run.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/vknabel/go-lithia/runtime"
10+
)
11+
12+
func init() {
13+
rootCmd.AddCommand(runCmd)
14+
}
15+
16+
var runCmd = &cobra.Command{
17+
Use: "run [script]",
18+
Short: "Runs a Lithia script",
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
runFile(args[0])
22+
},
23+
}
24+
25+
func runFile(fileName string) {
26+
scriptData, err := os.ReadFile(fileName)
27+
if err != nil {
28+
fmt.Fprint(os.Stderr, err)
29+
os.Exit(1)
30+
}
31+
inter := runtime.NewInterpreter(path.Dir(fileName))
32+
script := string(scriptData) + "\n"
33+
_, err = inter.Interpret(fileName, script)
34+
if err != nil {
35+
fmt.Fprint(os.Stderr, err)
36+
os.Exit(1)
37+
}
38+
}

app/lithia/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/vknabel/go-lithia/app/lithia/cmd"
8+
)
9+
10+
func main() {
11+
err := cmd.Execute()
12+
if err != nil {
13+
fmt.Fprint(os.Stderr, err)
14+
os.Exit(1)
15+
}
16+
}

cmd/lithia/main.go

-70
This file was deleted.

go.mod

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ module github.com/vknabel/go-lithia
33
go 1.16
44

55
require (
6-
github.com/davecgh/go-spew v1.1.1 // indirect
76
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82
8-
github.com/stretchr/testify v1.7.0 // indirect
7+
github.com/spf13/cobra v1.3.0
98
github.com/vknabel/tree-sitter-lithia v0.0.0-20220121161404-ed4529b7c21c
10-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
119
)

0 commit comments

Comments
 (0)