Skip to content

Commit

Permalink
Improved CLI (#26)
Browse files Browse the repository at this point in the history
* feat: better cli

* fix: imports and declarations in repl were broken
  • Loading branch information
vknabel authored Feb 5, 2022
1 parent 180add6 commit b022027
Show file tree
Hide file tree
Showing 12 changed files with 960 additions and 79 deletions.
10 changes: 5 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ before:
- go generate ./...
builds:
- id: lithia-macos
main: ./cmd/lithia
main: ./app/lithia
env:
- CGO_ENABLED=1
goos:
Expand All @@ -19,7 +19,7 @@ builds:
# brew install FiloSottile/musl-cross/musl-cross --with-aarch64
# brew install mingw-w64
- id: lithia-x-linux-amd64
main: ./cmd/lithia
main: ./app/lithia
env:
- CGO_ENABLED=1
- CC=x86_64-linux-musl-gcc
Expand All @@ -28,7 +28,7 @@ builds:
goarch:
- amd64
- id: lithia-x-linux-arm64
main: ./cmd/lithia
main: ./app/lithia
env:
- CGO_ENABLED=1
- CC=aarch64-linux-musl-gcc
Expand All @@ -37,7 +37,7 @@ builds:
goarch:
- arm64
- id: lithia-x-windows-amd64
main: ./cmd/lithia
main: ./app/lithia
env:
- CGO_ENABLED=1
- CC=x86_64-w64-mingw32-gcc
Expand Down Expand Up @@ -88,7 +88,7 @@ dockers:
# -> only the second stage
extra_files:
- ast
- cmd
- app
- parser
- reporting
- runtime
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.0.12-next

- cli: new CLI interface, including, help and version
- fix: imports and declarations in repl were broken

## v0.0.11

- compiler: binaries for macOS, linux, windows
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN go mod download

COPY ./ ./

RUN go build ./cmd/lithia
RUN go build ./app/lithia

##
## Deploy
Expand Down
50 changes: 50 additions & 0 deletions app/lithia/cmd/lsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
// rootCmd.AddCommand(lspCmd)
lspCmd.AddCommand(lspStdioCmd)
lspCmd.AddCommand(lspSocketCmd)

lspSocketCmd.Flags().StringVarP(
&lspSocketAddress,
"listen",
"l",
"127.0.0.1:7998",
"Address and port on which to listen for LSP connections",
)
}

var lspCmd = &cobra.Command{
Use: "lsp",
Short: "Language Server",
Long: `Runs the language server for the use inside an editor.`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
lspStdioCmd.Run(lspStdioCmd, args)
},
}

var lspStdioCmd = &cobra.Command{
Use: "stdio",
Aliases: []string{"stdin", "-"},
Short: "stdio mode. Supported by most editors.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("stdio")
},
}

var lspSocketAddress string = "127.0.0.1:7998"
var lspSocketCmd = &cobra.Command{
Use: "socket",
Short: `opens a socket on the specified address. Make sure the port is free.`,
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("socket", lspSocketAddress)
},
}
55 changes: 55 additions & 0 deletions app/lithia/cmd/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"bufio"
"fmt"
"io"
"os"

"github.com/spf13/cobra"
"github.com/vknabel/go-lithia/reporting"
"github.com/vknabel/go-lithia/runtime"
)

func init() {
rootCmd.AddCommand(replCmd)
}

var replCmd = &cobra.Command{
Use: "repl",
Short: "Runs interactive Lithia REPL.",
Long: ``,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
runPrompt()
},
}

func runPrompt() {
importRoot, err := os.Getwd()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
reader := bufio.NewReader(os.Stdin)
inter := runtime.NewInterpreter(importRoot)
for {
fmt.Print("> ")
line, err := reader.ReadString('\n')
if err == io.EOF {
return
}
if err != nil {
reporting.ReportErrorOrPanic(err)
continue
}
value, err := inter.InterpretEmbed("prompt", line)
if err != nil {
reporting.ReportErrorOrPanic(err)
continue
}
if value != nil {
fmt.Println("- ", value)
}
}
}
29 changes: 29 additions & 0 deletions app/lithia/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"github.com/spf13/cobra"
)

func Execute() error {
return rootCmd.Execute()
}

var rootCmd = &cobra.Command{
Use: "lithia",
Short: "Lithia programming language",
Long: "Lithia is an experimental functional programming language " +
"with an implicit but strong and dynamic type system.\n" +
"It is designed around a few core concepts in mind " +
"all language features contribute to.\n" +
"\n" +
"Lean more at https://github.com/vknabel/lithia",
Version: "0.0.12-next",
Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
runFile(args[0])
} else {
runPrompt()
}
},
}
38 changes: 38 additions & 0 deletions app/lithia/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"fmt"
"os"
"path"

"github.com/spf13/cobra"
"github.com/vknabel/go-lithia/runtime"
)

func init() {
rootCmd.AddCommand(runCmd)
}

var runCmd = &cobra.Command{
Use: "run [script]",
Short: "Runs a Lithia script",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
runFile(args[0])
},
}

func runFile(fileName string) {
scriptData, err := os.ReadFile(fileName)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
inter := runtime.NewInterpreter(path.Dir(fileName))
script := string(scriptData) + "\n"
_, err = inter.Interpret(fileName, script)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
16 changes: 16 additions & 0 deletions app/lithia/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"

"github.com/vknabel/go-lithia/app/lithia/cmd"
)

func main() {
err := cmd.Execute()
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
70 changes: 0 additions & 70 deletions cmd/lithia/main.go

This file was deleted.

4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ module github.com/vknabel/go-lithia
go 1.16

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82
github.com/stretchr/testify v1.7.0 // indirect
github.com/spf13/cobra v1.3.0
github.com/vknabel/tree-sitter-lithia v0.0.0-20220121161404-ed4529b7c21c
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit b022027

Please sign in to comment.