Skip to content

Commit

Permalink
feat: improve cli interation
Browse files Browse the repository at this point in the history
  • Loading branch information
pmqueiroz committed Sep 21, 2024
1 parent 3050c59 commit 13ad6bf
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
41 changes: 41 additions & 0 deletions cli/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cli

import (
"flag"
"fmt"
"os"
)

type Options struct {
PrintAst bool
}

type Args struct {
Options Options
Input string
Arguments []string
}

const HELP_HEADER = `Usage: umbra [options] [[file] [arguments]]
Options:`

func Parse() Args {
parsedArgs := Args{}

flag.BoolVar(&parsedArgs.Options.PrintAst, "ast", false, "Prints the AST of the program")
flag.Usage = func() {
fmt.Fprintln(os.Stderr, HELP_HEADER)
flag.PrintDefaults()
}
flag.Parse()

args := flag.Args()

if len(args) > 0 {
parsedArgs.Input = args[0]
parsedArgs.Arguments = args[1:]
}

return parsedArgs
}
10 changes: 10 additions & 0 deletions cli/print-ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cli

import (
"github.com/pmqueiroz/umbra/ast"
"github.com/sanity-io/litter"
)

func PrintAst(module ast.ModuleStatement) {
litter.Dump(module)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/pmqueiroz/umbra

go 1.21.6

require github.com/sanity-io/litter v1.5.5 // indirect
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
43 changes: 19 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@ package main

import (
"bufio"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/pmqueiroz/umbra/ast"
"github.com/pmqueiroz/umbra/cli"
"github.com/pmqueiroz/umbra/interpreter"
"github.com/pmqueiroz/umbra/tokens"
)

func jsonASTPrint(module ast.ModuleStatement) {
astJson, err := json.MarshalIndent(module, "", " ")
if err != nil {
fmt.Println("Failed to format ast JSON:", err)
return
}

fmt.Println(string(astJson))
}

func run(contents ...string) {
func run(options cli.Options, contents ...string) {
content := strings.Join(contents, "\n")
tokens, err := tokens.Tokenize(content)

Expand All @@ -31,22 +22,28 @@ func run(contents ...string) {

module := ast.Parse(tokens)

fmt.Printf("%#v\n", module)
if options.PrintAst {
cli.PrintAst(module)
}

env := interpreter.NewEnvironment(nil)

jsonASTPrint(module)
if err := interpreter.Interpret(module, env); err != nil {

Check failure on line 31 in main.go

View workflow job for this annotation

GitHub Actions / tests

undefined: interpreter.Interpret
fmt.Println("Erro:", err)
}
}

func runFile(path string) {
func runFile(path string, options cli.Options) {
fileContent, err := readFile(path)

if err != nil {
fmt.Printf("%s\n", err.Error())
}

run(fileContent)
run(options, fileContent)
}

func runPrompt() {
func runPrompt(options cli.Options) {
reader := bufio.NewReader(os.Stdin)

fmt.Print("Welcome to Umbra REPL!\nEnter :q to exit.\n")
Expand All @@ -59,21 +56,19 @@ func runPrompt() {
}

run(
options,
"module main",
line,
)
}
}

func main() {
args := os.Args[1:]
args := cli.Parse()

if len(args) > 1 {
fmt.Println("Usage: umbra [script]")
os.Exit(64)
} else if len(args) == 1 {
runFile(args[0])
if args.Input != "" {
runFile(args.Input, args.Options)
} else {
runPrompt()
runPrompt(args.Options)
}
}

0 comments on commit 13ad6bf

Please sign in to comment.